<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Ashish's Tech Blog &#187; XML Decoder</title>
	<atom:link href="http://www.ashishpaliwal.com/blog/tag/xml-decoder/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.ashishpaliwal.com/blog</link>
	<description>From Programmer, For Programmers</description>
	<lastBuildDate>Tue, 17 Aug 2010 12:04:43 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Implementing XML Decoder for Apache MINA</title>
		<link>http://www.ashishpaliwal.com/blog/2008/10/implementing-xml-decoder-for-apache-mina/</link>
		<comments>http://www.ashishpaliwal.com/blog/2008/10/implementing-xml-decoder-for-apache-mina/#comments</comments>
		<pubDate>Fri, 31 Oct 2008 14:50:05 +0000</pubDate>
		<dc:creator>ashish</dc:creator>
				<category><![CDATA[Apache MINA]]></category>
		<category><![CDATA[XML Decoder]]></category>

		<guid isPermaLink="false">http://www.ashishpaliwal.com/blog/?p=187</guid>
		<description><![CDATA[Will continues to post articles about MINA, to be updated, Subscribe in a reader Apache MINA has wonderful concept of ProtocolDecoder to process Decoding protocol specific messages. XML is one of the most widely used format for EDA. Lets see how can we implement a Protocol Decoder for Apache MINA. Algorithm The picture below describes [...]]]></description>
			<content:encoded><![CDATA[<p>Will continues to post articles about MINA, to be updated, <a rel="alternate" type="application/rss+xml" href="http://feedproxy.google.com/AshishsTechBlog"><img style="vertical-align:middle;border:0" src="http://www.feedburner.com/fb/images/pub/feed-icon16x16.png" alt="" /></a> <a rel="alternate" type="application/rss+xml" href="http://feedproxy.google.com/AshishsTechBlog">Subscribe in a reader</a></p>
<p>Apache MINA has wonderful concept of ProtocolDecoder to process Decoding protocol specific messages. XML is one of the most widely used format for EDA. Lets see how can we implement a Protocol Decoder for Apache MINA.</p>
<p><strong>Algorithm</strong></p>
<p>The picture below describes the basic algorithm that we need to use to construct an XML message from bytes.</p>
<p> </p>
<p><a href="http://www.ashishpaliwal.com/blog/wp-content/uploads/2008/10/decoderalgo.png"><img class="aligncenter size-full wp-image-188" title="decoderalgo" src="http://www.ashishpaliwal.com/blog/wp-content/uploads/2008/10/decoderalgo.png" alt="" width="340" height="399" /></a></p>
<p>The logic is simple, keep reading the bytes till the XML message is balanced. Balanced here means, that end of the root element has been achieved. For eg. If the xml document has root element as , we have to read the bytes till we received .</p>
<p>Its very particular to note that large XML packets when sent over TCP, may get fragmented and we shall received the same amount of read events while using Apache MINA low level API’s.</p>
<p>This type of situations where, we need to wait to data to completely arrive, calls for the use of CumulativeProtocolDecoder. As the name signifies, the decoder waits till, we get the balanced xml. Once the balanced XML is found, we write the parsed object to the output, to be processed further.</p>
<p>Lets see the code. My apologies for the unformatted code <img src='http://www.ashishpaliwal.com/blog/wp-includes/images/smilies/icon_sad.gif' alt=':-(' class='wp-smiley' /> </p>
<pre class="java5"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">abstract</span> <span style="color: #000000; font-weight: bold;">class</span> <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/beans/XMLDecoder.html"><span style="color: #aaaadd; font-weight: bold;">XMLDecoder</span></a> <span style="color: #000000; font-weight: bold;">extends</span> CumulativeProtocolDecoder  <span style="color: #66cc66;">&#123;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">/*
* As per XML specification 1.0, http://www.w3.org/TR/REC-xml
*/</span>
<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #993333;">char</span> XML_START_TAG = <span style="color: #ff0000;">'&amp;lt;'</span>;
<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #993333;">char</span> XML_END_TAG = <span style="color: #ff0000;">'&amp;gt;'</span>;
<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #993333;">char</span> XML_PI_TAG = <span style="color: #ff0000;">'?'</span>;
<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #993333;">char</span> XML_COMMENT_TAG = <span style="color: #ff0000;">'!'</span>;
&nbsp;
<span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">enum</span> ParseState <span style="color: #66cc66;">&#123;</span>ELEMENT_START, ELEMENT_END, COMMENTS, ENDELEMENT, PI, UNDEFINED<span style="color: #66cc66;">&#125;</span>;
&nbsp;
<span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #993333;">int</span> ELEMENT_START = <span style="color: #cc66cc;">1</span>;
<span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #993333;">int</span> ELEMENT_END = <span style="color: #cc66cc;">2</span>;
&nbsp;
<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/logging/Logger.html"><span style="color: #aaaadd; font-weight: bold;">Logger</span></a> logger = LoggerFactory.<span style="color: #006600;">getLogger</span><span style="color: #66cc66;">&#40;</span><a href="http://java.sun.com/j2se/1.5.0/docs/api/java/beans/XMLDecoder.html"><span style="color: #aaaadd; font-weight: bold;">XMLDecoder</span></a>.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
@<a href="http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Override.html"><span style="color: #aaaadd; font-weight: bold;">Override</span></a>
<span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #993333;">boolean</span> doDecode<span style="color: #66cc66;">&#40;</span>IoSession session, IoBuffer ioBuffer,
ProtocolDecoderOutput decoderOutput<span style="color: #66cc66;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Exception.html"><span style="color: #aaaadd; font-weight: bold;">Exception</span></a> <span style="color: #66cc66;">&#123;</span>
&nbsp;
<span style="color: #993333;">int</span> startPosition = ioBuffer.<span style="color: #006600;">position</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>!ioBuffer.<span style="color: #006600;">hasRemaining</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
logger.<span style="color: #006600;">debug</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;NO bytes to read keep waiting...&quot;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #b13366;">false</span>;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">// Continue to read the bytes and keep parsing</span>
<span style="color: #993333;">char</span> currentChar = <span style="color: #ff0000;">'0'</span>, previousChar = <span style="color: #ff0000;">'0'</span>;
&nbsp;
<span style="color: #993333;">boolean</span> rootElementStarted = <span style="color: #b13366;">false</span>;
<span style="color: #993333;">boolean</span> rootElementPresent = <span style="color: #b13366;">false</span>;
<span style="color: #993333;">boolean</span> isBalanced = <span style="color: #b13366;">false</span>;
&nbsp;
<span style="color: #993333;">int</span> rootStartPosition, rootEndPosition;
&nbsp;
ParseState parsingState = ParseState.<span style="color: #006600;">UNDEFINED</span>;
logger.<span style="color: #006600;">debug</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Lets start decoding the XML&quot;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<a href="http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html"><span style="color: #aaaadd; font-weight: bold;">String</span></a> root = <span style="color: #b13366;">null</span>;
&nbsp;
<span style="color: #993333;">boolean</span> markedForEndElement = <span style="color: #b13366;">false</span>;
&nbsp;
<span style="color: #b1b100;">while</span><span style="color: #66cc66;">&#40;</span>ioBuffer.<span style="color: #006600;">hasRemaining</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
previousChar = currentChar;
currentChar = <span style="color: #66cc66;">&#40;</span><span style="color: #993333;">char</span><span style="color: #66cc66;">&#41;</span>ioBuffer.<span style="color: #006600;">get</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #b1b100;">switch</span> <span style="color: #66cc66;">&#40;</span>parsingState<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
<span style="color: #b1b100;">case</span> ELEMENT_START:
<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>currentChar == XML_PI_TAG<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
logger.<span style="color: #006600;">debug</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Got PI Element&quot;</span><span style="color: #66cc66;">&#41;</span>;
parsingState = ParseState.<span style="color: #006600;">PI</span>;
<span style="color: #66cc66;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>currentChar == XML_COMMENT_TAG<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
logger.<span style="color: #006600;">debug</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Got Comment Element&quot;</span><span style="color: #66cc66;">&#41;</span>;
parsingState = ParseState.<span style="color: #006600;">COMMENTS</span>;
<span style="color: #66cc66;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>currentChar == <span style="color: #ff0000;">' '</span> || currentChar == XML_END_TAG<span style="color: #66cc66;">&#41;</span>
&amp;amp;&amp;amp; rootElementStarted &amp;amp;&amp;amp; !rootElementPresent<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
rootEndPosition = ioBuffer.<span style="color: #006600;">position</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
rootElementPresent = <span style="color: #b13366;">true</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">// Copy the Root Element</span>
<span style="color: #993333;">int</span> cPos = ioBuffer.<span style="color: #006600;">position</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #993333;">int</span> mPos = ioBuffer.<span style="color: #006600;">markValue</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #993333;">char</span><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span> rootChar = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #993333;">char</span><span style="color: #66cc66;">&#91;</span>cPos - mPos<span style="color: #66cc66;">&#93;</span>;
<span style="color: #b1b100;">for</span><span style="color: #66cc66;">&#40;</span><span style="color: #993333;">int</span> i = mPos - <span style="color: #cc66cc;">1</span>, j =<span style="color: #cc66cc;">0</span>; i &amp;lt; cPos - <span style="color: #cc66cc;">1</span>; i++<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
rootChar<span style="color: #66cc66;">&#91;</span>j++<span style="color: #66cc66;">&#93;</span> = <span style="color: #66cc66;">&#40;</span><span style="color: #993333;">char</span><span style="color: #66cc66;">&#41;</span>ioBuffer.<span style="color: #006600;">get</span><span style="color: #66cc66;">&#40;</span>i<span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
root = <span style="color: #000000; font-weight: bold;">new</span> <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html"><span style="color: #aaaadd; font-weight: bold;">String</span></a><span style="color: #66cc66;">&#40;</span>rootChar<span style="color: #66cc66;">&#41;</span>;
logger.<span style="color: #006600;">debug</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Root Element = &quot;</span>+ root<span style="color: #66cc66;">&#41;</span>;
parsingState = ParseState.<span style="color: #006600;">ELEMENT_END</span>;
logger.<span style="color: #006600;">debug</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Root Element detection completed &quot;</span>+rootEndPosition<span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>currentChar == XML_END_TAG<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
parsingState = ParseState.<span style="color: #006600;">ELEMENT_END</span>;
<span style="color: #66cc66;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>!rootElementStarted &amp;amp;&amp;amp; !rootElementPresent<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
rootStartPosition = ioBuffer.<span style="color: #006600;">position</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
ioBuffer.<span style="color: #006600;">mark</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
rootElementStarted = <span style="color: #b13366;">true</span>;
logger.<span style="color: #006600;">debug</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Got the root element at &quot;</span>+rootStartPosition<span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>currentChar == <span style="color: #ff0000;">'/'</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
<span style="color: #808080; font-style: italic;">// Change state</span>
<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>previousChar == XML_START_TAG<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
parsingState = ParseState.<span style="color: #006600;">ENDELEMENT</span>;
<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
<span style="color: #000000; font-weight: bold;">break</span>;
&nbsp;
<span style="color: #b1b100;">case</span> ENDELEMENT:
<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>currentChar == XML_END_TAG<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
parsingState = ParseState.<span style="color: #006600;">ELEMENT_END</span>;
&nbsp;
<span style="color: #993333;">int</span> cPos = ioBuffer.<span style="color: #006600;">position</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #993333;">int</span> mPos = ioBuffer.<span style="color: #006600;">markValue</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #993333;">char</span><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span> el = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #993333;">char</span><span style="color: #66cc66;">&#91;</span>cPos - mPos<span style="color: #66cc66;">&#93;</span>;
<span style="color: #b1b100;">for</span><span style="color: #66cc66;">&#40;</span><span style="color: #993333;">int</span> i = mPos - <span style="color: #cc66cc;">1</span>, j =<span style="color: #cc66cc;">0</span>; i &amp;lt; cPos - <span style="color: #cc66cc;">1</span>; i++<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
el<span style="color: #66cc66;">&#91;</span>j++<span style="color: #66cc66;">&#93;</span> = <span style="color: #66cc66;">&#40;</span><span style="color: #993333;">char</span><span style="color: #66cc66;">&#41;</span>ioBuffer.<span style="color: #006600;">get</span><span style="color: #66cc66;">&#40;</span>i<span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span>
markedForEndElement = <span style="color: #b13366;">false</span>;
<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>root.<span style="color: #006600;">equalsIgnoreCase</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html"><span style="color: #aaaadd; font-weight: bold;">String</span></a><span style="color: #66cc66;">&#40;</span>el<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
logger.<span style="color: #006600;">debug</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;XML is balanced.&quot;</span>+root<span style="color: #66cc66;">&#41;</span>;
isBalanced = <span style="color: #b13366;">true</span>;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">break</span>;
<span style="color: #66cc66;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>currentChar == <span style="color: #ff0000;">' '</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
<span style="color: #000000; font-weight: bold;">continue</span>;
<span style="color: #66cc66;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #66cc66;">&#123;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">// mark the position, we need to compare the it to see that if its the end element</span>
<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>!markedForEndElement<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
ioBuffer.<span style="color: #006600;">mark</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
markedForEndElement = <span style="color: #b13366;">true</span>;
<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
<span style="color: #000000; font-weight: bold;">break</span>;
&nbsp;
<span style="color: #b1b100;">case</span> ELEMENT_END:
<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>currentChar == XML_START_TAG<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
parsingState = ParseState.<span style="color: #006600;">ELEMENT_START</span>;
<span style="color: #66cc66;">&#125;</span>
<span style="color: #000000; font-weight: bold;">break</span>;
&nbsp;
<span style="color: #b1b100;">case</span> UNDEFINED:
<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>currentChar == XML_START_TAG<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
parsingState = ParseState.<span style="color: #006600;">ELEMENT_START</span>;
<span style="color: #66cc66;">&#125;</span>
<span style="color: #000000; font-weight: bold;">break</span>;
&nbsp;
<span style="color: #b1b100;">case</span> COMMENTS:
<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>currentChar == <span style="color: #ff0000;">'-'</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
previousChar = currentChar;
<span style="color: #66cc66;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>previousChar == <span style="color: #ff0000;">'-'</span> &amp;amp;&amp;amp; currentChar == <span style="color: #ff0000;">'&amp;gt;'</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
parsingState = ParseState.<span style="color: #006600;">ELEMENT_END</span>;
<span style="color: #66cc66;">&#125;</span>
<span style="color: #000000; font-weight: bold;">break</span>;
&nbsp;
<span style="color: #b1b100;">case</span> PI:
<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>currentChar == <span style="color: #ff0000;">'?'</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
previousChar = currentChar;
<span style="color: #66cc66;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>previousChar == <span style="color: #ff0000;">'?'</span> &amp;amp;&amp;amp; currentChar == XML_END_TAG<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
parsingState = ParseState.<span style="color: #006600;">ELEMENT_END</span>;
<span style="color: #66cc66;">&#125;</span>
<span style="color: #000000; font-weight: bold;">break</span>;
&nbsp;
<span style="color: #b1b100;">default</span>:
<span style="color: #000000; font-weight: bold;">break</span>;
<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>isBalanced<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
decoderOutput.<span style="color: #006600;">write</span><span style="color: #66cc66;">&#40;</span>parserXML<span style="color: #66cc66;">&#40;</span>ioBuffer<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>isBalanced &amp;amp;&amp;amp; !ioBuffer.<span style="color: #006600;">hasRemaining</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
logger.<span style="color: #006600;">debug</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;No more bytes to process&quot;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #b13366;">true</span>;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
ioBuffer.<span style="color: #006600;">position</span><span style="color: #66cc66;">&#40;</span>startPosition<span style="color: #66cc66;">&#41;</span>;
<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #b13366;">false</span>;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">/**
* Extending classes can implement their custom XML parsing to create Objects
* from XML and use them appropriately in Handler
*
* @param xmlBuffer
* @return
*/</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">abstract</span> <a href="http://www.google.com/search?sitesearch=java.sun.com&amp;q=allinurl%3Aj2se%2F1+5+0%2Fdocs%2Fapi+Object"><span style="color: #aaaadd; font-weight: bold;">Object</span></a> parserXML<span style="color: #66cc66;">&#40;</span>IoBuffer xmlBuffer<span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span></pre>
<p>The implementation is pretty straight forward. We take each character and try to match the characters as specified in XML specification.</p>
<p><strong>Some keys things in the implementation:</strong><br />
1.	The Decode function just collects the bytes till we get the balanced XML document<br />
2.	Once we get the balanced XML document, we shall call the abstract function parseXML(). The function has been kept abstract, so that its easy to implement custom parsing using desired XML library like JAXB, JIBX etc<br />
3.	We have to return true from doDecode(), the moment we have balanced XML. Return type true indicates to the framework that we are not waiting for any more data. A false, forces the framework to keep accumulating the data, till we write it to the output. Now it must be clear why, its called Cumulative decoder.</p>
<p>Still have Queries, please leave a comment and I shall revert back to you.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ashishpaliwal.com/blog/2008/10/implementing-xml-decoder-for-apache-mina/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
	</channel>
</rss>
