net.sf.jabb.netty
Class XmlDecoder

java.lang.Object
  extended by net.sf.jabb.netty.XmlDecoder

public class XmlDecoder
extends Object

A decoder for Netty to handle XML messages.
使得Netty能够对XML文本消息进行分段的decoder。

The usage is as the following:
使用方法如下:

 ChannelPipeline pipeline = ...;
 
 // construct a decoder 
 XmlDecoder xmlDecoder = new XmlDecoder(
                10000,                          // maximum length of the XML messages
                "env:Envelope",         // top level tag of the XML messages
                CharsetUtil.UTF_8);     // character encoding of the messages

 // setup the pipeline to use the decoder
 pipeline.addLast("frameDecoder", xmlDecoder.getFrameDecoder());
 pipeline.addLast("stringDecoder", xmlDecoder.getStringDecoder());

 
and then you can use a String instead of a ChannelBuffer when processing messages:
 void messageReceived(ChannelHandlerContext ctx, MessageEvent e) {
     String msg = (String) e.getMessage();
     System.out.print("The XML body is '" + msg + "'\n");
 }
 

Author:
Zhengmao HU (James)

Nested Class Summary
protected static class XmlDecoder.XmlStringDecoder
          Further process the messages already processed by FrameDecoder to make it clean XML text messages.
把经过FrameDecoder处理过后的消息,进一步加工为头尾完整干净的XML文本。
 
Field Summary
protected static byte[] endTagEndingByte
           
protected  org.jboss.netty.handler.codec.frame.DelimiterBasedFrameDecoder frameDecoder
           
protected static char[] startTagEndingChar
           
protected  XmlDecoder.XmlStringDecoder stringDecoder
           
 
Constructor Summary
XmlDecoder(int maxFrameLength, String topLevelTagName, Charset charset)
          Constructor after which both its getFrameDecoder() and getStringDecoder() methods should be called to get two decoders for Netty.
创建一个实例,创建好之后,它的getFrameDecoder()方法和getStringDecoder()方法都应该被调用 以获得给Netty用的两个decoder。
 
Method Summary
protected  org.jboss.netty.buffer.ChannelBuffer[] buildDelimiters(String tagName, Charset charset)
          Creates delimiters that will be used to construct DelimiterBasedFrameDecoder.
根据XML标签的名称,生成适合DelimiterBasedFrameDecoder用的delimiters。
 org.jboss.netty.handler.codec.frame.DelimiterBasedFrameDecoder getFrameDecoder()
          Get the frame decoder to be used by Netty.
获得给Netty用的frame decoder。
 org.jboss.netty.handler.codec.oneone.OneToOneDecoder getStringDecoder()
          Get the string decoder to be used by Netty.
获得给Netty用的string decoder。
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

startTagEndingChar

protected static char[] startTagEndingChar

endTagEndingByte

protected static byte[] endTagEndingByte

frameDecoder

protected org.jboss.netty.handler.codec.frame.DelimiterBasedFrameDecoder frameDecoder

stringDecoder

protected XmlDecoder.XmlStringDecoder stringDecoder
Constructor Detail

XmlDecoder

public XmlDecoder(int maxFrameLength,
                  String topLevelTagName,
                  Charset charset)
Constructor after which both its getFrameDecoder() and getStringDecoder() methods should be called to get two decoders for Netty.
创建一个实例,创建好之后,它的getFrameDecoder()方法和getStringDecoder()方法都应该被调用 以获得给Netty用的两个decoder。

Parameters:
maxFrameLength - Maximum length of XML text messages that might be received.
可能接收到的XML消息的最大长度。
topLevelTagName - Top level XML tag that marks the beginning and ending of XML messages.
用来标记每段XML消息开头与结束的顶层XML标签。
charset - Character set that the text messages are encoded in.
所接收到的消息的编码字符集。
Method Detail

buildDelimiters

protected org.jboss.netty.buffer.ChannelBuffer[] buildDelimiters(String tagName,
                                                                 Charset charset)
Creates delimiters that will be used to construct DelimiterBasedFrameDecoder.
根据XML标签的名称,生成适合DelimiterBasedFrameDecoder用的delimiters。

Parameters:
tagName - Name of the top level XML tag (not including <, /, etc).
XML标签的名称(不包括尖括号、斜杠这些)
charset - Character set encoding of the messages to be processed.
待解析的XML文本所采用的字符集编码方式
Returns:
Four delimiters in an array.
共4个delimiter放在一个数组里,都以“</”开头,然后是tag的名称,再接着分别是空格、制表符(TAB)、回车、换行

getFrameDecoder

public org.jboss.netty.handler.codec.frame.DelimiterBasedFrameDecoder getFrameDecoder()
Get the frame decoder to be used by Netty.
获得给Netty用的frame decoder。

Always use this method together with getStringDecoder().
这个方法总是与getStringDecoder()方法结合起来一起用。

Returns:
The decoder that separates XML messages.
用来把XML消息区分开来的decoder。

getStringDecoder

public org.jboss.netty.handler.codec.oneone.OneToOneDecoder getStringDecoder()
Get the string decoder to be used by Netty.
获得给Netty用的string decoder。

Always use this method together with getFrameDecoder().
这个方法总是与getFrameDecoder()方法结合起来一起用。

Returns:
the decoder that do final clean up for XML messages.
用来对XML消息进行最终清理的decoder。


Copyright © 2012. All Rights Reserved.