Package parse

Source Code of parse.ConfigXMLParser

package parse;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import value.FieldInfo;

@Component
class ConfigXMLParser {
    private static Log log = LogFactory.getLog(ConfigXMLParser.class);

    @Bean(name = "luceneConfig")
    LuceneConfiguration readLuceneConfiguration(@Value("${mvn.lucene.configFileLocation}") String fileLocation) {
        log.debug("Loading lucene config using file:" + fileLocation);
        LuceneConfiguration luceneConfiguration = new LuceneConfiguration();
        try {
            Element documentElement = getDocumentElement(new File(fileLocation));
            luceneConfiguration.setIndexLocation(new File(getParentNode(documentElement, "indexLocation")));
            luceneConfiguration.setSourceFileLocation(new File(getParentNode(documentElement, "sourceFileLocation")));
            luceneConfiguration.setOverWrite(getParentNode(documentElement, "overWrite"));
            luceneConfiguration.setSeparator(getParentNode(documentElement, "separator"));

            NodeList nodeLst = documentElement.getElementsByTagName("field");
            for (int s = 0; s < nodeLst.getLength(); s++) {
                List<FieldInfo> populateFieldInfo = populateFieldInfo(nodeLst, s);
                for (FieldInfo readInfo : populateFieldInfo) {
                    luceneConfiguration.addFieldInfo(readInfo.getFieldName(), readInfo);
                }
            }
        } catch (Exception e) {
            log.warn("Error parsing XML", e);
        }
        return luceneConfiguration;
    }

    private Element getDocumentElement(File file) throws Exception {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder documentBuilder = dbf.newDocumentBuilder();
        Document document = documentBuilder.parse(file);
        Element documentElement = document.getDocumentElement();
        documentElement.normalize();
        return documentElement;
    }

    private String getParentNode(Element element, String nodeName) {
        NodeList childNodes = element.getChildNodes();
        for (int i = 0; i < childNodes.getLength(); i++) {
            if (childNodes.item(i).getNodeName().equalsIgnoreCase(nodeName)) {
                return childNodes.item(i).getTextContent();
            }
        }
        return null;
    }

    private List<FieldInfo> populateFieldInfo(NodeList nodeLst, int s) {
        Node firstNode = nodeLst.item(s);
        List<FieldInfo> readFields = new ArrayList<FieldInfo>();
        if (firstNode.getNodeType() == Node.ELEMENT_NODE) {

            Element fieldElement = (Element) firstNode;

            String fieldName = getNode(fieldElement, "name");
            FieldInfo fieldInfo = new FieldInfo(fieldName);

            String type = getNode(fieldElement, "type");
            fieldInfo.setType(type);

            String analyzer = getNode(fieldElement, "analyzer");
            fieldInfo.setAnalyzer(analyzer);

            String store = getNode(fieldElement, "store");
            fieldInfo.setStore(store);
            readFields.add(fieldInfo);

        }
        return readFields;
    }

    private String getNode(Element fstElmnt, String nodeName) {
        NodeList typeElementList = fstElmnt.getElementsByTagName(nodeName);
        Element typeElement = (Element) typeElementList.item(0);
        NodeList typeList = typeElement.getChildNodes();
        String type = ((Node) typeList.item(0)).getNodeValue();
        return type;
    }

}
TOP

Related Classes of parse.ConfigXMLParser

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.