Package org.vast.xml
Class DOMHelper
java.lang.Object
org.vast.xml.DOMHelper
TODO DOMHelper description TODO try to avoid calls to xerces specific classes
- Since:
- Nov 3, 2005
-
Field Summary
FieldsModifier and TypeFieldDescriptionCurrent path inside the xml file being parsed.
Paths have to be given as element names separated by /
Namespaces are separated from the node name by ':'
Example: "gml:feature/gml:polyline/epsg"static final String
protected org.vast.xml.QName
protected Map<String,
XMLDocument> Linked documents (URI object -> XMLDocument Object)protected XMLFragment
Main Document objectprotected XMLNodeList
List of nodes returnedprotected static final String
User prefix to domain mapprotected boolean
protected String[]
static final String
static final String
-
Constructor Summary
ConstructorsConstructorDescriptionDOMHelper
(InputStream inputStream, boolean validation) Loads an existing XML document from the given InputStreamDOMHelper
(InputStream inputStream, boolean validation, Map<String, String> schemaLocations) Loads an existing XML document from the given InputStreamCreates a DOMHelper on a new document with the given QNameLoads an existing XML document from the given URLLoads an existing document from the given DOM Document object -
Method Summary
Modifier and TypeMethodDescriptionaddElement
(String nodePath) Default version of the previous method using the base element as the parent element.addElement
(Node parentNode, String nodePath) Adds an element to the document at the given location relative to the parent element.Adds any type of node to the parent nodevoid
addNSDeclaration
(String prefix, String nsUri) void
addSchemaLocation
(String nsUri, String schemaUrl) Add a schema location entry (xsi:schemaLocation attribute)void
addUserPrefix
(String prefix, String nsDomain) Add an entry to the table of user prefixes This table is used to map user prefixes to a real namespace uriappendChild
(Node parent, String child) Adds a node to the end of the children listprotected int
comparePaths
(XMLDocument currentDocument, ArrayList<String> actualPath, String[] wantedPath) Check if the paths are matching
Returns -1 if the beginning of wantedPath doesn't match actualPath
Otherwise returns the number levels still needed to reach the wantedPathvoid
createDocument
(String qname) Creates a new Document with the given qname The new document will replace the document loaded in this DOMHelpercreateElement
(String qname) Creates a new element using the main documentcreateElement
(XMLDocument parentDoc, String qname) Creates a new element using the underlying DOM Document.boolean
existAttribute
(String nodePath) Default version of the previous function with startNode = base node of document (not always the root !!)boolean
existAttribute
(Element startElement, String nodePath) Determine if the attribute corresponding to the given path existsboolean
existElement
(String nodePath) Default version of the previous function with startNode = base node of document (not always the root !!)boolean
existElement
(Element startElement, String nodePath) Determine if the element corresponding to the given path existsgetAllChildElements
(Element parentElement) Get all child elements of the given node (don't return any text or comment nodes !) This method will follow any xlink:href if specified.getAllElements
(String name) Default version of the previous function
with startNode = base node of document (not always the root !!)getAllElements
(Element startElement, String tagName) Look for all the elements below the startNode with the given tag name If the XML document defines namespaces, this method requires a fully qualified element name (prefix + local name), and the prefix needs to be defined by calling addUserPrefix(prefix, ns-uri)getAttributeValue
(String nodePath) Default version of the previous function with startNode = base node of document (not always the root !!)getAttributeValue
(Element startElement, String nodePath) Read the attribute valueReturns the base element of the fragment attached to this readergetChildElements
(Node parentNode) Get the child elements (only elements !!) of the parentNode This method is basic...Gets the DOM Document attached to this DOMHelpergetElement
(String nodePath) Default version of the previous function with startNode = base node of document (not always the root !!)getElement
(Element startElement, String nodePath) Find the first element matching the given pathgetElements
(String nodePath) Default version of the previous function with startNode = base node of document (not always the root !!)getElements
(Element startElement, String nodePath) Find elements matching the given path - ex: Sensor.locatedUsinggetElementValue
(String nodePath) Default version of the previous function that gets the value of the first element foundgetElementValue
(Element element) Default version of the getElementValue(Element, String) function.getElementValue
(Element startElement, String nodePath) Get the value of an element from its pathgetFirstChildElement
(Element parentElement) Get the first child of the parentNode.getLinkedFragment
(XMLDocument currentDocument, String idRef) Returns an xml element given its IDFind all nodes matching the given path - ex: Sensor/identificationgetParentDocument
(Node node) Retrieve XMLDocument this element is inprotected org.vast.xml.QName
getQName
(XMLDocument parentDoc, String qname) Creates a QName object from the given "prefix:name" String.Returns the root element of the documentGets the XMLDocument attached to this DOMHelperGets the XMLFragment attached to this DOMHelperboolean
Checks if the given node has the given name This does the comparison using user prefixesprotected XMLFragment
parseStream
(InputStream inputStream, boolean addToTable, Map<String, String> schemaLocations) This function parse the XML file if necessaryprotected XMLFragment
This function parse the XML file if necessary or reuse one already loaded if availableprotected boolean
readNode
(XMLDocument currentDocument, Node node, int nodeTypeFilter, int maxCount) This function is called recursively and look for the wanted nodes
The matching nodes are added to the class variable 'matchingNodes'protected void
removeAllText
(Node parent) Remove all text node children from the parentvoid
serialize
(Node node, OutputStream out, boolean pretty) Helper method to serialize this DOM to a streamvoid
setAttributeValue
(String nodePath, String text) Default version of the previous method using the base element as the parent element.void
setAttributeValue
(Element startElement, String nodePath, String val) Adds all necessary elements and the final attribute in the path and sets the text value of the attribute to the given String.void
setBaseElement
(Element elt) Sets the element to use as the base when reading/writing with default methodsvoid
setDocument
(Document domDocument) Sets the Document to wrap with this DOMHelpersetElementValue
(String nodePath, String text) Default version of the previous method using the base element as the parent element.void
setElementValue
(Element elt, String val) Default version of previous method to add value to an element directlysetElementValue
(Element parentElement, String nodePath, String val) Adds all necessary elements in the path and sets the text value of the leaf element to the given String.void
setIdAttribute
(String nodePath, String text) Special method to set an id attribute value on selected elementvoid
setNodeValue
(Node parentNode, String val) Sets the text content of a node (element or atribute)void
Adds the xsi:nil="true" attribute to the specified elementprotected boolean
writeNode
(XMLDocument currentDocument, Node node, int nodeType) Follow the path if nodes already exist or create necessary elementsvoid
writeToStreamWriter
(Element elt, XMLStreamWriter writer) Writes a DOM element using the given XMLStreamWriter
-
Field Details
-
XML_NS_URI
- See Also:
-
XSI_NS_URI
- See Also:
-
DEFAULT_PREFIX
- See Also:
-
PATH_SEPARATOR
- See Also:
-
userPrefixTable
User prefix to domain map -
loadedDocuments
Linked documents (URI object -> XMLDocument Object) -
mainFragment
Main Document object -
matchingNodes
List of nodes returned -
currentPath
Current path inside the xml file being parsed.
Paths have to be given as element names separated by /
Namespaces are separated from the node name by ':'
Example: "gml:feature/gml:polyline/epsg" -
wantedPath
-
eltQName
protected org.vast.xml.QName eltQName -
validation
protected boolean validation
-
-
Constructor Details
-
DOMHelper
public DOMHelper() -
DOMHelper
Loads an existing XML document from the given URL- Parameters:
url
-validation
-- Throws:
DOMHelperException
-
DOMHelper
Loads an existing XML document from the given InputStream- Parameters:
inputStream
-validation
-- Throws:
DOMHelperException
-
DOMHelper
public DOMHelper(InputStream inputStream, boolean validation, Map<String, String> schemaLocations) throws DOMHelperExceptionLoads an existing XML document from the given InputStream- Parameters:
inputStream
-validation
-schemaLocations
-- Throws:
DOMHelperException
-
DOMHelper
Loads an existing document from the given DOM Document object- Parameters:
domDocument
-
-
DOMHelper
Creates a DOMHelper on a new document with the given QName- Parameters:
docQName
-
-
-
Method Details
-
createDocument
Creates a new Document with the given qname The new document will replace the document loaded in this DOMHelper- Parameters:
qname
-
-
getRootElement
Returns the root element of the document- Returns:
- Root Element Node or null if root element is not found
-
getBaseElement
Returns the base element of the fragment attached to this reader- Returns:
- W3C Element
-
setBaseElement
Sets the element to use as the base when reading/writing with default methods- Parameters:
elt
-
-
getDocument
Gets the DOM Document attached to this DOMHelper- Returns:
- DOM Document owning the main fragment
-
setDocument
Sets the Document to wrap with this DOMHelper- Parameters:
domDocument
-
-
getXmlDocument
Gets the XMLDocument attached to this DOMHelper- Returns:
- XMLDocument
-
getXmlFragment
Gets the XMLFragment attached to this DOMHelper- Returns:
- XMLFragment
-
addUserPrefix
Add an entry to the table of user prefixes This table is used to map user prefixes to a real namespace uri- Parameters:
prefix
-nsDomain
-
-
addNSDeclaration
-
addSchemaLocation
Add a schema location entry (xsi:schemaLocation attribute)- Parameters:
nsUri
- namespace URI or null is this schema is associated to the default NSschemaUrl
- URL of the schema to use for validation
-
getNodes
Find all nodes matching the given path - ex: Sensor/identification- Parameters:
startNode
- DOM Node where to start the searchnodePath
- Path of the node relative to startNode (case independent, separation char = /)nodeTypeFilter
- type of node to look for (0 for any type)maxCount
- maximum number of nodes to look for- Returns:
- the NodeList of Node objects found or an Empty NodeList if no matching nodes are found
-
getElements
Find elements matching the given path - ex: Sensor.locatedUsing- Parameters:
startElement
- DOM Node where to start the searchnodePath
- Path of the element relative to startNode (case independent, separation char = /)- Returns:
- the NodeList of Element objects found or an Empty NodeList if no matching elements are found
-
getElements
Default version of the previous function with startNode = base node of document (not always the root !!)- Parameters:
nodePath
- Path of the element in the XML tree (case independent, separation char = /)- Returns:
- the NodeList of Element objects found or an Empty NodeList if no matching elements are found
-
getElement
Find the first element matching the given path- Parameters:
startElement
- DOM Node where to start the searchnodePath
- Path of the element relative to startNode (case independent, separation char = /)- Returns:
- the first Element object found or null if no matching element is found
-
getElement
Default version of the previous function with startNode = base node of document (not always the root !!)- Parameters:
nodePath
- Path of the element in the XML tree (case independent, separation char = "/")- Returns:
- the first Element object found or null if no matching nodes are found
-
getElementValue
Get the value of an element from its path- Parameters:
startElement
- DOM Node where to start the searchnodePath
- Path of the element in the XML tree (case independent, separation char = "/")- Returns:
- Node value as a String or null if no matching element is found
-
getElementValue
Default version of the previous function that gets the value of the first element found- Parameters:
nodePath
- Path of the element in the XML tree (case independent, separation char = "/")- Returns:
- Node value as a String or null if no matching element is found
-
getElementValue
Default version of the getElementValue(Element, String) function. The nodePath is here set to null and thus this method simply retrieves the text content of the given Element.- Parameters:
element
-- Returns:
- Element text value
-
getAttributeValue
Read the attribute value- Parameters:
startElement
- DOM Node where to start the searchnodePath
- Path of the element relative to startNode (case independent, separation char = "/")- Returns:
- the attribute value as a string or null if no matching attribute is found
-
getAttributeValue
Default version of the previous function with startNode = base node of document (not always the root !!)- Parameters:
nodePath
- Path of the element in the XML tree (case independent, separation char = "/")- Returns:
- the attribute value as a String or null if no matching attribute is found
-
existElement
Determine if the element corresponding to the given path exists- Parameters:
startElement
- DOM Node where to start the searchnodePath
- Path of the element in the DOM tree (case independent, separation char = "/")- Returns:
- true if the element is present
-
existElement
Default version of the previous function with startNode = base node of document (not always the root !!)- Parameters:
nodePath
- Path of the element in the XML tree (case independent, separation char = /)- Returns:
- true if the element is present
-
existAttribute
Determine if the attribute corresponding to the given path exists- Parameters:
startElement
- Node where to start the searchnodePath
- Path of the attribute in the DOM tree (case independent, separation char = /)- Returns:
- true if the attribute is present
-
existAttribute
Default version of the previous function with startNode = base node of document (not always the root !!)- Parameters:
nodePath
- Path of the attribute in the DOM tree (case independent, separation char = /)- Returns:
- true if the attribute is present
-
hasQName
Checks if the given node has the given name This does the comparison using user prefixes- Parameters:
node
-qname
-- Returns:
- true if QName matches
-
getAllElements
Look for all the elements below the startNode with the given tag name If the XML document defines namespaces, this method requires a fully qualified element name (prefix + local name), and the prefix needs to be defined by calling addUserPrefix(prefix, ns-uri)- Parameters:
startElement
- Node where to start the searchtagName
- Name of the nodes to look for- Returns:
- a NodeList containing all the matching elements or an Empty NodeList if no matching elements are found
-
getAllElements
Default version of the previous function
with startNode = base node of document (not always the root !!)- Parameters:
name
- String Name of the nodes to look for- Returns:
- NodeList containing all the matching elements or an Empty NodeList if no matching elements are found
-
getAllChildElements
Get all child elements of the given node (don't return any text or comment nodes !) This method will follow any xlink:href if specified.- Parameters:
parentElement
- Node to retrieve the children from- Returns:
- NodeList of child elements or an Empty NodeList if no children elements are found
-
getFirstChildElement
Get the first child of the parentNode. This method will follow any xlink:href if specified.- Parameters:
parentElement
- Node to retrieve the children from- Returns:
- NodeList of child elements or null if no children elements are found
-
getChildElements
Get the child elements (only elements !!) of the parentNode This method is basic... It won't follow xlink:href- Parameters:
parentNode
- Node to retrieve the children from- Returns:
- NodeList of child elements or an Empty NodeList if no children elements are found
-
addNode
Adds any type of node to the parent node- Parameters:
parentNode
-nodePath
-nodeType
-- Returns:
- the newly added Node
-
addElement
Adds an element to the document at the given location relative to the parent element. This will also add all the elements given in the path if they are not yet present or if marked with a '+' prefix.- Parameters:
parentNode
-nodePath
-- Returns:
- DOM Element after it has been created and appended to the DOM
-
addElement
Default version of the previous method using the base element as the parent element.- Parameters:
nodePath
-- Returns:
- DOM Element after it has been created and appended to the DOM
-
setNodeValue
Sets the text content of a node (element or atribute)- Parameters:
parentNode
-val
-
-
removeAllText
Remove all text node children from the parent- Parameters:
parent
-
-
setElementValue
Adds all necessary elements in the path and sets the text value of the leaf element to the given String.- Parameters:
parentElement
-nodePath
-val
-- Returns:
- the element to which the text node was appended
-
setElementValue
Default version of previous method to add value to an element directly- Parameters:
elt
-val
-
-
setElementValue
Default version of the previous method using the base element as the parent element.- Parameters:
nodePath
-text
-- Returns:
- the element to which the text node was appended
-
setAttributeValue
Adds all necessary elements and the final attribute in the path and sets the text value of the attribute to the given String.- Parameters:
startElement
-nodePath
-val
-
-
setAttributeValue
Default version of the previous method using the base element as the parent element.- Parameters:
nodePath
-text
-
-
setIdAttribute
Special method to set an id attribute value on selected element- Parameters:
nodePath
-text
-
-
setXsiNil
Adds the xsi:nil="true" attribute to the specified element- Parameters:
startElement
-nodePath
- must point to an element
-
createElement
Creates a new element using the underlying DOM Document.- Parameters:
parentDoc
-qname
- qname of the new element- Returns:
- Newly created DOM element (owned by the main document)
-
createElement
Creates a new element using the main document- Parameters:
qname
-- Returns:
- Newly created DOM element (owned by the main document)
-
writeNode
Follow the path if nodes already exist or create necessary elements- Parameters:
currentDocument
-node
-nodeType
-- Returns:
-
readNode
protected boolean readNode(XMLDocument currentDocument, Node node, int nodeTypeFilter, int maxCount) This function is called recursively and look for the wanted nodes
The matching nodes are added to the class variable 'matchingNodes'- Parameters:
currentDocument
- XML document where the search is performednode
- The node from which we start the searchnodeTypeFilter
- node type to be retrievedmaxCount
- maximum number of matching nodes to look for- Returns:
- boolean true if the search is done, false otherwise
-
serialize
Helper method to serialize this DOM to a stream- Parameters:
node
-out
-pretty
- set to true to indent output- Throws:
IOException
-
parseStream
protected XMLFragment parseStream(InputStream inputStream, boolean addToTable, Map<String, String> schemaLocations) throws DOMHelperExceptionThis function parse the XML file if necessary- Parameters:
inputStream
- Stream containing XML file we want to parse- Returns:
- The XMLDocument object created
- Throws:
DOMHelperException
IOException
-
parseURI
This function parse the XML file if necessary or reuse one already loaded if available- Parameters:
href
- link (URL#id) to the documentforceReload
- if true, force the document to reload- Returns:
- The XMLDocument object created
- Throws:
DOMHelperException
-
getLinkedFragment
public XMLFragment getLinkedFragment(XMLDocument currentDocument, String idRef) throws DOMHelperException Returns an xml element given its ID- Parameters:
currentDocument
- XMLDocument where to look for local IDsidRef
- String containing the unique id (local or global with a url)- Returns:
- Element
- Throws:
DOMHelperException
-
getParentDocument
Retrieve XMLDocument this element is in- Parameters:
node
- Node- Returns:
- XMLDocument
-
appendChild
Adds a node to the end of the children list- Parameters:
parent
- Node Parent node receiving the new childchild
- String Child node to be added- Returns:
- Newly created DOM Node after it has bee appended to the DOM tree
-
comparePaths
protected int comparePaths(XMLDocument currentDocument, ArrayList<String> actualPath, String[] wantedPath) Check if the paths are matching
Returns -1 if the beginning of wantedPath doesn't match actualPath
Otherwise returns the number levels still needed to reach the wantedPath- Parameters:
currentDocument
- XMLDocumentactualPath
- Actual path in the xml treewantedPath
- The path we are looking for- Returns:
- -1 or the number of levels still needed to reach the wanted node (positive integer)
-
getQName
Creates a QName object from the given "prefix:name" String.- Parameters:
qname
-- Returns:
-
getLastAccessedNode
-
writeToStreamWriter
Writes a DOM element using the given XMLStreamWriter- Parameters:
elt
- DOM element to writewriter
- writer object to use for serialization- Throws:
XMLStreamException
-