You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-users@xerces.apache.org by al...@nada.kth.se on 2006/03/14 15:14:44 UTC

XInclude and LSResourceResolver problem.

Hi,

I'm running into problems when using xinclude with my own
LSResourceResolver. I want to catch the call to the resolver and
return my own LSInput that the parser should use for retrieving the
included contents. What I really want is to filter the included file
myself before letting the parser see it but it fails even for the
simple example provided below. I get the following
NullPointerException:

java.lang.NullPointerException
        at
org.apache.xerces.xinclude.XIncludeHandler.searchForRecursiveIncludes(Unknown
Source)
        at
org.apache.xerces.xinclude.XIncludeHandler.startDocument(Unknown
Source)
        at
org.apache.xerces.impl.dtd.XMLDTDValidator.startDocument(Unknown
Source)
        at
org.apache.xerces.impl.XMLDocumentScannerImpl.startEntity(Unknown
Source)
        at
org.apache.xerces.impl.XMLVersionDetector.startDocumentParsing(Unknown
Source)
        at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
        at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
        at
org.apache.xerces.xinclude.XIncludeHandler.handleIncludeElement(Unknown
Source)
        at org.apache.xerces.xinclude.XIncludeHandler.emptyElement(Unknown
Source)
        at
org.apache.xerces.impl.XMLNSDocumentScannerImpl.scanStartElement(Unknown
Source)
        at
org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(Unknown
Source)
        at
org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown
Source)
        at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
        at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
        at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
        at org.apache.xerces.parsers.DOMParserImpl.parse(Unknown Source)
        at Error.main(Error.java:44)


Letting the resolver return null and thereby let the default handler take
care of the include works just fine in the example below. I'm using xerces
2.8.0 and jsdk 1.4.2 and 1.5.

Anyone got a clue what I'm missing?

Many thanks in advance,

/Per



================
Example class 'Error.java'

run

java Error err.xml

in order to reproduce the error.

================
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import org.w3c.dom.Document;
import org.w3c.dom.DOMError;
import org.w3c.dom.DOMErrorHandler;
import org.w3c.dom.bootstrap.DOMImplementationRegistry;
import org.w3c.dom.ls.DOMImplementationLS;
import org.w3c.dom.ls.LSException;
import org.w3c.dom.ls.LSInput;
import org.w3c.dom.ls.LSOutput;
import org.w3c.dom.ls.LSParser;
import org.w3c.dom.ls.LSResourceResolver;


public class Error {
     public static void main(String[] args) {
	  if (args.length != 1) {
	       System.out.println("Usage: java Error <instance file>");
	       System.exit(0);
	  }
	  String instanceFile = args[0];
	  try {
	       System.setProperty(DOMImplementationRegistry.PROPERTY,
				  "org.apache.xerces.dom.DOMImplementationSourceImpl");
	       DOMImplementationRegistry registry =
		    DOMImplementationRegistry.newInstance();
	       final DOMImplementationLS domImplementationLS =
		    (DOMImplementationLS)registry.getDOMImplementation("LS");
	       LSParser parser =
		    domImplementationLS.createLSParser(DOMImplementationLS.MODE_SYNCHRONOUS,
						       "http://www.w3.org/2001/XMLSchema");
	       parser.getDomConfig().setParameter("http://apache.org/xml/features/xinclude",
						  Boolean.TRUE);
	       parser.getDomConfig().setParameter("resource-resolver",
						  new TestResolver(domImplementationLS));
	       parser.getDomConfig().setParameter("error-handler", new
DOMErrorHandler() {
			 public boolean handleError(DOMError error) {
			      ((Exception)error.getRelatedException()).printStackTrace();
			      throw new LSException(LSException.PARSE_ERR, error.getMessage());
			 }
		    });

	       LSInput input = domImplementationLS.createLSInput();
	       LSOutput output = domImplementationLS.createLSOutput();
	       input.setByteStream(new FileInputStream(instanceFile));
	       output.setByteStream(System.out);
	       Document doc = parser.parse(input);
	       domImplementationLS.createLSSerializer().write(doc, output);
	       System.err.println();
	  } catch (ClassNotFoundException e) {
	       System.err.println("Unable to find DOM Parser: " + e.getMessage());
	       System.exit(1);
	  } catch (InstantiationException e) {
	       System.err.println("Unable to find DOM Parser: " + e.getMessage());
	       System.exit(1);
	  } catch (IllegalAccessException e) {
	       System.err.println("Unable to find DOM Parser: " + e.getMessage());
	       System.exit(1);
	  } catch (FileNotFoundException e) {
	       System.err.println("Unable to find file: " + instanceFile);
	       System.exit(1);
	  }
	  catch (Throwable t) {
	       t.printStackTrace();
	       System.exit(1);
	  }
     }
}

