KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > xml > dom > DOMOps


1 /*
2  * Enhydra Java Application Server Project
3  *
4  * The contents of this file are subject to the Enhydra Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License on
7  * the Enhydra web site ( http://www.enhydra.org/ ).
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
11  * the License for the specific terms governing rights and limitations
12  * under the License.
13  *
14  * The Initial Developer of the Enhydra Application Server is Lutris
15  * Technologies, Inc. The Enhydra Application Server and portions created
16  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
17  * All Rights Reserved.
18  *
19  * Contributor(s):
20  *
21  * $Id: DOMOps.java,v 1.3 2005/01/26 08:29:24 jkjome Exp $
22  */

23
24 package org.enhydra.xml.dom;
25
26 import java.lang.reflect.InvocationTargetException JavaDoc;
27 import java.lang.reflect.Method JavaDoc;
28
29 import org.enhydra.xml.lazydom.LazyDocument;
30 import org.enhydra.xml.xmlc.XMLObject;
31 import org.w3c.dom.DOMException JavaDoc;
32 import org.w3c.dom.Document JavaDoc;
33 import org.w3c.dom.Entity JavaDoc;
34 import org.w3c.dom.Node JavaDoc;
35 import org.w3c.dom.html.HTMLElement;
36
37 /**
38  * Various static methods the do simple operations on the DOM.
39  */

