Class XMLRecord
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.
- Version:
- 1.0 02/01/01
- Author:
- Dave Deniman
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected org.jdom.DocumentThe JDOM document this record maps.protected StringBufferA special comment intended as the final comment in the XML file.protected HashMapMaps reference pointers to elements in the XML hierarchy.protected XMLNodeThe root node of thisXMLRecord.protected FileThe source XML file used to identify the XML hierarchy. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidAdds a JDOM element to the current record.voidAdds one or more like elements to the current record.protected voidCalled byremoveNodesto remove a node and its children from the local map.voidclear()protected voidGetter method for the XML footnote comment.Returns a node of the current record, or null if the node does not exist.getRoot()Returns the root node of the current record.booleanOuputs the current record toxmlFile, which is the same file used to create the current record unlessoutputRecord(newfile)has been called.booleanoutputRecord(String newfile) A convenience method for outputting the current record to a given filename.protected voidRecursively iterates through the children XML elements, wrapping each in an XMLNode and adding the created node to the local map.protected voidProcess the document's (as opposed to an element's) comments for purposes of extracting the footnote.voidremoveNodes(String name, XMLNode parent, int num) Removes one or more like elements from the parentXMLNode.voidsetFootnote(String value) Setter method for the XML footnote comment.
-
Field Details
-
root
The root node of thisXMLRecord. -
doc
protected org.jdom.Document docThe JDOM document this record maps. -
map
Maps reference pointers to elements in the XML hierarchy. -
xmlFile
The source XML file used to identify the XML hierarchy. -
footnote
A special comment intended as the final comment in the XML file.
-
-
Constructor Details
-
XMLRecord
Accepts an XML file and maps its XML structure to aHashMap.- Parameters:
file- A well-formed XML file.- Throws:
Exception
-
-
Method Details
-
clear
public void clear() -
clearMap
-
process
Recursively iterates through the children XML elements, wrapping each in an XMLNode and adding the created node to the local map. Element parent/child relationships are maintained.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.
- Parameters:
parentNode- The node from which to start processing.
-
outputRecord
A convenience method for outputting the current record to a given filename. Typically, the class is instantiated with an XML file, and the no argumentoutputRecordmethod used to write the record to that same file. This method, however, reassigns the protected memberxmlFilein 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.- Parameters:
newfile- String representing the filename or absolute filename of the new file to write.- Returns:
- true if succeeded, false otherwise
-
outputRecord
public boolean outputRecord()Ouputs the current record toxmlFile, which is the same file used to create the current record unlessoutputRecord(newfile)has been called. Once that method is called,xmlFileis changed, and subsequent calls to this method will output to the new file.- Returns:
- true if succeeded, false otherwise
-
getRoot
Returns the root node of the current record.- Returns:
- The
XMLNodewrapper for the JDOM document root element
-
getNode
Returns a node of the current record, or null if the node does not exist. The node is identified by its path in the JDOM tree. SeeXMLNodefor details on path construction.- Parameters:
name- String representing the node path in the JDOM tree.- Returns:
XMLNodewrapper for the element requested
-
addNode
Adds a JDOM element to the current record. The element is added to parent XMLNode, so parent is assumed to exist within the current record. The element may be a complex element with mixed content. The element is added after like elements if other like elements are already attached to this parent node, otherwise it is added at the end of the parent node's list of children elements. After adding the element, the element is wrapped in anXMLNodeand 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.
- Parameters:
parent- XMLNode that this element should be added to.element- A new JDOM element to add to this parent node.
-
addNodes
Adds one or more like elements to the current record. The elements are added to the parentXMLNode, 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 theaddNodemethod 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.
- Parameters:
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.
-
removeNodes
Removes one or more like elements from the parentXMLNode. 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.- Parameters:
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.
-
cleanMap
Called byremoveNodesto remove a node and its children from the local map.- Parameters:
node- XMLNode to be removed.
-
getFootnote
Getter method for the XML footnote comment. The footnote is read from and written to the end of the XML file as an XML comment. Look at the source for implementation details, because the same strategy could be applied to include additional comments, such as headers, other footers, or specifc labeled comments to elements.- Returns:
- String representing the footnote comment.
-
setFootnote
Setter method for the XML footnote comment. The footnote is read from and written to the end of the XML file as an XML comment. Look at the source for implementation details, because the same strategy could be applied to include additional comments, such as headers, other footers, or specifc labeled comments to elements.- Parameters:
value- String representing the footnote comment to add.
-
processComments
protected void processComments()Process the document's (as opposed to an element's) comments for purposes of extracting the footnote. Also fixes a bug in JDOM which adds extraneous spaces to comments each time they get processed.
-