org.deduced.xml
Class XmlModelEncoder

java.lang.Object
  extended by org.deduced.xml.XmlModelEncoder

public class XmlModelEncoder
extends java.lang.Object

XmlPropertyEncoder

extract the list of instances and the list of types used to fetch them, link those instances to the actual data

The readability enhanced flag can be used to include additional information in the created XML document to make it more human readable ( includes the collection type names and the property instance names ).

This encoder supports internal references, external references and object inheritance

Author:
Steve McDuff

Nested Class Summary
private static class XmlModelEncoder.PackageSubObjectList
          Object that holds a package and all its types and sub packages
private static class XmlModelEncoder.ReferenceCollection
          Object representing a referenced collection
private static class XmlModelEncoder.TypeInstanceList
          Object holding a property type and its instance list
 
Field Summary
private  java.util.Map<java.lang.Object,PropertyCollection<?,?>> collectionByReferenceMap
          map of all the properties that reference other collections
private  java.util.Map<java.lang.Object,PropertyCollection<?,?>> collectionByValueMap
          map of all the properties that are contained by value
private  java.util.Map<java.lang.Object,PropertyCollection<?,?>> collectionMap
          map of all the property collections decoded in the XML file by their unique ID
private  org.w3c.dom.Document document
          XML document used to generate the XML Structure
private  java.util.Map<java.lang.Object,PropertyCollection<?,?>> instanceMap
          map of all the instances encoded in the XML file by their unique ID
private  boolean isReadabilityEnhanced
          flag used to enhance readability in the generated XML by adding names in instances and types
private static java.util.logging.Logger LOGGER
          Logger
private  PropertyCollection<?,?> modelLayer
          model layer used to create references
private  NameReferenceFinder nameReferenceFinder
          name reference finder used to find external references names
private  java.io.OutputStream outStream
          the output stream that will receive the XML content
private  org.w3c.dom.Element referenceListElement
          root reference list XML element
private  PropertyCollection<?,?> rootCollection
          root collection that will be read from the XML file
private  org.w3c.dom.Element rootElement
          root XML element
private  java.util.IdentityHashMap<PropertyCollection<?,?>,XmlModelEncoder.PackageSubObjectList> schemaRootPackageMap
          root package read in the XML file
private  org.w3c.dom.Element typeListElement
          root type list XML element
private  java.util.Map<java.lang.Object,XmlModelEncoder.TypeInstanceList> typeMap
          map of all the types encoded in the XML file by their unique ID
private  org.w3c.dom.Element valueListElement
          root value list XML element
 
Constructor Summary
XmlModelEncoder()
          constructor
 
Method Summary
private  void addAllInstances(PropertyCollection<?,?> collection)
          add All Instances of a property collection to the object maps
private  void addInstance(PropertyCollection<?,?> instance)
          add Instance to the object map
private  void addObjectToReferenceMap(PropertyCollection<?,?> collection, java.lang.Object uniqueID)
          add Object To Value Map
private  void addObjectToRootMap(PropertyCollection<?,?> collection, java.lang.Object uniqueID)
          add Object To Value Map
private  void addObjectToValueMap(PropertyCollection<?,?> collection, java.lang.Object uniqueID)
          add Object To Value Map
private  XmlModelEncoder.TypeInstanceList addPropertyType(PropertyCollection<?,?> ownerType)
          add Property Type to the object map
private  void addReferenceMap(java.util.Map<java.lang.String,XmlModelEncoder.ReferenceCollection> rootMap, PropertyCollection<?,?> collection)
          add a Reference to the reference Map
private  void buildObjectMap(PropertyCollection<?,?> collection)
          build a map of all the property collections linked to the specified collection.
private static PropertyCollection<?,?> buildPackageList(java.util.List<PropertyCollection<?,?>> packageList, PropertyCollection<?,?> ownerType)
          build a list that contains all the packages in the hierarchy of the specified type
private  void cleanupTemporaryObjects()
          cleanup Temporary Objects
private  void createCollectionDocument(org.w3c.dom.Element parentElement, PropertyCollection<?,?> collection, java.lang.Object uniqueID)
          create Collection DOM element
