Stream Closed on xsl transformation

View: New views
10 Messages — Rating Filter:   Alert me  

Parent Message unknown Stream Closed on xsl transformation

by Trenton D. Adams :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi Guys,

I'm having some strange problems with a stream being closed when I do an XSL transformation on an XML document that has an xml-stylesheet that is at a remote location.  If I instead put it locally, and and specify it as an xsltSource, it works fine.

I have also narrowed it down to the following working...
            final Document doc = stringToDocument((String) xml);
            logger.debug(documentToString(doc));
            xmlSource = new DOMSource(doc);

But not this...
            xmlSource = new StreamSource(new StringReader((String) xml));

Any ideas why this wouldn't work?

I get a stack trace like the following; where the final "Caused by" indicates it has a problem with the StringReader in the second example above.  But, my stringToDocument() method also does a StringReader.

Caused by: javax.xml.transform.TransformerException: java.io.IOException: Stream closed                            
        at org.apache.xalan.transformer.TransformerImpl.fatalError(TransformerImpl.java:741)                        
        at org.apache.xalan.transformer.TransformerImpl.transform(TransformerImpl.java:715)                        
        at org.apache.xalan.transformer.TransformerImpl.transform(TransformerImpl.java:1129)                        
        at org.apache.xalan.transformer.TransformerImpl.transform(TransformerImpl.java:1107)                        
        at ca.athabascau.apas.xml.XMLUtil.xslTransformation(XMLUtil.java:226)                                      
        at ca.athabascau.apas.xml.XMLUtil.xslTransformation(XMLUtil.java:251)                                      
        at ca.athabascau.apas.IncomingTranscripts.processTranscript(IncomingTranscripts.java:348)                  
        at ca.athabascau.apas.IncomingTranscripts.processWaitingTranscripts(IncomingTranscripts.java:107)          
        ... 28 more                                                                                                
Caused by: java.io.IOException: Stream closed                                                                      
        at java.io.StringReader.ensureOpen(StringReader.java:39)                                                    
        at java.io.StringReader.read(StringReader.java:73)                                                          
        at org.apache.xerces.impl.XMLEntityManager$EntityScanner.load(XMLEntityManager.java:3257)                  
        at org.apache.xerces.impl.XMLEntityManager$EntityScanner.skipString(XMLEntityManager.java:3017)            
        at org.apache.xerces.impl.XMLDocumentScannerImpl$XMLDeclDispatcher.dispatch(XMLDocumentScannerImpl.java:577)
        at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:333)                                                                                                                  
        at org.apache.xerces.parsers.DTDConfiguration.parse(DTDConfiguration.java:524)                              
        at org.apache.xerces.parsers.DTDConfiguration.parse(DTDConfiguration.java:580)                              
        at org.apache.xerces.parsers.XMLParser.parse(XMLParser.java:152)                                            
        at org.apache.xerces.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1169)                          
        at org.apache.xml.dtm.ref.DTMManagerDefault.getDTM(DTMManagerDefault.java:495)                              
        at org.apache.xalan.transformer.TransformerImpl.transform(TransformerImpl.java:658)                        
        ... 34 more                                                                                                


Trenton D. Adams
Systems Analyst/Web Software Engineer
Navy Penguins at your service!
Athabasca University
(780) 675-6195
:wq!

__
    This communication is intended for the use of the recipient to whom it
    is addressed, and may contain confidential, personal, and or privileged
    information. Please contact us immediately if you are not the intended
    recipient of this communication, and do not copy, distribute, or take
    action relying on it. Any communications received in error, or
    subsequent reply, should be deleted or destroyed.
---

Re: Stream Closed on xsl transformation

by Michael Ludwig-6 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi Trenton,

Trenton D. Adams schrieb am 05.11.2009 um 13:00:00 (-0700):

> I'm having some strange problems with a stream being closed when I do
> an XSL transformation on an XML document that has an xml-stylesheet
> that is at a remote location.  If I instead put it locally, and and
> specify it as an xsltSource, it works fine.
>
> I have also narrowed it down to the following working...
>             final Document doc = stringToDocument((String) xml);
>             logger.debug(documentToString(doc));
>             xmlSource = new DOMSource(doc);
>
> But not this...
>             xmlSource = new StreamSource(new StringReader((String) xml));

This approach works fine for me as in:

  Source src = new StreamSource(new StringReader("<Urmel/>"));

> I get a stack trace like the following; where the final "Caused by"
> indicates it has a problem with the StringReader in the second example
> above.  But, my stringToDocument() method also does a StringReader.