class TestResolver implements LSResourceResolver {
     private DOMImplementationLS domImplementationLS;

     public TestResolver(DOMImplementationLS domImplementationLS) {
	  this.domImplementationLS = domImplementationLS;
     }

     public LSInput resolveResource(String type,
				    String namespaceURI,
				    String publicId,
				    String systemId,
				    String baseURI) {
 	  System.err.println("==== Resolving '" + type + "' '" + namespaceURI +
"' '" +
 			     publicId + "' '" + systemId + "' '" + baseURI + "'");

	  LSInput ret = domImplementationLS.createLSInput();
	  FileInputStream is = null;
	  try {
	       is = new FileInputStream((new URL(systemId)).getPath());
	  } catch (IOException e) {
	       e.printStackTrace();
	       System.exit(1);
	  }
	  ret.setByteStream(is);
	  return ret;
//	  return null;
     }
}

====================
Example xml file 'err.xml'
====================
<?xml version="1.0"?>
<elem xmlns:xi="http://www.w3.org/2001/XInclude">
    <xi:include href="file:errinclude.xml"/>
</elem>

====================
Example xml file 'errinclude.xml'
====================
<anotherElement>Some text for example</anotherElement>



---------------------------------------------------------------------
To unsubscribe, e-mail: j-users-unsubscribe@xerces.apache.org
For additional commands, e-mail: j-users-help@xerces.apache.org


Re: XInclude and LSResourceResolver problem.

Posted by Peter McCracken <pe...@ca.ibm.com>.
Hello,

Try setting the systemId on the LSInput by adding the line 
"ret.setSystemId(systemId);" to the resolveResource() method of 
TestResolver, just before the return statement.  That solved the problem 
for me when I ran your test.

Xerces' XIncludeHandler checks for recursive includes by doing String 
comparison on the full pathnames of included resources (including a path 
that was included before will lead to an infinite recursion of includes). 
Because of this, it needs a String version of location of the included 
resource, which it gets from the systemId of the InputSource.

However, it's bad form for the XIncludeHandler to throw a 
NullPointerException instead of a useful error.  I'll file a bug about 
that.

Cheers,
-- 
Peter McCracken
XML Parser Development
IBM Toronto Lab
Email: peterjm@ca.ibm.com

alexius@nada.kth.se wrote on 03/14/2006 09:14:44 AM:

