org.idoox.xml
Class XMLWriterReader

java.lang.Object
  extended byorg.idoox.xml.XMLWriterReader
All Implemented Interfaces:
Tokenizer, TokenWriter

public class XMLWriterReader
extends java.lang.Object
implements Tokenizer, TokenWriter

This class represents XMLWriter (TokenWriter) which can be also used as XMLReader (Tokenizer). You can write XML tokens to this class and them they can read back (acts like a buffer).

This class is used in the protocol level Message implementation to allow processing of the header processors, which use Tokenizer instead of TokenWriter.

Since:
4.0
Component:
Core

Field Summary
 java.util.Map namespaceMap
          Stores mapping of namespaces to their prefixes
 java.util.Map prefixMap
          Stores mapping of prefixes to their namespace URIs
 
Fields inherited from interface org.idoox.xml.Tokenizer
CONTENT, END_DOCUMENT, END_TOKEN, START_TOKEN, typeNames, UNKNOWN
 
Constructor Summary
XMLWriterReader()
          Constructs an emty XMLWriterReader
 
Method Summary
 byte currentState()
          Returns the current state of the tokenizer.
 void enter(java.lang.String namespace, java.lang.String name)
          Create new element, namespace can be null for unqualified.
 void flush()
          Flushes the internal caches.
 java.util.Map getCurrentPrefixMap()
          Returns clone of the current prefix map.
 org.w3c.dom.Element getDOMRepresentation(org.w3c.dom.Document doc)
          Returns DOM representation of the element that is being parsed.
 java.lang.String getLocalName()
          Returns the local name of the current element.
 java.lang.String getNamespace()
          Returns the namespace URI of the current element.
 java.lang.String getNamespaceForPrefix(java.lang.String prefix)
          Returns a namespace URI for a declared prefix.
 java.lang.String getPrefixForNamespace(java.lang.String namespace)
          Returns prefix for a namespace URI, if prefix for namespace URI doesn't exist it will be automatically created as ns[:digit:]+.
 void leave()
          Terminate current element.
 byte next()
          Parses next part of the input XML document and returns the state of the tokenizer (one of UNKNOWN, START_TOKEN, END_TOKEN, CONTENT, END_DOCUMENT).
 QName parseQName(java.lang.String qName)
          Parses qName in the context of the opened element and returns the pair (namespaceURI, localName).
 int pushNewlyDeclaredPrefixes(DeclaredPrefixesStack prefixes)
          Adds prefixes newly declared in this token.
 java.lang.String readContent()
          Reads the content (PCDATA, CDATA).
 void readToken(Token token)
          Reads the start/end of an element.
 void setPrefixForNamespace(java.lang.String prefix, java.lang.String namespace)
          Sets prefix for specified namespace URI.
 boolean whitespaceContent()
          Returns true if the content contains only whitespaces.
 void writeAttribute(java.lang.String namespace, java.lang.String name, java.lang.String value)
          Can be called only when inside new element, not after outputting some content.
 void writeContent(java.lang.String content)
          Writes text content to a XML stream.
 void writeXmlAttribute(java.lang.String name, java.lang.String value)
          Outputs XML attribute, i.e.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

namespaceMap

public java.util.Map namespaceMap
Stores mapping of namespaces to their prefixes


prefixMap

public java.util.Map prefixMap
Stores mapping of prefixes to their namespace URIs

Constructor Detail

XMLWriterReader

public XMLWriterReader()
Constructs an emty XMLWriterReader

Method Detail

flush

public void flush()
Description copied from interface: TokenWriter
Flushes the internal caches.

Specified by:
flush in interface TokenWriter

enter

public void enter(java.lang.String namespace,
                  java.lang.String name)
Description copied from interface: TokenWriter
Create new element, namespace can be null for unqualified. When all the attributes and content of the element was written TokenWriter.leave() MUST be called.

