org.dhmp.io
Class MapWriter

java.lang.Object
  extended byorg.dhmp.io.MapWriter
All Implemented Interfaces:
MapOutput

public class MapWriter
extends java.lang.Object
implements MapOutput

This class can generate large output formed by several HierarchicalMap and also support relative key manipulation.

 <order>
    <address>
        <shipping>
            ...
        </shipping>
        <billing>
            ...
        </billing>
    </address>
 </order>
For instance, the structure above can be generated as follows:
 MapWriter mw  = new MapWriter(System.out);
 MapWriter mw2 = mw.writeMap("Order/Address");
 
 mw2.writeMap("Shipping", shippingMap);
 mw2.writeMap("Billing", billingMap);
 
Notice that each MapWriter keeps an internal reference. The subsequent writeMap methods will write the map starting from this reference. This object is not synchronizes and also it is a stateful object. Therefore, avoid sharing this object with other threads.

Author:
hideyuki

Constructor Summary
MapWriter(AbstractMapOutput out)
          Creates a new instance of MapWriter sharing the same relative position.
MapWriter(MapWriter out)
          Creates a new instance of MapWriter.
 
Method Summary
 void close()
          Closes this output stream and releases any system resources associated with the stream.
 void flush()
          Flushes this output stream and forces any buffered output bytes to be written out.
 void flushAll()
          Flushes this output stream and also ends any HierarchicalMap written so far.
 java.lang.String getCurrentPath()
          Gets the current path relative to the reference.
 java.lang.String getReferencePath()
          Gets the path used as reference for all writing methods.
 boolean hasSamePath()
          Returns true if the path has not changed after last writing method.
 void init(HierarchicalMap paramMap)
          Initializes this class using the configuration passed.
 void init(java.lang.String config)
          Initializes this class using the configuration passed.
 MapWriter newCurrentWriter()
          Returns new MapWriter with reference path set to current path of this class.
 MapWriter newCurrentWriter(java.lang.Object start)
          Returns new MapWriter with reference path set to start.
 MapWriter newLatestWriter()
          Returns new MapWriter with reference path set to last written position.
 MapWriter newWriter()
          Returns new MapWriter with same reference path.
 MapOutput writeMap(HierarchicalMap map)
          Writes out a HierarchicalMap to output.
 MapOutput writeMap(java.lang.Object start, HierarchicalMap map)
          Writes out a HierarchicalMap to output relative to the start.
 MapOutput writeMap(java.lang.String path)
          Writes a HierarchicalMap where each of the elements that composes the path are represented as nested nodes.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

MapWriter

public MapWriter(MapWriter out)
Creates a new instance of MapWriter.

Parameters:
out - MapOutput to where the maps are written.

MapWriter

public MapWriter(AbstractMapOutput out)
Creates a new instance of MapWriter sharing the same relative position.

Parameters:
out - MapOutput with shared output.
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".

Configuration change is accepted until the first writing method is called. Check the specific MapOutput init method for valid configurations.

Specified by:
init in interface MapOutput
Parameters:
config - configuration used by this class.

init

public void init(HierarchicalMap paramMap)
Initializes this class using the configuration passed. The configuration must be created using HierarchicalMap. The valid parameters are the same as from init(String). Configuration change is accepted until the first writing method is called.

Specified by:
init in interface MapOutput
Parameters:
paramMap - HierarchicalMap containing the configuration.

writeMap

public MapOutput writeMap(java.lang.String path)
                   throws MapIOException
Writes a HierarchicalMap where each of the elements that composes the path are represented as nested nodes. writeMap("a/b/c") is similar to the following code:
map.add("a/b/c")
 writeMap(map)

Specified by:
writeMap in interface MapOutput
Parameters:
path - each element of the key is written as node.
Returns:
newly created MaprWriter with reference set to path.
Throws:
MapIOException - if an I/O error occurs.

writeMap

public MapOutput writeMap(HierarchicalMap map)
                   throws MapIOException
Writes out a HierarchicalMap to output.

Specified by:
writeMap in interface MapOutput
Parameters:
map - HierarchicalMap to be written to the output.
Returns:
newly created MaprWriter with same reference.
Throws:
MapIOException - if an I/O error occurs.

writeMap

public MapOutput writeMap(java.lang.Object start,
                          HierarchicalMap map)
                   throws MapIOException
Writes out a HierarchicalMap to output relative to the start. Each element that composes the path "start" is written as node.

Specified by:
writeMap in interface MapOutput
Parameters:
map - HierarchicalMap to be written to the output.
start - node from where the writing starts.
Returns:
newly created MaprWriter with reference set to start.
Throws:
MapIOException - if an I/O error occurs.

flush

public void flush()
           throws MapIOException
Flushes this output stream and forces any buffered output bytes to be written out.

Specified by:
flush in interface MapOutput
Throws:
MapIOException - if an I/O error occurs.

close

public void close()
           throws MapIOException
Closes this output stream and releases any system resources associated with the stream.

Specified by:
close in interface MapOutput
Throws:
MapIOException - if an I/O error occurs.

getReferencePath

public java.lang.String getReferencePath()
Gets the path used as reference for all writing methods.

Returns:
reference path.

getCurrentPath

public java.lang.String getCurrentPath()
Gets the current path relative to the reference.

Returns:
current path.

hasSamePath

public boolean hasSamePath()
Returns true if the path has not changed after last writing method.

Returns:
true if path has not changed.

newWriter

public MapWriter newWriter()
Returns new MapWriter with same reference path.

Returns:
newly created MapWriter.

newCurrentWriter

public MapWriter newCurrentWriter()
Returns new MapWriter with reference path set to current path of this class.

Returns:
newly created MapWriter.

newCurrentWriter

public MapWriter newCurrentWriter(java.lang.Object start)
Returns new MapWriter with reference path set to start. This is equivalent to writeMap(start).

Parameters:
start - node referred by reference path.
Returns:
newly created MapWriter.

newLatestWriter

public MapWriter newLatestWriter()
Returns new MapWriter with reference path set to last written position. If writeMap(start, map) or writeMap(map) is called, this method will return a MapWriter with reference set to the last node inside map whereas the method newCurrentWriter will return a MapWriter with reference set to the start or to the original reference held by MapWriter before writing the map.

Returns:
newly created MapWriter.

flushAll

public void flushAll()
Description copied from interface: MapOutput
Flushes this output stream and also ends any HierarchicalMap written so far.

Specified by:
flushAll in interface MapOutput