> Caused by: java.io.IOException: Stream closed                                                                      
>         at java.io.StringReader.ensureOpen(StringReader.java:39)                                                    
>         at java.io.StringReader.read(StringReader.java:73)                                                          
>         at org.apache.xerces.impl.XMLEntityManager$EntityScanner.load(XMLEntityManager.java:3257)

The exception seems to occur in a large program. There must be something
wrong in those lines that you haven't shown. Rather than posting the
massive program, try boiling it down to a small, self-contained test
case. If the error persists, you might want to post the small test
program.

Best,

Michael Ludwig

Re: Stream Closed on xsl transformation

by Trenton D. Adams :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi Michael,

----- "Michael Ludwig" <milu71@...> wrote:

> From: "Michael Ludwig" <milu71@...>
> To: xalan-j-users@...
> Sent: Thursday, November 5, 2009 6:08:04 PM GMT -07:00 US/Canada Mountain
> Subject: Re: Stream Closed on xsl transformation
>
> Hi Trenton,
>
> Trenton D. Adams schrieb am 05.11.2009 um 13:00:00 (-0700):
>
> > I'm having some strange problems with a stream being closed when I
> do
> > an XSL transformation on an XML document that has an xml-stylesheet
> > that is at a remote location.  If I instead put it locally, and and
> > specify it as an xsltSource, it works fine.
> >
> > I have also narrowed it down to the following working...
> >             final Document doc = stringToDocument((String) xml);
> >             logger.debug(documentToString(doc));
> >             xmlSource = new DOMSource(doc);
> >
> > But not this...
> >             xmlSource = new StreamSource(new StringReader((String)
> xml));
>
> This approach works fine for me as in:
>
>   Source src = new StreamSource(new StringReader("<Urmel/>"));

Yeah, that wouldn't be a problem, as it does not have an xml-stylesheet processing instruction.

>
> > I get a stack trace like the following; where the final "Caused by"
> > indicates it has a problem with the StringReader in the second
> example
> > above.  But, my stringToDocument() method also does a StringReader.
>
> > Caused by: java.io.IOException: Stream closed                      
>                                                
> >         at java.io.StringReader.ensureOpen(StringReader.java:39)    
>                                                
> >         at java.io.StringReader.read(StringReader.java:73)          
>                                                
> >         at
> org.apache.xerces.impl.XMLEntityManager$EntityScanner.load(XMLEntityManager.java:3257)
>
> The exception seems to occur in a large program. There must be
> something
> wrong in those lines that you haven't shown. Rather than posting the
> massive program, try boiling it down to a small, self-contained test
> case. If the error persists, you might want to post the small test
> program.

