net.sf.jasperreports.engine.util.xml.JaxenNsAwareXPathExecuter Maven / Gradle / Ivy
/*
* JasperReports - Free Java Reporting Library.
* Copyright (C) 2001 - 2016 TIBCO Software Inc. All rights reserved.
* http://www.jaspersoft.com
*
* Unless you have purchased a commercial license agreement from Jaspersoft,
* the following license terms apply:
*
* This program is part of JasperReports.
*
* JasperReports is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* JasperReports is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with JasperReports. If not, see .
*/
package net.sf.jasperreports.engine.util.xml;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.sf.jasperreports.engine.JRException;
import org.apache.commons.collections.map.ReferenceMap;
import org.jaxen.JaxenException;
import org.jaxen.NamespaceContext;
import org.jaxen.XPath;
import org.jaxen.dom.DOMXPath;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
/**
* XPath executer implementation that uses a namespace aware Jaxen.
*
* @author Narcis Marcu ([email protected])
*/
public class JaxenNsAwareXPathExecuter extends JaxenXPathExecuter
{
private final Map cachedXPaths = new ReferenceMap();//soft cache
private Map xmlNamespaceMap;
private NamespaceContext context;
boolean detectXmlNamespaces;
/**
* Default constructor.
*/
public JaxenNsAwareXPathExecuter()
{
}
public Map getXmlNamespaceMap()
{
return xmlNamespaceMap;
}
public void setXmlNamespaceMap(Map xmlNamespaceMap) throws JRException
{
this.xmlNamespaceMap = xmlNamespaceMap;
}
public boolean getDetectXmlNamespaces()
{
return detectXmlNamespaces;
}
public void setDetectXmlNamespaces(boolean detectXmlNamespaces)
{
this.detectXmlNamespaces = detectXmlNamespaces;
}
protected XPath getXPath(Node contextNode, String expression) throws JRException
{
XPath xPath = cachedXPaths.get(expression);
if (xPath == null)
{
try
{
xPath = new DOMXPath(expression);
addNamespaceContext(contextNode, xPath, expression);
}
catch (JaxenException e)
{
throw
new JRException(
EXCEPTION_MESSAGE_KEY_XPATH_COMPILATION_FAILURE,
new Object[]{expression},
e);
}
cachedXPaths.put(expression, xPath);
}
return xPath;
}
@Override
public NodeList selectNodeList(Node contextNode, String expression) throws JRException
{
try
{
XPath xpath = getXPath(contextNode, expression);
Object object = xpath.evaluate(contextNode);
List