Class Encoder

  • public class Encoder
    extends Object
    Encodes objects as xml based on a schema.

    The function of the encoder is to traverse a tree of objects serializing 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:

    1. The root object in the tree to be encoded
    2. The schema / configuration of the instance document being encoded.
    3. A name of the element defined in the schema which corresponds to the root object in the tree.

    As an example, consider the encoding of a Filter instance.

      //instantiate the configuration for the filter schema
      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( "", "Filter" );
      encoder.encode( filter, name );
    Justin Deoliveira, The Open Planning Project
    • Constructor Detail

      • Encoder

        public Encoder​(Configuration configuration)
        Creates an encoder from a configuration.

        This constructor calls through to Encoder(Configuration, XSDSchema) obtaining the schema instance from {@link Configuration#getXSD()#getSchema()}).

        configuration - The encoder configuration.
      • Encoder

        public Encoder​(Configuration configuration,
                       XSDSchema schema)
        Creates an encoder from a configuration and a specific schema instance.
        configuration - The encoder configuration.
        schema - The schema instance.
    • Method Detail

      • getSchema

        public static XSDSchema getSchema​(Configuration configuration)
      • setEncoding

        public void setEncoding​(Charset charset)
        Sets the charset encoding scheme to be used in encoding XML content.

        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 to
      • getEncoding

        public Charset getEncoding()
        Returns the Charset defining the character encoding scheme this Encoder uses to encode XML content.

        If not otherwise set through setEncoding(Charset), UTF-8 is used.

        the character set used for encoding
      • setOmitXMLDeclaration

        public void setOmitXMLDeclaration​(boolean ommitXmlDeclaration)
        Sets XML declaration omitting on and off.
        ommitXmlDeclaration - true if XML declaration should be omitted
      • isOmitXMLDeclaration

        public boolean isOmitXMLDeclaration()
        Returns true if the XML document declaration should be omitted. The default is false.
        whether the xml declaration is omitted, defaults to false.
      • setIndenting

        public void setIndenting​(boolean doIndent)
        Sets the indentation on and off.

        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 setIndentSize(int) and setLineWidth(int)).

        doIndent - true if indentation should be on
      • isIndenting

        public boolean isIndenting()
        Returns whether this Encoder produces indented XML.

        Defaults to false.

        true if indentation was specified
        See Also:
      • setIndentSize

        public void setIndentSize​(int indentSize)
        Sets the indentation level in number of spaces used.

        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.
      • getIndentSize

        public int getIndentSize()
        Returns the indentation specified.

        If no indentation was specified, zero is returned and the document should not be indented.

        Defaults to 4

        zero if not indenting, the number of white space characters used for indentation otherwise.
        See Also:
      • setLineWidth

        public void setLineWidth​(int lineWidth)
        Sets the line width.

        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 widths
      • getLineWidth

        public int getLineWidth()
        Returns the line width for breaking up long lines.

        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.

        the number of characters at which line wrapping happens, or zero for no line wrapping
        See Also:
      • setNamespaceAware

        public void setNamespaceAware​(boolean namespaceAware)
        Sets wether the encoder should be namespace aware.

        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;

      • getNamespaces

        public NamespaceSupport getNamespaces()
        Returns the namespace mappings maintained by the encoder.

        Clients may register additional namespace mappings. This is useful when an application wishes 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", "" ); ...

        The namespace support containing prefix to uri mappings.
      • setRootElementType

        public 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.

        This method is used in cases where the element being encoded is not declared as global in the schema.

        rootElementType - The type name of the root element.
      • setSchemaLocation

        public void setSchemaLocation​(String namespaceURI,
                                      String location)
        Sets the schema location for a particular namespace uri.

        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.
      • getBindingWalker

        public BindingWalker getBindingWalker()
        The walker used to traverse bindings, this method is for internal use only.
      • getSchemaIndex

        public SchemaIndex getSchemaIndex()
        The index of schema components, this method is for internal use only.
      • getSchema

        public XSDSchema getSchema()
        the schema.
      • getDocument

        public Document getDocument()
        The document used as a factory to create dom nodes.
      • encode

        public void encode​(Object object,
                           QName name,
                           OutputStream out)
                    throws IOException
        Encodes an object.

        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.
      • encodeAsString

        public String encodeAsString​(Object object,
                                     QName name)
                              throws IOException
        Encodes an object directly to a string.

        Note that this method should be used for testing or convenience since it does not stream and loads the entire encoded result into memory.

      • closeIterator

        protected void closeIterator​(Iterator iterator,
                                     Object source)
      • encode

        protected Node encode​(Object object,
                              XSDNamedComponent component)
      • encode

        protected Node encode​(Object object,
                              XSDNamedComponent component,
                              XSDTypeDefinition container)
      • getConfiguration

        public Configuration getConfiguration()
        Returns the configuration used by the encoder
        the configuration
      • getBindingLoader

        public BindingLoader getBindingLoader()
        Returns the object used to load xml bindings in this encoder
      • getContext

        public PicoContainer getContext()
        Returns the Pico context used by this encoder