protected static org.w3c.dom.Document createDocument()
          create the XML Document.
protected static org.w3c.dom.Document createDocumentFromFactory(javax.xml.parsers.DocumentBuilderFactory factory)
          create Document From Factory
private  XmlModelEncoder.ReferenceCollection createReferenceCollection(PropertyCollection<?,?> child, java.lang.String name, java.util.Map<java.lang.String,XmlModelEncoder.ReferenceCollection> mapToSearch)
          create Reference Collection
private  void createRootElements()
          create Root Elements in the DOM
private  void extractModel()
          extract the model layer from the collection to encode
private  void extractNamedReferenceFinder(PropertyCollection<?,?> applicationModel)
          extract Name Reference Finder
private  XmlModelEncoder.PackageSubObjectList fetchSubPackageObjectList(XmlModelEncoder.PackageSubObjectList currentPackageObjectList, PropertyCollection<?,?> currentPackage)
          fetch or add the package holder object in the object map
private  void fillParentList(PropertyCollection<?,?> collection, java.util.List<PropertyCollection<?,?>> collectionTree)
          fill a list with all the Parents of a collection
 java.io.OutputStream getOutStream()
           
private  boolean isChildOfRoots(PropertyCollection<?,?> collection)
          test to see if the specified collection is a child of the root collection being encoded
private  boolean isParentOf(PropertyCollection<?,?> collection, PropertyCollection<?,?> possibleParent)
          test to see if a collection is a parent of another one
 boolean isReadabilityEnhanced()
           
 void setOutStream(java.io.OutputStream setOutStream)
           
 void setReadabilityEnhanced(boolean setIsReadabilityEnhanced)
           
private  boolean skipProperty(PropertyCollection<?,?> collection, PropertyCollection<?,?> instance)
          check to see if the property should be skipped while writing the document
protected static void writeDocumentToStream(org.w3c.dom.Document document, java.io.OutputStream stream)
          write Document To Stream
private  void writeDomStructure()
          write Dom Structure
private  void writeInstanceAttributes(org.w3c.dom.Element instanceElement, PropertyCollection<?,?> instance)
          write Instance Attributes to the DOM
private  void writePackageToDom(org.w3c.dom.Element parentElement, XmlModelEncoder.PackageSubObjectList packageObjects)
          write a package to the DOM
private  void writeProperty(Property<?> value, org.w3c.dom.Element domElement)
          write a property to the DOM
 void writePropertyCollection(PropertyCollection<?,?> collection)
          write Property Collection in the configured encoder
 void writePropertyCollection(PropertyCollection<?,?> collection, PropertyCollection<?,?> applicationModel)
          write a property collection to a XML stream
private  void writeReferenceElements(org.w3c.dom.Element parentElement, java.util.Map<java.lang.String,XmlModelEncoder.ReferenceCollection> rootMap)
          write Reference Elements
private  void writeReferencesToDom()
          write References To DOM
private  void writeSchemaToDom(org.w3c.dom.Element parentElement, PropertyCollection<?,?> schema, XmlModelEncoder.PackageSubObjectList rootPackage, java.util.List<java.lang.String> usedSchemaNames)
          write Schema To document object model
private  void writeTypeAttributes(org.w3c.dom.Element domElement, PropertyCollection<?,?> type, java.lang.Object uniqueID)
          write Type Attributes
private  void writeTypeDataToDom(org.w3c.dom.Element parentElement, XmlModelEncoder.TypeInstanceList typeData)
          write Type Data To the document object model
private  void writeTypesToDom()
          write Types To DOM
private  void writeValue(java.lang.Object currentValue, org.w3c.dom.Element propertyElement, PropertyCollection<?,?> instance)
          write a value to the DOM
private  void writeValuesToDom()
          write Values To DOM
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

LOGGER

private static final java.util.logging.Logger LOGGER
Logger


nameReferenceFinder

private NameReferenceFinder nameReferenceFinder
name reference finder used to find external references names


typeMap

private java.util.Map<java.lang.Object,XmlModelEncoder.TypeInstanceList> typeMap
map of all the types encoded in the XML file by their unique ID


