/** * Construct an XPath expression from a given string. * * @param xpathExpr the string representation of the XPath expression. * @throws JaxenException if there is a syntax error while parsing the expression */ public AXIOMXPath(String xpathExpr) throws JaxenException { super(xpathExpr, new DocumentNavigator()); }
/** * Retrieves the string-value of an element node. This may be the empty string if the element is * empty, but must not be null. * * @param object the comment node. * @return Returns the string-value of the node. */ public String getElementStringValue(Object object) { if (isElement(object)) { return getStringValue((OMElement) object, new StringBuffer()) .toString(); } return null; }
private StringBuffer getStringValue(OMNode node, StringBuffer buffer) { if (isText(node)) { buffer.append(((OMText) node).getText()); } else if (node instanceof OMElement) { Iterator children = ((OMElement) node).getChildren(); while (children.hasNext()) { getStringValue((OMNode) children.next(), buffer); } } return buffer; }
/** * Retrieves an <code>Iterator</code> matching the <code>attribute</code> XPath axis. * * @param contextNode the original context node * @return Returns an Iterator capable of traversing the axis, not null. * @throws UnsupportedAxisException if the semantics of the attribute axis are not supported by * this object model */ public Iterator getAttributeAxisIterator(Object contextNode) throws UnsupportedAxisException { if (isElement(contextNode)) { return ((OMElement) contextNode).getAllAttributes(); } else { return JaxenConstants.EMPTY_ITERATOR; } }
/** * Returns the document node that contains the given context node. * * @param contextNode the context node * @return Returns the document of the context node. * @see #isDocument(Object) */ public Object getDocumentNode(Object contextNode) { if (contextNode instanceof OMDocument) { return contextNode; } OMContainer parent = ((OMNode) contextNode).getParent(); if (parent == null) { // this node doesn't have a parent Document. So return the document element itself return contextNode; } else { return getDocumentNode(parent); } }
public static XPath getXPath(String path, Map<String, String> namespaces, Map<QName, Function> functions, Map<QName, Object> variables) throws JaxenException { DocumentNavigator nav = new DocumentNavigator(); XPath contextpath = new BaseXPath(path, nav); if (namespaces != null) { for (Map.Entry<String, String> entry : namespaces.entrySet()) { contextpath.addNamespace(entry.getKey(), entry.getValue()); } } if (functions != null) { contextpath.setFunctionContext(getFunctionContext(functions, (SimpleFunctionContext)contextpath .getFunctionContext())); } if (variables != null) contextpath.setVariableContext(getVariableContext(variables, (SimpleVariableContext)contextpath .getVariableContext())); return contextpath; }