KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jnlp > sample > servlet > XMLNode


1 /*
2  * @(#)XMLNode.java 1.6 05/11/17
3  *
4  * Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * -Redistribution of source code must retain the above copyright notice, this
10  * list of conditions and the following disclaimer.
11  *
12  * -Redistribution in binary form must reproduce the above copyright notice,
13  * this list of conditions and the following disclaimer in the documentation
14  * and/or other materials provided with the distribution.
15  *
16  * Neither the name of Sun Microsystems, Inc. or the names of contributors may
17  * be used to endorse or promote products derived from this software without
18  * specific prior written permission.
19  *
20  * This software is provided "AS IS," without a warranty of any kind. ALL
21  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
22  * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
23  * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MIDROSYSTEMS, INC. ("SUN")
24  * AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
25  * AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
26  * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
27  * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
28  * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
29  * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE,
30  * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
31  *
32  * You acknowledge that this software is not designed, licensed or intended
33  * for use in the design, construction, operation or maintenance of any
34  * nuclear facility.
35  */

36
37 package jnlp.sample.servlet;
38
39 import java.io.PrintWriter JavaDoc;
40 import java.io.StringWriter JavaDoc;
41
42 /** Class that contains information about an XML Node
43  */

44 public class XMLNode {
45     private boolean _isElement; // Element/PCTEXT
46
private String JavaDoc _name;
47     private XMLAttribute _attr;
48     private XMLNode _parent; // Parent Node
49
private XMLNode _nested; // Nested XML tags
50
private XMLNode _next; // Following XML tag on the same level
51

52     /** Creates a PCTEXT node */
53     public XMLNode(String JavaDoc name) {
54         this(name, null, null, null);
55         _isElement = false;
56     }
57     
58     /** Creates a ELEMENT node */
59     public XMLNode(String JavaDoc name, XMLAttribute attr) {
60         this(name, attr, null, null);
61     }
62     
63     /** Creates a ELEMENT node */
64     public XMLNode(String JavaDoc name, XMLAttribute attr, XMLNode nested, XMLNode next) {
65         _isElement = true;
66         _name = name;
67         _attr = attr;
68         _nested = nested;
69         _next = next;
70         _parent = null;
71     }
72     
73     public String JavaDoc getName() { return _name; }
74     public XMLAttribute getAttributes() { return _attr; }
75     public XMLNode getNested() { return _nested; }
76     public XMLNode getNext() { return _next; }
77     public boolean isElement() { return _isElement; }
78     
79     public void setParent(XMLNode parent) { _parent = parent; }
80     public XMLNode getParent() { return _parent; }
81         
82     public void setNext(XMLNode next) { _next = next; }
83     public void setNested(XMLNode nested) { _nested = nested; }
84     
85     public boolean equals(Object JavaDoc o) {
86         if (o == null || !(o instanceof XMLNode)) return false;
87         XMLNode other = (XMLNode)o;
88         boolean result =
89             match(_name, other._name) &&
90             match(_attr, other._attr) &&
91             match(_nested, other._nested) &&
92             match(_next, other._next);
93         return result;
94     }
95     
96     public String JavaDoc getAttribute(String JavaDoc name) {
97         XMLAttribute cur = _attr;
98         while(cur != null) {
99             if (name.equals(cur.getName())) return cur.getValue();
100             cur = cur.getNext();
101         }
102         return "";
103     }
104     
105     private static boolean match(Object JavaDoc o1, Object JavaDoc o2) {
106         if (o1 == null) return (o2 == null);
107         return o1.equals(o2);
108     }
109     
110     public void printToStream(PrintWriter JavaDoc out) {
111         printToStream(out, 0);
112     }
113     
114     public void printToStream(PrintWriter JavaDoc out, int n) {
115         if (!isElement()) {
116             out.print(_name);
117         } else {
118             if (_nested == null) {
119                 String JavaDoc attrString = (_attr == null) ? "" : (" " + _attr.toString());
120                 lineln(out, n, "<" + _name + attrString + "/>");
121             } else {
122                 String JavaDoc attrString = (_attr == null) ? "" : (" " + _attr.toString());
123                 lineln(out, n, "<" + _name + attrString + ">");
124                 _nested.printToStream(out, n + 1);
125                 if (_nested.isElement()) {
126                     lineln(out, n, "</" + _name + ">");
127                 } else {
128                     out.print("</" + _name + ">");
129                 }
130             }
131         }
132         if (_next != null) {
133             _next.printToStream(out, n);
134         }
135     }
136     
137     private static void lineln(PrintWriter JavaDoc out, int indent, String JavaDoc s) {
138         out.println("");
139         for(int i = 0; i < indent; i++) {
140             out.print(" ");
141         }
142         out.print(s);
143     }
144     
145     public String JavaDoc toString() {
146         StringWriter JavaDoc sw = new StringWriter JavaDoc(1000);
147         PrintWriter JavaDoc pw = new PrintWriter JavaDoc(sw);
148         printToStream(pw);
149         pw.close();
150         return sw.toString();
151     }
152 }
153
154
155
Popular Tags