instanceMap

private java.util.Map<java.lang.Object,PropertyCollection<?,?>> instanceMap
map of all the instances encoded in the XML file by their unique ID


collectionByReferenceMap

private java.util.Map<java.lang.Object,PropertyCollection<?,?>> collectionByReferenceMap
map of all the properties that reference other collections


collectionByValueMap

private java.util.Map<java.lang.Object,PropertyCollection<?,?>> collectionByValueMap
map of all the properties that are contained by value


collectionMap

private java.util.Map<java.lang.Object,PropertyCollection<?,?>> collectionMap
map of all the property collections decoded in the XML file by their unique ID


schemaRootPackageMap

private java.util.IdentityHashMap<PropertyCollection<?,?>,XmlModelEncoder.PackageSubObjectList> schemaRootPackageMap
root package read in the XML file


rootCollection

private PropertyCollection<?,?> rootCollection
root collection that will be read from the XML file


modelLayer

private PropertyCollection<?,?> modelLayer
model layer used to create references


outStream

private java.io.OutputStream outStream
the output stream that will receive the XML content


document

private org.w3c.dom.Document document
XML document used to generate the XML Structure


rootElement

private org.w3c.dom.Element rootElement
root XML element


typeListElement

private org.w3c.dom.Element typeListElement
root type list XML element


referenceListElement

private org.w3c.dom.Element referenceListElement
root reference list XML element


valueListElement

private org.w3c.dom.Element valueListElement
root value list XML element


isReadabilityEnhanced

private boolean isReadabilityEnhanced
flag used to enhance readability in the generated XML by adding names in instances and types

Constructor Detail

XmlModelEncoder

public XmlModelEncoder()
constructor

Method Detail

writePropertyCollection

public void writePropertyCollection(PropertyCollection<?,?> collection)
                             throws EncodeException
write Property Collection in the configured encoder

Parameters:
collection - the collection to write
Throws:
EncodeException - any error that occurred during encoding

writePropertyCollection

public void writePropertyCollection(PropertyCollection<?,?> collection,
                                    PropertyCollection<?,?> applicationModel)
                             throws EncodeException
write a property collection to a XML stream

Parameters:
collection - the collection to encode
applicationModel - the application model used to extract references. If null is specified, then the owning model layer is used to extract references.
Throws:
EncodeException - any error encountered while encoding the document

extractNamedReferenceFinder

private void extractNamedReferenceFinder(PropertyCollection<?,?> applicationModel)
                                  throws EncodeException
extract Name Reference Finder

Parameters:
applicationModel - the application model to use to create the name reference finder
Throws:
EncodeException - any error encountered while encoding the document

extractModel

private void extractModel()
extract the model layer from the collection to encode


cleanupTemporaryObjects

private void cleanupTemporaryObjects()
cleanup Temporary Objects


isChildOfRoots

private boolean isChildOfRoots(PropertyCollection<?,?> collection)
test to see if the specified collection is a child of the root collection being encoded

Parameters:
collection - the collection to test
Returns:
true if the collection can be found under the root

isParentOf

private boolean isParentOf(PropertyCollection<?,?> collection,
                           PropertyCollection<?,?> possibleParent)
test to see if a collection is a parent of another one

Parameters:
collection - the collection to test
possibleParent - the possible parent
Returns:
true if the possible parent is really a parent of the specified collection

writeDomStructure

private void writeDomStructure()
                        throws EncodeException
write Dom Structure

Throws:
EncodeException - any encoding error

writeReferencesToDom

private void writeReferencesToDom()
                           throws EncodeException
write References To DOM

Throws:
EncodeException - any encoding errors

writeReferenceElements

private void writeReferenceElements(org.w3c.dom.Element parentElement,
                                    java.util.Map<java.lang.String,XmlModelEncoder.ReferenceCollection> rootMap)
write Reference Elements

Parameters:
parentElement - the parent element
rootMap - the map of all the references

addReferenceMap

private void addReferenceMap(java.util.Map<java.lang.String,XmlModelEncoder.ReferenceCollection> rootMap,
                             PropertyCollection<?,?> collection)
                      throws EncodeException
