DOM2Helperpublic class DOM2Helper extends DOMHelper
Fields Summary |
---|
private Document | m_docField m_doc: Document Node for the document this helper is currently
accessing or building |
Constructors Summary |
---|
public DOM2Helper()Construct an instance.
|
Methods Summary |
---|
public void | checkNode(org.w3c.dom.Node node)Check node to see if it was created by a DOM implementation
that this helper is intended to support. This is currently
disabled, and assumes all nodes are acceptable rather than checking
that they implement com.sun.org.apache.xerces.internal.dom.NodeImpl.
// if(!(node instanceof com.sun.org.apache.xerces.internal.dom.NodeImpl))
// throw new TransformerException(XSLMessages.createXPATHMessage(XPATHErrorResources.ER_XERCES_CANNOT_HANDLE_NODES, new Object[]{((Object)node).getClass()})); //"DOM2Helper can not handle nodes of type"
//+((Object)node).getClass());
| public org.w3c.dom.Document | getDocument()Query which document this helper is currently operating on.
return m_doc;
| public org.w3c.dom.Element | getElementByID(java.lang.String id, org.w3c.dom.Document doc)Given an XML ID, return the element. This requires assistance from the
DOM and parser, and is meaningful only in the context of a DTD
or schema which declares attributes as being of type ID. This
information may or may not be available in all parsers, may or
may not be available for specific documents, and may or may not
be available when validation is not turned on.
return doc.getElementById(id);
| public java.lang.String | getLocalNameOfNode(org.w3c.dom.Node n)Returns the local name of the given node, as defined by the
XML Namespaces specification. This is prepared to handle documents
built using DOM Level 1 methods by falling back upon explicitly
parsing the node name.
String name = n.getLocalName();
return (null == name) ? super.getLocalNameOfNode(n) : name;
| public java.lang.String | getNamespaceOfNode(org.w3c.dom.Node n)Returns the Namespace Name (Namespace URI) for the given node.
In a Level 2 DOM, you can ask the node itself. Note, however, that
doing so conflicts with our decision in getLocalNameOfNode not
to trust the that the DOM was indeed created using the Level 2
methods. If Level 1 methods were used, these two functions will
disagree with each other.
TODO: Reconcile with getLocalNameOfNode.
return n.getNamespaceURI();
| public static org.w3c.dom.Node | getParentOfNode(org.w3c.dom.Node node)Get the XPath-model parent of a node. This version takes advantage
of the DOM Level 2 Attr.ownerElement() method; the base version we
would otherwise inherit is prepared to fall back on exhaustively
walking the document to find an Attr's parent.
Node parent=node.getParentNode();
if(parent==null && (Node.ATTRIBUTE_NODE == node.getNodeType()) )
parent=((Attr) node).getOwnerElement();
return parent;
| public static boolean | isNodeAfter(org.w3c.dom.Node node1, org.w3c.dom.Node node2)Figure out whether node2 should be considered as being later
in the document than node1, in Document Order as defined
by the XPath model. This may not agree with the ordering defined
by other XML applications.
There are some cases where ordering isn't defined, and neither are
the results of this function -- though we'll generally return true.
TODO: Make sure this does the right thing with attribute nodes!!!
// Assume first that the nodes are DTM nodes, since discovering node
// order is massivly faster for the DTM.
if(node1 instanceof DOMOrder && node2 instanceof DOMOrder)
{
int index1 = ((DOMOrder) node1).getUid();
int index2 = ((DOMOrder) node2).getUid();
return index1 <= index2;
}
else
{
// isNodeAfter will return true if node is after countedNode
// in document order. The base isNodeAfter is sloooow (relatively).
return DOMHelper.isNodeAfter(node1, node2);
}
| public void | parse(org.xml.sax.InputSource source)Parse an XML document.
Right now the Xerces DOMParser class is used. This needs
fixing, either via jaxp, or via some other, standard method.
The application can use this method to instruct the SAX parser
to begin parsing an XML document from any valid input
source (a character stream, a byte stream, or a URI).
Applications may not invoke this method while a parse is in
progress (they should create a new Parser instead for each
additional XML document). Once a parse is complete, an
application may reuse the same Parser object, possibly with a
different input source.
try
{
// I guess I should use JAXP factory here... when it's legal.
// com.sun.org.apache.xerces.internal.parsers.DOMParser parser
// = new com.sun.org.apache.xerces.internal.parsers.DOMParser();
DocumentBuilderFactory builderFactory =
DocumentBuilderFactory.newInstance();
builderFactory.setNamespaceAware(true);
builderFactory.setValidating(true);
DocumentBuilder parser = builderFactory.newDocumentBuilder();
/*
// domParser.setFeature("http://apache.org/xml/features/dom/create-entity-ref-nodes", getShouldExpandEntityRefs()? false : true);
if(m_useDOM2getNamespaceURI)
{
parser.setFeature("http://apache.org/xml/features/dom/defer-node-expansion", true);
parser.setFeature("http://xml.org/sax/features/namespaces", true);
}
else
{
parser.setFeature("http://apache.org/xml/features/dom/defer-node-expansion", false);
}
parser.setFeature("http://apache.org/xml/features/allow-java-encodings", true);
*/
parser.setErrorHandler(
new com.sun.org.apache.xml.internal.utils.DefaultErrorHandler());
// if(null != m_entityResolver)
// {
// System.out.println("Setting the entity resolver.");
// parser.setEntityResolver(m_entityResolver);
// }
setDocument(parser.parse(source));
}
catch (org.xml.sax.SAXException se)
{
throw new TransformerException(se);
}
catch (ParserConfigurationException pce)
{
throw new TransformerException(pce);
}
catch (IOException ioe)
{
throw new TransformerException(ioe);
}
// setDocument(((com.sun.org.apache.xerces.internal.parsers.DOMParser)parser).getDocument());
| public void | setDocument(org.w3c.dom.Document doc)Specify which document this helper is currently operating on.
m_doc = doc;
| public boolean | supportsSAX()Returns true if the DOM implementation handled by this helper
supports the SAX ContentHandler interface.
return true;
|
|