org.jaxen.XPath Java Examples

The following examples show how to use org.jaxen.XPath. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: JaxenDemo.java    From tutorials with MIT License 6 votes vote down vote up
public List getAllTutorial() {
    try {
        FileInputStream fileIS = new FileInputStream(this.getFile());
        DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();

        DocumentBuilder builder = builderFactory.newDocumentBuilder();

        Document xmlDocument = builder.parse(fileIS);

        String expression = "/tutorials/tutorial";

        XPath path = new DOMXPath(expression);
        List result = path.selectNodes(xmlDocument);
        return result;

    } catch (SAXException | IOException | ParserConfigurationException | JaxenException e) {
        e.printStackTrace();
        return null;
    }

}
 
Example #2
Source File: AuthoringXml.java    From sakai with Educational Community License v2.0 6 votes vote down vote up
/**
 * Based on method in XmlStringBuffer
 * @author rpembry
 * @author casong changed XmlStringBuffer to be org.w3c.dom compliance,
 * @author Ed Smiley [email protected] changed method signatures used Document
 *
 * @return a List of Nodes
 */
public final List selectNodes(Document document, String xpath)
{
  if (log.isDebugEnabled())
  {
    log.debug("selectNodes(String " + xpath + ")");
  }

  List result = new ArrayList();

  try
  {
    XPath path = new DOMXPath(xpath);
    result = path.selectNodes(document);
  }
  catch (JaxenException je)
  {
    log.error(je.getMessage(), je);
  }

  return result;
}
 
Example #3
Source File: XmlConfiguration.java    From carbon-identity with Apache License 2.0 6 votes vote down vote up
public OMElement[] getElements(String xPath) {
    SimpleNamespaceContext nsCtx = new SimpleNamespaceContext();
    nsCtx.addNamespace("ns", serverNamespace);
    try {
        XPath xp = new AXIOMXPath(xPath);
        xp.setNamespaceContext(nsCtx);
        OMElement elem = builder.getDocumentElement();
        if (elem != null) {
            List nodeList = xp.selectNodes(elem);
            return (OMElement[]) nodeList.toArray(new OMElement[nodeList.size()]);
        }
    } catch (JaxenException e) {
        throw new RuntimeException("XPath expression " + xPath + " failed", e);
    }
    return new OMElement[0];
}
 
Example #4
Source File: XmlConfiguration.java    From carbon-identity with Apache License 2.0 6 votes vote down vote up
public String getUniqueValue(String xPath) {
    SimpleNamespaceContext nsCtx = new SimpleNamespaceContext();
    nsCtx.addNamespace("ns", serverNamespace);
    try {
        XPath xp = new AXIOMXPath(xPath);
        xp.setNamespaceContext(nsCtx);
        OMElement elem = builder.getDocumentElement();
        if (elem != null) {
            List nodeList = xp.selectNodes(elem);
            Object obj;
            if (!nodeList.isEmpty() && ((obj = nodeList.get(0)) != null)) {
                return ((OMElement) obj).getText();
            }
        }
    } catch (JaxenException e) {
        throw new RuntimeException("XPath expression " + xPath + " failed", e);
    }
    return null;
}
 
Example #5
Source File: JaxenNsAwareXPathExecuter.java    From jasperreports with GNU Lesser General Public License v3.0 6 votes vote down vote up
protected void addNamespaceContext(Node contextNode, XPath xPath, String expression) throws JRException {
	if (xmlNamespaceMap == null && detectXmlNamespaces && containsPrefixes(expression) && context == null)
	{
		xmlNamespaceMap = extractXmlNamespaces(contextNode);
	}
	
	if (xmlNamespaceMap != null && xmlNamespaceMap.size() > 0)
	{
		if (context == null) {
			context = new NamespaceContext() {
				
				@Override
				public String translateNamespacePrefixToUri(String prefix) {
					return xmlNamespaceMap.get(prefix);
				}
			};
		}
		xPath.setNamespaceContext(context);
	}
}
 
Example #6
Source File: AuthoringXml.java    From sakai with Educational Community License v2.0 6 votes vote down vote up
/**
 * Based on method in XmlStringBuffer
 * @author rpembry
 * @author casong changed XmlStringBuffer to be org.w3c.dom compliance,
 * @author Ed Smiley [email protected] changed method signatures used Document
 *
 * @return a List of Nodes
 */
