public class XMLRecord
extends java.lang.Object
HashMap
. JDOM
provides the capability to parse, create, edit and write XML documents,
however it lacks an efficient method for locating individual elements,
as well as needing some help with controlling where new elements get
inserted and how comments get processed. It should be noted that
only a limited subset of JDOM's capability is actually utilized by
this implementation.
Individual JDOM XML elements are wrapped in XMLNode
objects before being stored in the local HashMap. The map key for each
XMLNode
is a dot delimited string formed from the paths
through the JDOM hierarchy, obtained when reading in the XML template.
Key paths are constructed and described in the XMLNode
details.
Currently, XMLRecord
is instantiated with an XML
file, to provide the mappings from the JDOM tree to the local
HashMap
. The JDOM document is created, and that
document's root element is wrapped in an XMLNode
. Then each
child element is recursively processed. As each child element is processed,
it is likewise wrapped in an XMLNode
, its key path is
constructed, and then is inserted into the map. Each node maintains the
proper parent-child relations.
Modifier and Type | Field and Description |
---|---|
protected org.jdom.Document |
doc
The JDOM document this record maps.
|
protected java.lang.StringBuffer |
footnote
A special comment intended as the final comment in the XML file.
|
protected java.util.HashMap |
map
Maps reference pointers to elements in the XML hierarchy.
|
protected XMLNode |
root
The root node of this
XMLRecord . |
protected java.io.File |
xmlFile
The source XML file used to identify the XML hierarchy.
|
Constructor and Description |
---|
XMLRecord(java.io.File file)
Accepts an XML file and maps its XML structure to a
HashMap . |
Modifier and Type | Method and Description |
---|---|
void |
addNode(XMLNode parent,
org.jdom.Element element)
Adds a JDOM element to the current record.
|
void |
addNodes(java.lang.String name,
XMLNode parent,
int num)
Adds one or more like elements to the current record.
|
protected void |
cleanMap(XMLNode node)
Called by
removeNodes to remove a node and its children
from the local map. |
void |
clear() |
protected void |
clearMap(XMLNode node) |
java.lang.String |
getFootnote()
Getter method for the XML footnote comment.
|
XMLNode |
getNode(java.lang.String name)
Returns a node of the current record, or null if the node does not
exist.
|
XMLNode |
getRoot()
Returns the root node of the current record.
|
boolean |
outputRecord()
Ouputs the current record to
xmlFile , which
is the same file used to create the current record unless
outputRecord(newfile) has been called. |
boolean |
outputRecord(java.lang.String newfile)
A convenience method for outputting the current record to a given
filename.
|
protected void |
process(XMLNode parentNode)
Recursively iterates through the children XML elements, wrapping each
in an XMLNode and adding the created node to the local map.
|
protected void |
processComments()
Process the document's (as opposed to an element's) comments for
purposes of extracting the footnote.
|
void |
removeNodes(java.lang.String name,
XMLNode parent,
int num)
Removes one or more like elements from the parent
XMLNode . |
void |
setFootnote(java.lang.String value)
Setter method for the XML footnote comment.
|
protected XMLNode root
XMLRecord
.protected org.jdom.Document doc
protected java.util.HashMap map
protected java.io.File xmlFile
protected java.lang.StringBuffer footnote
public XMLRecord(java.io.File file) throws java.lang.Exception
HashMap
.file
- A well-formed XML file.java.lang.Exception
public void clear()
protected void clearMap(XMLNode node)
protected void process(XMLNode parentNode)
If called on the root node, this method effectively builds the entire map. If called on a subordinate node, then it will build only the local sub-hierarchy.
parentNode
- The node from which to start processing.public boolean outputRecord(java.lang.String newfile)
outputRecord
method used to write
the record to that same file. This method, however, reassigns the
protected member xmlFile
in order to write to a new
file. This is done in the case when an XML template file is used,
and the template should be preserved.newfile
- String representing the filename or absolute filename
of the new file to write.public boolean outputRecord()
xmlFile
, which
is the same file used to create the current record unless
outputRecord(newfile)
has been called. Once that method
is called, xmlFile
is changed, and subsequent
calls to this method will output to the new file.public XMLNode getRoot()
XMLNode
wrapper for the JDOM document
root elementpublic XMLNode getNode(java.lang.String name)
XMLNode
for details on path construction.name
- String representing the node path in the JDOM tree.XMLNode
wrapper for the element requestedpublic void addNode(XMLNode parent, org.jdom.Element element)
XMLNode
and processed (added to
the local map).
NOTE: Both parent and element are assumed to be non-null in the current implementation, future implementations will provide this assertion check.
parent
- XMLNode that this element should be added to.element
- A new JDOM element to add to this parent node.public void addNodes(java.lang.String name, XMLNode parent, int num)
XMLNode
, and it is assumed that
there already exists one such element as a child to the parent node.
If no such like element exists, then it should be created and the
addNode
method called first. Then invoke this method to
add multiples of the same element as new XMLNodes in the local map.
Once the elements are added as nodes to the map, their content
may be defined.
NOTE: New elements are created as copies of existing elements, which means their text content cannot be assumed empty.
name
- String representing the name of like elements to add.parent
- XMLNode that the like elements should be added to.num
- Number of like elements to add.public void removeNodes(java.lang.String name, XMLNode parent, int num)
XMLNode
.
No more elements can be removed than what exists, and no errors are
generated if an attempt to do so is made. The wrapping nodes are also
removed from the local map, preserving the integrity of the relationship
with the JDOM tree.name
- String representing the name of the like elements.parent
- XMLNode that the like elements should be removed from.num
- Number of like elements to remove.protected void cleanMap(XMLNode node)
removeNodes
to remove a node and its children
from the local map.node
- XMLNode to be removed.public java.lang.String getFootnote()
public void setFootnote(java.lang.String value)
value
- String representing the footnote comment to add.protected void processComments()