logo       

Re: XInclude and LSResourceResolver problem.: msg#00026

Subject: Re: XInclude and LSResourceResolver problem.
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@xxxxxxxxxx

alexius@xxxxxxxxxxx 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@xxxxxxxxxxxxxxxxx
> For additional commands, e-mail: j-users-help@xxxxxxxxxxxxxxxxx
> 


<Prev in Thread] Current Thread [Next in Thread>
Google Custom Search

Recently Viewed:
boot-loaders.gr...    php.pear.genera...    debugging.valgr...    kde.redhat.user...    text.xml.xsl.ge...    culture.languag...    hardware.microc...    java.servicemix...    redhat.release....    web.zope.plone....    user-groups.lin...    opendarwin.webk...    video.mjpeg.use...    sysutils.bcfg2....    encryption.gpg....    lx-office.devel...    xfree86.forum/2...    mail.mutt.devel...    acpi.devel/2003...    qnx.openqnx.dev...    network.irc.irs...    freebsd.devel.m...   
Home | blog view | USPTO Patent Archive | advertise | OSDir is an inevitable website. super tiny logo

Free Magazines

Cisco News
Receive a free quarterly e-newsletter with exclusive articles on how Cisco IT uses its own products and solutions to enable the business.
subscribe

Systems Management News, the newspaper for IT systems administration and data center managers! Each issue of Systems Management News is chock-full of news and analysis to help you understand what's happening in your field.
subscribe

The Enterprise Newsweekly eWeek is the essential technology information source for builders of e-business.
subscribe

Oracle Magazine Oracle Magazine contains technology strategy articles, sample code, tips, Oracle and partner news, how to articles for developers and DBAs, and more. Oracle (NASDAQ: ORCL) is the world's largest enterprise software company.
subscribe

Total Telecom Total Telecom is "The Economist of the communications industry".
subscribe