Saturday, October 20, 2007

Do People With High Cholesterol Sleep A Lot

Parsing XML provided by the GWT WYSIWYG

I start from introducing the problem, which of course does not exist until it sees it:) The problem lies in the fact that part of the XML parser closes the input stream (InputStream ) when it encounters EOF (-1). This applies even if Xerces, which is the default parser for the JDOM library. Of course not would be nothing wrong if the input stream was not SocketInputStream since its closure does not necessarily cause a desired result, namely the closure of socket, which in effect is useless which means nothing else than the fact that he can not send a reply.

For a large part of the application sockets are used for the implementation of the classic request-response model, for example, the client sends an XML document server, the server processes it, and then replies to the customer another XML document. But where, after loading the XML document by the server socket is closed, the client will not be answered.

What can you do in that case to achieve the desired effect? There are two solutions while the second one can be accomplished in several ways. The first solution is a package InputStream (with socket) in such a way that it did not close the connection, or override methods close () . The second solution is to read everything in the buffer, and then transfer the contents to the XML parser. At the same time what to do if the input data stream is large and we do not want to wait unnecessarily until all loaded and only then will pass it to the parser. After all incoming bytes can be processed by the XML parser before it will come next.

How do I do in that case effectively:
  BufferedInputStream clientSocketInputStream = 
new BufferedInputStream (clientSocket.getInputStream ());
juxtaposes two streams piped type:
  PipedOutputStream PipedOutputStream pipedOutputStream = new (); 
pipedInputStream = new PipedInputStream ();
pipedOutputStream.connect (pipedInputStream);
create thread connecting the piped output stream from the socket input stream. Its task is to read data from clientSocketInputStream to pipedOutputStream , which is connected to pipedInputStream that we can directly refer to the XML parser:
  Output2InputThread output2InputThread = 
new Output2InputThread (pipedOutputStream, clientSocketInputStream);
output2InputThread.start ();
SAXBuilder builder = new SAXBuilder ( );
Document inputDocument = builder.build (pipedInputStream);
Implementation class Output2InputThread will not he published in its entirety, but will put the passages to help you get what should be found in it:
  private static final int BUF_SIZE = 4096; 
byte [] buf = new byte [BUF_SIZE];

public void run () {int
readedCount;
while (true) {
/ / Load the socket into the buffer
readedCount = clientSocketInputStream . read (buf);

/ / TODO need to add more code,
/ / TODO check when you should stop reading

/ / Saves the buffer to pipedOutputStream
pipedOutputStream.write (buf, 0, readedCount);

}}
not posted the code to check when you should stop reading, but one thing everyone can achieve alone. Previously, of course, you need to determine how to inform the completion of the transfer of an XML document, it can be done for example by sending the document header to the number of bytes to be read or the end of a contractual document marker. I should also note that setting the buffer size to the specified value does not mean that podaczas one iteration, it will be all filled in a particular case can be loaded only one byte.

encourage you to share their methods for solving the problem presented by me.

0 comments:

Post a Comment