> Hi,
> 
> I'm running into problems when using xinclude with my own
> LSResourceResolver. I want to catch the call to the resolver and
> return my own LSInput that the parser should use for retrieving the
> included contents. What I really want is to filter the included file
> myself before letting the parser see it but it fails even for the
> simple example provided below. I get the following
> NullPointerException:
> 
> java.lang.NullPointerException
>         at
> 
org.apache.xerces.xinclude.XIncludeHandler.searchForRecursiveIncludes(Unknown
> Source)
>         at
> org.apache.xerces.xinclude.XIncludeHandler.startDocument(Unknown
> Source)
>         at
> org.apache.xerces.impl.dtd.XMLDTDValidator.startDocument(Unknown
> Source)
>         at
> org.apache.xerces.impl.XMLDocumentScannerImpl.startEntity(Unknown
> Source)
>         at
> org.apache.xerces.impl.XMLVersionDetector.startDocumentParsing(Unknown
> Source)
>         at org.apache.xerces.parsers.XML11Configuration.parse(Unknown 
Source)
>         at org.apache.xerces.parsers.XML11Configuration.parse(Unknown 
Source)
>         at
> org.apache.xerces.xinclude.XIncludeHandler.handleIncludeElement(Unknown
> Source)
>         at 
org.apache.xerces.xinclude.XIncludeHandler.emptyElement(Unknown
> Source)
>         at
> org.apache.xerces.impl.XMLNSDocumentScannerImpl.scanStartElement(Unknown
> Source)
>         at
> org.apache.xerces.impl.
> 
XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(Unknown
> Source)
>         at
> 
org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown
> Source)
>         at org.apache.xerces.parsers.XML11Configuration.parse(Unknown 
Source)
>         at org.apache.xerces.parsers.XML11Configuration.parse(Unknown 
Source)
>         at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
>         at org.apache.xerces.parsers.DOMParserImpl.parse(Unknown Source)
>         at Error.main(Error.java:44)
> 
> 
> Letting the resolver return null and thereby let the default handler 
take
> care of the include works just fine in the example below. I'm using 
xerces
> 2.8.0 and jsdk 1.4.2 and 1.5.
> 
> Anyone got a clue what I'm missing?
> 
> Many thanks in advance,
> 
> /Per
> 
> 
> 
> ================
> Example class 'Error.java'
> 
> run
> 
> java Error err.xml
> 
> in order to reproduce the error.
> 
> ================
> import java.io.FileInputStream;
> import java.io.FileNotFoundException;
> import java.io.IOException;
> import java.net.MalformedURLException;
> import java.net.URL;
> import org.w3c.dom.Document;
> import org.w3c.dom.DOMError;
> import org.w3c.dom.DOMErrorHandler;
> import org.w3c.dom.bootstrap.DOMImplementationRegistry;
> import org.w3c.dom.ls.DOMImplementationLS;
> import org.w3c.dom.ls.LSException;
> import org.w3c.dom.ls.LSInput;
> import org.w3c.dom.ls.LSOutput;
> import org.w3c.dom.ls.LSParser;
> import org.w3c.dom.ls.LSResourceResolver;
> 
> 
> public class Error {
>      public static void main(String[] args) {
>      if (args.length != 1) {
>           System.out.println("Usage: java Error <instance file>");
>           System.exit(0);
>      }
>      String instanceFile = args[0];
>      try {
>           System.setProperty(DOMImplementationRegistry.PROPERTY,
>               "org.apache.xerces.dom.DOMImplementationSourceImpl");
>           DOMImplementationRegistry registry =
>           DOMImplementationRegistry.newInstance();
>           final DOMImplementationLS domImplementationLS =
>           (DOMImplementationLS)registry.getDOMImplementation("LS");
>           LSParser parser =
>           domImplementationLS.createLSParser(DOMImplementationLS.
> MODE_SYNCHRONOUS,
>                          "http://www.w3.org/2001/XMLSchema");
>           parser.getDomConfig().setParameter("http://apache.
> org/xml/features/xinclude",
>                     Boolean.TRUE);
>           parser.getDomConfig().setParameter("resource-resolver",
>                     new TestResolver(domImplementationLS));
>           parser.getDomConfig().setParameter("error-handler", new
> DOMErrorHandler() {
>           public boolean handleError(DOMError error) {
> ((Exception)error.getRelatedException()).printStackTrace();
>                throw new LSException(LSException.PARSE_ERR, error.
> getMessage());
>           }
>           });
> 
>           LSInput input = domImplementationLS.createLSInput();
>           LSOutput output = domImplementationLS.createLSOutput();
>           input.setByteStream(new FileInputStream(instanceFile));
>           output.setByteStream(System.out);
>           Document doc = parser.parse(input);
>           domImplementationLS.createLSSerializer().write(doc, output);
>           System.err.println();
>      } catch (ClassNotFoundException e) {
>           System.err.println("Unable to find DOM Parser: " + 
e.getMessage());
>           System.exit(1);
>      } catch (InstantiationException e) {
>           System.err.println("Unable to find DOM Parser: " + 
e.getMessage());
>           System.exit(1);
>      } catch (IllegalAccessException e) {
>           System.err.println("Unable to find DOM Parser: " + 
e.getMessage());
>           System.exit(1);
>      } catch (FileNotFoundException e) {
>           System.err.println("Unable to find file: " + instanceFile);
>           System.exit(1);
>      }
>      catch (Throwable t) {
>           t.printStackTrace();
>           System.exit(1);
>      }
>      }
> }
> 
> class TestResolver implements LSResourceResolver {
>      private DOMImplementationLS domImplementationLS;
> 
>      public TestResolver(DOMImplementationLS domImplementationLS) {
>      this.domImplementationLS = domImplementationLS;
>      }
> 
>      public LSInput resolveResource(String type,
>                 String namespaceURI,
>                 String publicId,
>                 String systemId,
>                 String baseURI) {
>       System.err.println("==== Resolving '" + type + "' '" + 
namespaceURI +
> "' '" +
>                publicId + "' '" + systemId + "' '" + baseURI + "'");
> 
>      LSInput ret = domImplementationLS.createLSInput();
>      FileInputStream is = null;
>      try {
>           is = new FileInputStream((new URL(systemId)).getPath());
>      } catch (IOException e) {
>           e.printStackTrace();
>           System.exit(1);
>      }
>      ret.setByteStream(is);
>      return ret;
> //     return null;
>      }
> }
> 
> ====================
> Example xml file 'err.xml'
> ====================
> <?xml version="1.0"?>
> <elem xmlns:xi="http://www.w3.org/2001/XInclude">
>     <xi:include href="file:errinclude.xml"/>
> </elem>
> 
> ====================
> Example xml file 'errinclude.xml'
> ====================
> <anotherElement>Some text for example</anotherElement>
> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: j-users-unsubscribe@xerces.apache.org
> For additional commands, e-mail: j-users-help@xerces.apache.org
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: j-users-unsubscribe@xerces.apache.org
For additional commands, e-mail: j-users-help@xerces.apache.org