KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > xml > internal > serializer > utils > DOM2Helper


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 /*
17  * $Id: DOM2Helper.java,v 1.1.4.1 2005/09/08 11:03:09 suresh_emailid Exp $
18  */

19 package com.sun.org.apache.xml.internal.serializer.utils;
20
21 import java.io.IOException JavaDoc;
22
23 import javax.xml.parsers.DocumentBuilder JavaDoc;
24 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
25 import javax.xml.parsers.ParserConfigurationException JavaDoc;
26 import javax.xml.transform.TransformerException JavaDoc;
27
28 import org.w3c.dom.Attr JavaDoc;
29 import org.w3c.dom.Document JavaDoc;
30 import org.w3c.dom.Element JavaDoc;
31 import org.w3c.dom.Node JavaDoc;
32
33 import org.xml.sax.InputSource JavaDoc;
34
35 /**
36  * This class provides a DOM level 2 "helper", which provides services currently
37  * not provided be the DOM standard.
38  *
39  * This class is a copy of the one in com.sun.org.apache.xml.internal.utils.
40  * It exists to cut the serializers dependancy on that package.
41  *
42  * The differences from the original class are:
43  * it doesn't extend DOMHelper, not depricated,
44  * dropped method isNodeAfter(Node node1, Node node2)
45  * dropped method parse(InputSource)
46  * dropped method supportSAX()
47  * dropped method setDocument(doc)
48  * dropped method checkNode(Node)
49  * dropped method getDocument()
50  * dropped method getElementByID(String id, Document doc)
51  * dropped method getParentOfNode(Node node)
52  * dropped field Document m_doc;
53  * made class non-public
54  *
55  * This class is not a public API, it is only public because it is
56  * used in com.sun.org.apache.xml.internal.serializer.
57  *
58  * @xsl.usage internal
59  */

60 public final class DOM2Helper
61 {
62
63   /**
64    * Construct an instance.
65    */

66   public DOM2Helper(){}
67
68   /**
69    * Returns the local name of the given node, as defined by the
70    * XML Namespaces specification. This is prepared to handle documents
71    * built using DOM Level 1 methods by falling back upon explicitly
72    * parsing the node name.
73    *
74    * @param n Node to be examined
75    *
76    * @return String containing the local name, or null if the node
77    * was not assigned a Namespace.
78    */

79   public String JavaDoc getLocalNameOfNode(Node JavaDoc n)
80   {
81
82     String JavaDoc name = n.getLocalName();
83
84     return (null == name) ? getLocalNameOfNodeFallback(n) : name;
85   }
86   
87   /**
88    * Returns the local name of the given node. If the node's name begins
89    * with a namespace prefix, this is the part after the colon; otherwise
90    * it's the full node name.
91    *
92    * This method is copied from com.sun.org.apache.xml.internal.utils.DOMHelper
93    *
94    * @param n the node to be examined.
95    *
96    * @return String containing the Local Name
97    */

98   private String JavaDoc getLocalNameOfNodeFallback(Node JavaDoc n)
99   {
100
101     String JavaDoc qname = n.getNodeName();
102     int index = qname.indexOf(':');
103
104     return (index < 0) ? qname : qname.substring(index + 1);
105   }
106
107   /**
108    * Returns the Namespace Name (Namespace URI) for the given node.
109    * In a Level 2 DOM, you can ask the node itself. Note, however, that
110    * doing so conflicts with our decision in getLocalNameOfNode not
111    * to trust the that the DOM was indeed created using the Level 2
112    * methods. If Level 1 methods were used, these two functions will
113    * disagree with each other.
114    * <p>
115    * TODO: Reconcile with getLocalNameOfNode.
116    *
117    * @param n Node to be examined
118    *
119    * @return String containing the Namespace URI bound to this DOM node
120    * at the time the Node was created.
121    */

122   public String JavaDoc getNamespaceOfNode(Node JavaDoc n)
123   {
124     return n.getNamespaceURI();
125   }
126
127   /** Field m_useDOM2getNamespaceURI is a compile-time flag which
128    * gates some of the parser options used to build a DOM -- but
129    * that code is commented out at this time and nobody else
130    * references it, so I've commented this out as well. */

131   //private boolean m_useDOM2getNamespaceURI = false;
132
}
133
Popular Tags