|
|
|
import javax.xml.parsers.DocumentBuilder;
|
|
import javax.xml.parsers.DocumentBuilderFactory;
|
|
import javax.xml.xpath.XPath;
|
|
import javax.xml.xpath.XPathFactory;
|
|
import javax.xml.xpath.XPathVariableResolver;
|
|
import javax.xml.xpath.XPathFactoryConfigurationException;
|
|
|
|
import org.w3c.dom.Node;
|
|
|
|
|
|
public class StandaloneSaxonTest {
|
|
|
|
/*
|
|
How to run sample (unix based Machine) in the same directory as class:
|
|
1. "mkdir ./build" - Create build directory where output compilation will go to
|
|
2. "javac -cp <Path/to/Saxon-HE-11.4.jar> -d ./build *.java" - Compile sample to build directory with classpath pointing to SaxonHE 11.4 jar
|
|
3. "cd ./build" - Move to build directory which should contain output compilation classes
|
|
4. "jar cvf Test.jar *" - Jar up all classes to Test.jar
|
|
5. "java -Dtest.property.useSaxonTransformer=[true|false] -cp "<Path/to/Saxon-HE-11.4.jar>:<Path/to/xmlresolver-4.4.3.jar>:./Test.jar" StandaloneSaxonTest" - Run sample in previously built jar with classpath pointing to Saxon and xmlresolver dependency
|
|
*/
|
|
|
|
public StandaloneSaxonTest() {
|
|
}
|
|
|
|
|
|
private static XPathFactory XPATH_FACTORY;
|
|
|
|
// Decides whether to use Saxon XPathFactory or JDK XPathFactory
|
|
private static final String USE_SAXON_PROPERTY = "test.property.useSaxonTransformer";
|
|
private static final boolean USE_SAXON = Boolean.getBoolean(USE_SAXON_PROPERTY);
|
|
|
|
|
|
private static class CustomVariableResolver implements javax.xml.xpath.XPathVariableResolver
|
|
{
|
|
public Object resolveVariable(javax.xml.namespace.QName qname) throws NullPointerException {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
private static void initXPathFactory() {
|
|
System.out.println("Using Saxon XPathFactory? " + USE_SAXON);
|
|
if(!USE_SAXON){
|
|
try {
|
|
XPATH_FACTORY = XPathFactory.newInstance(XPathFactory.DEFAULT_OBJECT_MODEL_URI);
|
|
} catch (XPathFactoryConfigurationException xpce) {
|
|
xpce.printStackTrace();
|
|
}
|
|
}else{
|
|
XPATH_FACTORY = new net.sf.saxon.xpath.XPathFactoryImpl();
|
|
}
|
|
}
|
|
|
|
public static void main(String[] args) throws Exception{
|
|
// Init which XPathFactory to use
|
|
initXPathFactory();
|
|
// Create fake document to run evaluate on
|
|
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
|
|
factory.setNamespaceAware(true);
|
|
factory.setValidating( false );
|
|
DocumentBuilder builder = factory.newDocumentBuilder();
|
|
Node doc = builder.newDocument();
|
|
// Initialize an XPath from XPathFactory
|
|
XPath xpath = XPATH_FACTORY.newXPath();
|
|
// Add custom resolver returning null
|
|
XPathVariableResolver vr = new CustomVariableResolver();
|
|
xpath.setXPathVariableResolver(vr);
|
|
// Evaluate the result. Should get an exception according to JAXP 1.3 Spec:
|
|
//"... An javax.xml.xpath.XPathExpressionException is raised if the variable resolver is undefined or the resolver returns null for the variable."
|
|
String res = xpath.evaluate("$doc//a", doc);
|
|
System.out.println("Failed to see exception. Got result: "+res);
|
|
}
|
|
|
|
}
|