KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > apache > xerces > validators > schema > XUtil


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 1999 The Apache Software Foundation. All rights
6  * reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Apache Software Foundation (http://www.apache.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Xerces" and "Apache Software Foundation" must
28  * not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact apache@apache.org.
31  *
32  * 5. Products derived from this software may not be called "Apache",
33  * nor may "Apache" appear in their name, without prior written
34  * permission of the Apache Software Foundation.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  * ====================================================================
49  *
50  * This software consists of voluntary contributions made by many
51  * individuals on behalf of the Apache Software Foundation and was
52  * originally based on software copyright (c) 1999, International
53  * Business Machines, Inc., http://www.apache.org. For more
54  * information on the Apache Software Foundation, please see
55  * <http://www.apache.org/>.
56  */

57
58 package org.enhydra.apache.xerces.validators.schema;
59
60 import org.enhydra.apache.xerces.dom.AttrImpl;
61 import org.enhydra.apache.xerces.dom.DocumentImpl;
62 import org.w3c.dom.Attr JavaDoc;
63 import org.w3c.dom.DOMException JavaDoc;
64 import org.w3c.dom.Document JavaDoc;
65 import org.w3c.dom.Element JavaDoc;
66 import org.w3c.dom.NamedNodeMap JavaDoc;
67 import org.w3c.dom.Node JavaDoc;
68
69 /**
70  * Some useful utility methods.
71  */

72 public class XUtil {
73
74     //
75
// Constructors
76
//
77

78     /** This class cannot be instantiated. */
79     protected XUtil() {}
80
81     //
82
// Public static methods
83
//
84

85     /**
86      * Copies the source tree into the specified place in a destination
87      * tree. The source node and its children are appended as children
88      * of the destination node.
89      * <p>
90      * <em>Note:</em> This is an iterative implementation.
91      */

92     public static void copyInto(Node JavaDoc src, Node JavaDoc dest) throws DOMException JavaDoc {
93
94         // get node factory
95
Document JavaDoc factory = dest.getOwnerDocument();
96         boolean domimpl = factory instanceof DocumentImpl;
97
98         // placement variables
99
Node JavaDoc start = src;
100         Node JavaDoc parent = src;
101         Node JavaDoc place = src;
102
103         // traverse source tree
104
while (place != null) {
105
106             // copy this node
107
Node JavaDoc node = null;
108             int type = place.getNodeType();
109             switch (type) {
110                 case Node.CDATA_SECTION_NODE: {
111                     node = factory.createCDATASection(place.getNodeValue());
112                     break;
113                 }
114                 case Node.COMMENT_NODE: {
115                     node = factory.createComment(place.getNodeValue());
116                     break;
117                 }
118                 case Node.ELEMENT_NODE: {
119                     Element JavaDoc element = factory.createElement(place.getNodeName());
120                     node = element;
121                     NamedNodeMap JavaDoc attrs = place.getAttributes();
122                     int attrCount = attrs.getLength();
123                     for (int i = 0; i < attrCount; i++) {
124                         Attr JavaDoc attr = (Attr JavaDoc)attrs.item(i);
125                         String JavaDoc attrName = attr.getNodeName();
126                         String JavaDoc attrValue = attr.getNodeValue();
127                         element.setAttribute(attrName, attrValue);
128                         if (domimpl && !attr.getSpecified()) {
129                             ((AttrImpl)element.getAttributeNode(attrName)).setSpecified(false);
130                         }
131                     }
132                     break;
133                 }
134                 case Node.ENTITY_REFERENCE_NODE: {
135                     node = factory.createEntityReference(place.getNodeName());
136                     break;
137                 }
138                 case Node.PROCESSING_INSTRUCTION_NODE: {
139                     node = factory.createProcessingInstruction(place.getNodeName(),
140                                                                place.getNodeValue());
141                     break;
142                 }
143                 case Node.TEXT_NODE: {
144                     node = factory.createTextNode(place.getNodeValue());
145                     break;
146                 }
147                 default: {
148                     throw new IllegalArgumentException JavaDoc("can't copy node type, "+
149                                                        type+" ("+
150                                                        node.getNodeName()+')');
151                 }
152             }
153             dest.appendChild(node);
154
155             // iterate over children
156
if (place.hasChildNodes()) {
157                 parent = place;
158                 place = place.getFirstChild();
159                 dest = node;
160             }
161
162             // advance
163
else {
164                 place = place.getNextSibling();
165                 while (place == null && parent != start) {
166                     place = parent.getNextSibling();
167                     parent = parent.getParentNode();
168                     dest = dest.getParentNode();
169                 }
170             }
171
172         }
173
174     } // copyInto(Node,Node)
175

176     /** Finds and returns the first child element node. */
177     public static Element JavaDoc getFirstChildElement(Node JavaDoc parent) {
178
179         // search for node
180
Node JavaDoc child = parent.getFirstChild();
181         while (child != null) {
182             if (child.getNodeType() == Node.ELEMENT_NODE) {
183                 return (Element JavaDoc)child;
184             }
185             child = child.getNextSibling();
186         }
187
188         // not found
189
return null;
190
191     } // getFirstChildElement(Node):Element
192

193     /** Finds and returns the last child element node. */
194     public static Element JavaDoc getLastChildElement(Node JavaDoc parent) {
195
196         // search for node
197
Node JavaDoc child = parent.getLastChild();
198         while (child != null) {
199             if (child.getNodeType() == Node.ELEMENT_NODE) {
200                 return (Element JavaDoc)child;
201             }
202             child = child.getPreviousSibling();
203         }
204
205         // not found
206
return null;
207
208     } // getLastChildElement(Node):Element
209

210     /** Finds and returns the next sibling element node. */
211     public static Element JavaDoc getNextSiblingElement(Node JavaDoc node) {
212
213         // search for node
214
Node JavaDoc sibling = node.getNextSibling();
215         while (sibling != null) {
216             if (sibling.getNodeType() == Node.ELEMENT_NODE) {
217                 return (Element JavaDoc)sibling;
218             }
219             sibling = sibling.getNextSibling();
220         }
221
222         // not found
223
return null;
224
225     } // getNextSiblingdElement(Node):Element
226

227     /** Finds and returns the first child node with the given name. */
228     public static Element JavaDoc getFirstChildElement(Node JavaDoc parent, String JavaDoc elemName) {
229
230         // search for node
231
Node JavaDoc child = parent.getFirstChild();
232         while (child != null) {
233             if (child.getNodeType() == Node.ELEMENT_NODE) {
234                 if (child.getNodeName().equals(elemName)) {
235                     return (Element JavaDoc)child;
236                 }
237             }
238             child = child.getNextSibling();
239         }
240
241         // not found
242
return null;
243
244     } // getFirstChildElement(Node,String):Element
245

246     /** Finds and returns the last child node with the given name. */
247     public static Element JavaDoc getLastChildElement(Node JavaDoc parent, String JavaDoc elemName) {
248
249         // search for node
250
Node JavaDoc child = parent.getLastChild();
251         while (child != null) {
252             if (child.getNodeType() == Node.ELEMENT_NODE) {
253                 if (child.getNodeName().equals(elemName)) {
254                     return (Element JavaDoc)child;
255                 }
256             }
257             child = child.getPreviousSibling();
258         }
259
260         // not found
261
return null;
262
263     } // getLastChildElement(Node,String):Element
264

265     /** Finds and returns the next sibling node with the given name. */
266     public static Element JavaDoc getNextSiblingElement(Node JavaDoc node, String JavaDoc elemName) {
267
268         // search for node
269
Node JavaDoc sibling = node.getNextSibling();
270         while (sibling != null) {
271             if (sibling.getNodeType() == Node.ELEMENT_NODE) {
272                 if (sibling.getNodeName().equals(elemName)) {
273                     return (Element JavaDoc)sibling;
274                 }
275             }
276             sibling = sibling.getNextSibling();
277         }
278
279         // not found
280
return null;
281
282     } // getNextSiblingdElement(Node,String):Element
283

284     /** Finds and returns the first child node with the given qualified name. */
285     public static Element JavaDoc getFirstChildElementNS(Node JavaDoc parent,
286                                                  String JavaDoc uri, String JavaDoc localpart) {
287
288         // search for node
289
Node JavaDoc child = parent.getFirstChild();
290         while (child != null) {
291             if (child.getNodeType() == Node.ELEMENT_NODE) {
292                 String JavaDoc childURI = child.getNamespaceURI();
293                 if (childURI != null && childURI.equals(uri) &&
294                     child.getLocalName().equals(localpart)) {
295                     return (Element JavaDoc)child;
296                 }
297             }
298             child = child.getNextSibling();
299         }
300
301         // not found
302
return null;
303
304     } // getFirstChildElementNS(Node,String,String):Element
305

306     /** Finds and returns the last child node with the given qualified name. */
307     public static Element JavaDoc getLastChildElementNS(Node JavaDoc parent,
308                                                 String JavaDoc uri, String JavaDoc localpart) {
309
310         // search for node
311
Node JavaDoc child = parent.getLastChild();
312         while (child != null) {
313             if (child.getNodeType() == Node.ELEMENT_NODE) {
314                 String JavaDoc childURI = child.getNamespaceURI();
315                 if (childURI != null && childURI.equals(uri) &&
316                     child.getLocalName().equals(localpart)) {
317                     return (Element JavaDoc)child;
318                 }
319             }
320             child = child.getPreviousSibling();
321         }
322
323         // not found
324
return null;
325
326     } // getLastChildElementNS(Node,String,String):Element
327

328     /** Finds and returns the next sibling node with the given qualified name. */
329     public static Element JavaDoc getNextSiblingElementNS(Node JavaDoc node,
330                                                   String JavaDoc uri, String JavaDoc localpart) {
331
332         // search for node
333
Node JavaDoc sibling = node.getNextSibling();
334         while (sibling != null) {
335             if (sibling.getNodeType() == Node.ELEMENT_NODE) {
336                 String JavaDoc siblingURI = sibling.getNamespaceURI();
337                 if (siblingURI != null && siblingURI.equals(uri) &&
338                     sibling.getLocalName().equals(localpart)) {
339                     return (Element JavaDoc)sibling;
340                 }
341             }
342             sibling = sibling.getNextSibling();
343         }
344
345         // not found
346
return null;
347
348     } // getNextSiblingdElementNS(Node,String,String):Element
349

350     /** Finds and returns the first child node with the given name. */
351     public static Element JavaDoc getFirstChildElement(Node JavaDoc parent, String JavaDoc elemNames[]) {
352
353         // search for node
354
Node JavaDoc child = parent.getFirstChild();
355         while (child != null) {
356             if (child.getNodeType() == Node.ELEMENT_NODE) {
357                 for (int i = 0; i < elemNames.length; i++) {
358                     if (child.getNodeName().equals(elemNames[i])) {
359                         return (Element JavaDoc)child;
360                     }
361                 }
362             }
363             child = child.getNextSibling();
364         }
365
366         // not found
367
return null;
368
369     } // getFirstChildElement(Node,String[]):Element
370

371     /** Finds and returns the last child node with the given name. */
372     public static Element JavaDoc getLastChildElement(Node JavaDoc parent, String JavaDoc elemNames[]) {
373
374         // search for node
375
Node JavaDoc child = parent.getLastChild();
376         while (child != null) {
377             if (child.getNodeType() == Node.ELEMENT_NODE) {
378                 for (int i = 0; i < elemNames.length; i++) {
379                     if (child.getNodeName().equals(elemNames[i])) {
380                         return (Element JavaDoc)child;
381                     }
382                 }
383             }
384             child = child.getPreviousSibling();
385         }
386
387         // not found
388
return null;
389
390     } // getLastChildElement(Node,String[]):Element
391

392     /** Finds and returns the next sibling node with the given name. */
393     public static Element JavaDoc getNextSiblingElement(Node JavaDoc node, String JavaDoc elemNames[]) {
394
395         // search for node
396
Node JavaDoc sibling = node.getNextSibling();
397         while (sibling != null) {
398             if (sibling.getNodeType() == Node.ELEMENT_NODE) {
399                 for (int i = 0; i < elemNames.length; i++) {
400                     if (sibling.getNodeName().equals(elemNames[i])) {
401                         return (Element JavaDoc)sibling;
402                     }
403                 }
404             }
405             sibling = sibling.getNextSibling();
406         }
407
408         // not found
409
return null;
410
411     } // getNextSiblingdElement(Node,String[]):Element
412

413     /** Finds and returns the first child node with the given qualified name. */
414     public static Element JavaDoc getFirstChildElementNS(Node JavaDoc parent,
415                                                  String JavaDoc[][] elemNames) {
416
417         // search for node
418
Node JavaDoc child = parent.getFirstChild();
419         while (child != null) {
420             if (child.getNodeType() == Node.ELEMENT_NODE) {
421                 for (int i = 0; i < elemNames.length; i++) {
422                     String JavaDoc uri = child.getNamespaceURI();
423                     if (uri != null && uri.equals(elemNames[i][0]) &&
424                         child.getLocalName().equals(elemNames[i][1])) {
425                         return (Element JavaDoc)child;
426                     }
427                 }
428             }
429             child = child.getNextSibling();
430         }
431
432         // not found
433
return null;
434
435     } // getFirstChildElementNS(Node,String[][]):Element
436

437     /** Finds and returns the last child node with the given qualified name. */
438     public static Element JavaDoc getLastChildElementNS(Node JavaDoc parent,
439                                                 String JavaDoc[][] elemNames) {
440
441         // search for node
442
Node JavaDoc child = parent.getLastChild();
443         while (child != null) {
444             if (child.getNodeType() == Node.ELEMENT_NODE) {
445                 for (int i = 0; i < elemNames.length; i++) {
446                     String JavaDoc uri = child.getNamespaceURI();
447                     if (uri != null && uri.equals(elemNames[i][0]) &&
448                         child.getLocalName().equals(elemNames[i][1])) {
449                         return (Element JavaDoc)child;
450                     }
451                 }
452             }
453             child = child.getPreviousSibling();
454         }
455
456         // not found
457
return null;
458
459     } // getLastChildElementNS(Node,String[][]):Element
460

461     /** Finds and returns the next sibling node with the given qualified name. */
462     public static Element JavaDoc getNextSiblingElementNS(Node JavaDoc node,
463                                                   String JavaDoc[][] elemNames) {
464
465         // search for node
466
Node JavaDoc sibling = node.getNextSibling();
467         while (sibling != null) {
468             if (sibling.getNodeType() == Node.ELEMENT_NODE) {
469                 for (int i = 0; i < elemNames.length; i++) {
470                     String JavaDoc uri = sibling.getNamespaceURI();
471                     if (uri != null && uri.equals(elemNames[i][0]) &&
472                         sibling.getLocalName().equals(elemNames[i][1])) {
473                         return (Element JavaDoc)sibling;
474                     }
475                 }
476             }
477             sibling = sibling.getNextSibling();
478         }
479
480         // not found
481
return null;
482
483     } // getNextSiblingdElementNS(Node,String[][]):Element
484

485     /**
486      * Finds and returns the first child node with the given name and
487      * attribute name, value pair.
488      */

489     public static Element JavaDoc getFirstChildElement(Node JavaDoc parent,
490                                                String JavaDoc elemName,
491                                                String JavaDoc attrName,
492                                                String JavaDoc attrValue) {
493
494         // search for node
495
Node JavaDoc child = parent.getFirstChild();
496         while (child != null) {
497             if (child.getNodeType() == Node.ELEMENT_NODE) {
498                 Element JavaDoc element = (Element JavaDoc)child;
499                 if (element.getNodeName().equals(elemName) &&
500                     element.getAttribute(attrName).equals(attrValue)) {
501                     return element;
502                 }
503             }
504             child = child.getNextSibling();
505         }
506
507         // not found
508
return null;
509
510     } // getFirstChildElement(Node,String,String,String):Element
511

512     /**
513      * Finds and returns the last child node with the given name and
514      * attribute name, value pair.
515      */

516     public static Element JavaDoc getLastChildElement(Node JavaDoc parent,
517                                                String JavaDoc elemName,
518                                                String JavaDoc attrName,
519                                                String JavaDoc attrValue) {
520
521         // search for node
522
Node JavaDoc child = parent.getLastChild();
523         while (child != null) {
524             if (child.getNodeType() == Node.ELEMENT_NODE) {
525                 Element JavaDoc element = (Element JavaDoc)child;
526                 if (element.getNodeName().equals(elemName) &&
527                     element.getAttribute(attrName).equals(attrValue)) {
528                     return element;
529                 }
530             }
531             child = child.getPreviousSibling();
532         }
533
534         // not found
535
return null;
536
537     } // getLastChildElement(Node,String,String,String):Element
538

539     /**
540      * Finds and returns the next sibling node with the given name and
541      * attribute name, value pair. Since only elements have attributes,
542      * the node returned will be of type Node.ELEMENT_NODE.
543      */

544     public static Element JavaDoc getNextSiblingElement(Node JavaDoc node,
545                                                 String JavaDoc elemName,
546                                                 String JavaDoc attrName,
547                                                 String JavaDoc attrValue) {
548
549         // search for node
550
Node JavaDoc sibling = node.getNextSibling();
551         while (sibling != null) {
552             if (sibling.getNodeType() == Node.ELEMENT_NODE) {
553                 Element JavaDoc element = (Element JavaDoc)sibling;
554                 if (element.getNodeName().equals(elemName) &&
555                     element.getAttribute(attrName).equals(attrValue)) {
556                     return element;
557                 }
558             }
559             sibling = sibling.getNextSibling();
560         }
561
562         // not found
563
return null;
564
565     } // getNextSiblingElement(Node,String,String,String):Element
566

567     /**
568      * Returns the concatenated child text of the specified node.
569      * This method only looks at the immediate children of type
570      * <code>Node.TEXT_NODE</code> or the children of any child
571      * node that is of type <code>Node.CDATA_SECTION_NODE</code>
572      * for the concatenation.
573      *
574      * @param node The node to look at.
575      */

576     public static String JavaDoc getChildText(Node JavaDoc node) {
577
578         // is there anything to do?
579
if (node == null) {
580             return null;
581         }
582
583         // concatenate children text
584
StringBuffer JavaDoc str = new StringBuffer JavaDoc();
585         Node JavaDoc child = node.getFirstChild();
586         while (child != null) {
587             short type = child.getNodeType();
588             if (type == Node.TEXT_NODE) {
589                 str.append(child.getNodeValue());
590             }
591             else if (type == Node.CDATA_SECTION_NODE) {
592                 str.append(getChildText(child));
593             }
594             child = child.getNextSibling();
595         }
596
597         // return text value
598
return str.toString();
599
600     } // getChildText(Node):String
601

602 } // class XUtil
603
Popular Tags