de.odysseus.staxon.json
Class JsonXMLStreamWriter

java.lang.Object
  extended by de.odysseus.staxon.base.AbstractXMLStreamWriter<de.odysseus.staxon.json.JsonXMLStreamWriter.ScopeInfo>
      extended by de.odysseus.staxon.json.JsonXMLStreamWriter
All Implemented Interfaces:
XMLStreamWriter

public class JsonXMLStreamWriter
extends AbstractXMLStreamWriter<de.odysseus.staxon.json.JsonXMLStreamWriter.ScopeInfo>

JSON XML stream writer.

Limitations

The writer may consume processing instructions (e.g. <?xml-multiple element-name?>) to properly insert JSON array tokens ('[' and ']'). The client provides this instruction through the AbstractXMLStreamWriter.writeProcessingInstruction(String, String) method, passing the (possibly prefixed) field name as data e.g.

   ...
   writer.writeProcessingInstruction("xml-multiple", "item");
   for (Item item : items) {
     writer.writeStartElement("item");
     ...
     writer.writeEndElement();
   }
   ...
 

The element name passed as processing instruction data is optional. If omitted, the next element within the current scope will start an array. Note, that this method does not allow to create empty arrays (in fact, the above code sample could create unexpected results, if the name would have been omitted and collection were empty).


Constructor Summary
JsonXMLStreamWriter(JsonStreamTarget target, boolean repairNamespaces, boolean multiplePI, char namespaceSeparator, boolean namespaceDeclarations)
          Create writer instance.
 
Method Summary
 void close()
           
 void flush()
           
protected  void writeAttr(String prefix, String localName, String namespaceURI, String value)
          Write attribute.
 void writeBoolean(Boolean value)
          Write boolean value.
protected  void writeData(Object data, int type)
          Write characters/comment/dtd/entity data.
 void writeEndArray()
           
 void writeEndDocument()
           
protected  void writeEndElementTag()
          Write end element tag.
protected  void writeNsDecl(String prefix, String namespaceURI)
          Write namespace declaration.
 void writeNumber(Number value)
          Write number value.
protected  void writePI(String target, String data)
          Read processing instruction.
 void writeStartArray(String fieldName)
           
 void writeStartDocument(String encoding, String version)
           
protected  de.odysseus.staxon.json.JsonXMLStreamWriter.ScopeInfo writeStartElementTag(String prefix, String localName, String namespaceURI)
          Write open start element tag.
protected  void writeStartElementTagEnd()
          Write close start element tag.
 
Methods inherited from class de.odysseus.staxon.base.AbstractXMLStreamWriter
getNamespaceContext, getPrefix, getProperty, getScope, isStartDocumentWritten, setDefaultNamespace, setNamespaceContext, setPrefix, writeAttribute, writeAttribute, writeAttribute, writeCData, writeCharacters, writeCharacters, writeCharacters, writeComment, writeDefaultNamespace, writeDTD, writeEmptyElement, writeEmptyElement, writeEmptyElement, writeEndElement, writeEntityRef, writeNamespace, writeProcessingInstruction, writeProcessingInstruction, writeStartDocument, writeStartDocument, writeStartElement, writeStartElement, writeStartElement
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

JsonXMLStreamWriter

public JsonXMLStreamWriter(JsonStreamTarget target,
                           boolean repairNamespaces,
                           boolean multiplePI,
                           char namespaceSeparator,
                           boolean namespaceDeclarations)
Create writer instance.

Parameters:
target - stream target
multiplePI - whether to consume <xml-multiple?> PIs to trigger array start
namespaceSeparator - namespace prefix separator
namespaceDeclarations - whether to write namespace declarations
Method Detail

writeStartElementTag

protected de.odysseus.staxon.json.JsonXMLStreamWriter.ScopeInfo writeStartElementTag(String prefix,
                                                                                     String localName,
                                                                                     String namespaceURI)
                                                                              throws XMLStreamException
Description copied from class: AbstractXMLStreamWriter
Write open start element tag. The returned scope info is stored in the new scope and will be available via getScope().getInfo().

Specified by:
writeStartElementTag in class AbstractXMLStreamWriter<de.odysseus.staxon.json.JsonXMLStreamWriter.ScopeInfo>
Parameters:
prefix - element prefix (may be XMLConstants.DEFAULT_NS_PREFIX)
localName - local name
namespaceURI - namespace URI
Returns:
new scope info
Throws:
XMLStreamException

