org.dhmp.io
Class XMLMapInputStream

java.lang.Object
  extended byorg.dhmp.io.AbstractMapInput
      extended byorg.dhmp.io.XMLMapInputStream
All Implemented Interfaces:
MapInput

public class XMLMapInputStream
extends AbstractMapInput

This class reads XML input and generates HierarchicalMap. Each element of XML will become a key for HierarchicalMap's entry and the corresponding value will become a value for that key. So a XML like <Element>Value of Element</Element> will generate a entry in the HierarchicalMap with "Element" as a key and "Value of Element" as its value.

Due to the concept of map, HierarchicalMap can only hold a single value, some adjustments was made to represent the XML using HierarchicalMap. Basically, there are two modes to map XML into HierarchicalMap: simplified mode (default) and pure mode. The latter mode can be set initializing the XMLMapInputStream calling init method:

XMLMapInputStream xin = new XMLMapInputStream(new FileInputStream("somexmlfile.xml"));
 xin.init("<Param pure='true'/>"); 
In pure mode, there are two auxiliary classes: org.dhmp.util.xml.Attribute and org.dhmp.util.xml.ElementValue. If an element has a set of attributes, a new HierarchicalMap will be created holding each attribute/value pair. This HierarchicalMap will be added as a value for that element. The value will be a String in simplified mode and Class Attribute will be used to hold the attribute's value to distinguish from element's value in pure mode. If the element has itself a value, this will be put in the same HierarchicalMap as for attributes but the class ElementValue will be used to distinguish from attribute's value and the key "_ElementValue" will be used. Let's use the followin XML as example:
<Root_Element Root_attribute="roots attribute">
   roots text
   <Element/>
   <ElementWithValue>elements value</ElementWithValue>
   <ElementWithChild>
      <Element Attrib1="a1" Attrib2="a2"/>
      <Element Attrib3="a3" Attrib4="a4">e0</Element>
      <Element Attrib5="a5" Attrib6="a6">
         <Element1>e1</Element1>
      </Element>
      <Element Attrib7="a7" Attrib8="a8">
         <Element2>e2</Element2>
         asd hjk
         <Element3 Attrib9="a9" Attrib10="a10">e3</Element3>
         def ghi
         jkl mno
      </Element>
      <Element Attrib11="a11" Attrib12="a12">e1</Element>
   </ElementWithChild>
 </Root_Element>

This will generate the following HierarchicalMap:
<Root_Element>
  <Root_attribute>roots attribute</Root_attribute>
  <_ElementValue>roots text</_ElementValue>
  <Element></Element>
  <ElementWithValue>elements value</ElementWithValue>
  <ElementWithChild>
    <Element>
      <Attrib1>a1</Attrib1>
      <Attrib2>a2</Attrib2>
    </Element>
    <Element>
      <Attrib3>a3</Attrib3>
      <Attrib4>a4</Attrib4>
      <_ElementValue>e0</_ElementValue>
    </Element>
    <Element>
      <Attrib5>a5</Attrib5>
      <Attrib6>a6</Attrib6>
      <Element1>e1</Element1>
    </Element>
    <Element>
      <Attrib7>a7</Attrib7>
      <Attrib8>a8</Attrib8>
      <Element2>e2</Element2>
      <_ElementValue>asd hjk</_ElementValue>
      <Element3>
        <Attrib9>a9</Attrib9>
        <Attrib10>a10</Attrib10>
        <_ElementValue>e3</_ElementValue>
      </Element3>
      <_ElementValue>def ghi      jkl mno</_ElementValue>
    </Element>
    <Element>
      <Attrib11>a11</Attrib11>
      <Attrib12>a12</Attrib12>
      <_ElementValue>e1</_ElementValue>
    </Element>
  </ElementWithChild>
 </Root_Element>

Version:
1.0
Author:
Hideyuki

Constructor Summary
XMLMapInputStream(java.io.InputStream in)
          Creates a new instance of XMLMapInputStream.
XMLMapInputStream(java.io.InputStream in, java.nio.charset.Charset cs)
          Creates a new instance of XMLMapInputStream.
XMLMapInputStream(java.io.InputStream in, java.lang.String charsetName)
          Creates a new instance of XMLMapInputStream.
 
Method Summary
protected  java.lang.String getKey()
           
 java.lang.String getPrefix(java.lang.String UI)
           
protected  int getReference()
           
protected  int getType()
          Error handler to report errors and warnings
protected  java.lang.Object getValue()
           
protected  void init()
           
 void init(HierarchicalMap paramMap)
          Initializes this class using the configuration passed.
 void init(java.lang.String config)
          Initializes this class using the configuration passed.
 
Methods inherited from class org.dhmp.io.AbstractMapInput
close, getBaseMap, readMap, readMap, readMap, readMap, readMapUntil, readMapUntil, replace, setBaseMap
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

XMLMapInputStream

public XMLMapInputStream(java.io.InputStream in)
Creates a new instance of XMLMapInputStream.

Parameters:
in - Input stream from where the maps are read.

XMLMapInputStream

public XMLMapInputStream(java.io.InputStream in,
                         java.lang.String charsetName)
Creates a new instance of XMLMapInputStream.

Parameters:
in - Input stream from where the maps are read.
charsetName - name of charset to be used.

XMLMapInputStream

public XMLMapInputStream(java.io.InputStream in,
                         java.nio.charset.Charset cs)
Creates a new instance of XMLMapInputStream.

Parameters:
in - Input stream from where the maps are read.
cs - charset to be used.
Method Detail

init

public void init(java.lang.String config)
Initializes this class using the configuration passed. The configuration must be in XML format like "<Param compress=\"true\"/>" or "<Param compress='true\'>". Note that first element must be "Param".

Valid configurations are (bold for default value) :

Specified by:
init in interface MapInput
Overrides:
init in class AbstractMapInput
Parameters:
config - configuration used by this class.

init

public void init(HierarchicalMap paramMap)
Description copied from interface: MapInput
Initializes this class using the configuration passed. The configuration must be created using HierarchicalMap. The valid configuration are the same as from init(String).

Specified by:
init in interface MapInput
Overrides:
init in class AbstractMapInput

init

protected void init()
Overrides:
init in class AbstractMapInput

getPrefix

public java.lang.String getPrefix(java.lang.String UI)

getType

protected int getType()
               throws java.io.IOException
Error handler to report errors and warnings

Specified by:
getType in class AbstractMapInput
Throws:
java.io.IOException

getKey

protected java.lang.String getKey()
                           throws java.io.IOException
Specified by:
getKey in class AbstractMapInput
Throws:
java.io.IOException

getReference

protected int getReference()
                    throws java.io.IOException
Specified by:
getReference in class AbstractMapInput
Throws:
java.io.IOException

getValue

protected java.lang.Object getValue()
                             throws java.io.IOException
Specified by:
getValue in class AbstractMapInput
Throws:
java.io.IOException