Specified by:
enter in interface TokenWriter
Parameters:
name - name of the element
namespace - namespace URI of the element

writeAttribute

public void writeAttribute(java.lang.String namespace,
                           java.lang.String name,
                           java.lang.String value)
                    throws java.lang.IllegalStateException
Description copied from interface: TokenWriter
Can be called only when inside new element, not after outputting some content.

Specified by:
writeAttribute in interface TokenWriter
Parameters:
name - name of the attribute
namespace - namespace URI of the attribute
value - value of the attribute
Throws:
java.lang.IllegalStateException - if called in an invalid place

writeXmlAttribute

public void writeXmlAttribute(java.lang.String name,
                              java.lang.String value)
                       throws java.lang.IllegalStateException
Description copied from interface: TokenWriter
Outputs XML attribute, i.e. the one starting with 'xml:', such as xml:lang or xml:space. Can be called only when inside new element, not after outputting some content.

Specified by:
writeXmlAttribute in interface TokenWriter
Parameters:
name - name of the attribute, without 'xml:' prefix
value - value of the attribute
Throws:
java.lang.IllegalStateException - if called in an invalid place

writeContent

public void writeContent(java.lang.String content)
                  throws java.io.IOException,
                         java.lang.IllegalStateException
Description copied from interface: TokenWriter
Writes text content to a XML stream.

Specified by:
writeContent in interface TokenWriter
Parameters:
content - the content to be written
Throws:
java.io.IOException - if IOException occurs
java.lang.IllegalStateException - if called in an invalid place

leave

public void leave()
           throws java.io.IOException,
                  java.lang.IllegalStateException
Description copied from interface: TokenWriter
Terminate current element. Called when all attributes and all content of the element was written. Each TokenWriter.enter(java.lang.String, java.lang.String) call MUST has corresponding TokenWriter.leave() call.

Specified by:
leave in interface TokenWriter
Throws:
java.lang.IllegalStateException
java.io.IOException

getPrefixForNamespace

public java.lang.String getPrefixForNamespace(java.lang.String namespace)
                                       throws java.lang.IllegalStateException
Description copied from interface: TokenWriter
Returns prefix for a namespace URI, if prefix for namespace URI doesn't exist it will be automatically created as ns[:digit:]+.

Specified by:
getPrefixForNamespace in interface TokenWriter
Parameters:
namespace - the namespace URI for which prefix is returned
Returns:
the appropriate prefix
Throws:
java.lang.IllegalStateException - if not called at the start of the element and some definition is required

setPrefixForNamespace

public void setPrefixForNamespace(java.lang.String prefix,
                                  java.lang.String namespace)
                           throws java.lang.IllegalStateException
Description copied from interface: TokenWriter
Sets prefix for specified namespace URI.

Note: Prefixes that look like ns[:digit:]+ are used by automatic prefix generation and MUST NOT be used by user.

Note: There are no internal checks for validity of namespace prefix mapping. The caller must ensure that the mapping is valid.

Specified by:
setPrefixForNamespace in interface TokenWriter
Parameters:
prefix - prefix to be registered
namespace - the namespace URI to be registered with the prefix
Throws:
java.lang.IllegalStateException - if not called at the start of element

getLocalName

public java.lang.String getLocalName()
                              throws TokenizerException
Description copied from interface: Tokenizer
Returns the local name of the current element. This function may be called only if the tokenizer is on START_TOKEN.

Specified by:
getLocalName in interface Tokenizer
Returns:
the local name of the current element
Throws:
TokenizerException - if the tokenizer is not on the START_TOKEN

getNamespace

public java.lang.String getNamespace()
                              throws TokenizerException
Description copied from interface: Tokenizer
Returns the namespace URI of the current element. This function may be called only if the tokenizer is on START_TOKEN.

Specified by:
getNamespace in interface Tokenizer
Returns:
the namespace URI of the current element
Throws:
TokenizerException - if the tokenizer is not on the START_TOKEN