Yes, I did narrow it down to a small test case (or semi-small, the xml/xslt isn't so small).  I will see if I can provide it when I get to work tomorrow.  I'll also see if I can make it happen with a smaller XML/XSLT.

>
> Best,
>
> Michael Ludwig

__
    This communication is intended for the use of the recipient to whom it
    is addressed, and may contain confidential, personal, and or privileged
    information. Please contact us immediately if you are not the intended
    recipient of this communication, and do not copy, distribute, or take
    action relying on it. Any communications received in error, or
    subsequent reply, should be deleted or destroyed.
---

Parent Message unknown Re: Stream Closed on xsl transformation

by Trenton D. Adams :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi Michael

Here is a 10K tar.gz file of a test case with a maven build project.  Run with the "test" profile, as in...

mvn -P test install

The full stack trace is in xml-util.log.

Take note of the comment in XMLUtil.java:167, as I can get it to work if I convert to a DOMSource instead.

http://www.trentonadams.ca/xml-util.tar.gz

Thanks.

Trenton D. Adams
Systems Analyst/Web Software Engineer
Navy Penguins at your service!
Athabasca University
(780) 675-6195
:wq!

----- "Michael Ludwig" <milu71@...> wrote:

> From: "Michael Ludwig" <milu71@...>
> To: xalan-j-users@...
> Sent: Thursday, November 5, 2009 6:08:04 PM GMT -07:00 US/Canada Mountain
> Subject: Re: Stream Closed on xsl transformation
>
> Hi Trenton,
>
> Trenton D. Adams schrieb am 05.11.2009 um 13:00:00 (-0700):
>
> > I'm having some strange problems with a stream being closed when I
> do
> > an XSL transformation on an XML document that has an xml-stylesheet
> > that is at a remote location.  If I instead put it locally, and and
> > specify it as an xsltSource, it works fine.
> >
> > I have also narrowed it down to the following working...
> >             final Document doc = stringToDocument((String) xml);
> >             logger.debug(documentToString(doc));
> >             xmlSource = new DOMSource(doc);
> >
> > But not this...
> >             xmlSource = new StreamSource(new StringReader((String)
> xml));
>
> This approach works fine for me as in:
>
>   Source src = new StreamSource(new StringReader("<Urmel/>"));
>
> > I get a stack trace like the following; where the final "Caused by"
> > indicates it has a problem with the StringReader in the second
> example
> > above.  But, my stringToDocument() method also does a StringReader.
>
> > Caused by: java.io.IOException: Stream closed                      
>                                                
> >         at java.io.StringReader.ensureOpen(StringReader.java:39)    
>                                                
> >         at java.io.StringReader.read(StringReader.java:73)          
>                                                
> >         at
> org.apache.xerces.impl.XMLEntityManager$EntityScanner.load(XMLEntityManager.java:3257)
>
> The exception seems to occur in a large program. There must be
> something
> wrong in those lines that you haven't shown. Rather than posting the
> massive program, try boiling it down to a small, self-contained test
> case. If the error persists, you might want to post the small test
> program.
>
> Best,
>
> Michael Ludwig

__
    This communication is intended for the use of the recipient to whom it
    is addressed, and may contain confidential, personal, and or privileged
    information. Please contact us immediately if you are not the intended
    recipient of this communication, and do not copy, distribute, or take
    action relying on it. Any communications received in error, or
    subsequent reply, should be deleted or destroyed.
---

Re: Stream Closed on xsl transformation

by Trenton D. Adams :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I do have the java 1.4 restriction set in the pom, but I figured I'd give you my exact java version...

java version "1.4.2-03"
Java(TM) 2 Runtime Environment, Standard Edition (build Blackdown-1.4.2-03)
Java HotSpot(TM) 64-Bit Server VM (build Blackdown-1.4.2-03, mixed mode)


Trenton D. Adams
Systems Analyst/Web Software Engineer
Navy Penguins at your service!
Athabasca University
(780) 675-6195
:wq!

----- "Trenton D. Adams" <trenta@...> wrote:

> From: "Trenton D. Adams" <trenta@...>
> To: "Michael Ludwig" <milu71@...>
> Cc: xalan-j-users@...
> Sent: Saturday, November 7, 2009 4:09:07 PM GMT -07:00 US/Canada Mountain
> Subject: Re: Stream Closed on xsl transformation
>
> Hi Michael
>
> Here is a 10K tar.gz file of a test case with a maven build project.
> Run with the "test" profile, as in...
>
> mvn -P test install
>
> The full stack trace is in xml-util.log.
>
> Take note of the comment in XMLUtil.java:167, as I can get it to work
> if I convert to a DOMSource instead.
>
> http://www.trentonadams.ca/xml-util.tar.gz
>
> Thanks.
>
> Trenton D. Adams
> Systems Analyst/Web Software Engineer
> Navy Penguins at your service!
> Athabasca University
> (780) 675-6195
> :wq!
>
> ----- "Michael Ludwig" <milu71@...> wrote:
>
> > From: "Michael Ludwig" <milu71@...>
> > To: xalan-j-users@...
> > Sent: Thursday, November 5, 2009 6:08:04 PM GMT -07:00 US/Canada
> Mountain
> > Subject: Re: Stream Closed on xsl transformation
> >
> > Hi Trenton,
> >
> > Trenton D. Adams schrieb am 05.11.2009 um 13:00:00 (-0700):
> >
> > > I'm having some strange problems with a stream being closed when
> I
> > do
> > > an XSL transformation on an XML document that has an
> xml-stylesheet
> > > that is at a remote location.  If I instead put it locally, and
> and
> > > specify it as an xsltSource, it works fine.
> > >
> > > I have also narrowed it down to the following working...
> > >             final Document doc = stringToDocument((String) xml);
> > >             logger.debug(documentToString(doc));
> > >             xmlSource = new DOMSource(doc);
> > >
> > > But not this...
> > >             xmlSource = new StreamSource(new
> StringReader((String)
> > xml));
> >
> > This approach works fine for me as in:
> >
> >   Source src = new StreamSource(new StringReader("<Urmel/>"));
> >
> > > I get a stack trace like the following; where the final "Caused
> by"
> > > indicates it has a problem with the StringReader in the second
> > example
> > > above.  But, my stringToDocument() method also does a
> StringReader.
> >
> > > Caused by: java.io.IOException: Stream closed                    
>  
> >                                                
> > >         at java.io.StringReader.ensureOpen(StringReader.java:39)  
>  
> >                                                
> > >         at java.io.StringReader.read(StringReader.java:73)        
>  
> >                                                
> > >         at
> >
> org.apache.xerces.impl.XMLEntityManager$EntityScanner.load(XMLEntityManager.java:3257)
> >
> > The exception seems to occur in a large program. There must be
> > something
> > wrong in those lines that you haven't shown. Rather than posting
> the
> > massive program, try boiling it down to a small, self-contained
> test
> > case. If the error persists, you might want to post the small test
> > program.
> >
> > Best,
> >
> > Michael Ludwig
>
> __
>     This communication is intended for the use of the recipient to
> whom it
>     is addressed, and may contain confidential, personal, and or
> privileged
>     information. Please contact us immediately if you are not the
> intended
>     recipient of this communication, and do not copy, distribute, or
> take
>     action relying on it. Any communications received in error, or
>     subsequent reply, should be deleted or destroyed.
> ---

Re: Stream Closed on xsl transformation

by Michael Ludwig-6 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Trenton D. Adams schrieb am 07.11.2009 um 16:09:07 (-0700):
> Hi Michael
>
> Here is a 10K tar.gz file of a test case with a maven build project.

Hi Trenton,

it looks like the attachment didn't make it to the list. Anyway, 10 KB
of gzipped text is not exactly small. I would have thought of a simple
main() style test case.

Best,

--
Michael Ludwig

Re: Stream Closed on xsl transformation

by bimargulies :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Apache lists strip attachments. I suppose you could open a JIRA and attach it there.

On Wed, Nov 11, 2009 at 7:10 AM, Michael Ludwig <milu71@...> wrote:
Trenton D. Adams schrieb am 07.11.2009 um 16:09:07 (-0700):
> Hi Michael
>
> Here is a 10K tar.gz file of a test case with a maven build project.

Hi Trenton,

it looks like the attachment didn't make it to the list. Anyway, 10 KB
of gzipped text is not exactly small. I would have thought of a simple
main() style test case.

Best,

--
Michael Ludwig


Parent Message unknown Re: Stream Closed on xsl transformation

by Trenton D. Adams :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I didn't send an attachment, I sent a link.  Do links get removed too?

http://         www.trentonadams.ca       /xml-util.tar.gz

As for it being 10k, the important part is that it does not work with a processing instruction of "<?xml-stylesheet...?>", but does work without one (i.e. specifying one in the java code).  It's not the XML/XSLT that is important to look at, but the Java code, which is very small, and easy to follow, especially considering it's a simple JUnit test.

Thanks.

Trenton D. Adams
Systems Analyst/Web Software Engineer
Navy Penguins at your service!
Athabasca University
(780) 675-6195
:wq!

----- "Michael Ludwig" <milu71@...> wrote:

> From: "Michael Ludwig" <milu71@...>
> To: xalan-j-users@...
> Sent: Wednesday, November 11, 2009 5:10:52 AM GMT -07:00 US/Canada Mountain
> Subject: Re: Stream Closed on xsl transformation
>
> Trenton D. Adams schrieb am 07.11.2009 um 16:09:07 (-0700):
> > Hi Michael
> >
> > Here is a 10K tar.gz file of a test case with a maven build
> project.
>
> Hi Trenton,
>
> it looks like the attachment didn't make it to the list. Anyway, 10
> KB
> of gzipped text is not exactly small. I would have thought of a
> simple
> main() style test case.
>
> Best,
>
> --
> Michael Ludwig

__
    This communication is intended for the use of the recipient to whom it
    is addressed, and may contain confidential, personal, and or privileged
    information. Please contact us immediately if you are not the intended
    recipient of this communication, and do not copy, distribute, or take
    action relying on it. Any communications received in error, or
    subsequent reply, should be deleted or destroyed.
---

Re: Stream Closed on xsl transformation

by Michael Ludwig-6 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Trenton D. Adams schrieb am 12.11.2009 um 16:19:35 (-0700):
> I didn't send an attachment, I sent a link.  Do links get removed too?

I must have been very tired when looking for the attachment.

> As for it being 10k, the important part is that it does not work with
> a processing instruction of "<?xml-stylesheet...?>", but does work
> without one (i.e. specifying one in the java code).  It's not the
> XML/XSLT that is important to look at, but the Java code, which is
> very small, and easy to follow, especially considering it's a simple
> JUnit test.

Maybe, but it is way more complex than it should have to be.

Anyway, I get two failures because of:

  junit.framework.AssertionFailedError
    caused by
  javax.xml.transform.TransformerException
    caused by
  java.net.UnknownHostException

And it is little wonder as your stylesheet PI misses a valid URI:

  <?xml-stylesheet type="text/xsl"
    href="file://${basedir}/target/test-classes/transcript.xsl"?>

Corrected this to:

  http://localhost/g/xml-util/target/test-classes/transcript.xsl

Now:

  junit.framework.AssertionFailedError
    caused by
  java.io.IOException: Stream closed

Okay, the error is in reading the PI from the StreamSource by calling
getAssociatedStylesheet(), and then trying to read again from the same
StreamSource by calling transform(). Streams are to be used once only.

Fixing this in your program is cumbersome, because you have one big
method with parameters of type Object which you then cast to whatever
you need based on what instanceof tells you.

  public static String xslTransformation(
    final Object xml,
    final Object xsl,
    final Map parameters )

What's wrong with using polymorphism instead? It's vastly superior.

Also, please consider providing simple test cases. You'd have discovered
this error yourself if you had taken the time to build a simple test
case without so many complicating ingredients. It bears repeating here
that simplifying is a basic problem solving technique.

Best,
--
Michael Ludwig

Re: Stream Closed on xsl transformation

by Trenton D. Adams :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

----- "Michael Ludwig" <milu71@...> wrote:

> From: "Michael Ludwig" <milu71@...>
> To: xalan-j-users@...
> Sent: Thursday, November 12, 2009 7:08:24 PM GMT -07:00 US/Canada Mountain
> Subject: Re: Stream Closed on xsl transformation
>
> Trenton D. Adams schrieb am 12.11.2009 um 16:19:35 (-0700):
> > I didn't send an attachment, I sent a link.  Do links get removed
> too?
>
> I must have been very tired when looking for the attachment.
>
> > As for it being 10k, the important part is that it does not work
> with
> > a processing instruction of "<?xml-stylesheet...?>", but does work
> > without one (i.e. specifying one in the java code).  It's not the
> > XML/XSLT that is important to look at, but the Java code, which is
> > very small, and easy to follow, especially considering it's a
> simple
> > JUnit test.
>
> Maybe, but it is way more complex than it should have to be.
>
> Anyway, I get two failures because of:
>
>   junit.framework.AssertionFailedError
>     caused by
>   javax.xml.transform.TransformerException
>     caused by
>   java.net.UnknownHostException
>
> And it is little wonder as your stylesheet PI misses a valid URI:
>
>   <?xml-stylesheet type="text/xsl"
>     href="file://${basedir}/target/test-classes/transcript.xsl"?>
>
> Corrected this to:
>
>   http://localhost/g/xml-util/target/test-classes/transcript.xsl

Yeah, that's because it's setup to use maven filtering.  You have to actually run it through maven to work.  It replaces "basedir" with an appropriate path.

>
> Now:
>
>   junit.framework.AssertionFailedError
>     caused by
>   java.io.IOException: Stream closed
>
> Okay, the error is in reading the PI from the StreamSource by calling
> getAssociatedStylesheet(), and then trying to read again from the
> same
> StreamSource by calling transform(). Streams are to be used once
> only.

Oh yes, in my rush to get a project done here, I hadn't though of the fact that you can't use the source twice; which is duh, it uses a stream, hehe.  Silly me.

>
> Fixing this in your program is cumbersome, because you have one big
> method with parameters of type Object which you then cast to whatever
> you need based on what instanceof tells you.
>
>   public static String xslTransformation(
>     final Object xml,
>     final Object xsl,
>     final Map parameters )
>
> What's wrong with using polymorphism instead? It's vastly superior.

Well, v0.1.X is just a hack to get things done until I have time to fix it all up properly.  That is initial development, it isn't always pretty.

>
> Also, please consider providing simple test cases. You'd have
> discovered
> this error yourself if you had taken the time to build a simple test
> case without so many complicating ingredients. It bears repeating
> here
> that simplifying is a basic problem solving technique.

I don't think it had much to do with the complexity of the code, as it isn't very complex.  It had a lot more to do with rushing through.

Thanks for the suggestions though.

__
    This communication is intended for the use of the recipient to whom it
    is addressed, and may contain confidential, personal, and or privileged
    information. Please contact us immediately if you are not the intended
    recipient of this communication, and do not copy, distribute, or take
    action relying on it. Any communications received in error, or
    subsequent reply, should be deleted or destroyed.
---