List of usage examples for javax.xml.xpath XPathConstants NODE
QName NODE
To view the source code for javax.xml.xpath XPathConstants NODE.
Click Source Link
The XPath 1.0 NodeSet data type.
From source file:org.linguafranca.pwdb.kdbx.dom.DomSerializableDatabase.java
@Override public void setHeaderHash(byte[] hash) { // Android compatibility String base64String = new String(Base64.encodeBase64(hash)); try {/*from w w w. ja v a 2 s.com*/ ((Element) DomHelper.xpath.evaluate("//HeaderHash", doc, XPathConstants.NODE)) .setTextContent(base64String); } catch (XPathExpressionException e) { throw new IllegalStateException("Can't set header hash", e); } }
From source file:org.lockss.plugin.clockss.XPathXmlMetadataParser.java
private ArticleMetadata extractDataFromNode(Object startNode, XPathInfo[] xPathList) throws XPathExpressionException { ArticleMetadata returnAM = makeNewArticleMetadata(); NumberFormat format = NumberFormat.getInstance(); for (int i = 0; i < xPathList.length; i++) { log.debug3("evaluate xpath: " + xPathList[i].xKey.toString()); QName definedType = xPathList[i].xVal.getType(); Object itemResult = xPathList[i].xExpr.evaluate(startNode, XPathConstants.NODESET); NodeList resultNodeList = (NodeList) itemResult; log.debug3(resultNodeList.getLength() + " results for this xKey"); for (int p = 0; p < resultNodeList.getLength(); p++) { Node resultNode = resultNodeList.item(p); if (resultNode == null) { continue; }/*from ww w . j a v a2s .c om*/ String value = null; if (definedType == XPathConstants.NODE) { // filter node value = xPathList[i].xVal.getValue(resultNode); } else if (definedType == XPathConstants.STRING) { // filter node text content String text = resultNode.getTextContent(); if (!StringUtil.isNullString(text)) { value = xPathList[i].xVal.getValue(text); } } else if (definedType == XPathConstants.BOOLEAN) { // filter boolean value of node text content String text = resultNode.getTextContent(); if (!StringUtil.isNullString(text)) { value = xPathList[i].xVal.getValue(Boolean.parseBoolean(text)); } } else if (definedType == XPathConstants.NUMBER) { // filter number value of node text content try { String text = resultNode.getTextContent(); if (!StringUtil.isNullString(text)) { value = xPathList[i].xVal.getValue(format.parse(text)); } } catch (ParseException ex) { // ignore invalid number log.debug3("ignore invalid number", ex); } } else { log.debug("Unknown nodeValue type: " + definedType.toString()); } if (!StringUtil.isNullString(value)) { log.debug3(" returning (" + xPathList[i].xKey + ", " + value); returnAM.putRaw(xPathList[i].xKey, value); } } } return returnAM; }
From source file:org.megadix.jfcm.utils.FcmIOTest.java
@Test public void test_saveAsXml_in_memory() throws Exception { ByteArrayOutputStream baos = new ByteArrayOutputStream(); // save//from w w w . ja v a 2 s. c o m FcmIO.saveAsXml(map, baos); // check results DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder(); InputStream is = new ByteArrayInputStream(baos.toByteArray()); Document doc = db.parse(is); XPath xpath = XPathFactory.newInstance().newXPath(); Element mapElem = (Element) xpath.evaluate("/maps/map[position() = 1]", doc, XPathConstants.NODE); assertEquals("Test Map", xpath.evaluate("/maps/map[@name='Test Map']/@name", doc)); checkXmlDescription(xpath, mapElem, "This is a test map"); NodeList nodelist; Node node; Map<String, String> expectedParams = new HashMap<String, String>(); // check <concepts> nodelist = (NodeList) xpath.evaluate("/maps/map[@name='Test Map']/concepts/concept", doc, XPathConstants.NODESET); assertEquals(4, nodelist.getLength()); // ----- node = (Node) xpath.evaluate("/maps/map[@name='Test Map']/concepts/concept[@name='c1']", doc, XPathConstants.NODE); expectedParams.clear(); expectedParams.put("threshold", "1.1"); expectedParams.put("includePreviousOutput", "false"); expectedParams.put("mode", "BINARY"); checkConceptXml(xpath, (Element) node, "c1", "c1 description <>&\"", "SIGNUM", "1.0", "1.1", null, expectedParams); // ----- node = (Node) xpath.evaluate("/maps/map[@name='Test Map']/concepts/concept[@name='c2']", doc, XPathConstants.NODE); expectedParams.clear(); expectedParams.put("k", "2.2"); checkConceptXml(xpath, (Element) node, "c2", "c2 description", "SIGMOID", null, "3.3", null, expectedParams); node = (Node) xpath.evaluate("/maps/map[@name='Test Map']/concepts/concept[@name='c3']", doc, XPathConstants.NODE); expectedParams.clear(); expectedParams.put("threshold", "3.3"); checkConceptXml(xpath, (Element) node, "c3", "c3 description", "TANH", "3.0", "1.23", "true", expectedParams); // ----- node = (Node) xpath.evaluate("/maps/map[@name='Test Map']/concepts/concept[@name='c4']", doc, XPathConstants.NODE); expectedParams.clear(); expectedParams.put("threshold", "1.4"); expectedParams.put("factor", "1.2"); expectedParams.put("min", "-2.0"); expectedParams.put("max", "2.0"); checkConceptXml(xpath, (Element) node, "c4", "c4 description", "LINEAR", "3.0", "1.23", "true", expectedParams); // check <connections> nodelist = (NodeList) xpath.evaluate("/maps/map[@name='Test Map']/connections/connection", doc, XPathConstants.NODESET); assertEquals(4, nodelist.getLength()); // ----- expectedParams.clear(); expectedParams.put("weight", "1.1"); expectedParams.put("delay", "5"); checkConnectionXml(xpath, (Element) nodelist.item(0), "c1-c2", "c1-c2 description <>&\"", "c1", "c2", "WEIGHTED", expectedParams); // ----- expectedParams.clear(); expectedParams.put("weight", "2.2"); checkConnectionXml(xpath, (Element) nodelist.item(1), "c1-c3", "c1-c3 description", "c1", "c3", "WEIGHTED", expectedParams); // ----- expectedParams.clear(); expectedParams.put("weight", "3.3"); checkConnectionXml(xpath, (Element) nodelist.item(2), "c2-c3", "c2-c3 description", "c2", "c3", "WEIGHTED", expectedParams); // ----- expectedParams.clear(); expectedParams.put("weight", "4.4"); checkConnectionXml(xpath, (Element) nodelist.item(3), "c3-c1", "c3-c1 description", "c3", "c1", "WEIGHTED", expectedParams); }
From source file:org.megadix.jfcm.utils.FcmIOTest.java
@Test public void test_saveActivators() throws Exception { // create test map CognitiveMap map = new CognitiveMap(); Concept c;/* w w w . j a v a2s . c om*/ c = new Concept("c1", null); c.setConceptActivator(new CauchyActivator(1.1, true)); map.addConcept(c); c = new Concept("c2", null); c.setConceptActivator(new GaussianActivator(1.2, false, 2.0)); map.addConcept(c); c = new Concept("c3", null); c.setConceptActivator( new IntervalActivator(1.3, true, org.megadix.jfcm.act.IntervalActivator.Mode.BINARY, -0.5, 2.0)); map.addConcept(c); c = new Concept("c4", null); c.setConceptActivator(new LinearActivator(1.4, false, 2.0, 0.1, 0.2)); map.addConcept(c); c = new Concept("c5", null); c.setConceptActivator(new NaryActivator(1.5, true, 3)); map.addConcept(c); c = new Concept("c6", null); c.setConceptActivator(new SigmoidActivator(1.6, false, 1.1)); map.addConcept(c); c = new Concept("c7", null); c.setConceptActivator(new SignumActivator(1.7, true, Mode.BINARY, 0.1)); map.addConcept(c); c = new Concept("c8", null); c.setConceptActivator(new HyperbolicTangentActivator(1.8, false)); map.addConcept(c); // save ByteArrayOutputStream baos = new ByteArrayOutputStream(); FcmIO.saveAsXml(map, baos); // check results DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder(); InputStream is = new ByteArrayInputStream(baos.toByteArray()); Document doc = db.parse(is); XPath xpath = XPathFactory.newInstance().newXPath(); NodeList nodelist; Node node; Map<String, String> expectedParams = new HashMap<String, String>(); // check <concepts> nodelist = (NodeList) xpath.evaluate("/maps/map/concepts/concept", doc, XPathConstants.NODESET); assertEquals(8, nodelist.getLength()); // ----- node = (Node) xpath.evaluate("/maps/map/concepts/concept[@name='c1']", doc, XPathConstants.NODE); expectedParams.clear(); expectedParams.put("threshold", "1.1"); checkConceptXml(xpath, (Element) node, "c1", null, "CAUCHY", null, null, null, expectedParams); // ----- node = (Node) xpath.evaluate("/maps/map/concepts/concept[@name='c2']", doc, XPathConstants.NODE); expectedParams.clear(); expectedParams.put("threshold", "1.2"); expectedParams.put("includePreviousOutput", "false"); expectedParams.put("width", "2.0"); checkConceptXml(xpath, (Element) node, "c2", null, "GAUSS", null, null, null, expectedParams); // ----- node = (Node) xpath.evaluate("/maps/map/concepts/concept[@name='c3']", doc, XPathConstants.NODE); expectedParams.clear(); expectedParams.put("threshold", "1.3"); expectedParams.put("mode", "BINARY"); expectedParams.put("zeroValue", "-0.5"); expectedParams.put("amplitude", "2.0"); checkConceptXml(xpath, (Element) node, "c3", null, "INTERVAL", null, null, null, expectedParams); // ----- node = (Node) xpath.evaluate("/maps/map/concepts/concept[@name='c4']", doc, XPathConstants.NODE); expectedParams.clear(); expectedParams.put("threshold", "1.4"); expectedParams.put("includePreviousOutput", "false"); expectedParams.put("factor", "2.0"); expectedParams.put("min", "0.1"); expectedParams.put("max", "0.2"); checkConceptXml(xpath, (Element) node, "c4", null, "LINEAR", null, null, null, expectedParams); // ----- node = (Node) xpath.evaluate("/maps/map/concepts/concept[@name='c5']", doc, XPathConstants.NODE); expectedParams.clear(); expectedParams.put("threshold", "1.5"); expectedParams.put("n", "3"); checkConceptXml(xpath, (Element) node, "c5", null, "NARY", null, null, null, expectedParams); // ----- node = (Node) xpath.evaluate("/maps/map/concepts/concept[@name='c6']", doc, XPathConstants.NODE); expectedParams.clear(); expectedParams.put("threshold", "1.6"); expectedParams.put("includePreviousOutput", "false"); expectedParams.put("k", "1.1"); checkConceptXml(xpath, (Element) node, "c6", null, "SIGMOID", null, null, null, expectedParams); // ----- node = (Node) xpath.evaluate("/maps/map/concepts/concept[@name='c7']", doc, XPathConstants.NODE); expectedParams.clear(); expectedParams.put("threshold", "1.7"); expectedParams.put("mode", "BINARY"); expectedParams.put("zeroValue", "0.1"); checkConceptXml(xpath, (Element) node, "c7", null, "SIGNUM", null, null, null, expectedParams); // ----- node = (Node) xpath.evaluate("/maps/map/concepts/concept[@name='c8']", doc, XPathConstants.NODE); expectedParams.clear(); expectedParams.put("threshold", "1.8"); expectedParams.put("includePreviousOutput", "false"); checkConceptXml(xpath, (Element) node, "c8", null, "TANH", null, null, null, expectedParams); }
From source file:org.megadix.jfcm.utils.FcmIOTest.java
private void checkConceptXml(XPath xpath, Element elem, String name, String description, String act, String input, String output, String fixed, Map<String, String> expectedParams) throws XPathExpressionException { assertEquals(name, elem.getAttribute("name")); if (description != null) { checkXmlDescription(xpath, elem, description); }// ww w .ja va2 s .com checkXmlAttribute(elem, "act", act); checkXmlAttribute(elem, "input", input); checkXmlAttribute(elem, "output", output); checkXmlAttribute(elem, "fixed", fixed); Element paramsElem = (Element) xpath.evaluate("params", elem, XPathConstants.NODE); assertNotNull(paramsElem); NodeList paramsList = paramsElem.getElementsByTagName("param"); for (int i = 0; i < paramsList.getLength(); i++) { Element param = (Element) paramsList.item(i); String paramName = param.getAttribute("name"); assertTrue(StringUtils.isNotBlank(paramName)); String paramValue = param.getAttribute("value"); assertTrue(StringUtils.isNotBlank(paramValue)); assertTrue("Parameter not found: " + paramName, expectedParams.containsKey(paramName)); assertEquals(paramValue, expectedParams.get(paramName)); expectedParams.remove(paramName); } assertEquals("Parameter not found: " + expectedParams.keySet().toString(), 0, expectedParams.size()); }
From source file:org.megadix.jfcm.utils.FcmIOTest.java
private void checkConnectionXml(XPath xpath, Element elem, String name, String description, String from, String to, String type, Map<String, String> expectedParams) throws XPathExpressionException { assertEquals(name, elem.getAttribute("name")); checkXmlDescription(xpath, elem, description); checkXmlAttribute(elem, "from", from); checkXmlAttribute(elem, "to", to); checkXmlAttribute(elem, "type", type); Element paramsElem = (Element) xpath.evaluate("params", elem, XPathConstants.NODE); assertNotNull(paramsElem);/*from ww w . j a v a 2 s. c o m*/ NodeList paramsList = paramsElem.getElementsByTagName("param"); for (int i = 0; i < paramsList.getLength(); i++) { Element param = (Element) paramsList.item(i); String paramName = param.getAttribute("name"); assertTrue(StringUtils.isNotBlank(paramName)); String paramValue = param.getAttribute("value"); assertTrue(StringUtils.isNotBlank(paramValue)); assertTrue("Parameter not found: " + paramName, expectedParams.containsKey(paramName)); assertEquals(paramValue, expectedParams.get(paramName)); expectedParams.remove(paramName); } assertEquals(0, expectedParams.size()); }
From source file:org.megadix.jfcm.utils.FcmIOTest.java
private void checkXmlDescription(XPath xpath, Element elem, String value) throws XPathExpressionException { Element descrElem = (Element) xpath.evaluate("description", elem, XPathConstants.NODE); assertNotNull(descrElem);/*from ww w . jav a2s. c o m*/ assertEquals(value, descrElem.getTextContent()); }
From source file:org.meveo.service.billing.impl.InvoiceService.java
public void producePdf(Map<String, Object> parameters, User currentUser, String invoiceXmlFileName, String meveoDir, boolean isInvoiceAdjustment) throws Exception { log.info("PDFInvoiceGenerationJob is invoice key exists=" + ((parameters != null) ? parameters.containsKey(PdfGeneratorConstants.INVOICE) + "" : "parameters is null")); String pdfDirectory = meveoDir + "invoices" + File.separator + "pdf" + File.separator; (new File(pdfDirectory)).mkdirs(); Invoice invoice = (Invoice) parameters.get(PdfGeneratorConstants.INVOICE); String INVOICE_TAG_NAME = "invoice"; File invoiceXmlFile = new File(invoiceXmlFileName); if (!invoiceXmlFile.exists()) { throw new InvoiceXmlNotFoundException("The xml invoice file doesn't exist."); }/* www . ja v a 2 s. c o m*/ BillingAccount billingAccount = invoice.getBillingAccount(); BillingCycle billingCycle = null; if (billingAccount != null && billingAccount.getBillingCycle() != null) { billingCycle = billingAccount.getBillingCycle(); } String billingTemplate = (billingCycle != null && billingCycle.getBillingTemplateName() != null) ? billingCycle.getBillingTemplateName() : "default"; String resDir = meveoDir + "jasper"; File destDir = new File(resDir + File.separator + billingTemplate + File.separator + "pdf"); if (!destDir.exists()) { destDir.mkdirs(); String sourcePath = Thread.currentThread().getContextClassLoader().getResource("./jasper").getPath(); File sourceFile = new File(sourcePath); if (!sourceFile.exists()) { VirtualFile vfDir = VFS .getChild("/content/" + ParamBean.getInstance().getProperty("meveo.moduleName", "meveo") + ".war/WEB-INF/classes/jasper"); log.info("default jaspers path :" + vfDir.getPathName()); URL vfPath = VFSUtils.getPhysicalURL(vfDir); sourceFile = new File(vfPath.getPath()); if (!sourceFile.exists()) { throw new BusinessException("embedded jasper report for invoice isn't existed!"); } } FileUtils.copyDirectory(sourceFile, destDir); } File destDirInvoiceAdjustment = new File( resDir + File.separator + billingTemplate + File.separator + "invoiceAdjustmentPdf"); if (!destDirInvoiceAdjustment.exists()) { destDirInvoiceAdjustment.mkdirs(); String sourcePathInvoiceAdjustment = Thread.currentThread().getContextClassLoader() .getResource("./invoiceAdjustment").getPath(); File sourceFileInvoiceAdjustment = new File(sourcePathInvoiceAdjustment); if (!sourceFileInvoiceAdjustment.exists()) { VirtualFile vfDir = VFS .getChild("/content/" + ParamBean.getInstance().getProperty("meveo.moduleName", "meveo") + ".war/WEB-INF/classes/invoiceAdjustment"); URL vfPath = VFSUtils.getPhysicalURL(vfDir); sourceFileInvoiceAdjustment = new File(vfPath.getPath()); if (!sourceFileInvoiceAdjustment.exists()) { throw new BusinessException("embedded jasper report for invoice isn't existed!"); } } FileUtils.copyDirectory(sourceFileInvoiceAdjustment, destDirInvoiceAdjustment); } File jasperFile = getJasperTemplateFile(resDir, billingTemplate, billingAccount.getPaymentMethod(), isInvoiceAdjustment); if (!jasperFile.exists()) { throw new InvoiceJasperNotFoundException("The jasper file doesn't exist."); } log.info(String.format("Jasper template used: %s", jasperFile.getCanonicalPath())); InputStream reportTemplate = new FileInputStream(jasperFile); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); Document xmlDocument = db.parse(invoiceXmlFile); xmlDocument.getDocumentElement().normalize(); Node invoiceNode = xmlDocument.getElementsByTagName(INVOICE_TAG_NAME).item(0); Transformer trans = TransformerFactory.newInstance().newTransformer(); trans.setOutputProperty(OutputKeys.INDENT, "yes"); StringWriter writer = new StringWriter(); trans.transform(new DOMSource(xmlDocument), new StreamResult(writer)); log.debug(writer.getBuffer().toString().replaceAll("\n|\r", "")); XPath xPath = XPathFactory.newInstance().newXPath(); XPathExpression expr = xPath.compile("/invoice"); Object result = expr.evaluate(xmlDocument, XPathConstants.NODE); Node node = (Node) result; JRXmlDataSource dataSource = null; if (node != null) { dataSource = new JRXmlDataSource(new ByteArrayInputStream(getNodeXmlString(invoiceNode).getBytes()), "/invoice"); } else { dataSource = new JRXmlDataSource(new ByteArrayInputStream(getNodeXmlString(invoiceNode).getBytes()), "/invoice"); } JasperReport jasperReport = (JasperReport) JRLoader.loadObject(reportTemplate); JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, dataSource); String pdfFileName = getNameWoutSequence(pdfDirectory, invoice.getInvoiceDate(), (!StringUtils.isBlank(invoice.getInvoiceNumber()) ? invoice.getInvoiceNumber() : invoice.getTemporaryInvoiceNumber())) + ".pdf"; if (isInvoiceAdjustment) { pdfFileName = getNameWoutSequence(pdfDirectory, invoice.getInvoiceDate(), paramBean.getProperty("invoicing.invoiceAdjustment.prefix", "_IA_") + invoice.getInvoiceNumber()) + ".pdf"; } JasperExportManager.exportReportToPdfFile(jasperPrint, pdfFileName); log.info(String.format("PDF file '%s' produced", pdfFileName)); FileInputStream fileInputStream = null; try { File file = new File(pdfFileName); long fileSize = file.length(); if (fileSize > Integer.MAX_VALUE) { throw new IllegalArgumentException("File is too big to put it to buffer in memory."); } byte[] fileBytes = new byte[(int) file.length()]; fileInputStream = new FileInputStream(file); fileInputStream.read(fileBytes); invoice.setPdf(fileBytes); update(invoice, currentUser); log.debug("invoice.setPdf update ok"); } catch (Exception e) { log.error("Error saving file to DB as blob. {}", e); } finally { if (fileInputStream != null) { try { fileInputStream.close(); } catch (IOException e) { log.error("Error closing file input stream."); } } } }
From source file:org.miloss.fgsms.common.Utility.java
/** * uses a variety of classes to determine the name of the first XML child * node of a SOAP:envelope node/* w ww . ja v a2 s . co m*/ * * @param xml * @return */ public static String getActionNameFromXML(String xml) { if (stringIsNullOrEmpty(xml)) { return ""; } try { InputStream is = null; Document xmlDocument = null; try { is = new ByteArrayInputStream(xml.trim().getBytes(Constants.CHARSET)); xmlDocument = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(is); } catch (Exception ex) { log.log(Level.DEBUG, "fgsms Utility, error identifying request method by building request message as xml document: " + ex.getLocalizedMessage()); } if (xmlDocument == null) { String workingdoc = ""; try { workingdoc = xml.substring(xml.indexOf("<"), xml.lastIndexOf(">")); } catch (Exception ex) { } if (Utility.stringIsNullOrEmpty(workingdoc)) { log.log(Level.WARN, "fgsms Utility, error identifying request method by building request message as xml document: this most likely isn't xml"); return ""; } if (Utility.stringIsNullOrEmpty(xml)) // return ""; { is = new ByteArrayInputStream(workingdoc.getBytes(Constants.CHARSET)); } xmlDocument = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(is); } XPath xpath = XPathFactory.newInstance().newXPath(); javax.xml.xpath.XPathExpression xp = xpath.compile("/Envelope/Body"); try { DeferredElementImpl j = (DeferredElementImpl) xp.evaluate(xmlDocument, XPathConstants.NODE); //org.apache.xerces.dom.DeferredElementImpl j2 = (org.apache.xerces.dom.DeferredElementImpl) for (int i = 0; i < j.getChildNodes().getLength(); i++) { if (!j.getChildNodes().item(i).getNodeName().equalsIgnoreCase("#text")) { log.log(Level.DEBUG, "Found action via com.sun.org.apache.xerces.internal.dom.DeferredElementImpl"); return j.getChildNodes().item(i).getNodeName(); } } } catch (NoClassDefFoundError c) { log.log(Level.DEBUG, "couldn't find com.sun.org.apache.xerces.internal.dom.DeferredElementImpl, trying alternate method..."); } catch (ClassCastException e) { log.log(Level.DEBUG, "couldn't find com.sun.org.apache.xerces.internal.dom.DeferredElementImpl, trying alternate method..."); } catch (Exception ex) { log.log(Level.DEBUG, "error caught using com.sun.org.apache.xerces.internal.dom.DeferredElementImpl, trying alternate method...", ex); } try { org.apache.xerces.dom.DeferredElementImpl j = (org.apache.xerces.dom.DeferredElementImpl) xp .evaluate(xmlDocument, XPathConstants.NODE); for (int i = 0; i < j.getChildNodes().getLength(); i++) { if (!j.getChildNodes().item(i).getNodeName().equalsIgnoreCase("#text")) { Node n = j.getChildNodes().item(i); String s = n.getNodeName(); s = n.getNodeValue(); s = n.getNodeValue(); s = n.getBaseURI(); log.log(Level.DEBUG, "Found action via org.apache.xerces.dom.DeferredElementImpl"); return j.getChildNodes().item(i).getNodeName(); } } } catch (NoClassDefFoundError c) { log.log(Level.DEBUG, "couldn't find org.apache.xerces.dom.DeferredElementImpl, giving up determining action"); } catch (Exception ex) { log.log(Level.DEBUG, "error caught using org.apache.xerces.dom.DeferredElementImpl", ex); } } catch (Exception ex) { log.log(Level.DEBUG, "fgsms Utility, error identifying request method from xml, defaulted to urn:undeterminable", ex); } return ""; }
From source file:org.mrgeo.colorscale.ColorScale.java
public void fromXML(final Document doc) throws ColorScaleException { try {//from w w w . jav a 2 s . c om clear(); final XPath xpath = XmlUtils.createXPath(); name = xpath.evaluate("/ColorMap/@name", doc); final Node nodeTitle = (Node) xpath.evaluate("/ColorMap/Title", doc, XPathConstants.NODE); if (nodeTitle != null) { title = xpath.evaluate("text()", nodeTitle); } final Node nodeDesc = (Node) xpath.evaluate("/ColorMap/Description", doc, XPathConstants.NODE); if (nodeDesc != null) { description = xpath.evaluate("text()", nodeDesc); } scaling = Scaling.valueOf(xpath.evaluate("/ColorMap/Scaling/text()", doc)); final String reliefShadingStr = xpath.evaluate("/ColorMap/ReliefShading/text()", doc).toLowerCase(); reliefShading = reliefShadingStr.equals("1") || reliefShadingStr.equals("true"); final String interpolateStr = xpath.evaluate("/ColorMap/Interpolate/text()", doc).toLowerCase(); interpolate = interpolateStr.isEmpty() || (interpolateStr.equals("1") || interpolateStr.equals("true")); final String forceStr = xpath.evaluate("/ColorMap/ForceValuesIntoRange/text()", doc).toLowerCase(); forceValuesIntoRange = (forceStr.equals("1") || forceStr.equals("true")); final Node nullColorNode = (Node) xpath.evaluate("/ColorMap/NullColor", doc, XPathConstants.NODE); if (nullColorNode != null) { final String colorStr = xpath.evaluate("@color", nullColorNode); final String opacityStr = xpath.evaluate("@opacity", nullColorNode); parseColor(colorStr, opacityStr, nullColor); } final int[] color = new int[4]; final XPathExpression expr = xpath.compile("/ColorMap/Color"); final NodeList nodes = (NodeList) expr.evaluate(doc, XPathConstants.NODESET); for (int i = 0; i < nodes.getLength(); i++) { final Node node = nodes.item(i); final String valueStr = xpath.evaluate("@value", node); final String colorStr = xpath.evaluate("@color", node); final String opacityStr = xpath.evaluate("@opacity", node); if (valueStr.isEmpty()) { throw new IOException("Error parsing XML: A value must be specified for a color element."); } final double value = Double.valueOf(valueStr); parseColor(colorStr, opacityStr, color); put(value, color); } cache = null; } catch (final Exception e) { throw new BadXMLException(e); } }