You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-users@xalan.apache.org by Thomas Nichols <nx...@yahoo.co.uk> on 2002/06/19 17:39:14 UTC

newbie: converting a Result to a Source

Hi,
newbie question about using the JAXP API using Xalan - apologies if this is 
a generic JAXP query, please redirect me if appropriate.

I'm using an API which has a foo (javax.xml.Source doc1, javax.xml.Source 
doc2, javax.xml.Result res) method. I need to take the Result, and have it 
become one of the Source params for a second invocation of foo(). How can I 
accomplish this, short of serializing the data out to a StringBuffer and 
reading it back in again?

I've looked through the Pipe and XMLFilter examples - which are splendid, 
but rely on Transformer instances, AFAICT. From a recent discussion on 
xalan-j-users I've also checked out
http://xml.apache.org/xalan-j/extensionslib.html#pipedocument
-- but I need a generic JAXP solution, this seems Xalan-specific.


The only solution I can currently see is to write a
FooTransformer extends Transformer {
     private FooHelper fooHelper;
     private Source source;
     FooTransformer (FooHelper fh, Source s) {
         fooHelper = fh;
         source = s;
     }
     ...
     void transform(Source s, Result r) {
         fooHelper.foo (source, s, r);
     }
}

I could then use a FooTransformer as per the Pipe or XMLFilter examples.

Is this brain-dead - is there some obvious solution I'm missing here?

And if this is a workable approach, what are the relative benefits of the 
push (Pipe) and pull (XMLFilter) approaches?

Thanks in advance for any pointers. Xalan-J is a truly excellent package, 
many thanks to everyone who's contributed.

Best Regards,
Thomas.


Re: newbie: converting a Result to a Source

Posted by Thomas Nichols <nx...@yahoo.co.uk>.
Jens,

Thanks, this is a great help.
         - Thomas.

At 15:45 26/06/2002 +0200, Jens v. Pilgrim wrote:
>Hello Thomas Nichols,
>
>Wednesday, June 19, 2002, 5:39:14 PM, Thomas Nichols wrote:
>
>TN> I need to take the Result, and have it
>TN> become one of the Source params for a second invocation of foo(). How 
>can I
>TN> accomplish this, short of serializing the data out to a StringBuffer and
>TN> reading it back in again?
>
>I'm not sure how you generate the first call to foo() and how your
>documents look like. But I think the easiest way to create a Source
>from a Result is by usng DOMSource and DOMResult. Since you're
>creating the Source and the Result parameter when passing the
>instances to the transformer, it's no problem:
>
>----------8X----------8X----------8X----------8X----------8X----------
>Transformer trans = [some transformer instance];
>Source    initialSource = [some Source];
>DOMResult myDOMResult = new javax.xml.transform.dom.Result();
>
>trans.transform(initialSource, myDOMResult);
>
>DOMSource myDOMSource =
>           new javax.xml.transform.dom.Source(myDOMResult.getNode());
>
>// .. and here we go ...
>----------8X----------8X----------8X----------8X----------8X----------
>
>Best regards,
>  Jens


Re: newbie: converting a Result to a Source

Posted by "Jens v. Pilgrim" <je...@jevopi.de>.
Hello Thomas Nichols,

Wednesday, June 19, 2002, 5:39:14 PM, Thomas Nichols wrote:

TN> I need to take the Result, and have it
TN> become one of the Source params for a second invocation of foo(). How can I 
TN> accomplish this, short of serializing the data out to a StringBuffer and 
TN> reading it back in again?

I'm not sure how you generate the first call to foo() and how your
documents look like. But I think the easiest way to create a Source
from a Result is by usng DOMSource and DOMResult. Since you're
creating the Source and the Result parameter when passing the
instances to the transformer, it's no problem:

----------8X----------8X----------8X----------8X----------8X----------
Transformer trans = [some transformer instance];
Source    initialSource = [some Source];
DOMResult myDOMResult = new javax.xml.transform.dom.Result();

trans.transform(initialSource, myDOMResult);

DOMSource myDOMSource =
          new javax.xml.transform.dom.Source(myDOMResult.getNode());

// .. and here we go ...
----------8X----------8X----------8X----------8X----------8X----------

Best regards,
 Jens