getNamespaceForPrefix

public java.lang.String getNamespaceForPrefix(java.lang.String prefix)
Description copied from interface: Tokenizer
Returns a namespace URI for a declared prefix.

Specified by:
getNamespaceForPrefix in interface Tokenizer
Parameters:
prefix - the declared prefix
Returns:
the namespace URI for the prefix or null if the prefix has not been declared

parseQName

public QName parseQName(java.lang.String qName)
Description copied from interface: Tokenizer
Parses qName in the context of the opened element and returns the pair (namespaceURI, localName).

Specified by:
parseQName in interface Tokenizer
Parameters:
qName - the qualified name
Returns:
the resolved (expanded) name

next

public byte next()
          throws TokenizerException,
                 java.io.IOException
Description copied from interface: Tokenizer
Parses next part of the input XML document and returns the state of the tokenizer (one of UNKNOWN, START_TOKEN, END_TOKEN, CONTENT, END_DOCUMENT).

Specified by:
next in interface Tokenizer
Returns:
the state of the tokenizer
Throws:
TokenizerException - if there is an error in the XML document or the tokenizer is beyond the end of the document (the previous call to next() has returned END_DOCUMENT).
java.io.IOException - if some IOException has occured

currentState

public byte currentState()
Description copied from interface: Tokenizer
Returns the current state of the tokenizer. See Tokenizer.next() for details.

Specified by:
currentState in interface Tokenizer
Returns:
the current token

readContent

public java.lang.String readContent()
                             throws TokenizerException
Description copied from interface: Tokenizer
Reads the content (PCDATA, CDATA).

Specified by:
readContent in interface Tokenizer
Returns:
the content
Throws:
TokenizerException - if the tokenizer is not in CONTENT state

readToken

public void readToken(Token token)
               throws TokenizerException
Description copied from interface: Tokenizer
Reads the start/end of an element. Result is stored in the out parameter stoken. This function may be called only if the tokenizer is on START_TOKEN or END_TOKEN.

Specified by:
readToken in interface Tokenizer
Parameters:
token - structure containing name, namespace URI and attribute pairs; holder for result
Throws:
TokenizerException - if the tokenizer is not in START_TOKEN, END_TOKEN or the document is not a well-formed XML.

whitespaceContent

public boolean whitespaceContent()
                          throws TokenizerException
Description copied from interface: Tokenizer
Returns true if the content contains only whitespaces.

Specified by:
whitespaceContent in interface Tokenizer
Returns:
true if the content contains only whitespaces
Throws:
TokenizerException - if the tokenizer is not in CONTENT state

getCurrentPrefixMap

public java.util.Map getCurrentPrefixMap()
Description copied from interface: Tokenizer
Returns clone of the current prefix map. This function might be used for example for implementing caching or multi-pass tokenizers.

Specified by:
getCurrentPrefixMap in interface Tokenizer
Returns:
the current prefix to namespace map

pushNewlyDeclaredPrefixes

public int pushNewlyDeclaredPrefixes(DeclaredPrefixesStack prefixes)
Description copied from interface: Tokenizer
Adds prefixes newly declared in this token. May be called only when the tokenizer is in START_TOKEN state.

Specified by:
pushNewlyDeclaredPrefixes in interface Tokenizer
Parameters:
prefixes - the newly declared prefixes
Returns:
the number of prefixes added

getDOMRepresentation

public org.w3c.dom.Element getDOMRepresentation(org.w3c.dom.Document doc)
                                         throws TokenizerException
Description copied from interface: Tokenizer
Returns DOM representation of the element that is being parsed. Might be called only at the beginning of an element.

Specified by:
getDOMRepresentation in interface Tokenizer
Parameters:
doc - the document within which the element should be created
Returns:
element containing DOM representation of the XML tag being parsed
Throws:
TokenizerException - if there is an error in tokenizing the XML document