public final List selectNodes(Document document, String xpath)
{
  if (log.isDebugEnabled())
  {
    log.debug("selectNodes(String " + xpath + ")");
  }

  List result = new ArrayList();

  try
  {
    XPath path = new DOMXPath(xpath);
    result = path.selectNodes(document);
  }
  catch (JaxenException je)
  {
    log.error(je.getMessage(), je);
  }

  return result;
}
 
Example #7
Source File: XmlUtils.java    From OpenEstate-IO with Apache License 2.0 5 votes vote down vote up
/**
 * Create a {@link XPath} expression.
 *
 * @param expression      string with the XPath expression to create
 * @param doc             the document, whose namespace is bound to the XPath expression
 * @param namespacePrefix prefix of the document namespace, that is bound to the XPath expression
 * @return the created XPath expression
 * @throws JaxenException if the XPath is not creatable
 */
public static XPath newXPath(String expression, Document doc, String namespacePrefix) throws JaxenException {
    DOMXPath xpath = new DOMXPath(expression);
    //LOGGER.debug( "new xpath: " + xpath.debug() );
    if (doc != null && namespacePrefix != null) {
        Element root = XmlUtils.getRootElement(doc);
        String uri = StringUtils.trimToEmpty(root.getNamespaceURI());
        xpath.addNamespace(namespacePrefix, uri);
    }
    return xpath;
}
 
Example #8
Source File: XPathHelper.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
public static String getNodeValue(String expression, Node node) {
	if (node == null) return "";
	String value = null;
	try {
		XPath xpath = new DOMXPath(expression);
		value = xpath.stringValueOf(node);
	} catch (JaxenException e) {
		return null;
	}
	return value;
}
 
Example #9
Source File: XPathHelper.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
public static Node selectNode(String expression, Object doc) {
	if (doc == null) return null;
	Object node = null;
	try {
		XPath xpath = new DOMXPath(expression);
		node = xpath.selectSingleNode(doc);
	} catch (JaxenException e) {
		return null;
	}
	return (Node)node;  
}
 
Example #10
Source File: XPathHelper.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
public static List selectNodes(String expression, Object doc) {
	if (doc == null) return new ArrayList();
	List nodes = null;
	try {
		XPath xpath = new DOMXPath(expression);
		nodes = xpath.selectNodes(doc);
	} catch (JaxenException e) {
		return null;
	}
	return nodes;  
}
 
Example #11
Source File: XmlCode.java    From RADL with Apache License 2.0 5 votes vote down vote up
public <T> Iterable<T> multiple(String path, Class<T> returnType) {
  Collection<T> result = new ArrayList<>();
  try {
    XPath xpath = new DOMXPath(path);
    for (Entry<String, String> entry : namespaces.entrySet()) {
      xpath.addNamespace(entry.getKey(), entry.getValue());
    }
    for (Object found : xpath.selectNodes(getRoot())) {
      result.add(returnType.cast(found));
    }
  } catch (JaxenException e) {
    throw new RuntimeException(e);
  }
  return result;
}
 
Example #12
Source File: XPathHelper.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
public static String getNodeValue(String expression, Node node) {
	if (node == null) return "";
	String value = null;
	try {
		XPath xpath = new DOMXPath(expression);
		value = xpath.stringValueOf(node);
	} catch (JaxenException e) {
		return null;
	}
	return value;
}
 
Example #13
Source File: XPathHelper.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
public static Node selectNode(String expression, Object doc) {
	if (doc == null) return null;
	Object node = null;
	try {
		XPath xpath = new DOMXPath(expression);
		node = xpath.selectSingleNode(doc);
	} catch (JaxenException e) {
		return null;
	}
	return (Node)node;  
}
 
Example #14
Source File: XPathHelper.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
public static List selectNodes(String expression, Object doc) {
	if (doc == null) return new ArrayList();
	List nodes = null;
	try {
		XPath xpath = new DOMXPath(expression);
		nodes = xpath.selectNodes(doc);
	} catch (JaxenException e) {
		return null;
	}
	return nodes;  
}
 
