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
- compileXPathExpression(String xPathExpression)
- constructXPathForElement(Element inElem, String xPathRest)
- createFactory()
- createNewXPath(@Nullable final NamespaceContext aNamespaceContext)
- createQueryResourcePropertiesCallBody(String aXqueryExpression)
- createXPath()
- createXPath(NamespaceContext namespaceContext, XPathFunctionResolver functionResolver)
- createXPathExpression(NamespaceContext context, String xPathQuery)
- createXPathExpression(String expression, NamespaceContext namespaceContext)