I'd suggest changing your DTDEntityResoler to accept a java.io.File in the contructor or a file name and then construct and use a FileReader and InputSource on the call to getExternalSubset.
-Nathan On Tue, Jul 29, 2008 at 8:26 AM, Nida Duckett <[EMAIL PROTECTED]>wrote: > Thank you for your quick response. > > The problem is that in my EntityResolver, I don't create a new FileReader. > I create a new FileReader only when I construct the EntityResolver, and that > is only done once during initialization. When I submit the first input XML > file, it returns the expected validation errors. When I submit a second XML > file (the same one), the code encounters this error: > > javax.xml.transform.TransformerException: java.io.IOException: Stream > closed > Here is my code (please forgive how amateur it is -- I am new to this). > > final String ENTITY_RESOLVER_2_ID = " > http://xml.org/sax/features/use-entity-resolver2"; > > SAXParserFactory factory = SAXParserFactory.newInstance(); > factory.setFeature(ENTITY_RESOLVER_2_ID, true); > factory.setValidating( true ); > SAXParser parser = factory.newSAXParser(); > > reader = parser.getXMLReader(); > reader.setEntityResolver(new DTDEntityResolver(new > FileReader("MYDTD.dtd"))); > reader.setFeature( "http://xml.org/sax/features/validation", true ); > > ... > > > > And here is the EntityResolver class definition : > > public class DTDEntityResolver implements EntityResolver2 > { > InputSource input; > > public DTDEntityResolver(Reader dtd) { input = new InputSource(dtd); } > > public InputSource getExternalSubset(String name, String baseURI) throws > SAXException { return input; } > > public InputSource resolveEntity(String publicId, String systemId) { > return null; } > > public InputSource resolveEntity(String name, String publicId, String > baseURI, > String systemId) throws SAXException { return null; } > } > > > Any help would be appreciated. Thanks in advance. > > Nida > > > > On 7/28/08, Michael Glavassevich <[EMAIL PROTECTED]> wrote: >> >> Hello Nida, >> >> In general InputStream and Reader objects can only be consumed once. You >> should create and return a new FileReader (or whatever) each time your >> EntityResolver is called. >> >> Thanks. >> >> Michael Glavassevich >> XML Parser Development >> IBM Toronto Lab >> E-mail: [EMAIL PROTECTED] >> E-mail: [EMAIL PROTECTED] >> >> "Nida Duckett" <[EMAIL PROTECTED]> wrote on 07/28/2008 04:33:21 PM: >> >> > Hello, >> >> > >> > I am trying to set up a SAXParser to parse and validate any input >> > XML file that contains both DOCTYPE and no DOCTYPE definitions using >> > a DTD that I specify. After combing through the mailing list >> > archives, I was able to get it working (thanks to posting >> > 9e9da3a00702250947n1571e875g92fbe3aa030b23c7 () mail ! gmail ! com). >> > >> > However, the posting also warned about it only works for one >> > document and will not work for subsequent documents. I was >> > wondering if any workaround or fix has been found to resolve that issue. >> >> > >> > Thanks in advance. >> > >> > Nida >> > >> > Here is the thread from that old posting for reference: >> > >> > List: xerces-j-dev >> > Subject: Re: Specifying a DTD file when parsing XML files that >> > don't contain !DOCTYPE lines >> > From: "Will Holcomb" <wholcomb () gmail ! com> >> > Date: 2007-02-25 17:47:29 >> > Message-ID: 9e9da3a00702250947n1571e875g92fbe3aa030b23c7 () mail ! gmail >> ! com >> > [Download message RAW] >> > >> > One more note about a bug I found in this code. It only works once. When >> you >> > parse a second document, you get an exception because the FileReader for >> the >> > DTD has already been closed. >> > >> > Will Holcomb >> > >> > On 2/25/07, Will Holcomb <[EMAIL PROTECTED]> wrote: >> > > >> > > I happened to send a message to this list asking this exact question >> > > > earlier this week. I figure I'll pass on the kindness someone >> > showed me and >> > > > answer it this time/ (Perhaps this should go in the FAQ because it >> isn't >> > > > really intuitive.) >> > > > >> > > > final static String ENTITY_RESOLVER_2_ID = "http://xml. >> > org/sax/features/use-entity-resolver2"; >> > > > >> > > > DocumentBuilderFactory dbFactory = >> DocumentBuilderFactory.newInstance(); >> > > > dbFactory.setFeature(ENTITY_RESOLVER_2_ID, true); >> > > > DocumentBuilder builder = dbFactory.newDocumentBuilder(); >> > > > builder.setEntityResolver(new DTDEntityResolver(new >> > > > FileReader(dtdFilename))); >> > > > >> > > > public class DTDEntityResolver implements EntityResolver2 { >> > > > InputSource input; >> > > > public DTDEntityResolver(Reader dtd) { input = new >> InputSource(dtd); } >> > > > public InputSource getExternalSubset(String name, String baseURI) >> > > >> > > throws SAXException { return input; } >> > > > public InputSource resolveEntity(String publicId, String >> > systemId) {return null;} >> > > > public InputSource resolveEntity(String name, String publicId, >> String >> > > > baseURI, String systemId) >> > > > throws SAXException { return null; } >> > > > } >> >> >