40 public final class DOMOps {
41     
42     /** Type constant for {@link #callByReflection(Object,String,String) */
43     private static final Class JavaDoc[] ARGTYPES_STRING = { String JavaDoc.class };
44
45     /** Type constant for {@link #callByReflection(Object,String,boolean) */
46     private static final Class JavaDoc[] ARGTYPES_PRIM_BOOLEAN = { Boolean.TYPE };
47
48     /** Type constant for {@link #adoptNode */
49     private static final Class JavaDoc[] ARGTYPES_NODE = { Node JavaDoc.class };
50
51     /** Type constant for {@link #getContentDocument */
52     private static final Class JavaDoc[] ARGTYPES_HTMLELEMENT = { HTMLElement.class };
53
54     /**
55      * Prevent instantiation.
56      */

57     private DOMOps() {
58     }
59
60     /**
61      * Get the owner document object for a node. This gets the actual
62      * document. If a XMLC document class instance the contained document is
63      * returned.
64      */

65     public static Document getDocument(Node JavaDoc node) {
66         if (node instanceof XMLObject) {
67             // Get real document from XMLC.
68
return ((XMLObject)node).getDocument();
69         } else if (node instanceof Document) {
70             return (Document)node;
71         } else {
72             return node.getOwnerDocument();
73         }
74     }
75
76     /**
77      * Get the real DOM node, bypassing an XMLC-generated container. If a XMLC
78      * document class instance the contained document is returned. Otherwise
79      * the node is returned.
80      */

81     public static Node JavaDoc getActualNode(Node JavaDoc node) {
82         if (node instanceof XMLObject) {
83             return ((XMLObject)node).getDocument();
84         } else {
85             return node;
86         }
87     }
88
89     /**
90      * Replace a node with one imported from another document.
91      * @param srcNode The node to clone and insert
92      * @param destNode The node to be replaced
93      * @return The new node that replaces the destination node.
94      */

95     public static Node JavaDoc replaceNode(Node JavaDoc srcNode, Node JavaDoc destNode) {
96         Node JavaDoc node = destNode.getOwnerDocument().importNode(srcNode, true);
97         Node JavaDoc parent = destNode.getParentNode();
98         if (parent != null){
99             parent.replaceChild(node, destNode);
100         }
101         return node;
102     }
103
104     /**
105      * Count the number of children in a node has.
106      */

107     public static int countChildren(Node JavaDoc node) {
108         int cnt = 0;
109         for (Node JavaDoc child = node.getFirstChild(); child != null;
110              child = child.getNextSibling()) {
111             cnt++;
112         }
113         return cnt;
114     }
115
116     /**
117      * Determine if a document is a LazyDOM instance document.
118      * @return true if its a LazyDOM instance, false if its a LazyDOM template
119      * or not a LazyDOM at all.
120      */

121     public static boolean isLazyDOMInstance(Document doc) {
122         return ((doc instanceof LazyDocument) && !((LazyDocument)doc).isTemplateNode());
123     }
124
125     /** Calls a method using reflection.
126      * If the desired method is not found, an {@link
127      * UnsupportedOperationException} is thrown.
128      * @param obj the object to call the method on
129      * @param name the method name
130      * @param argtypes the argument types. May be <code>null</code>
131      * for a void method
132      * @param args the arguments. Primitive types must be wrapped in
133      * the corresponding class
134      * @return whatever the called method returns
135      * @throws UnsupportedOperationException if the method cannot be
136      * found or cannot be called via reflection
137      * @throws InvocationTargetException if the called method throws
138      * a checked exception. Note that errors and runtime exceptions
139      * thrown by the called method are automatically propagated.
140      */

141     private static Object JavaDoc callByReflection(Object JavaDoc obj, String JavaDoc name,
142                     Class JavaDoc[] argtypes, Object JavaDoc[] args)
143         throws UnsupportedOperationException JavaDoc,
144                InvocationTargetException JavaDoc {
145     Class JavaDoc cl = obj.getClass();
146     try {
147         Method JavaDoc m = cl.getMethod(name, argtypes);
148         return m.invoke(obj, args);
149     } catch (NoSuchMethodException JavaDoc e) {
150         throw new UnsupportedOperationException JavaDoc(e.toString());
151     } catch (SecurityException JavaDoc e) {
152         throw new UnsupportedOperationException JavaDoc(e.toString());
153     } catch (IllegalAccessException JavaDoc e) {
154         throw new UnsupportedOperationException JavaDoc(e.toString());
155     } catch (IllegalArgumentException JavaDoc e) {
156         throw new UnsupportedOperationException JavaDoc(e.toString());
157     } catch (InvocationTargetException JavaDoc e) {
158         Throwable JavaDoc cause = e.getTargetException();
159         if (cause instanceof Error JavaDoc) {
160         throw (Error JavaDoc)cause;
161         } else if (cause instanceof RuntimeException JavaDoc) {
162         throw (RuntimeException JavaDoc)cause;
163         } else {
164         throw e;
165         }
166     }
167     }
168
169     /** Calls a method using reflection. This is exactly the same
170      * as calling <code>callByReflection(obj, name, new Class[] {String.class}, new Object[] { arg })</code>.
171      * @param obj the object to call the method on
172      * @param name the method name
173      * @param arg the single boolean argument to the method
174      * for a void method
175      * @return whatever the called method returns
176      * @throws UnsupportedOperationException if the method cannot be
177      * found or cannot be called via reflection
178      * @throws InvocationTargetException if the called method throws
179      * a checked exception. Note that errors and runtime exceptions
180      * thrown by the called method are automatically propagated.
181      */

182     private static Object JavaDoc callByReflection(Object JavaDoc obj, String JavaDoc name,
183                        boolean arg)
184         throws UnsupportedOperationException JavaDoc,
185                InvocationTargetException JavaDoc {
186
187     return callByReflection(obj, name, ARGTYPES_PRIM_BOOLEAN,
188                 new Object JavaDoc[] { (arg?Boolean.TRUE:Boolean.FALSE) });
189     }
190
191     /** Calls a method using reflection. This is exactly the same
192      * as calling <code>callByReflection(obj, name, new Class[] {Boolean.TYPE}, new Object[] { new Boolean(arg) })</code>.
193      * @param obj the object to call the method on
194      * @param name the method name
195      * @param arg the single string argument to the method
196      * for a void method
197      * @return whatever the called method returns
198      * @throws UnsupportedOperationException if the method cannot be
199      * found or cannot be called via reflection
200      * @throws InvocationTargetException if the called method throws
201      * a checked exception. Note that errors and runtime exceptions
202      * thrown by the called method are automatically propagated.
203      */

204     private static Object JavaDoc callByReflection(Object JavaDoc obj, String JavaDoc name, String JavaDoc arg)
205         throws UnsupportedOperationException JavaDoc,
206                InvocationTargetException JavaDoc {
207
208     return callByReflection(obj, name,
209                 ARGTYPES_STRING, new Object JavaDoc[] { arg });
210     }
211
212     /** Call Document#getStandalone() on <code>doc</code> if
213      * supported.
214      * By using reflection, this method avoids problems when
215      * <code>Document.getStandalone()</code> is not supported by the
216      * given implementation of <code>org.w3c.dom.Document</code>
217      * (e.g. the one provided by JDK 1.4).
218      * @param doc the document
219      * @return <code>true</code> if the document is standalone,
220      * <code>false</code> else
221      * @throws UnsupportedOperationException if <code>doc</code> does
222      * not support the method
223      */

224     public static boolean getStandalone(Document doc) {
225     try {
226         Boolean JavaDoc result =
227         (Boolean JavaDoc)callByReflection(doc, "getStandalone", null, null);
228         return result.booleanValue();
229     } catch (InvocationTargetException JavaDoc e) {
230         // Something went seriously wrong - we don't expect any
231
// checked exception here...
232
throw new Error JavaDoc("Unexpected exception: " + e.getTargetException());
233     }
234     }
235
236     /** Call Document#setStandalone(val) on <code>doc</code> if
237      * supported.
238      * By using reflection, this method avoids problems when
239      * <code>Document.setStandalone()</code> is not supported by the
240      * given implementation of <code>org.w3c.dom.Document</code>
241      * (e.g. the one provided by JDK 1.4).
242      * @param doc the document
243      * @param val the new value
244      * @throws UnsupportedOperationException if <code>doc</code> does
245      * not support the method
246      */

247     public static void setStandalone(Document doc, boolean val) {
248     try {
249         callByReflection(doc, "setStandalone", val);
250     } catch (InvocationTargetException JavaDoc e) {
251         // Something went seriously wrong - we don't expect any
252
// checked exception here...
253
throw new Error JavaDoc("Unexpected exception: " + e.getTargetException());
254     }
255     }
256      
257
258     /** Call Document#getEncoding() on <code>doc</code> if
259      * supported.
260      * By using reflection, this method avoids problems when
261      * <code>Document.getEncoding()</code> is not supported by the
262      * given implementation of <code>org.w3c.dom.Document</code>
263      * (e.g. the one provided by JDK 1.4).
264      * @param doc the document
265      * @return the encoding
266      * @throws UnsupportedOperationException if <code>doc</code> does
267      * not support the method
268      */

269     public static String JavaDoc getEncoding(Document doc) {
270     try {
271         return (String JavaDoc)callByReflection(doc, "getEncoding", null, null);
272     } catch (InvocationTargetException JavaDoc e) {
273         // Something went seriously wrong - we don't expect any
274
// checked exception here...
275
throw new Error JavaDoc("Unexpected exception: " + e.getTargetException());
276     }
277     }
278      
279     /** Call Document#setEncoding(enc) on <code>doc</code> if
280      * supported.
281      * By using reflection, this method avoids problems when
282      * <code>Document.setEncoding()</code> is not supported by the
283      * given implementation of <code>org.w3c.dom.Document</code>
284      * (e.g. the one provided by JDK 1.4).
285      * @param doc the document
286      * @param enc the encoding
287      * @return the encoding
288      * @throws UnsupportedOperationException if <code>doc</code> does
289      * not support the method
290      */

291     public static void setEncoding(Document doc, String JavaDoc enc) {
292     try {
293         callByReflection(doc, "setEncoding", enc);
294     } catch (InvocationTargetException JavaDoc e) {
295         // Something went seriously wrong - we don't expect any
296
// checked exception here...
297
throw new Error JavaDoc("Unexpected exception: " + e.getTargetException());
298     }
299     }
300      
301     /** Call Document#getStrictErrorChecking() on <code>doc</code> if
302      * supported.
303      * By using reflection, this method avoids problems when
304      * <code>Document.getStrictErrorChecking()</code> is not supported by the
305      * given implementation of <code>org.w3c.dom.Document</code>
306      * (e.g. the one provided by JDK 1.4).
307      * @param doc the document
308      * @return <code>true</code> if the document has strict error checking,
309      * <code>false</code> else
310      * @throws UnsupportedOperationException if <code>doc</code> does
311      * not support the method
312      */

313     public static boolean getStrictErrorChecking(Document doc) {
314     try {
315         Boolean JavaDoc result =
316         (Boolean JavaDoc)callByReflection(doc, "getStrictErrorChecking",
317                       null, null);
318         return result.booleanValue();
319     } catch (InvocationTargetException JavaDoc e) {
320         // Something went seriously wrong - we don't expect any
321
// checked exception here...
322
throw new Error JavaDoc("Unexpected exception: " + e.getTargetException());
323     }
324     }
325
326     /** Call Document#setStrictErrorChecking(val) on
327      * <code>doc</code> if supported. By using reflection, this
328      * method avoids problems when
329      * <code>Document.setStrictErrorChecking()</code> is not supported by the
330      * given implementation of <code>org.w3c.dom.Document</code>
331      * (e.g. the one provided by JDK 1.4).
332      * @param doc the document
333      * @param val the new value
334      * @throws UnsupportedOperationException if <code>doc</code> does
335      * not support the method
336      */

337     public static void setStrictErrorChecking(Document doc, boolean val) {
338     try {
339         callByReflection(doc, "setStrictErrorChecking", val);
340     } catch (InvocationTargetException JavaDoc e) {
341         // Something went seriously wrong - we don't expect any
342
// checked exception here...
343
throw new Error JavaDoc("Unexpected exception: " + e.getTargetException());
344     }
345     }
346      
347     /** Call Document#getVersion() on <code>doc</code> if
348      * supported.
349      * By using reflection, this method avoids problems when
350      * <code>Document.getVersion()</code> is not supported by the
351      * given implementation of <code>org.w3c.dom.Document</code>
352      * (e.g. the one provided by JDK 1.4).
353      * @param doc the document
354      * @return the version
355      * @throws UnsupportedOperationException if <code>doc</code> does
356      * not support the method
357      */

358     public static String JavaDoc getVersion(Document doc) {
359     try {
360         return (String JavaDoc)callByReflection(doc, "getVersion", null, null);
361     } catch (InvocationTargetException JavaDoc e) {
362         // Something went seriously wrong - we don't expect any
363
// checked exception here...
364
throw new Error JavaDoc("Unexpected exception: " + e.getTargetException());
365     }
366     }
367
368     /** Call Document#setVersion(version) on <code>doc</code> if
369      * supported.
370      * By using reflection, this method avoids problems when
371      * <code>Document.setVersion()</code> is not supported by the
372      * given implementation of <code>org.w3c.dom.Document</code>
373      * (e.g. the one provided by JDK 1.4).
374      * @param doc the document
375      * @param version the version
376      * @return the version
377      * @throws UnsupportedOperationException if <code>doc</code> does
378      * not support the method
379      */

380     public static void setVersion(Document doc, String JavaDoc version) {
381     try {
382         callByReflection(doc, "setVersion", version);
383     } catch (InvocationTargetException JavaDoc e) {
384         // Something went seriously wrong - we don't expect any
385
// checked exception here...
386
throw new Error JavaDoc("Unexpected exception: " + e.getTargetException());
387     }
388     }
389
390     /** Call Document#adoptNode(node) on <code>doc</code> if
391      * supported.
392      * By using reflection, this method avoids problems when
393      * <code>Document.adoptNode()</code> is not supported by the
394      * given implementation of <code>org.w3c.dom.Document</code>
395      * (e.g. the one provided by JDK 1.4).
396      * @param doc the document
397      * @param node the node
398      * @return the adopted node, or <code>null</code> if this
399      * operation fails, such as when the source node comes from a
400      * different implementation.
401      * @throws UnsupportedOperationException if <code>doc</code> does
402      * not support the method
403      * @throws DOMException NOT_SUPPORTED_ERR: Raised if the source
404      * node is of type DOCUMENT,
405      * DOCUMENT_TYPE. NO_MODIFICATION_ALLOWED_ERR: Raised when the
406      * source node is readonly.
407      */

408     public static Node JavaDoc adoptNode(Document doc, Node JavaDoc node) throws DOMException JavaDoc {
409     try {
410         return (Node JavaDoc)callByReflection(doc, "adoptNode",
411                         ARGTYPES_NODE,
412                         new Object JavaDoc[] { node });
413     } catch (InvocationTargetException JavaDoc e) {
414         Throwable JavaDoc cause = e.getTargetException();
415         if (cause instanceof DOMException JavaDoc) {
416         throw (DOMException JavaDoc) cause;
417         } else {
418         // Something went seriously wrong - we don't expect any
419
// other checked exceptions here...
420
throw new Error JavaDoc("Unexpected exception: " + cause);
421         }
422     }
423     }
424
425     
426
427     /** Call Entity#getEncoding on <code>entity</code> if
428      * supported.
429      * By using reflection, this method avoids problems when
430      * <code>Entity.getEncoding()</code> is not supported by the
431      * given implementation of <code>org.w3c.dom.Entity</code>
432      * (e.g. the one provided by JDK 1.4).
433      * @param entity the entity
434      * @return the encoding
435      * @throws UnsupportedOperationException if <code>entity</code> does
436      * not support the method
437      */

438     public static String JavaDoc getEncoding(Entity JavaDoc entity) {
439     try {
440         return (String JavaDoc)callByReflection(entity, "getEncoding", null, null);
441     } catch (InvocationTargetException JavaDoc e) {
442         // Something went seriously wrong - we don't expect any
443
// checked exception here...
444
throw new Error JavaDoc("Unexpected exception: " + e.getTargetException());
445     }
446     }
447      
448     /** Call Entity#setEncoding(enc) on <code>entity</code> if
449      * supported.
450      * By using reflection, this method avoids problems when
451      * <code>Entity.setEncoding()</code> is not supported by the
452      * given implementation of <code>org.w3c.dom.Entity</code>
453      * (e.g. the one provided by JDK 1.4).
454      * @param entity the entity
455      * @param enc the encoding
456      * @return the encoding
457      * @throws UnsupportedOperationException if <code>entity</code> does
458      * not support the method
459      */

460     public static void setEncoding(Entity JavaDoc entity, String JavaDoc enc) {
461     try {
462         callByReflection(entity, "setEncoding", enc);
463     } catch (InvocationTargetException JavaDoc e) {
464         // Something went seriously wrong - we don't expect any
465
// checked exception here...
466
throw new Error JavaDoc("Unexpected exception: " + e.getTargetException());
467     }
468     }
469      
470     /** Call HTMLElement#getContentDocument(node) on <code>elem</code> if
471      * supported.
472      * By using reflection, this method avoids problems when
473      * <code>HTMLElement.getContentDocument()</code> is not supported by the
474      * given implementation of <code>org.w3c.dom.HTMLElement</code>
475      * (e.g. the one provided by JDK 1.4).
476      * @param elem the element
477      * @return the adopted node, or <code>null</code> if this
478      * operation fails, such as when the source node comes from a
479      * different implementation.
480      * @throws UnsupportedOperationException if <code>elem</code> does
481      * not support the method
482      */

483     public static Document getContentDocument(HTMLElement elem) {
484     try {
485         return (Document)callByReflection(elem, "getContentDocument",
486                           ARGTYPES_HTMLELEMENT,
487                           new Object JavaDoc[] { elem });
488     } catch (InvocationTargetException JavaDoc e) {
489         Throwable JavaDoc cause = e.getTargetException();
490         if (cause instanceof DOMException JavaDoc) {
491         throw (DOMException JavaDoc) cause;
492         } else {
493         // Something went seriously wrong - we don't expect any
494
// other checked exceptions here...
495
throw new Error JavaDoc("Unexpected exception: " + cause);
496         }
497     }
498     }
499 }
500
Popular Tags