Class HashMapperStringToComplex

  • All Implemented Interfaces:
    IAliasable, IMarshaller, IUnmarshaller

    public class HashMapperStringToComplex
    extends java.lang.Object
    implements IMarshaller, IUnmarshaller, IAliasable

    Custom marshaller/unmarshaller for java.util.Map instances. This handles mapping hash maps with simple keys and complex values to and from XML. There are a number of limitations, though. First off, the key objects are marshalled as simple text values, using the toString() method to convert them to String. When unmarshalling the keys are always treated as String values. The corresponding values can be any complex type with a <mapping> defined in the binding. The name of the top-level element in the XML structure can be configured in the binding definition, but the rest of the names are predefined and set in the code (though the namespace configured for the top-level element will be used with all the names).

    The net effect is that the XML structure will always be of the form:

    <map-name size="3">
       <entry key="38193">
         <customer state="WA" zip="98059">
           <name first-name="John" last-name="Smith"/>
           <street>12345 Happy Lane</street>
           <city>Plunk</city>
         </customer>
       </entry>
       <entry key="39122">
         <customer state="WA" zip="98094">
           <name first-name="Sally" last-name="Port"/>
           <street>932 Easy Street</street>
           <city>Fort Lewis</city>
         </customer>
       </entry>
       <entry key="83132">
         <customer state="WA" zip="98059">
           <name first-name="Mary" last-name="Smith"/>
           <street>12345 Happy Lane</street>
           <city>Plunk</city>
         </customer>
       </entry>
     </map-name>

    where "map-name" is the configured top-level element name, the "size" attribute is the number of pairs in the hash map, and the "entry" elements are the actual entries in the hash map.

    This is obviously not intended to handle all types of hash maps, but it should be useful as written for many applications and easily customized to handle other requirements.

    Author:
    Dennis M. Sosnoski
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private static int DEFAULT_SIZE  
      private int m_index  
      private java.lang.String m_name  
      private java.lang.String m_uri  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      protected java.lang.String getEntryElementName()
      Method which can be overridden to supply a different name for the element used to represent each item in the map.
      protected java.lang.String getKeyAttributeName()
      Method which can be overridden to supply a different name for the attribute defining the key value for each item in the map.
      protected java.lang.String getSizeAttributeName()
      Method which can be overridden to supply a different name for the wrapper element attribute used to give the number of items present.
      boolean isExtension​(java.lang.String mapname)
      Check if marshaller represents an extension mapping.
      boolean isPresent​(IUnmarshallingContext ctx)
      Check if instance present in XML.
      void marshal​(java.lang.Object obj, IMarshallingContext ictx)
      Marshal instance of handled class.
      java.lang.Object unmarshal​(java.lang.Object obj, IUnmarshallingContext ictx)
      Unmarshal instance of handled class.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • m_uri

        private java.lang.String m_uri
      • m_index

        private int m_index
      • m_name

        private java.lang.String m_name
    • Constructor Detail

      • HashMapperStringToComplex

        public HashMapperStringToComplex()
        Default constructor. This uses a pre-defined name for the top-level element. It'll be used by JiBX when no name information is supplied by the mapping which references this custom marshaller/unmarshaller.
      • HashMapperStringToComplex

        public HashMapperStringToComplex​(java.lang.String uri,
                                         int index,
                                         java.lang.String name)
        Aliased constructor. This takes a name definition for the top-level element. It'll be used by JiBX when a name is supplied by the mapping which references this custom marshaller/unmarshaller.
        Parameters:
        uri - namespace URI for the top-level element (also used for all other names within the binding)
        index - namespace index corresponding to the defined URI within the marshalling context definitions
        name - local name for the top-level element
    • Method Detail

      • getSizeAttributeName

        protected java.lang.String getSizeAttributeName()
        Method which can be overridden to supply a different name for the wrapper element attribute used to give the number of items present. If present, this attribute is used when unmarshalling to set the initial size of the hashmap. It will be generated when marshalling if the supplied name is non-null.
      • getEntryElementName

        protected java.lang.String getEntryElementName()
        Method which can be overridden to supply a different name for the element used to represent each item in the map.
      • getKeyAttributeName

        protected java.lang.String getKeyAttributeName()
        Method which can be overridden to supply a different name for the attribute defining the key value for each item in the map.
      • isExtension

        public boolean isExtension​(java.lang.String mapname)
        Description copied from interface: IMarshaller
        Check if marshaller represents an extension mapping. This is used by the framework in generated code to verify compatibility of objects being marshalled using an abstract mapping.
        Specified by:
        isExtension in interface IMarshaller
        Parameters:
        mapname - marshaller mapping name (generally the class name to be handled, or abstract mapping type name)
        Returns:
        true if this mapping is an extension of the abstract mapping, false if not
      • marshal

        public void marshal​(java.lang.Object obj,
                            IMarshallingContext ictx)
                     throws JiBXException
        Description copied from interface: IMarshaller
        Marshal instance of handled class. This method call is responsible for all handling of the marshalling of an object to XML text. It is called at the point where the start tag for the associated element should be generated.
        Specified by:
        marshal in interface IMarshaller
        Parameters:
        obj - object to be marshalled (may be null if property is not optional)
        ictx - XML text output context
        Throws:
        JiBXException - on error in marshalling process
      • isPresent

        public boolean isPresent​(IUnmarshallingContext ctx)
                          throws JiBXException
        Description copied from interface: IUnmarshaller
        Check if instance present in XML. This method can be called when the unmarshalling context is positioned at or just before the start of the data corresponding to an instance of this mapping. It verifies that the expected data is present.
        Specified by:
        isPresent in interface IUnmarshaller
        Parameters:
        ctx - unmarshalling context
        Returns:
        true if expected parse data found, false if not
        Throws:
        JiBXException - on error in unmarshalling process
      • unmarshal

        public java.lang.Object unmarshal​(java.lang.Object obj,
                                          IUnmarshallingContext ictx)
                                   throws JiBXException
        Description copied from interface: IUnmarshaller
        Unmarshal instance of handled class. This method call is responsible for all handling of the unmarshalling of an object from XML text, including creating the instance of the handled class if an instance is not supplied. When it is called the unmarshalling context is always positioned at or just before the start tag corresponding to the start of the class data.
        Specified by:
        unmarshal in interface IUnmarshaller
        Parameters:
        obj - object to be unmarshalled (may be null)
        ictx - unmarshalling context
        Returns:
        unmarshalled object (may be null)
        Throws:
        JiBXException - on error in unmarshalling process