http://nagoya.apache.org/bugzilla/show_bug.cgi?id=1830

*** shadow/1830 Thu Jun 14 22:35:00 2001
--- shadow/1830.tmp.22436       Fri Jun 15 00:34:35 2001
***************
*** 2,9 ****
  | EntityResolver not propagated to new XMLReader                             |
  +----------------------------------------------------------------------------+
  |        Bug #: 1830                        Product: XalanJ2                 |
! |       Status: RESOLVED                    Version: 2.0.1                   |
! |   Resolution: FIXED                      Platform: All                     |
  |     Severity: Normal                   OS/Version: All                     |
  |     Priority: Low                       Component: org.apache.xpath        |
  +----------------------------------------------------------------------------+
--- 2,9 ----
  | EntityResolver not propagated to new XMLReader                             |
  +----------------------------------------------------------------------------+
  |        Bug #: 1830                        Product: XalanJ2                 |
! |       Status: REOPENED                    Version: 2.0.1                   |
! |   Resolution:                            Platform: All                     |
  |     Severity: Normal                   OS/Version: All                     |
  |     Priority: Low                       Component: org.apache.xpath        |
  +----------------------------------------------------------------------------+
***************
*** 181,183 ****
--- 181,354 ----
  the routine to construct the new XMLReader now obtains the most recently used 
  EntityResolver for this DTMManagerDefault, if any, and carries that over to the 
  new XMLReader.
+ 
+ ------- Additional Comments From [EMAIL PROTECTED]  2001-06-15 00:34 -------
+ Vladimir --
+ I am reopening this bug after input from Scott Boag with which I agree.  He 
+ rightly points out that there is nothing in the specs that says that the 
+ EntityResolver needs to be propagated to XMLReaders created in order to read 
+ files brought in with the document() function.  If we propagate the 
+ EntityResolver, what else should we also propagate, the DTDHandler, the 
+ ErrorHandler, the features and properties?  And what if propagation is not 
+ what's wanted?
+ 
+ As he reminded me, this is exactly what the URIResolver was created for.  When 
+ accessing a new URI, it gives you the opportunity to supply your own source, 
+ which may be a SAXSource that contains its own XMLReader.  This way you have 
+ access to controlling -all- of the features and options, not just the 
+ EntityResolver.  I've included a modified version of SimpleURIResolver to 
+ demonstrate how your URIResolver should be changed to achieve the behavior that 
+ you desire.
+ 
+ I would be very interested in your feedback and that of others that have run 
+ into this problem.  I realize that this is more coding work for you and others 
+ in your situation but it is a lot cleaner than simply propagating the 
+ EntityResolver, I think.
+ 
+ The code that I've included in the first example is what Xalan goes through to 
+ create a new XMLReader.  If you have simpler requirements, you can of course 
+ eliminate some of the generality in the Xalan solution and just create your own 
+ Xerces XMLReader, say, by calling
+   new SAXParser();
+ I'll indicate that in the second sample.
+ 
+ Sample URIResolver:
+ import java.io.*;
+ import javax.xml.transform.URIResolver;
+ import javax.xml.transform.Source;
+ import javax.xml.transform.stream.StreamSource;
+ 
+ import javax.xml.transform.sax.SAXSource;
+ import org.xml.sax.InputSource;
+ import org.xml.sax.XMLReader;
+ import org.xml.sax.helpers.XMLReaderFactory;
+ import javax.xml.transform.TransformerException;
+ 
+ public class SimpleURIResolver implements URIResolver {
+ 
+   public Source resolve(String href, String base) throws TransformerException
+ 
+   {
+ 
+     System.out.println("Called href " + href + " ,base " + base);
+ 
+     InputStream in = null;
+     XMLReader myReader;
+ 
+     try
+     {
+       in = new FileInputStream(href);
+     }
+     catch (IOException ioe)
+     {
+       System.out.println("Did not " + href);
+       return null;
+     }
+ 
+     // StreamSource source = new StreamSource(in);
+ 
+     try
+     {
+       javax.xml.parsers.SAXParserFactory factory =
+                 javax.xml.parsers.SAXParserFactory.newInstance();
+ 
+       factory.setNamespaceAware(true);
+ 
+       javax.xml.parsers.SAXParser jaxpParser = factory.newSAXParser();
+ 
+       myReader = jaxpParser.getXMLReader();
+ 
+       if (null == myReader)
+         myReader = XMLReaderFactory.createXMLReader();
+       }
+     catch (javax.xml.parsers.ParserConfigurationException ex)
+     {
+       throw new TransformerException(ex);
+     }
+     catch (javax.xml.parsers.FactoryConfigurationError ex1)
+     {
+       throw new TransformerException(ex1);
+     }
+     catch (org.xml.sax.SAXException saxe)
+     {
+       throw new TransformerException(saxe);
+     }
+ 
+     try
+     {
+       myReader.setFeature("http://xml.org/sax/features/namespace-prefixes";,
+                       true);
+     }
+     catch (org.xml.sax.SAXException se) {}
+ 
+     try
+     {
+       myReader.setFeature("http://apache.org/xml/features/validation/dynamic";,
+                       true);
+     }
+     catch (org.xml.sax.SAXException se) {}
+ 
+     myReader.setEntityResolver(new SimpleEntityResolver());
+ 
+     SAXSource source = new SAXSource(myReader, new InputSource(in));
+     source.setSystemId(href);
+     return source;
+   }
+ }
+ 
+ =========================================================================
+ Simpler URIResolver:
+ import java.io.*;
+ import javax.xml.transform.URIResolver;
+ import javax.xml.transform.Source;
+ import javax.xml.transform.stream.StreamSource;
+ 
+ import javax.xml.transform.sax.SAXSource;
+ import org.xml.sax.InputSource;
+ import org.xml.sax.XMLReader;
+ import org.xml.sax.helpers.XMLReaderFactory;
+ import javax.xml.transform.TransformerException;
+ 
+ import org.apache.xerces.parsers.SAXParser;
+ 
+ public class SimpleURIResolver implements URIResolver {
+ 
+   public Source resolve(String href, String base) throws TransformerException
+ 
+   {
+ 
+     System.out.println("Called href " + href + " ,base " + base);
+ 
+     InputStream in = null;
+     XMLReader myReader;
+ 
+     try
+     {
+       in = new FileInputStream(href);
+     }
+     catch (IOException ioe)
+     {
+       System.out.println("Did not " + href);
+       return null;
+     }
+ 
+     // StreamSource source = new StreamSource(in);
+ 
+     try
+     {
+       myReader = new SAXParser();
+       myReader.setNamespaces(true);
+       myReader.setNamespacePrefixes(true);
+       myReader.setValidationDynamic(true);
+       myReader.setEntityResolver(new SimpleEntityResolver());
+     }
+     catch (org.xml.sax.SAXException saxe)
+     {
+       throw new TransformerException(saxe);
+     }
+ 
+     SAXSource source = new SAXSource(myReader, new InputSource(in));
+     source.setSystemId(href);
+     return source;
+   }
+ }
\ No newline at end of file

Reply via email to