|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
ObjectEncoder
public class Encoder
Encodes objects as xml based on a schema.
The function of the encoder is to traverse a tree of objects seializing them
out as xml as it goes. Navigation and serialization of the tree is performed by
instances of Binding
which are bound to types in the
schema.
To execute the encoder, one must have 3 bits of information:
As an exmaple, consider the encoding of a Filter
instance.
//instantiate hte configuration for the filter schmea
Configuration configuration = new OGCConfiguration();
//create the encoder
Encoder encoder = new Encoder( configuration );
//get a filter
Filter filter = ...;
//get the name of the 'filter' element in the schema
QName name = new QName( "http://www.opengis.net/ogc", "Filter" );
//encode
encoder.encode( filter, name );
Field Summary | |
---|---|
static QName |
COMMENT
Special name recognized by the encoder as a comment. |
Constructor Summary | |
---|---|
Encoder(Configuration configuration)
Creates an encoder from a configuration. |
|
Encoder(Configuration configuration,
XSDSchema schema)
Creates an encoder from a configuration and a specific schema instance. |
Method Summary | |
---|---|
protected void |
closeIterator(Iterator iterator,
Object source)
|
protected void |
comment(Element element)
|
void |
encode(Object object,
QName name,
ContentHandler handler)
|
void |
encode(Object object,
QName name,
OutputStream out)
Encodes an object. |
protected Node |
encode(Object object,
XSDNamedComponent component)
|
protected Node |
encode(Object object,
XSDNamedComponent component,
XSDTypeDefinition container)
|
Document |
encodeAsDOM(Object object,
QName name)
Encodes an object directly to a dom. |
String |
encodeAsString(Object object,
QName name)
Encodes an object directly to a string. |
protected void |
end(Element element,
XSDElementDeclaration declaration)
|
BindingWalker |
getBindingWalker()
|
Document |
getDocument()
|
Charset |
getEncoding()
Returns the Charset defining the character encoding scheme this Encoder uses to encode XML content. |
int |
getIndentSize()
Returns the indentation specified. |
int |
getLineWidth()
Returns the line width for breaking up long lines. |
NamespaceSupport |
getNamespaces()
Returns the namespace mappings maintained by the encoder. |
XSDSchema |
getSchema()
|
SchemaIndex |
getSchemaIndex()
|
boolean |
isIndenting()
Returns whether this Encoder produces indented XML. |
boolean |
isOmitXMLDeclaration()
Returns true if the XML document declaration should be omitted. |
void |
setEncodeFullDocument(boolean encodeFullDocument)
Deprecated. use setInline(boolean) . |
void |
setEncoding(Charset charset)
Sets the charset encoding scheme to be used in encoding XML content. |
void |
setIndenting(boolean doIndent)
Sets the indentation on and off. |
void |
setIndentSize(int indentSize)
Sets the indentation level in number of spaces used. |
void |
setInline(boolean inline)
Sets the encoder to "inline" mode. |
void |
setLineWidth(int lineWidth)
Sets the line width. |
void |
setNamespaceAware(boolean namespaceAware)
Sets wether the encoder should be namespace aware. |
void |
setOmitXMLDeclaration(boolean ommitXmlDeclaration)
Sets XML declaration omitting on and off. |
void |
setRootElementType(QName rootElementType)
Informs the encoder of the type of the root element to be used in cases where it can not be inferred. |
void |
setSchemaLocation(String namespaceURI,
String location)
Sets the schema location for a particular namespace uri. |
protected void |
start(Element element,
XSDElementDeclaration declaration)
|
void |
write(Object object,
QName name,
OutputStream out)
Deprecated. use encode(Object, QName, OutputStream) . |
Methods inherited from class Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
public static final QName COMMENT
Bindings can return this name in ComplexBinding.getProperties(Object)
to provide comments to be encoded.
Constructor Detail |
---|
public Encoder(Configuration configuration)
This constructor calls through to Encoder(Configuration, XSDSchema)
obtaining the schema instance from Configuration.schema()
.
configuration
- The encoder configuration.public Encoder(Configuration configuration, XSDSchema schema)
configuration
- The encoder configuration.schema
- The schema instance.Method Detail |
---|
public void setEncoding(Charset charset)
This encoding will determine the resulting character encoding for the XML content generated by this Encoder and will be reflected in the XML declaration tag.
charset
- the (non null) charset to encode XML content accordingly topublic Charset getEncoding()
If not otherwise set through setEncoding(Charset)
,
UTF-8
is used.
public void setOmitXMLDeclaration(boolean ommitXmlDeclaration)
ommitXmlDeclaration
- true
if XML declaration should be omittedpublic boolean isOmitXMLDeclaration()
public void setIndenting(boolean doIndent)
When set on, the default indentation level and default line wrapping is
used (see getIndentSize()
and getLineWidth()
). To
specify a different indentation level or line wrapping, use
#setIndent(int)
and setLineWidth(int)
).
doIndent
- true
if indentation should be onpublic boolean isIndenting()
Defaults to false
.
true
if indentation was specifiedsetIndentSize(int)
public void setIndentSize(int indentSize)
The document will not be indented if the indentation is set to zero.
Calling setIndenting(false)
will reset this value to zero,
calling it with true
will reset this value to the default.
indentSize
- the number, greater or equal than zero, of characters used to
indent, zero for no indentation.public int getIndentSize()
If no indentation was specified, zero is returned and the document should not be indented.
Defaults to 4
setIndenting(boolean)
public void setLineWidth(int lineWidth)
If zero then no line wrapping will occur. Calling
setIndenting(false)
will reset this value to zero, calling
setIndenting(true)
will set this value to the default.
lineWidth
- a number >= 0 used to limit line widthspublic int getLineWidth()
When indenting, and only when indenting, long lines will be broken at space boundaries based on this line width. No line wrapping occurs if this value is zero.
Defaults to 72
characters per line.
isIndenting()
public void setNamespaceAware(boolean namespaceAware)
Warning that setting this to false
will result in no
namespace prefixes on encoded elements and attributes, and no schema
declarations on the root element.document;
namespaces
- public NamespaceSupport getNamespaces()
Clients may register additional namespace mappings. This is useful when an application whishes to provide some "default" namespace mappings.
Clients should register namespace mappings in the current "context", ie
do not call NamespaceSupport.pushContext()
. Example:
Encoder parser = new Encoder( ... );
encoder.getNamespaces().declarePrefix( "foo", "http://www.foo.com" );
...
public void setEncodeFullDocument(boolean encodeFullDocument)
setInline(boolean)
.
encodeFullDocument
- public void setInline(boolean inline)
When this flag is set encode(Object, QName, ContentHandler)
should
be used to encode.
public void setRootElementType(QName rootElementType)
This method is used in cases where the element being encoded is not declared as global in the schema.
typeName
- The type name of the root element.public void setSchemaLocation(String namespaceURI, String location)
Registering a schema location will include it on the "schemaLocation" attribute of the root element of the encoding.
namespaceURI
- A namespace uri.location
- A schema location.public BindingWalker getBindingWalker()
public SchemaIndex getSchemaIndex()
public XSDSchema getSchema()
public void write(Object object, QName name, OutputStream out) throws IOException, SAXException
encode(Object, QName, OutputStream)
.
IOException
SAXException
public Document getDocument()
public void encode(Object object, QName name, OutputStream out) throws IOException
An object is encoded as an object, name pair, where the name is the name of an element declaration in a schema.
object
- The object being encoded.name
- The name of the element being encoded in the schema.out
- The output stream.
IOException
public void encode(Object object, QName name, ContentHandler handler) throws IOException, SAXException
IOException
SAXException
public Document encodeAsDOM(Object object, QName name) throws IOException, SAXException, TransformerException
Note that this method should be used for testing or convenience since it does not stream and loads the entire encoded result into memory.
IOException
SAXException
TransformerException
public String encodeAsString(Object object, QName name) throws IOException
Note that this method should be used for testing or convenience since it does not stream and loads the entire encoded result into memory.
IOException
protected void closeIterator(Iterator iterator, Object source)
protected Node encode(Object object, XSDNamedComponent component)
protected Node encode(Object object, XSDNamedComponent component, XSDTypeDefinition container)
protected void start(Element element, XSDElementDeclaration declaration) throws SAXException
SAXException
protected void comment(Element element) throws SAXException, IOException
SAXException
IOException
protected void end(Element element, XSDElementDeclaration declaration) throws SAXException
SAXException
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |