Exception when trying to create xpath object

2009-09-14
2012-10-08
  • David Paterson
    David Paterson
    2009-09-14

    Hi,

    When I try to create an xpath object I get the following exception;

    Exception in thread "main" java.lang.IllegalAccessError: tried to
    access method net.sf.saxon.functions.SystemFunctionLibrary.<init>(I)V
    from class net.sf.saxon.xpath.StandaloneContext
    at
    net.sf.saxon.xpath.StandaloneContext.<init>(StandaloneContext.java:75)
    at net.sf.saxon.xpath.XPathEvaluator.<init>(XPathEvaluator.java:56)
    at net.sf.saxon.xpath.XPathFactoryImpl.newXPath(XPathFactoryImpl.java:96)
    at
    com.bp.ist.tct.gops.gh.MessageValidator.XMLValidate(MessageValidator.java:147)
    at com.bp.ist.tct.gops.gh.TestValidator.main(TestValidator.java:21)

    Here is the function that it is ocuring in;

    package com.bp.ist.tct.gops.gh;
    import java.io.ByteArrayInputStream;
    import java.io.IOException;
    import java.util.*;

    import org.w3c.dom.;
    import org.xml.sax.
    ;

    import javax.xml.parsers.*;

    import javax.xml.namespace.NamespaceContext;

    import net.sf.saxon.dom.;
    import net.sf.saxon.xpath.
    ;
    import net.sf.saxon.instruct.*;
    import net.sf.saxon.om.NamespaceConstant;

    import com.ghc.ghTester.expressions.;
    import javax.xml.xpath.
    ;

    public static String XMLValidate(String Source, String Target,String Criteria)
    throws SAXException, ParserConfigurationException {

    //System.setProperty("javax.xml.parsers.DocumentBuilderFactory",&quo
    t;net.sf.saxon.om.DocumentBuilderFactoryImpl");

    Boolean valid = true;
    StringBuffer strResult = new StringBuffer();
    DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
    builderFactory.setNamespaceAware(true);

    DocumentBuilder builder = null;
    builder = builderFactory.newDocumentBuilder();

    Document sourceDocument = null;
    Document targetDocument = null;

    XPathFactory xpathFact;
    try {
    xpathFact = net.sf.saxon.xpath.XPathFactoryImpl.newInstance(NamespaceConstant.
    OBJECT_MODEL_SAXON);
    XPath xpath = xpathFact.newXPath();
    xpath.setNamespaceContext(ctx);

    try {
    sourceDocument = builder.parse(new InputSource(new
    ByteArrayInputStream(Source.getBytes())));
    } catch (IOException e) {
    e.printStackTrace();
    }

    try {
    targetDocument = builder.parse(new InputSource(new
    ByteArrayInputStream(Target.getBytes())));
    } catch (IOException e) {
    e.printStackTrace();
    }

    String paths = null;

    if (Criteria.startsWith("\&quot;")) {
    Criteria = Criteria.substring(1);
    }

    if (Criteria.endsWith("\&quot;")) {
    Criteria = Criteria.substring(0, Criteria.length()-1);
    }

    paths = Criteria.split(",");

    for (String path : paths) {

    XPathExpression expr = null;

    System.out.println("Checking Path->" + path);

    try {
    expr = xpath.compile(path);
    } catch (XPathExpressionException e) {
    e.printStackTrace();
    }

    String sourceValue = null;
    net.sf.saxon.dom.NodeWrapper sourceNode;
    try {
    sourceNode = (net.sf.saxon.dom.NodeWrapper)expr.evaluate(sourceDocument,
    XPathConstants.NODE);
    sourceValue = sourceNode.getStringValue();

    } catch (Exception e) {
    sourceValue = "SOURCE ELEMENT NOT FOUND! " + e;
    }

    String targetValue = null;
    Node targetNode;
    try {
    targetNode = (Node)expr.evaluate(targetDocument, XPathConstants.NODE);
    targetValue = targetNode.getTextContent();

    } catch (Exception e) {
    targetValue = "TARGET ELEMENT NOT FOUND! " + e;
    }

    if (!sourceValue.equals(targetValue)) {

    strResult.append("\nCritera -> " + path + "\n");
    strResult.append("Source -> " + sourceValue + "\n");
    strResult.append("Target -> " + targetValue + "\n");

    valid = false;
    }
    }

    } catch (XPathFactoryConfigurationException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
    }

    return valid.toString().toUpperCase() + "\n" + strResult.toString();

    }

    I would appreciate any help you could give, thanks

     
  • Michael Kay
    Michael Kay
    2009-09-15

    I think the most likely explanation for this error is a classpath problem: you
    are loading the saxon9-xpath.jar issued with a different version of Saxon from
    the main saxon9.jar, and hence you have a problem when doing a method call
    across the boundary between these Jar files.