List of usage examples for javax.xml.xpath XPathExpressionException XPathExpressionException
public XPathExpressionException(Throwable cause)
From source file:de.codesourcery.jasm16.ide.ProjectConfiguration.java
private List<String> getValues(XPathExpression expr, Document doc) throws XPathExpressionException { final NodeList nodes = (NodeList) expr.evaluate(doc, XPathConstants.NODESET); final List<String> result = new ArrayList<String>(); for (int i = 0; i < nodes.getLength(); i++) { final Node node = nodes.item(i); final String value = node.getTextContent(); if (value == null || StringUtils.isBlank(value)) { LOG.error("getValues(): Invalid project XML - blank/empty value"); throw new XPathExpressionException( "Invalid project XML - blank/empty value for " + " expression " + expr); }//from ww w.j a va 2 s.c om result.add(value.trim()); } return result; }
From source file:erwins.util.repack.xml.XMLBuilder.java
/** * Find the first element in the builder's DOM matching the given * XPath expression, where the expression may include namespaces if * a {@link NamespaceContext} is provided. * * @param xpath/*from w ww . j a v a2 s. c o m*/ * An XPath expression that *must* resolve to an existing Element within * the document object model. * @param nsContext * a mapping of prefixes to namespace URIs that allows the XPath expression * to use namespaces. * * @return * a builder node representing the first Element that matches the * XPath expression. * * @throws XPathExpressionException * If the XPath is invalid, or if does not resolve to at least one * {@link Node#ELEMENT_NODE}. */ public XMLBuilder xpathFind(String xpath, NamespaceContext nsContext) throws XPathExpressionException { Node foundNode = (Node) this.xpathQuery(xpath, XPathConstants.NODE, nsContext); if (foundNode == null || foundNode.getNodeType() != Node.ELEMENT_NODE) { throw new XPathExpressionException("XPath expression \"" + xpath + "\" does not resolve to an Element in context " + this.xmlNode + ": " + foundNode); } return new XMLBuilder(foundNode, null); }
From source file:fr.gouv.finances.dgfip.xemelios.importers.archives.ArchiveImporter.java
protected HashMap<String, Object> extractPropertiesFromArchiveManifeste(Document archiveManifeste) throws XPathExpressionException { HashMap<String, Object> props = new HashMap<String, Object>(); props.put("archiveName", archiveManifeste.query("/manifeste/@archive-name").get(0).getValue()); Nodes versionList = archiveManifeste.query("/manifeste/@version"); // on peut ne pas avoir de version if (versionList.size() > 0) props.put("archiveVersion", versionList.get(0).getValue()); else/*from w w w .j av a 2 s . com*/ props.put("archiveVersion", "1"); props.put("archiveFile", fileToImport.getAbsolutePath()); ArrayList<String> documentsTypes = new ArrayList<String>(); Processor proc = new Processor(FactoryProvider.getSaxonConfiguration()); try { XPathExecutable xpe = proc.newXPathCompiler().compile("distinct-values(//document/@type)"); XPathSelector xps = xpe.load(); xps.setContextItem(new XdmNode( new DocumentWrapper(archiveManifeste, "", FactoryProvider.getSaxonConfiguration()))); for (Iterator<XdmItem> it = xps.iterator(); it.hasNext();) { documentsTypes.add(normalizeDocumentType(it.next().getStringValue())); } String[] docTypesArray = new String[documentsTypes.size()]; documentsTypes.toArray(docTypesArray); props.put("archiveDocumentTypes", docTypesArray); } catch (Exception ex) { props.put("archiveDocumentTypes", null); throw new XPathExpressionException(ex); } return props; }
From source file:fr.gouv.finances.dgfip.xemelios.importers.archives.ArchiveImporter.java
protected void definePropertiesFromImportedManifeste(Document importedArchiveManifeste, HashMap<String, Object> props) throws XPathExpressionException { if (importedArchiveManifeste == null) { props.put("archiveImported", false); props.put("archiveImportedVersion", null); props.put("archiveImportedDocumentTypes", null); props.put("archiveDataPresent", false); return;//w w w . jav a 2s . com } props.put("archiveImported", true); // a l'import du manifeste dans XeMeLios, on change le namespace // la version peut tre null String version = importedArchiveManifeste.getRootElement().getAttributeValue("version"); if (version == null || version.length() == 0) version = "1"; props.put("archiveImportedVersion", version); Processor proc = new Processor(FactoryProvider.getSaxonConfiguration()); try { ArrayList<String> documentsTypes = new ArrayList<String>(); XPathCompiler xpc = proc.newXPathCompiler(); xpc.declareNamespace("m", Constants.MANIFESTE_NS_URI); XPathExecutable xpe = xpc.compile("distinct-values(//m:document/@type)"); XPathSelector xps = xpe.load(); xps.setContextItem(new XdmNode( new DocumentWrapper(importedArchiveManifeste, "", FactoryProvider.getSaxonConfiguration()))); for (Iterator<XdmItem> it = xps.iterator(); it.hasNext();) { documentsTypes.add(normalizeDocumentType(it.next().getStringValue())); } String[] docTypesArray = new String[documentsTypes.size()]; documentsTypes.toArray(docTypesArray); props.put("archiveImportedDocumentTypes", docTypesArray); props.put("archiveDataPresent", "Oui".equals(importedArchiveManifeste .query("/m:manifeste/@added:archive", getNamespaceCtx()).get(0).getValue())); } catch (Exception ex) { props.put("archiveImportedDocumentTypes", null); throw new XPathExpressionException(ex); } }
From source file:org.apache.geode.management.internal.configuration.utils.XmlUtils.java
public static Element querySingleElement(Node node, String searchString, final XPathContext xPathContext) throws XPathExpressionException { XPath xpath = XPathFactory.newInstance().newXPath(); xpath.setNamespaceContext(xPathContext); Object result = xpath.evaluate(searchString, node, XPathConstants.NODE); try {//from w w w . j av a 2 s . c o m return (Element) result; } catch (ClassCastException e) { throw new XPathExpressionException("Not an org.w3c.dom.Element: " + result); } }
From source file:org.kuali.rice.kew.xml.xstream.XStreamSafeEvaluator.java
/** * Parses the next segment of the given XPath expression by grabbing the first * segment off of the given xpath expression. The given xpath expression must * start with either ./, /, or // otherwise an XPathExpressionException is thrown. *//* w w w.java2 s .c om*/ private XPathSegment parseInitialSegment(String xPathExpression) throws XPathExpressionException { // TODO we currently can't support expressions that start with .// if (xPathExpression.startsWith(MATCH_CURRENT + MATCH_ANY)) { throw new XPathExpressionException( "XStream safe evaluator currenlty does not support expressions that start with " + MATCH_CURRENT + MATCH_ANY); } //int operatorLength = 3; //int firstIndex = xPathExpression.indexOf(MATCH_CURRENT+MATCH_ANY); //if (firstIndex != 0) { int operatorLength = 2; int firstIndex = xPathExpression.indexOf(MATCH_CURRENT + MATCH_ROOT); if (firstIndex != 0) { firstIndex = xPathExpression.indexOf(MATCH_ANY); if (firstIndex != 0) { operatorLength = 1; firstIndex = xPathExpression.indexOf(MATCH_ROOT); } } //} // the operator should be at the beginning of the string if (firstIndex != 0) { throw new XPathExpressionException( "Could not locate an appropriate ./, /, or // operator at the begginingg of the xpath segment: " + xPathExpression); } int nextIndex = xPathExpression.indexOf(MATCH_ROOT, operatorLength); if (nextIndex == -1) { nextIndex = xPathExpression.length(); } return new XPathSegment(xPathExpression.substring(0, operatorLength), xPathExpression.substring(operatorLength, nextIndex), true); }
From source file:org.kuali.rice.kew.xml.xstream.XStreamSafeEvaluator.java
/** * Parses the next segment of the given XPath expression by grabbing the first * segment off of the given xpath expression. The given xpath expression must * start with / otherwise an XPathExpressionException is thrown. This is because * the "next" segments represent the internal pieces in an XPath expression. *//*from w w w . ja va2 s . c om*/ private XPathSegment parseNextSegment(String xPathExpression) throws XPathExpressionException { if (!xPathExpression.startsWith(MATCH_ROOT)) { throw new XPathExpressionException( "Illegal xPath segment, the given segment is not a valid segment and should start with a '" + MATCH_ROOT + "'. Value was: " + xPathExpression); } int operatorLength = MATCH_ROOT.length(); int nextIndex = xPathExpression.indexOf(MATCH_ROOT, operatorLength); if (nextIndex == -1) { nextIndex = xPathExpression.length(); } return new XPathSegment(MATCH_CURRENT + MATCH_ROOT, xPathExpression.substring(operatorLength, nextIndex), false); }
From source file:org.kuali.rice.kew.xml.xstream.XStreamSafeEvaluator.java
/** * Resolves the reference to a Node by checking for a "reference" attribute and returning the resolved node if * it's there. The resolution happens by grabbing the value of the reference and evaluation it as an XPath * expression against the given Node. If there is no reference attribute, the node passed in is returned. * The method is recursive in the fact that it will continue to follow XStream "reference" attributes until it * reaches a resolved node./* w w w . j a v a2 s .c o m*/ */ private Node resolveNodeReference(XPath xpath, Node node) throws XPathExpressionException { NamedNodeMap attributes = node.getAttributes(); if (attributes != null) { Node referenceNode = attributes.getNamedItem(XSTREAM_REFERENCE_ATTRIBUTE); if (referenceNode != null) { node = (Node) xpath.evaluate(referenceNode.getNodeValue(), node, XPathConstants.NODE); if (node != null) { node = resolveNodeReference(xpath, node); } else { throw new XPathExpressionException( "Could not locate the node for the given XStream references expression: '" + referenceNode.getNodeValue() + "'"); } } } return node; }
From source file:org.wso2.carbon.automation.engine.context.DefaultInstance.java
public String getTenantDomain(boolean isTenantAdmin, boolean isClustered) throws XPathExpressionException { String tenantDomain = null;/*from w w w.ja v a2s . c o m*/ try { if (isTenantAdmin) { tenantDomain = getConfigurationValue(ContextXpathConstants.USER_MANAGEMENT_SUPER_TENANT_DOMAIN); } else { tenantDomain = getConfigurationValue(ContextXpathConstants.TENANT_DOMAIN); } } catch (XPathExpressionException e) { log.error("Error while reading the super Tenant:", e); throw new XPathExpressionException( "Error While Reading default Tenant Domain:- " + e.getStackTrace().toString()); } return tenantDomain; }
From source file:org.wso2.carbon.automation.engine.context.DefaultInstance.java
public String getUserKey(String tenantDomain, boolean isAdminUser) throws XPathExpressionException { String tenantKey = null;/*from w w w . j av a2 s .co m*/ String adminUserReplacement = ContextXpathConstants.ADMIN; try { if (!isAdminUser) { adminUserReplacement = ContextXpathConstants.USERS; } if (tenantDomain.equals(getConfigurationValue(ContextXpathConstants.SUPER_TENANT_DOMAIN))) { tenantKey = getConfigurationValue(String .format(ContextXpathConstants.USER_MANAGEMENT_SUPER_TENANT_USER_KEY, adminUserReplacement)); } else { tenantKey = getConfigurationValue(String.format( ContextXpathConstants.USER_MANAGEMENT_TENANT_USER_KEY, tenantDomain, adminUserReplacement)); } } catch (XPathExpressionException e) { log.error("Error while reading the Tenant:" + e.getMessage()); throw new XPathExpressionException( "Error While Reading default User Key:- " + e.getStackTrace().toString()); } return tenantKey; }