writeStartElementTagEnd

protected void writeStartElementTagEnd()
                                throws XMLStreamException
Description copied from class: AbstractXMLStreamWriter
Write close start element tag.

Specified by:
writeStartElementTagEnd in class AbstractXMLStreamWriter<de.odysseus.staxon.json.JsonXMLStreamWriter.ScopeInfo>
Throws:
XMLStreamException

writeEndElementTag

protected void writeEndElementTag()
                           throws XMLStreamException
Description copied from class: AbstractXMLStreamWriter
Write end element tag.

Specified by:
writeEndElementTag in class AbstractXMLStreamWriter<de.odysseus.staxon.json.JsonXMLStreamWriter.ScopeInfo>
Throws:
XMLStreamException

writeAttr

protected void writeAttr(String prefix,
                         String localName,
                         String namespaceURI,
                         String value)
                  throws XMLStreamException
Description copied from class: AbstractXMLStreamWriter
Write attribute.

Specified by:
writeAttr in class AbstractXMLStreamWriter<de.odysseus.staxon.json.JsonXMLStreamWriter.ScopeInfo>
Parameters:
prefix - attribute prefix (may be XMLConstants.DEFAULT_NS_PREFIX)
localName - local name
namespaceURI - namespace URI
value - attribute value
Throws:
XMLStreamException

writeNsDecl

protected void writeNsDecl(String prefix,
                           String namespaceURI)
                    throws XMLStreamException
Description copied from class: AbstractXMLStreamWriter
Write namespace declaration.

Specified by:
writeNsDecl in class AbstractXMLStreamWriter<de.odysseus.staxon.json.JsonXMLStreamWriter.ScopeInfo>
Parameters:
prefix - namespace prefix
namespaceURI - namespace URI
Throws:
XMLStreamException

writeData

protected void writeData(Object data,
                         int type)
                  throws XMLStreamException
Description copied from class: AbstractXMLStreamWriter
Write characters/comment/dtd/entity data.

Specified by:
writeData in class AbstractXMLStreamWriter<de.odysseus.staxon.json.JsonXMLStreamWriter.ScopeInfo>
Parameters:
data - text/data
type - one of CHARACTERS, COMMENT, CDATA, DTD, ENTITY_REFERENCE, SPACE
Throws:
XMLStreamException

writeStartDocument

public void writeStartDocument(String encoding,
                               String version)
                        throws XMLStreamException
Specified by:
writeStartDocument in interface XMLStreamWriter
Overrides:
writeStartDocument in class AbstractXMLStreamWriter<de.odysseus.staxon.json.JsonXMLStreamWriter.ScopeInfo>
Throws:
XMLStreamException

writeEndDocument

public void writeEndDocument()
                      throws XMLStreamException
Specified by:
writeEndDocument in interface XMLStreamWriter
Overrides:
writeEndDocument in class AbstractXMLStreamWriter<de.odysseus.staxon.json.JsonXMLStreamWriter.ScopeInfo>
Throws:
XMLStreamException

writeStartArray

public void writeStartArray(String fieldName)
                     throws XMLStreamException
Throws:
XMLStreamException

writeEndArray

public void writeEndArray()
                   throws XMLStreamException
Throws:
XMLStreamException

close

public void close()
           throws XMLStreamException
Specified by:
close in interface XMLStreamWriter
Overrides:
close in class AbstractXMLStreamWriter<de.odysseus.staxon.json.JsonXMLStreamWriter.ScopeInfo>
Throws:
XMLStreamException

flush

public void flush()
           throws XMLStreamException
Throws:
XMLStreamException

writePI

protected void writePI(String target,
                       String data)
                throws XMLStreamException
Description copied from class: AbstractXMLStreamWriter
Read processing instruction.

Specified by:
writePI in class AbstractXMLStreamWriter<de.odysseus.staxon.json.JsonXMLStreamWriter.ScopeInfo>
Parameters:
target - PI target
data - PI data (may be null)
Throws:
XMLStreamException

writeNumber

public void writeNumber(Number value)
                 throws XMLStreamException
Write number value.

Parameters:
value -
Throws:
XMLStreamException

writeBoolean

public void writeBoolean(Boolean value)
                  throws XMLStreamException
Write boolean value.

Parameters:
value -
Throws:
XMLStreamException


Copyright © 2011-2012. All Rights Reserved.