public class DOMHelper extends Object
TODO DOMHelper description TODO try to avoid calls to xerces specific classes
Modifier and Type | Field and Description |
---|---|
protected ArrayList<String> |
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" |
static String |
DEFAULT_PREFIX |
protected org.vast.xml.QName |
eltQName |
protected Map<String,XMLDocument> |
loadedDocuments
Linked documents (URI object -> XMLDocument Object)
|
protected XMLFragment |
mainFragment
Main Document object
|
protected XMLNodeList |
matchingNodes
List of nodes returned
|
protected static String |
PATH_SEPARATOR |
protected Map<String,String> |
userPrefixTable
User prefix to domain map
|
protected boolean |
validation |
protected String[] |
wantedPath |
static String |
XML_NS_URI |
static String |
XSI_NS_URI |
Constructor and Description |
---|
DOMHelper() |
DOMHelper(Document domDocument)
Loads an existing document from the given DOM Document object
|
DOMHelper(InputStream inputStream,
boolean validation)
Loads an existing XML document from the given InputStream
|
DOMHelper(InputStream inputStream,
boolean validation,
Map<String,String> schemaLocations)
Loads an existing XML document from the given InputStream
|
DOMHelper(String docQName)
Creates a DOMHelper on a new document with the given QName
|
DOMHelper(String url,
boolean validation)
Loads an existing XML document from the given URL
|
Modifier and Type | Method and Description |
---|---|
Element |
addElement(Node parentNode,
String nodePath)
Adds an element to the document at the given location relative
to the parent element.
|
Element |
addElement(String nodePath)
Default version of the previous method using the base element
as the parent element.
|
Node |
addNode(Node parentNode,
String nodePath,
int nodeType)
Adds any type of node to the parent node
|
void |
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 uri
|
Node |
appendChild(Node parent,
String child)
Adds a node to the end of the children list
|
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 |
void |
createDocument(String qname)
Creates a new Document with the given qname
The new document will replace the document loaded in this DOMHelper
|
Element |
createElement(String qname)
Creates a new element using the main document
|
Element |
createElement(XMLDocument parentDoc,
String qname)
Creates a new element using the underlying DOM Document.
|
boolean |
existAttribute(Element startElement,
String nodePath)
Determine if the attribute corresponding to the given path exists
|
boolean |
existAttribute(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 exists
|
boolean |
existElement(String nodePath)
Default version of the previous function with startNode = base node of document (not always the root !!)
|
NodeList |
getAllChildElements(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.
|
NodeList |
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)
|
NodeList |
getAllElements(String name)
Default version of the previous function
with startNode = base node of document (not always the root !!) |
String |
getAttributeValue(Element startElement,
String nodePath)
Read the attribute value
|
String |
getAttributeValue(String nodePath)
Default version of the previous function with startNode = base node of document (not always the root !!)
|
Element |
getBaseElement()
Returns the base element of the fragment attached to this reader
|
NodeList |
getChildElements(Node parentNode)
Get the child elements (only elements !!) of the parentNode
This method is basic...
|
Document |
getDocument()
Gets the DOM Document attached to this DOMHelper
|
Element |
getElement(Element startElement,
String nodePath)
Find the first element matching the given path
|
Element |
getElement(String nodePath)
Default version of the previous function with startNode = base node of document (not always the root !!)
|
NodeList |
getElements(Element startElement,
String nodePath)
Find elements matching the given path - ex: Sensor.locatedUsing
|
NodeList |
getElements(String nodePath)
Default version of the previous function with startNode = base node of
document (not always the root !!)
|
String |
getElementValue(Element element)
Default version of the getElementValue(Element, String) function.
|
String |
getElementValue(Element startElement,
String nodePath)
Get the value of an element from its path
|
String |
getElementValue(String nodePath)
Default version of the previous function that gets the value of the first element found
|
Element |
getFirstChildElement(Element parentElement)
Get the first child of the parentNode.
|
Node |
getLastAccessedNode() |
XMLFragment |
getLinkedFragment(XMLDocument currentDocument,
String idRef)
Returns an xml element given its ID
|
NodeList |
getNodes(Node startNode,
String nodePath,
int nodeTypeFilter,
int maxCount)
Find all nodes matching the given path - ex: Sensor/identification
|
XMLDocument |
getParentDocument(Node node)
Retrieve XMLDocument this element is in
|
protected org.vast.xml.QName |
getQName(XMLDocument parentDoc,
String qname)
Creates a QName object from the given "prefix:name" String.
|
Element |
getRootElement()
Returns the root element of the document
|
XMLDocument |
getXmlDocument()
Gets the XMLDocument attached to this DOMHelper
|
XMLFragment |
getXmlFragment()
Gets the XMLFragment attached to this DOMHelper
|
boolean |
hasQName(Node node,
String qname)
Checks if the given node has the given name
This does the comparison using user prefixes
|
protected XMLFragment |
parseStream(InputStream inputStream,
boolean addToTable,
Map<String,String> schemaLocations)
This function parse the XML file if necessary
|
protected XMLFragment |
parseURI(String href,
boolean forceReload)
This function parse the XML file if necessary or reuse one already loaded if available
|
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' |
protected void |
removeAllText(Node parent)
Remove all text node children from the parent
|
void |
serialize(Node node,
OutputStream out,
boolean pretty)
Helper method to serialize this DOM to a stream
|
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 |
setAttributeValue(String nodePath,
String text)
Default version of the previous method using the base element
as the parent element.
|
void |
setBaseElement(Element elt)
Sets the element to use as the base when reading/writing
with default methods
|
void |
setDocument(Document domDocument)
Sets the Document to wrap with this DOMHelper
|
void |
setElementValue(Element elt,
String val)
Default version of previous method to add value to an element directly
|
Element |
setElementValue(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.
|
Element |
setElementValue(String nodePath,
String text)
Default version of the previous method using the base element
as the parent element.
|
void |
setIdAttribute(String nodePath,
String text)
Special method to set an id attribute value on selected element
|
void |
setNodeValue(Node parentNode,
String val)
Sets the text content of a node (element or atribute)
|
void |
setXsiNil(Element startElement,
String nodePath)
Adds the xsi:nil="true" attribute to the specified element
|
protected boolean |
writeNode(XMLDocument currentDocument,
Node node,
int nodeType)
Follow the path if nodes already exist or create necessary elements
|
void |
writeToStreamWriter(Element elt,
XMLStreamWriter writer)
Writes a DOM element using the given XMLStreamWriter
|
public static final String XML_NS_URI
public static final String XSI_NS_URI
protected static final String PATH_SEPARATOR
protected Map<String,XMLDocument> loadedDocuments
protected XMLFragment mainFragment
protected XMLNodeList matchingNodes
protected ArrayList<String> currentPath
protected String[] wantedPath
protected org.vast.xml.QName eltQName
protected boolean validation
public static final String DEFAULT_PREFIX
public DOMHelper()
public DOMHelper(String url, boolean validation) throws DOMHelperException
url
- validation
- DOMHelperException
public DOMHelper(InputStream inputStream, boolean validation) throws DOMHelperException
inputStream
- validation
- DOMHelperException
public DOMHelper(InputStream inputStream, boolean validation, Map<String,String> schemaLocations) throws DOMHelperException
inputStream
- validation
- schemaLocations
- DOMHelperException
public DOMHelper(Document domDocument)
domDocument
- public DOMHelper(String docQName)
docQName
- public void createDocument(String qname)
qname
- public Element getRootElement()
public Element getBaseElement()
public void setBaseElement(Element elt)
elt
- public Document getDocument()
public void setDocument(Document domDocument)
domDocument
- public XMLDocument getXmlDocument()
public XMLFragment getXmlFragment()
public void addUserPrefix(String prefix, String nsDomain)
prefix
- nsDomain
- public void addSchemaLocation(String nsUri, String schemaUrl)
nsUri
- namespace URI or null is this schema is associated to the default NSschemaUrl
- URL of the schema to use for validationpublic NodeList getNodes(Node startNode, String nodePath, int nodeTypeFilter, int maxCount)
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 forpublic NodeList getElements(Element startElement, String nodePath)
startElement
- DOM Node where to start the searchnodePath
- Path of the element relative to startNode (case independent, separation char = /)public NodeList getElements(String nodePath)
nodePath
- Path of the element in the XML tree (case independent, separation char = /)public Element getElement(Element startElement, String nodePath)
startElement
- DOM Node where to start the searchnodePath
- Path of the element relative to startNode (case independent, separation char = /)public Element getElement(String nodePath)
nodePath
- Path of the element in the XML tree (case independent, separation char = "/")public String getElementValue(Element startElement, String nodePath)
startElement
- DOM Node where to start the searchnodePath
- Path of the element in the XML tree (case independent, separation char = "/")public String getElementValue(String nodePath)
nodePath
- Path of the element in the XML tree (case independent, separation char = "/")public String getElementValue(Element element)
element
- public String getAttributeValue(Element startElement, String nodePath)
startElement
- DOM Node where to start the searchnodePath
- Path of the element relative to startNode (case independent, separation char = "/")public String getAttributeValue(String nodePath)
nodePath
- Path of the element in the XML tree (case independent, separation char = "/")public boolean existElement(Element startElement, String nodePath)
startElement
- DOM Node where to start the searchnodePath
- Path of the element in the DOM tree (case independent, separation char = "/")public boolean existElement(String nodePath)
nodePath
- Path of the element in the XML tree (case independent, separation char = /)public boolean existAttribute(Element startElement, String nodePath)
startElement
- Node where to start the searchnodePath
- Path of the attribute in the DOM tree (case independent, separation char = /)public boolean existAttribute(String nodePath)
nodePath
- Path of the attribute in the DOM tree (case independent, separation char = /)public boolean hasQName(Node node, String qname)
node
- qname
- public NodeList getAllElements(Element startElement, String tagName)
startElement
- Node where to start the searchtagName
- Name of the nodes to look forpublic NodeList getAllElements(String name)
name
- String Name of the nodes to look forpublic NodeList getAllChildElements(Element parentElement)
parentElement
- Node to retrieve the children frompublic Element getFirstChildElement(Element parentElement)
parentElement
- Node to retrieve the children frompublic NodeList getChildElements(Node parentNode)
parentNode
- Node to retrieve the children frompublic Node addNode(Node parentNode, String nodePath, int nodeType)
parentNode
- nodePath
- nodeType
- public Element addElement(Node parentNode, String nodePath)
parentNode
- nodePath
- public Element addElement(String nodePath)
nodePath
- public void setNodeValue(Node parentNode, String val)
parentNode
- val
- protected void removeAllText(Node parent)
parent
- public Element setElementValue(Element parentElement, String nodePath, String val)
parentElement
- nodePath
- val
- public void setElementValue(Element elt, String val)
elt
- val
- public Element setElementValue(String nodePath, String text)
nodePath
- text
- public void setAttributeValue(Element startElement, String nodePath, String val)
startElement
- nodePath
- val
- public void setAttributeValue(String nodePath, String text)
nodePath
- text
- public void setIdAttribute(String nodePath, String text)
nodePath
- text
- public void setXsiNil(Element startElement, String nodePath)
startElement
- nodePath
- must point to an elementpublic Element createElement(XMLDocument parentDoc, String qname)
parentDoc
- qname
- qname of the new elementpublic Element createElement(String qname)
qname
- protected boolean writeNode(XMLDocument currentDocument, Node node, int nodeType)
currentDocument
- node
- nodeType
- protected boolean readNode(XMLDocument currentDocument, Node node, int nodeTypeFilter, int maxCount)
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 forpublic void serialize(Node node, OutputStream out, boolean pretty) throws IOException
node
- out
- pretty
- set to true to indent outputIOException
protected XMLFragment parseStream(InputStream inputStream, boolean addToTable, Map<String,String> schemaLocations) throws DOMHelperException, IOException
inputStream
- Stream containing XML file we want to parseDOMHelperException
IOException
protected XMLFragment parseURI(String href, boolean forceReload) throws DOMHelperException
href
- link (URL#id) to the documentforceReload
- if true, force the document to reloadDOMHelperException
public XMLFragment getLinkedFragment(XMLDocument currentDocument, String idRef) throws DOMHelperException
currentDocument
- XMLDocument where to look for local IDsidRef
- String containing the unique id (local or global with a url)DOMHelperException
public XMLDocument getParentDocument(Node node)
node
- Nodepublic Node appendChild(Node parent, String child)
parent
- Node Parent node receiving the new childchild
- String Child node to be addedprotected int comparePaths(XMLDocument currentDocument, ArrayList<String> actualPath, String[] wantedPath)
currentDocument
- XMLDocumentactualPath
- Actual path in the xml treewantedPath
- The path we are looking forprotected org.vast.xml.QName getQName(XMLDocument parentDoc, String qname)
qname
- public Node getLastAccessedNode()
public void writeToStreamWriter(Element elt, XMLStreamWriter writer) throws XMLStreamException
elt
- DOM element to writewriter
- writer object to use for serializationXMLStreamException
Copyright © 2016 Sensia Software LLC. All rights reserved.