add a Reference to the reference Map

Parameters:
rootMap - the map that receives the reference
collection - the collection to add to the map
Throws:
EncodeException - any encoding error

createReferenceCollection

private XmlModelEncoder.ReferenceCollection createReferenceCollection(PropertyCollection<?,?> child,
                                                                      java.lang.String name,
                                                                      java.util.Map<java.lang.String,XmlModelEncoder.ReferenceCollection> mapToSearch)
                                                               throws EncodeException
create Reference Collection

Parameters:
child - the child collection
name - the name of this child
mapToSearch - the map to search for the name
Returns:
the reference collection
Throws:
EncodeException - any encoding error

fillParentList

private void fillParentList(PropertyCollection<?,?> collection,
                            java.util.List<PropertyCollection<?,?>> collectionTree)
fill a list with all the Parents of a collection

Parameters:
collection - the collection to fetch the hierarchy
collectionTree - the list that receives the list of parents

writeTypesToDom

private void writeTypesToDom()
                      throws EncodeException
write Types To DOM

Throws:
EncodeException - any error encountered while encoding the document

writeSchemaToDom

private void writeSchemaToDom(org.w3c.dom.Element parentElement,
                              PropertyCollection<?,?> schema,
                              XmlModelEncoder.PackageSubObjectList rootPackage,
                              java.util.List<java.lang.String> usedSchemaNames)
                       throws EncodeException
write Schema To document object model

Parameters:
parentElement - the xml element to get the schema
schema - the schema to write
rootPackage - the root package of the schema
usedSchemaNames - the list of schema names already used
Throws:
EncodeException - any error encountered while encoding the document

writePackageToDom

private void writePackageToDom(org.w3c.dom.Element parentElement,
                               XmlModelEncoder.PackageSubObjectList packageObjects)
write a package to the DOM

Parameters:
parentElement - the parent DOM element
packageObjects - the package object holder

writeTypeDataToDom

private void writeTypeDataToDom(org.w3c.dom.Element parentElement,
                                XmlModelEncoder.TypeInstanceList typeData)
write Type Data To the document object model

Parameters:
parentElement - the parent DOM element
typeData - the type data holder

writeInstanceAttributes

private void writeInstanceAttributes(org.w3c.dom.Element instanceElement,
                                     PropertyCollection<?,?> instance)
write Instance Attributes to the DOM

Parameters:
instanceElement - the DOM element that receives the attributes
instance - the instance to write

writeTypeAttributes

private void writeTypeAttributes(org.w3c.dom.Element domElement,
                                 PropertyCollection<?,?> type,
                                 java.lang.Object uniqueID)
write Type Attributes

Parameters:
domElement - the DOM element to receive the attributes
type - the type tow rite
uniqueID - the unique ID of the type

writeValuesToDom

private void writeValuesToDom()
                       throws EncodeException
write Values To DOM

Throws:
EncodeException - when encoding fails

createRootElements

private void createRootElements()
create Root Elements in the DOM


createDocument

protected static org.w3c.dom.Document createDocument()
                                              throws EncodeException
create the XML Document.

Returns:
the created document
Throws:
EncodeException - if creating the document causes a problem

createDocumentFromFactory

protected static org.w3c.dom.Document createDocumentFromFactory(javax.xml.parsers.DocumentBuilderFactory factory)
                                                         throws EncodeException
create Document From Factory

Parameters:
factory - the factory to use during creation
Returns:
the created document
Throws:
EncodeException - if creating the document causes a problem

writeDocumentToStream

protected static void writeDocumentToStream(org.w3c.dom.Document document,
                                            java.io.OutputStream stream)
                                     throws EncodeException
write Document To Stream

Parameters:
document - the document to write
stream - the stream to use
Throws:
EncodeException - any encoding error

createCollectionDocument

private void createCollectionDocument(org.w3c.dom.Element parentElement,
                                      PropertyCollection<?,?> collection,
                                      java.lang.Object uniqueID)
                               throws EncodeException
create Collection DOM element

Parameters:
parentElement - the parent DOM element
collection - the collection to write
uniqueID - the unique ID of the collection
Throws:
EncodeException - when encoding fails

