KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > xml > xmlc > XMLCUtil


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: XMLCUtil.java,v 1.3 2005/01/26 08:29:24 jkjome Exp $
22  */

23
24 package org.enhydra.xml.xmlc;
25
26 import java.io.OutputStream JavaDoc;
27 import java.io.PrintWriter JavaDoc;
28
29 import org.enhydra.xml.dom.DOMInfo;
30 import org.enhydra.xml.dom.DOMOps;
31 import org.w3c.dom.Attr JavaDoc;
32 import org.w3c.dom.Document JavaDoc;
33 import org.w3c.dom.Element JavaDoc;
34 import org.w3c.dom.NamedNodeMap JavaDoc;
35 import org.w3c.dom.Node JavaDoc;
36 import org.w3c.dom.Text JavaDoc;
37
38 //FIXME: move to DOMOps..
39

40 /**
41  * Utility methods for dealing with DOMs.
42  */

43 public abstract class XMLCUtil {
44     /**
45      * Find an attribute of a node by name.
46      *
47      * @param node Node who's attribute is desired.
48      * @param name The name of the desired attribute.
49      * @return The attribute or null if its not found.
50      * @deprecated use org.w3c.dom.Element#getAttributeNode
51      * @see Element#getAttributeNode
52      */

53     public static Attr JavaDoc getAttributeByName(Node JavaDoc node, String JavaDoc name) {
54         NamedNodeMap JavaDoc nodeMap = node.getAttributes();
55         if (nodeMap == null) {
56             return null;
57         } else {
58             return (Attr JavaDoc)nodeMap.getNamedItem(name);
59         }
60     }
61
62     /**
63      * Find the first text descendent node of an element.
64      * This recursively looks more than one level to search
65      * for text in font nodes, etc.
66      *
67      * @param node The starting node for the search.
68      * @return The text node or null if not found.
69      */

70     public static Text JavaDoc findFirstText(Node JavaDoc node) {
71         if (node instanceof Text JavaDoc) {
72             return (Text JavaDoc)node;
73         }
74         for (Node JavaDoc child = node.getFirstChild(); child != null;
75              child = child.getNextSibling()) {
76             Text JavaDoc text = findFirstText(child);
77             if (text != null) {
78                 return text;
79             }
80         }
81         return null;
82     }
83
84     /**
85      * Find the first text descendent node of an element.
86      * This recursively looks more than one level to search
87      * for text in font nodes, etc. If a text node is not
88      * found, generate an error.
89      *
90      * @param node The starting node for the search.
91      * @return The text node.
92      * @exception XMLCError If a text node was not found.
93      */

94     public static Text JavaDoc getFirstText(Node JavaDoc node) {
95         Text JavaDoc text = findFirstText(node);
96         if (text == null) {
97             String JavaDoc msg = "No child text mode found for element";
98             Attr JavaDoc id = getAttributeByName(node, "id");
99             if (id != null) {
100                 msg += "; id=\"" + id.getValue() + "\"";
101             }
102             throw new XMLCError(msg);
103         }
104         return text;
105     }
106
107     /**
108      * Recursively search for an element by id starting at a node.
109      * Assumes id attribute is named "id". With XMLC, it normally
110      * better to access elements in a compiled objects directly with
111      * the generated access methods. This is provided for working on
112      * cloned pieces of the document or other tasks outside of the XMLC
113      * generated object.
114      *
115      * @param id The element id to find. Case is ignored.
116      * @param node The node to start the search at.
117      * @return A point the element if found, otherwise null.
118      */

119     public static Element JavaDoc getElementById(String JavaDoc id, Node JavaDoc node) {
120         //FIXME: make LazyDOM aware
121
if (node instanceof Element JavaDoc) {
122             Element JavaDoc elem = (Element JavaDoc)node;
123             Attr JavaDoc nodeId = getAttributeByName(elem, "id");
124             if ((nodeId != null) && nodeId.getValue().equalsIgnoreCase(id)) {
125                 return (Element JavaDoc)node;
126             }
127         }
128
129         // Search children
130
for (Node JavaDoc child = node.getFirstChild(); child != null;
131              child = child.getNextSibling()) {
132             Element JavaDoc childElem = getElementById(id, child);
133             if (childElem != null) {
134                 return childElem;
135             }
136         }
137         return null;
138     }
139
140     /**
141      * Recursively search for an required element by id starting at a node.
142      *
143      * @param id The element id to find. Case is ignored.
144      * @param node The node to start the search at.
145      * @return A point the element if found.
146      * @exception XMLCError Thrown if the element is not found.
147      * @see #getElementById
148      */

149     public static Element JavaDoc getRequiredElementById(String JavaDoc id, Node JavaDoc node) {
150         Element JavaDoc elem = getElementById(id, node);
151         if (elem == null) {
152             // Not found, generate an error.
153
String JavaDoc msg = "No element found for id \"" + id + "\"";
154             Attr JavaDoc nodeId = getAttributeByName(node, "id");
155             if (nodeId != null) {
156                 msg += "; starting at id=\"" + nodeId.getValue() + "\"";
157             }
158             throw new XMLCError(msg);
159         } else {
160             return elem;
161         }
162     }
163
164     /**
165      * Copy a node and its children from one document to another.
166      *
167      * @param srcNode The node to copy.
168      * @param destDocument The destination document. The node will
169      * belong to this document but will not be inserted in it.
170      * @return The new node.
171      * @deprecated use org.w3c.dom.Document.importNode
172      * @see Document#importNode
173      */

174     public static Node JavaDoc copyNode(Node JavaDoc srcNode, Document JavaDoc destDocument) {
175         return destDocument.importNode(srcNode, true);
176     }
177
178     /**
179      * Replace a node with a given one from another document.
180      * @param srcElement The node to clone and insert
181      * @param destElement The node to be replaced
182      * @return the new node that replaces the destination node
183      * @deprecated use org.enhydra.xml.dom.DOMOps#replaceNode
184      * @see DOMOps#replaceNode
185      */

186     public static Node JavaDoc replaceNode(Node JavaDoc srcNode, Node JavaDoc destNode) {
187         return DOMOps.replaceNode(srcNode, destNode);
188     }
189
190     // Deprecated functions for 3.0 compatibility.
191

192     /**
193      * @deprecated Use DOMInfo
194      * @see DOMInfo#printTree
195      */

196     public static final int PRINT_COMMENT = 0x00;
197
198     /**
199      * @deprecated Use DOMInfo
200      * @see DOMInfo#printTree
201      */

202     public static final int PRINT_TEXT = 0x00;
203
204     /**
205      * @deprecated Use DOMInfo
206      * @see DOMInfo#printTree
207      * Option to printNode to print the contents of Text nodes.
208      */

209     public static final int PRINT_CDATA = 0x00;
210
211     /**
212      * @deprecated Use DOMInfo
213      * @see DOMInfo#printTree
214      * Option to printNode to print the contents of DocumentType nodes.
215      */

216     public static final int PRINT_DOCUMENTTYPE = 0x00;
217
218     /**
219      * @deprecated Use DOMInfo
220      * @see DOMInfo#printTree
221      * All Print options.
222      */

223     public static final int PRINT_ALL = DOMInfo.PRINT_ALL;
224
225     /**
226      * @deprecated Use DOMInfo
227      * @see DOMInfo#printTree
228      * Default print options.
229      */

230     public static final int PRINT_DEFAULT = DOMInfo.PRINT_DEFAULT;
231
232     /**
233      * @deprecated Use DOMInfo
234      * @see DOMInfo#printTree
235      */

236     public static void printNode(String JavaDoc msg, Node JavaDoc node,
237                                  int options,
238                                  PrintWriter JavaDoc out) {
239         DOMInfo.printTree(msg, node, options, out);
240     }
241
242     /**
243      * @deprecated Use DOMInfo
244      * @see DOMInfo#printTree
245      */

246     public static void printNode(String JavaDoc msg, Node JavaDoc node,
247                                  PrintWriter JavaDoc out) {
248         DOMInfo.printTree(msg, node, out);
249     }
250
251     /**
252      * @deprecated Use DOMInfo
253      * @see DOMInfo#printTree
254      */

255     public static void printNode(String JavaDoc msg, Node JavaDoc node,
256                                  OutputStream JavaDoc out) {
257         DOMInfo.printTree(msg, node, out);
258     }
259 }
260
Popular Tags