001: /*
002: * Copyright 2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: /*
017: * $Id: DOM2Helper.java,v 1.1 2004/10/14 18:30:53 minchau Exp $
018: */
019: package org.apache.xml.serializer.utils;
020:
021: import java.io.IOException;
022:
023: import javax.xml.parsers.DocumentBuilder;
024: import javax.xml.parsers.DocumentBuilderFactory;
025: import javax.xml.parsers.ParserConfigurationException;
026: import javax.xml.transform.TransformerException;
027:
028: import org.w3c.dom.Attr;
029: import org.w3c.dom.Document;
030: import org.w3c.dom.Element;
031: import org.w3c.dom.Node;
032:
033: import org.xml.sax.InputSource;
034:
035: /**
036: * This class provides a DOM level 2 "helper", which provides services currently
037: * not provided be the DOM standard.
038: *
039: * This class is a copy of the one in org.apache.xml.utils.
040: * It exists to cut the serializers dependancy on that package.
041: *
042: * The differences from the original class are:
043: * it doesn't extend DOMHelper, not depricated,
044: * dropped method isNodeAfter(Node node1, Node node2)
045: * dropped method parse(InputSource)
046: * dropped method supportSAX()
047: * dropped method setDocument(doc)
048: * dropped method checkNode(Node)
049: * dropped method getDocument()
050: * dropped method getElementByID(String id, Document doc)
051: * dropped method getParentOfNode(Node node)
052: * dropped field Document m_doc;
053: * made class non-public
054: *
055: * This class is not a public API, it is only public because it is
056: * used in org.apache.xml.serializer.
057: *
058: * @xsl.usage internal
059: */
060: public final class DOM2Helper {
061:
062: /**
063: * Construct an instance.
064: */
065: public DOM2Helper() {
066: }
067:
068: /**
069: * Returns the local name of the given node, as defined by the
070: * XML Namespaces specification. This is prepared to handle documents
071: * built using DOM Level 1 methods by falling back upon explicitly
072: * parsing the node name.
073: *
074: * @param n Node to be examined
075: *
076: * @return String containing the local name, or null if the node
077: * was not assigned a Namespace.
078: */
079: public String getLocalNameOfNode(Node n) {
080:
081: String name = n.getLocalName();
082:
083: return (null == name) ? getLocalNameOfNodeFallback(n) : name;
084: }
085:
086: /**
087: * Returns the local name of the given node. If the node's name begins
088: * with a namespace prefix, this is the part after the colon; otherwise
089: * it's the full node name.
090: *
091: * This method is copied from org.apache.xml.utils.DOMHelper
092: *
093: * @param n the node to be examined.
094: *
095: * @return String containing the Local Name
096: */
097: private String getLocalNameOfNodeFallback(Node n) {
098:
099: String qname = n.getNodeName();
100: int index = qname.indexOf(':');
101:
102: return (index < 0) ? qname : qname.substring(index + 1);
103: }
104:
105: /**
106: * Returns the Namespace Name (Namespace URI) for the given node.
107: * In a Level 2 DOM, you can ask the node itself. Note, however, that
108: * doing so conflicts with our decision in getLocalNameOfNode not
109: * to trust the that the DOM was indeed created using the Level 2
110: * methods. If Level 1 methods were used, these two functions will
111: * disagree with each other.
112: * <p>
113: * TODO: Reconcile with getLocalNameOfNode.
114: *
115: * @param n Node to be examined
116: *
117: * @return String containing the Namespace URI bound to this DOM node
118: * at the time the Node was created.
119: */
120: public String getNamespaceOfNode(Node n) {
121: return n.getNamespaceURI();
122: }
123:
124: /** Field m_useDOM2getNamespaceURI is a compile-time flag which
125: * gates some of the parser options used to build a DOM -- but
126: * that code is commented out at this time and nobody else
127: * references it, so I've commented this out as well. */
128: //private boolean m_useDOM2getNamespaceURI = false;
129: }
|