writeProperty

private void writeProperty(Property<?> value,
                           org.w3c.dom.Element domElement)
                    throws EncodeException
write a property to the DOM

Parameters:
value - they property value
domElement - the parent DOM element
Throws:
EncodeException - when encoding fails

writeValue

private void writeValue(java.lang.Object currentValue,
                        org.w3c.dom.Element propertyElement,
                        PropertyCollection<?,?> instance)
                 throws EncodeException
write a value to the DOM

Parameters:
currentValue - the value to write
propertyElement - the DOM element that receives the value
instance - the instance of the value
Throws:
EncodeException - when encoding fails

buildObjectMap

private void buildObjectMap(PropertyCollection<?,?> collection)
                     throws EncodeException
build a map of all the property collections linked to the specified collection. This method will fill all the collection, references, types and instances to be written to the XML document

Parameters:
collection - the collection that will be written in the XML document
Throws:
EncodeException - any encoding error

addObjectToValueMap

private void addObjectToValueMap(PropertyCollection<?,?> collection,
                                 java.lang.Object uniqueID)
add Object To Value Map

Parameters:
collection - the collection to add
uniqueID - the unique ID of the collection

addObjectToRootMap

private void addObjectToRootMap(PropertyCollection<?,?> collection,
                                java.lang.Object uniqueID)
add Object To Value Map

Parameters:
collection - the collection to add
uniqueID - the unique ID of the collection

addObjectToReferenceMap

private void addObjectToReferenceMap(PropertyCollection<?,?> collection,
                                     java.lang.Object uniqueID)
add Object To Value Map

Parameters:
collection - the collection to add
uniqueID - the unique ID of the collection

addAllInstances

private void addAllInstances(PropertyCollection<?,?> collection)
                      throws EncodeException
add All Instances of a property collection to the object maps

Parameters:
collection - the collection to add to the object map
Throws:
EncodeException - any encoding error

skipProperty

private boolean skipProperty(PropertyCollection<?,?> collection,
                             PropertyCollection<?,?> instance)
check to see if the property should be skipped while writing the document

Parameters:
collection - the collection to write
instance - the instance to write
Returns:
true if the property should be skipped

addInstance

private void addInstance(PropertyCollection<?,?> instance)
                  throws EncodeException
add Instance to the object map

Parameters:
instance - the instance to add
Throws:
EncodeException - any encoding error

addPropertyType

private XmlModelEncoder.TypeInstanceList addPropertyType(PropertyCollection<?,?> ownerType)
                                                  throws EncodeException
add Property Type to the object map

Parameters:
ownerType - the property type to add
Returns:
the object that holds the type
Throws:
EncodeException - any encoding error

fetchSubPackageObjectList

private XmlModelEncoder.PackageSubObjectList fetchSubPackageObjectList(XmlModelEncoder.PackageSubObjectList currentPackageObjectList,
                                                                       PropertyCollection<?,?> currentPackage)
fetch or add the package holder object in the object map

Parameters:
currentPackageObjectList - the package to search
currentPackage - the sub package to add or fetch
Returns:
the sub package holder object

buildPackageList

private static PropertyCollection<?,?> buildPackageList(java.util.List<PropertyCollection<?,?>> packageList,
                                                        PropertyCollection<?,?> ownerType)
                                                 throws EncodeException
build a list that contains all the packages in the hierarchy of the specified type

Parameters:
packageList - the package list that receives all the packages
ownerType - the type to fetch the package list from
Returns:
returns the schema containing the type
Throws:
EncodeException - any encoding error

getOutStream

public java.io.OutputStream getOutStream()
Returns:
Returns the outStream.

setOutStream

public void setOutStream(java.io.OutputStream setOutStream)
Parameters:
setOutStream - The outStream to set.

isReadabilityEnhanced

public boolean isReadabilityEnhanced()
Returns:
Returns the isReadabilityEnhanced.

setReadabilityEnhanced

public void setReadabilityEnhanced(boolean setIsReadabilityEnhanced)
Parameters:
setIsReadabilityEnhanced - The isReadabilityEnhanced to set.