KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright 1999-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.5 2004/02/17 04:21:14 minchau Exp $
18  */

19 package com.sun.org.apache.xml.internal.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  * @deprecated Since the introduction of the DTM, this class will be removed.
37  * This class provides a DOM level 2 "helper", which provides services currently
38  * not provided be the DOM standard.
39  */

40 public class DOM2Helper extends DOMHelper
41 {
42
43   /**
44    * Construct an instance.
45    */

46   public DOM2Helper(){}
47
48   /**
49    * Check node to see if it was created by a DOM implementation
50    * that this helper is intended to support. This is currently
51    * disabled, and assumes all nodes are acceptable rather than checking
52    * that they implement com.sun.org.apache.xerces.internal.dom.NodeImpl.
53    *
54    * @param node The node to be tested.
55    *
56    * @throws TransformerException if the node is not one which this
57    * DOM2Helper can support. If we return without throwing the exception,
58    * the node is compatable.
59    * @xsl.usage internal
60    */

61   public void checkNode(Node JavaDoc node) throws TransformerException JavaDoc
62   {
63
64     // if(!(node instanceof com.sun.org.apache.xerces.internal.dom.NodeImpl))
65
// throw new TransformerException(XSLMessages.createXPATHMessage(XPATHErrorResources.ER_XERCES_CANNOT_HANDLE_NODES, new Object[]{((Object)node).getClass()})); //"DOM2Helper can not handle nodes of type"
66
//+((Object)node).getClass());
67
}
68
69   /**
70    * Returns true if the DOM implementation handled by this helper
71    * supports the SAX ContentHandler interface.
72    *
73    * @return true (since Xerces does).
74    */

75   public boolean supportsSAX()
76   {
77     return true;
78   }
79
80   /** Field m_doc: Document Node for the document this helper is currently
81    * accessing or building
82    * @see #setDocument
83    * @see #getDocument
84    * */

85   private Document JavaDoc m_doc;
86
87   /**
88    * Specify which document this helper is currently operating on.
89    *
90    * @param doc The DOM Document node for this document.
91    * @see #getDocument
92    */

93   public void setDocument(Document JavaDoc doc)
94   {
95     m_doc = doc;
96   }
97
98   /**
99    * Query which document this helper is currently operating on.
100    *
101    * @return The DOM Document node for this document.
102    * @see #setDocument
103    */

104   public Document JavaDoc getDocument()
105   {
106     return m_doc;
107   }
108
109   /**
110    * Parse an XML document.
111    *
112    * <p>Right now the Xerces DOMParser class is used. This needs
113    * fixing, either via jaxp, or via some other, standard method.</p>
114    *
115    * <p>The application can use this method to instruct the SAX parser
116    * to begin parsing an XML document from any valid input
117    * source (a character stream, a byte stream, or a URI).</p>
118    *
119    * <p>Applications may not invoke this method while a parse is in
120    * progress (they should create a new Parser instead for each
121    * additional XML document). Once a parse is complete, an
122    * application may reuse the same Parser object, possibly with a
123    * different input source.</p>
124    *
125    * @param source The input source for the top-level of the
126    * XML document.
127    *
128    * @throws TransformerException if any checked exception is thrown.
129    * @xsl.usage internal
130    */

131   public void parse(InputSource JavaDoc source) throws TransformerException JavaDoc
132   {
133
134     try
135     {
136
137       // I guess I should use JAXP factory here... when it's legal.
138
// com.sun.org.apache.xerces.internal.parsers.DOMParser parser
139
// = new com.sun.org.apache.xerces.internal.parsers.DOMParser();
140
DocumentBuilderFactory JavaDoc builderFactory =
141         DocumentBuilderFactory.newInstance();
142
143       builderFactory.setNamespaceAware(true);
144       builderFactory.setValidating(true);
145
146       DocumentBuilder JavaDoc parser = builderFactory.newDocumentBuilder();
147
148       /*
149       // domParser.setFeature("http://apache.org/xml/features/dom/create-entity-ref-nodes", getShouldExpandEntityRefs()? false : true);
150       if(m_useDOM2getNamespaceURI)
151       {
152       parser.setFeature("http://apache.org/xml/features/dom/defer-node-expansion", true);
153       parser.setFeature("http://xml.org/sax/features/namespaces", true);
154       }
155       else
156       {
157       parser.setFeature("http://apache.org/xml/features/dom/defer-node-expansion", false);
158       }
159
160       parser.setFeature("http://apache.org/xml/features/allow-java-encodings", true);
161       */

162
163       parser.setErrorHandler(
164         new com.sun.org.apache.xml.internal.utils.DefaultErrorHandler());
165
166       // if(null != m_entityResolver)
167
// {
168
// System.out.println("Setting the entity resolver.");
169
// parser.setEntityResolver(m_entityResolver);
170
// }
171
setDocument(parser.parse(source));
172     }
173     catch (org.xml.sax.SAXException JavaDoc se)
174     {
175       throw new TransformerException JavaDoc(se);
176     }
177     catch (ParserConfigurationException JavaDoc pce)
178     {
179       throw new TransformerException JavaDoc(pce);
180     }
181     catch (IOException JavaDoc ioe)
182     {
183       throw new TransformerException JavaDoc(ioe);
184     }
185
186     // setDocument(((com.sun.org.apache.xerces.internal.parsers.DOMParser)parser).getDocument());
187
}
188
189   /**
190    * Given an XML ID, return the element. This requires assistance from the
191    * DOM and parser, and is meaningful only in the context of a DTD
192    * or schema which declares attributes as being of type ID. This
193    * information may or may not be available in all parsers, may or
194    * may not be available for specific documents, and may or may not
195    * be available when validation is not turned on.
196    *
197    * @param id The ID to search for, as a String.
198    * @param doc The document to search within, as a DOM Document node.
199    * @return DOM Element node with an attribute of type ID whose value
200    * uniquely matches the requested id string, or null if there isn't
201    * such an element or if the DOM can't answer the question for other
202    * reasons.
203    */

204   public Element JavaDoc getElementByID(String JavaDoc id, Document JavaDoc doc)
205   {
206     return doc.getElementById(id);
207   }
208
209   /**
210    * Figure out whether node2 should be considered as being later
211    * in the document than node1, in Document Order as defined
212    * by the XPath model. This may not agree with the ordering defined
213    * by other XML applications.
214    * <p>
215    * There are some cases where ordering isn't defined, and neither are
216    * the results of this function -- though we'll generally return true.
217    * <p>
218    * TODO: Make sure this does the right thing with attribute nodes!!!
219    *
220    * @param node1 DOM Node to perform position comparison on.
221    * @param node2 DOM Node to perform position comparison on .
222    *
223    * @return false if node2 comes before node1, otherwise return true.
224    * You can think of this as
225    * <code>(node1.documentOrderPosition &lt;= node2.documentOrderPosition)</code>.
226    */

227   public static boolean isNodeAfter(Node JavaDoc node1, Node JavaDoc node2)
228   {
229
230     // Assume first that the nodes are DTM nodes, since discovering node
231
// order is massivly faster for the DTM.
232
if(node1 instanceof DOMOrder && node2 instanceof DOMOrder)
233     {
234       int index1 = ((DOMOrder) node1).getUid();
235       int index2 = ((DOMOrder) node2).getUid();
236
237       return index1 <= index2;
238     }
239     else
240     {
241
242       // isNodeAfter will return true if node is after countedNode
243
// in document order. The base isNodeAfter is sloooow (relatively).
244
return DOMHelper.isNodeAfter(node1, node2);
245     }
246   }
247
248   /**
249    * Get the XPath-model parent of a node. This version takes advantage
250    * of the DOM Level 2 Attr.ownerElement() method; the base version we
251    * would otherwise inherit is prepared to fall back on exhaustively
252    * walking the document to find an Attr's parent.
253    *
254    * @param node Node to be examined
255    *
256    * @return the DOM parent of the input node, if there is one, or the
257    * ownerElement if the input node is an Attr, or null if the node is
258    * a Document, a DocumentFragment, or an orphan.
259    */

260   public static Node JavaDoc getParentOfNode(Node JavaDoc node)
261   {
262           Node JavaDoc parent=node.getParentNode();
263           if(parent==null && (Node.ATTRIBUTE_NODE == node.getNodeType()) )
264            parent=((Attr JavaDoc) node).getOwnerElement();
265           return parent;
266   }
267
268   /**
269    * Returns the local name of the given node, as defined by the
270    * XML Namespaces specification. This is prepared to handle documents
271    * built using DOM Level 1 methods by falling back upon explicitly
272    * parsing the node name.
273    *
274    * @param n Node to be examined
275    *
276    * @return String containing the local name, or null if the node
277    * was not assigned a Namespace.
278    */

279   public String JavaDoc getLocalNameOfNode(Node JavaDoc n)
280   {
281
282     String JavaDoc name = n.getLocalName();
283
284     return (null == name) ? super.getLocalNameOfNode(n) : name;
285   }
286
287   /**
288    * Returns the Namespace Name (Namespace URI) for the given node.
289    * In a Level 2 DOM, you can ask the node itself. Note, however, that
290    * doing so conflicts with our decision in getLocalNameOfNode not
291    * to trust the that the DOM was indeed created using the Level 2
292    * methods. If Level 1 methods were used, these two functions will
293    * disagree with each other.
294    * <p>
295    * TODO: Reconcile with getLocalNameOfNode.
296    *
297    * @param n Node to be examined
298    *
299    * @return String containing the Namespace URI bound to this DOM node
300    * at the time the Node was created.
301    */

302   public String JavaDoc getNamespaceOfNode(Node JavaDoc n)
303   {
304     return n.getNamespaceURI();
305   }
306
307   /** Field m_useDOM2getNamespaceURI is a compile-time flag which
308    * gates some of the parser options used to build a DOM -- but
309    * that code is commented out at this time and nobody else
310    * references it, so I've commented this out as well. */

311   //private boolean m_useDOM2getNamespaceURI = false;
312
}
313
Popular Tags