Example #15
Source File: MCRJaxenXPathFactory.java    From mycore with GNU General Public License v3.0 5 votes vote down vote up
private <T> void addExtensionFunctions(XPathExpression<T> jaxenCompiled) {
    try {
        XPath xPath = getXPath(jaxenCompiled);
        addExtensionFunctions(xPath);
    } catch (Exception ex) {
        LOGGER.warn(ex);
    }
}
 
Example #16
Source File: KbCsvMojo.java    From Asqatasun with GNU Affero General Public License v3.0 5 votes vote down vote up
private List<String> getUrls(String url) throws JDOMException, JaxenException, IOException {
    SAXBuilder builder = new SAXBuilder();
    EntityResolver resolver = new XhtmlEntityResolver();
    builder.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false);
    builder.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
    builder.setEntityResolver(resolver);
    Document document = builder.build(new URL(url));
    XPath xpath = new JDOMXPath("//*[@id='resultat']//*[@href]/@href");
    List<Attribute> results = xpath.selectNodes(document);
    List<String> urls = new ArrayList<String>();
    for (Attribute attr : results) {
        urls.add(attr.getValue());
    }
    return urls;
}
 
Example #17
Source File: MCRJaxenXPathFactory.java    From mycore with GNU General Public License v3.0 5 votes vote down vote up
private void addExtensionFunctions(XPath xPath) {
    XPathFunctionContext xfc = (XPathFunctionContext) (xPath.getFunctionContext());
    for (ExtensionFunction function : functions) {
        function.register(xfc);
    }
    xPath.setFunctionContext(xfc);
}
 
Example #18
Source File: JaxenXPathExecuter.java    From jasperreports with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public Object selectObject(Node contextNode, String expression) throws JRException
{
	try
	{
		XPath xpath = new DOMXPath(expression);
		Object object = xpath.evaluate(contextNode);
		Object value;
		if (object instanceof List<?>)
		{
			List<?> list = (List<?>) object;
			if (list.isEmpty())
			{
				value = null;
			}
			else
			{
				value = list.get(0);
			}
		}
		else if (object instanceof Number || object instanceof Boolean)
		{
			value = object;
		}
		else
		{
			value = object.toString();
		}
		return value;
	}
	catch (JaxenException e)
	{
		throw 
			new JRException(
				EXCEPTION_MESSAGE_KEY_XPATH_SELECTION_FAILURE,
				new Object[]{expression},
				e);
	}
}
 
Example #19
Source File: JaxenXPathExecuter.java    From jasperreports with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public NodeList selectNodeList(Node contextNode, String expression) throws JRException
{
	try
	{
		XPath xpath = getXPath(expression);
		Object object = xpath.evaluate(contextNode);
		List<Object> nodes;
		if (object instanceof List<?>)
		{
			nodes = (List<Object>) object;
		}
		else
		{
			nodes = new ArrayList<Object>();
			nodes.add(object);
		}
		return new NodeListWrapper(nodes);
	}
	catch (JaxenException e)
	{
		throw 
			new JRException(
				EXCEPTION_MESSAGE_KEY_XPATH_SELECTION_FAILURE,
				new Object[]{expression},
				e);
	}		
}
 
Example #20
Source File: MCRJaxenXPathFactory.java    From mycore with GNU General Public License v3.0 5 votes vote down vote up
private <T> XPath getXPath(XPathExpression<T> jaxenCompiled) throws NoSuchFieldException, IllegalAccessException {
    Field xPathField = jaxenCompiled.getClass().getDeclaredField("xPath");
    xPathField.setAccessible(true);
    XPath xPath = (XPath) (xPathField.get(jaxenCompiled));
    xPathField.setAccessible(false);
    return xPath;
}
 
