View Javadoc
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;
40  import java.io.StringWriter;
41  
42  /**
43   * Class that contains information about an XML Node
44   */
45  public class XMLNode
46  {
47      private boolean _isElement;     // Element/PCTEXT
48  
49      private String _name;
50  
51      private XMLAttribute _attr;
52  
53      private XMLNode _parent;  // Parent Node
54  
55      private XMLNode _nested;  // Nested XML tags
56  
57      private XMLNode _next;    // Following XML tag on the same level
58  
59      /**
60       * Creates a PCTEXT node
61       */
62      public XMLNode( String name )
63      {
64          this( name, null, null, null );
65          _isElement = false;
66      }
67  
68      /**
69       * Creates a ELEMENT node
70       */
71      public XMLNode( String name, XMLAttribute attr )
72      {
73          this( name, attr, null, null );
74      }
75  
76      /**
77       * Creates a ELEMENT node
78       */
79      public XMLNode( String name, XMLAttribute attr, XMLNode nested, XMLNode next )
80      {
81          _isElement = true;
82          _name = name;
83          _attr = attr;
84          _nested = nested;
85          _next = next;
86          _parent = null;
87      }
88  
89      public String getName()
90      {
91          return _name;
92      }
93  
94      public XMLAttribute getAttributes()
95      {
96          return _attr;
97      }
98  
99      public XMLNode getNested()
100     {
101         return _nested;
102     }
103 
104     public XMLNode getNext()
105     {
106         return _next;
107     }
108 
109     public boolean isElement()
110     {
111         return _isElement;
112     }
113 
114     public void setParent( XMLNode parent )
115     {
116         _parent = parent;
117     }
118 
119     public XMLNode getParent()
120     {
121         return _parent;
122     }
123 
124     public void setNext( XMLNode next )
125     {
126         _next = next;
127     }
128 
129     public void setNested( XMLNode nested )
130     {
131         _nested = nested;
132     }
133 
134     public boolean equals( Object o )
135     {
136         if ( o == null || !( o instanceof XMLNode ) )
137         {
138             return false;
139         }
140         XMLNode other = (XMLNode) o;
141         boolean result =
142             match( _name, other._name ) && match( _attr, other._attr ) && match( _nested, other._nested ) &&
143                 match( _next, other._next );
144         return result;
145     }
146 
147     public String getAttribute( String name )
148     {
149         XMLAttribute cur = _attr;
150         while ( cur != null )
151         {
152             if ( name.equals( cur.getName() ) )
153             {
154                 return cur.getValue();
155             }
156             cur = cur.getNext();
157         }
158         return "";
159     }
160 
161     private static boolean match( Object o1, Object o2 )
162     {
163         if ( o1 == null )
164         {
165             return ( o2 == null );
166         }
167         return o1.equals( o2 );
168     }
169 
170     public void printToStream( PrintWriter out )
171     {
172         printToStream( out, 0 );
173     }
174 
175     public void printToStream( PrintWriter out, int n )
176     {
177         if ( !isElement() )
178         {
179             out.print( _name );
180         }
181         else
182         {
183             if ( _nested == null )
184             {
185                 String attrString = ( _attr == null ) ? "" : ( " " + _attr.toString() );
186                 lineln( out, n, "<" + _name + attrString + "/>" );
187             }
188             else
189             {
190                 String attrString = ( _attr == null ) ? "" : ( " " + _attr.toString() );
191                 lineln( out, n, "<" + _name + attrString + ">" );
192                 _nested.printToStream( out, n + 1 );
193                 if ( _nested.isElement() )
194                 {
195                     lineln( out, n, "</" + _name + ">" );
196                 }
197                 else
198                 {
199                     out.print( "</" + _name + ">" );
200                 }
201             }
202         }
203         if ( _next != null )
204         {
205             _next.printToStream( out, n );
206         }
207     }
208 
209     private static void lineln( PrintWriter out, int indent, String s )
210     {
211         out.println( "" );
212         for ( int i = 0; i < indent; i++ )
213         {
214             out.print( "  " );
215         }
216         out.print( s );
217     }
218 
219     public String toString()
220     {
221         StringWriter sw = new StringWriter( 1000 );
222         PrintWriter pw = new PrintWriter( sw );
223         printToStream( pw );
224         pw.close();
225         return sw.toString();
226     }
227 }
228 
229