XMLLightweightParser have error.I modified it,please look it

Hi all,

I am using wildfire 3.1.

And When client send a packet such as "

then error.


/**

  • $RCSfile$

  • $Revision: $

  • $Date: $

  • Copyright © 2006 Jive Software. All rights reserved.
  • This software is published under the terms of the GNU Public License (GPL),

  • a copy of which is included in this distribution.

*/

package server;

import java.io.IOException;

import java.io.InputStream;

import java.nio.ByteBuffer;

import java.nio.CharBuffer;

import java.nio.channels.Channels;

import java.nio.channels.ReadableByteChannel;

import java.nio.charset.Charset;

import java.util.ArrayList;

import java.util.List;

/**

  • This is a Light-Weight XML Parser.

  • It read data from a channel and collect data until data are available in

  • the channel.

  • When a message is complete you can retrieve messages invoking the method

  • getMsgs() and you can invoke the method areThereMsgs() to know if at least

  • an message is presents.

  • @author Daniele Piras

*/

class XMLLightweightParser

{

// Chars that rappresent CDATA section start

protected static char[] CDATA_START = {’’<’’,’’!’’,’’[’’,’‘C’’,’‘D’’,’‘A’’,’‘T’’,’‘A’’,’’[’’};

// Chars that rappresent CDATA section end

protected static char[] CDATA_END = {’’]’’,’’]’’,’’>’’};

// Buffer with all data retrieved

protected StringBuilder buffer = new StringBuilder();

// -


INTERNAL STATUS -


// Initial status

protected static final int INIT = 0;

// Status used when the first tag name is retrieved

protected static final int HEAD = 2;

// Status used when robot is inside the xml and it looking for the tag conclusion

protected static final int INSIDE = 3;

// Status used when a ‘’();

protected boolean insideChildrenTag = false;

private ReadableByteChannel inputChannel;

byte[] rawByteBuffer;

ByteBuffer byteBuffer;

Charset encoder;

public ReadableByteChannel getChannel()

{

return inputChannel;

}

public XMLLightweightParser( ReadableByteChannel channel, String charset )

{

rawByteBuffer = new byte[4096];

byteBuffer = ByteBuffer.wrap( rawByteBuffer );

setInput( channel, charset );

}

public XMLLightweightParser( InputStream is , String charset)

{

rawByteBuffer = new byte[4096];

byteBuffer = ByteBuffer.wrap( rawByteBuffer );

setInput( is, charset );

}

public void setInput( InputStream is, String charset )

{

inputChannel = Channels.newChannel( is );

encoder = Charset.forName( charset );

invalidateBuffer();

}

public void setInput( ReadableByteChannel channel, String charset )

{

inputChannel = channel;

encoder = Charset.forName( charset );

invalidateBuffer();

}

/*

  • true if the parser has found some complete xml message.

*/

public boolean areThereMsgs()

{

return ( msgs.size() > 0 );

}

/*

  • @return an array with all messages found

*/

public String[] getMsgs()

{

String[] res = new String[ msgs.size() ];

for ( int i = 0; i < res.length; i++ )

{

res[ i ] = msgs.get( i );

}

msgs.clear();

invalidateBuffer();

return res;

}

/*

  • Method use to re-initialize the buffer

*/

protected void invalidateBuffer()

{

if ( buffer.length() > 0 )

{

String str = buffer.substring( startLastMsg ).toString().trim();

buffer.delete( 0, buffer.length() );

buffer.append( str );

buffer.trimToSize();

}

startLastMsg = 0;

}

/*

  • Method that add a message to the list and reinit parser.

*/

protected void foundMsg( String msg )

{

// Add message to the complete message list

if ( msg != null )

{

msgs.add( msg.trim() );

}

// Move the position into the buffer

status = INIT;

tailCount = 0;

cdataOffset = 0;

head.setLength( 0 );

insideRootTag = false;

insideChildrenTag = false;

}

/*

  • Main reading method

*/

public void read() throws Exception

{

// Reset buffer

byteBuffer.limit( rawByteBuffer.length );

byteBuffer.rewind();

int readByte = inputChannel.read( byteBuffer );

if ( readByte == -1 )

{

// ERROR ON SOCKET!!

throw new IOException( “ReadByte == -1.Socket Close” );

}

else if ( readByte " );

status = INSIDE;

insideRootTag = true;

insideChildrenTag = false;

continue;

}

head.append( (char)ch );

}

else if ( status == INIT )

{

if ( ch != ‘’ ‘’ && ch != ‘’\r’’ && ch != ‘’\n’’ && ch != ‘’<’’ )

{

invalidateBuffer();

return;

}

if ( ch == ‘’<’’ )

{

status = HEAD;

}

}

}

}

}

see http://www.jivesoftware.org/community/thread.jspa?messageID=134281&#134281