com.sap.tc.mobile.cfs.xml.api
Class MIXMLParser

java.lang.Object
  extended by com.sap.tc.mobile.cfs.xml.api.MIXMLParser

public class MIXMLParser
extends java.lang.Object

MI-private non-checking fast XML parser.

Author:
SAP AG

Nested Class Summary
static class MIXMLParser.CopyToWriterContentHandler
          This class wrapps any MIContentHandler and copies the xml into a writer (if set) after calling the wrapper.
 
Field Summary
protected  MIContentHandler contentHandler
          The ContentHandler that will receive the events
protected static Location logger
          Logger for messages.
 
Constructor Summary
MIXMLParser()
          Constructor.
 
Method Summary
protected  void checkNameChar(char c, boolean isFirst)
          Check if character is name character.
protected static char[] doubleLength(char[] characterArray)
          Creates a new char array with the same entries as the original one but the double length.
 MIContentHandler getContentHandler()
          Get content handler of this parser.
protected  char nextChar()
          Get next character from input buffer.
 void parse(java.io.InputStream inputStream)
          Parse data from input stream (utf-8 encoding).
 void parse(java.io.Reader reader)
          Parse data from reader (encoding given by reader).
 void setContentHandler(MIContentHandler contentHandler)
          Set content handler for this parser.
 void setWriter(java.io.Writer writer)
          Set a writer to copy the xml into.
protected  char skipWS(char readCharacter)
          Skip whitespace.
 void stopParsing()
          Stops parsing after current element is finished.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

logger

protected static final Location logger
Logger for messages.


contentHandler

protected MIContentHandler contentHandler
The ContentHandler that will receive the events

Constructor Detail

MIXMLParser

public MIXMLParser()
Constructor.

Method Detail

setContentHandler

public void setContentHandler(MIContentHandler contentHandler)
Set content handler for this parser.

Parameters:
contentHandler - new content handler.

getContentHandler

public MIContentHandler getContentHandler()
Get content handler of this parser.

Returns:
content handler.

stopParsing

public void stopParsing()
Stops parsing after current element is finished.


parse

public void parse(java.io.InputStream inputStream)
Parse data from input stream (utf-8 encoding).

Parameters:
inputStream - stream to parse.

parse

public void parse(java.io.Reader reader)
Parse data from reader (encoding given by reader).

Parameters:
reader - reader to parse.

setWriter

public void setWriter(java.io.Writer writer)
Set a writer to copy the xml into. This can be set and unset during the parsing process, so it is possible to copy only a part of the parsing Xml document to this writer. As soon as this method is called all xml parts (elements, attributes, characters) are written to the Writer AFTER the MIContentHandler is called. To unset the writer you need to pass null as agrument to this method.

Example: The following implementattion of MIContentHandler copies all xml content inside the xml element "writerTest" into a writer.
 
          final MIContentHandler contentHandler = new MIContentHandler(){

                        private boolean startWriter = false;

                        public void endAttributes() {
                                if(this.startWriter){
                                        parser.setWriter(writer);
                                        this.startWriter = false;
                                }
                        }

                        public void startElement(char[] localName, int start, int length) {
                                if(MIXMLTools.equals(localName, start, length, "writerTest")){
                                        this.startWriter = true;
                                }
                        }

                        public void endElement(char[] localName, int start, int length) {
                                if(MIXMLTools.equals(localName, start, length, "writerTest")){
                                        parser.setWriter(null);
                                }
                        }

                        public void endDocument() {
                                writer.flush();
                        }

                        public void characters(char[] ch, int start, int length) {}
                        public void attribute(char[] name, int startName, int lengthName, char[] value, int startValue, int lengthValue) {}
                        public void startDocument() {}
                }


 
If there are any IO Exception while writing to the writer the parser will throw a RuntimeException.

Parameters:
writer - the Writer to copy the xml into or null to end the copying process and unset the writer.

skipWS

protected final char skipWS(char readCharacter)
Skip whitespace.

Parameters:
readCharacter - read character that might be whitespace.
Returns:
next non-whitespace character (may be same readCharacter).

nextChar

protected final char nextChar()
Get next character from input buffer.

Returns:
next character from input buffer.

doubleLength

protected static char[] doubleLength(char[] characterArray)
Creates a new char array with the same entries as the original one but the double length.

Parameters:
characterArray -
Returns:

checkNameChar

protected void checkNameChar(char c,
                             boolean isFirst)
Check if character is name character.

Parameters:
c - character to check.
isFirst - if true, check for first name character.