Java XPath Expression createReverseCMDIComponentItemMap(String urlToComponent)

Here you can find the source of createReverseCMDIComponentItemMap(String urlToComponent)

Description

Create a mapping out of simple CMDI components for instance: lists of items: ti Will become key (after removal of trailing 2 or 3 letter codes), values: Tigrinya, ti

License

Open Source License

Parameter

Parameter Description
urlToComponent a parameter

Exception

Parameter Description
XPathExpressionException an exception
IOException an exception
SAXException an exception
ParserConfigurationException an exception

Return

Map with item_value, AppInfo_value pairs

Declaration

public static Map<String, String> createReverseCMDIComponentItemMap(String urlToComponent)
        throws XPathExpressionException, SAXException, IOException, ParserConfigurationException 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.IOException;
import java.net.URL;
import java.util.HashMap;

import java.util.Map;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class Main {
    /**//ww w . j a v  a2s  . c o  m
     * Create a mapping out of simple CMDI components for instance: lists of
     * items: <item AppInfo="Tigrinya">ti</item> Will become key (after removal
     * of trailing 2 or 3 letter codes), values: Tigrinya, ti
     *
     * @param urlToComponent
     * @return Map with item_value, AppInfo_value pairs
     * @throws XPathExpressionException
     * @throws IOException
     * @throws SAXException
     * @throws ParserConfigurationException
     */
    public static Map<String, String> createReverseCMDIComponentItemMap(String urlToComponent)
            throws XPathExpressionException, SAXException, IOException, ParserConfigurationException {
        Map<String, String> result = new HashMap<String, String>();
        DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
        domFactory.setNamespaceAware(true);
        URL url = new URL(urlToComponent);
        //TODO: Process XML as stream for much better performance (no need to build entire DOM)
        DocumentBuilder builder = domFactory.newDocumentBuilder();
        Document doc = builder.parse(url.openStream());
        XPath xpath = XPathFactory.newInstance().newXPath();
        NodeList nodeList = (NodeList) xpath.evaluate("//item", doc, XPathConstants.NODESET);
        for (int i = 0; i < nodeList.getLength(); i++) {
            Node node = nodeList.item(i);
            String shortName = node.getTextContent();
            String longName = node.getAttributes().getNamedItem("AppInfo").getNodeValue()
                    .replaceAll(" \\([a-zA-Z]+\\)$", "");
            result.put(longName, shortName);
        }
        return result;
    }
}

Related

  1. compileXPathExpression(String xPathExpression)
  2. constructXPathForElement(Element inElem, String xPathRest)
  3. createFactory()
  4. createNewXPath(@Nullable final NamespaceContext aNamespaceContext)
  5. createQueryResourcePropertiesCallBody(String aXqueryExpression)
  6. createXPath()
  7. createXPath(NamespaceContext namespaceContext, XPathFunctionResolver functionResolver)
  8. createXPathExpression(NamespaceContext context, String xPathQuery)
  9. createXPathExpression(String expression, NamespaceContext namespaceContext)