By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,806 Members | 1,457 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,806 IT Pros & Developers. It's quick & easy.

Create OutputStream based on ContentHandler

P: n/a
Hi,

Is there a wrapper class, or combination of classes, that will allow me
to do the following:

class Wrapper extends java.io.OutputStream {
public void setContentHandler(org.xml.sax.ContentHandler handler);
...
}

Or is there some reason why this won't work?

BEA appear to have something like this, with a
"weblogic.xml.stream.XMLOutputStream", but I'm wondering if it's
possible to create this with standard SAX components.

Cheers,

Barney

Mar 23 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a
ba******@iname.com wrote:
class Wrapper extends java.io.OutputStream {
public void setContentHandler(org.xml.sax.ContentHandler handler);
...
}

BEA appear to have something like this, with a
"weblogic.xml.stream.XMLOutputStream", but I'm wondering if it's
possible to create this with standard SAX components.


Sounds like what you're looking for is a SAX handler that implements a
serializer. Most SAX systems come with one, and they're commonly
available. They aren't generally implemented as subclassing
OutputStream; instead they point to an OutputStream.

--
() ASCII Ribbon Campaign | Joe Kesselman
/\ Stamp out HTML e-mail! | System architexture and kinetic poetry
Mar 23 '06 #2

P: n/a

I think the handlers that contain an output stream write their content
to this stream - what I'm after is a way to stream data *into* a
content handler. For example, with a serializer which converts an
object to XML:

interface XMLSerializer() {
static void serialize(Object o, OutputStream os);
}

wrapper = new Wrapper(...);
wrapper.setContentHandler(handler);
XMLSerializer.serialize(object, wrapper);

and let the content handler process the XML produced in exactly the
same way in which the XMLReader.parse() method does, but by pushing the
streaming data in rather than pulling it via the parse method.

Sorry - should probably have been more specific in the first post.

Cheers,
Barney
--
baarney
------------------------------------------------------------------------
baarney's Profile: http://techiegroups.com/member.php?userid=84
View this thread: http://www.techiegroups.com/showthread.php?t=105008

Mar 23 '06 #3

P: n/a
baarney wrote:
what I'm after is a way to stream data *into* a
content handler.


Huh? That's known as a parser...

--
() ASCII Ribbon Campaign | Joe Kesselman
/\ Stamp out HTML e-mail! | System architexture and kinetic poetry
Mar 24 '06 #4

P: n/a
Joe Kesselman wrote:
what I'm after is a way to stream data *into* a
content handler.


Huh? That's known as a parser...


Clarification: If your problem is that you want to feed the data via an
OutputStream, whereas parsers normally want an InputStream, then what
you need is a simple adapter which has a buffer in the middle and some
handshaking so that when the buffer fills the source pauses in writing
to it, and as it empties the source is allowed to fill it up again.
Typically that's done by running the data source and the parser-driven
process in separate threads and using a queue or ringbuffer between them.

I'm sure such out-to-in stream adapters are available as an
off-the-shelf item; do a bit of websearching.

--
() ASCII Ribbon Campaign | Joe Kesselman
/\ Stamp out HTML e-mail! | System architexture and kinetic poetry
Mar 24 '06 #5

P: n/a

That's pretty much exactly much what I was looking for :-)

Rather than separate threads, I was hoping for something that monitored
the state of the internal buffer during the OutputStream.write() calls,
and fired off the appropriate ContentHandler methods when it had
received enough data to form a valid call, blocking the write until the
event returned - like once a "<" character is seen, monitor the input
until the corresponding ">" character is found and then fire the
handler's startElement(...) method.

It's obviously going to be quite a bit more involved that this, which
is why I was hoping someone had already written it, but the basic idea
seems like it ought to work.
I'm sure such out-to-in stream adapters are available as an
off-the-shelf item; do a bit of websearching.


So was I, so I did :-)

There's a bunch of generic stream adapters, but I couldn't find
anything which actually parsed the input stream into XML. I know
there's a bunch of projects that must do this (Cocoon springs to mind),
but I didn't find any likely candidate classes. Posting here was the
next step in my web searching.

If I don;t find anything then I may have a go at writing one, there's
plenty of parser code out there to base it on.

Cheers,
Barney
--
baarney
------------------------------------------------------------------------
baarney's Profile: http://techiegroups.com/member.php?userid=84
View this thread: http://www.techiegroups.com/showthread.php?t=105008

Mar 24 '06 #6

P: n/a
baarney wrote:
There's a bunch of generic stream adapters, but I couldn't find
anything which actually parsed the input stream into XML.


Adapt the stream from push to pull, feed it into a parser that accepts a
stream, done. No need to modify the parser to achieve this.

--
() ASCII Ribbon Campaign | Joe Kesselman
/\ Stamp out HTML e-mail! | System architexture and kinetic poetry
Mar 24 '06 #7

P: n/a
baarney wrote:
It's obviously going to be quite a bit more involved that this, which
is why I was hoping someone had already written it, but the basic idea
seems like it ought to work.


The problem is one of retaining state. EVERYONE wants to be the inner
loop, because that means they don't get interrupted, and therefore they
don't have to deal with saving their state partway through processing
and then restoring it and proceeding next time they're called.

Coroutines are the real way to solve this, but most languages don't have
coroutines. Building a coroutine-like system out of threads is an
adequate answer.

Trying to write a full XML parser which can really be driven by pushing
data into it rather than having it pull data as needed is nontrivial.
Feel free to take a crack at it, of course.
--
() ASCII Ribbon Campaign | Joe Kesselman
/\ Stamp out HTML e-mail! | System architexture and kinetic poetry
Mar 24 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.