Example #21
Source File: JaxenNsAwareXPathExecuter.java    From jasperreports with GNU Lesser General Public License v3.0 5 votes vote down vote up
private Map<String, String> extractXmlNamespaces(Node contextNode) throws JRException 
{
	Map<String, String> namespaces = new HashMap<String, String>();
	List<Node> nlist;
	String namespaceXPathString = "//namespace::node()";

	try
	{
		XPath xpath = new DOMXPath(namespaceXPathString);
		nlist = xpath.selectNodes(contextNode);
		
		for (int i = 0; i < nlist.size(); i++) 
		{
			Node node = nlist.get(i);
			if(node.getParentNode() != null && node.getParentNode().getPrefix() != null)
			{
				if (!namespaces.containsKey(node.getParentNode().getPrefix()))
				{
					namespaces.put(node.getParentNode().getPrefix(), node.getParentNode().getNamespaceURI());
				}
			}
		}
		
	} catch (JaxenException e)
	{
		throw 
			new JRException(
				EXCEPTION_MESSAGE_KEY_XPATH_SELECTION_FAILURE,
				new Object[]{namespaceXPathString},
				e);
	}
	
	return namespaces;
}
 
Example #22
Source File: JaxenNsAwareXPathExecuter.java    From jasperreports with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public Object selectObject(Node contextNode, String expression) throws JRException
{
	try
	{
		XPath xpath = getXPath(contextNode, expression);
		Object object = xpath.evaluate(contextNode);
		Object value;
		if (object instanceof List<?>)
		{
			List<?> list = (List<?>) object;
			if (list.isEmpty())
			{
				value = null;
			}
			else
			{
				value = list.get(0);
			}
		}
		else if (object instanceof Number || object instanceof Boolean)
		{
			value = object;
		}
		else
		{
			value = object.toString();
		}
		return value;
	}
	catch (JaxenException e)
	{
		throw 
			new JRException(
				EXCEPTION_MESSAGE_KEY_XPATH_SELECTION_FAILURE,
				new Object[]{expression},
				e);
	}
}
 
Example #23
Source File: JaxenNsAwareXPathExecuter.java    From jasperreports with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public NodeList selectNodeList(Node contextNode, String expression) throws JRException
{
	try
	{
		XPath xpath = getXPath(contextNode, expression);
		Object object = xpath.evaluate(contextNode);
		List<Object> nodes;
		if (object instanceof List<?>)
		{
			nodes = (List<Object>) object;
		}
		else
		{
			nodes = new ArrayList<Object>();
			nodes.add(object);
		}
		return new NodeListWrapper(nodes);
	}
	catch (JaxenException e)
	{
		throw 
			new JRException(
				EXCEPTION_MESSAGE_KEY_XPATH_SELECTION_FAILURE,
				new Object[]{expression},
				e);
	}		
}
 
Example #24
Source File: SwingNavigator.java    From jstarcraft-core with Apache License 2.0 4 votes vote down vote up
@Override
public XPath parseXPath(String xpath) throws JaxenException {
    return new SwingXPath(xpath);
}
 
Example #25
Source File: DocumentNavigator.java    From alfresco-repository with GNU Lesser General Public License v3.0 4 votes vote down vote up
public XPath parseXPath(String o) throws JaxenException
{
    return new NodeServiceXPath(o, this, null);
}
 
Example #26
Source File: NormalizedNodeNavigator.java    From yangtools with Eclipse Public License 1.0 4 votes vote down vote up
@Override
public XPath parseXPath(final String xpath) throws SAXPathException {
    // FIXME: need to bind YangXPath probably
    throw new UnsupportedOperationException();
}
 
Example #27
Source File: ParseNodeNavigator.java    From APICloud-Studio with GNU General Public License v3.0 4 votes vote down vote up
/**
 * @see org.jaxen.Navigator#parseXPath(java.lang.String)
 */
public XPath parseXPath(String xpath)
{
	return null;
}
 
Example #28
Source File: DocumentNavigator.java    From contribution with GNU Lesser General Public License v2.1 4 votes vote down vote up
/**
 * @see org.jaxen.DefaultNavigator#parseXPath(java.lang.String)
 */
public XPath parseXPath(String aObject)
    throws SAXPathException
{
    return null;
}
 
Example #29
Source File: DocumentNavigator.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
/**
 * @see org.jaxen.DefaultNavigator#parseXPath(java.lang.String)
 */
public XPath parseXPath(String aObject)
    throws SAXPathException
{
    return null;
}
 
Example #30
Source File: HtmlNavigator.java    From jstarcraft-core with Apache License 2.0 4 votes vote down vote up
@Override
public XPath parseXPath(String xpath) throws JaxenException {
    return new HtmlXPath(xpath);
}