KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > soot > xml > XMLNode


1 /* Soot - a J*va Optimization Framework
2  * Copyright (C) 2002 David Eng
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */

19
20 /*
21  * Modified by the Sable Research Group and others 1997-1999.
22  * See the 'credits' file distributed with Soot for the complete list of
23  * contributors. (Soot is distributed at http://www.sable.mcgill.ca/soot)
24  */

25
26
27 package soot.xml;
28
29
30 /** XML helper */
31 public class XMLNode extends XMLRoot
32 {
33     // constants
34
public static final int TAG_STRING_BUFFER = 4096;
35
36     // node pointers
37
public XMLNode next = null; // -> to next node
38
public XMLNode prev = null; // -> to previous node
39
public XMLNode parent = null; // -> to parent node
40
public XMLNode child = null; // -> to child node
41
public XMLRoot root = null; // -> to root node
42

43     public XMLNode( String JavaDoc in_name, String JavaDoc in_value, String JavaDoc[] in_attributes, String JavaDoc[] in_values )
44     {
45         name = in_name;
46         value = in_value;
47         attributes = in_attributes;
48         values = in_values;
49     }
50
51     public XMLNode( XMLNode node )
52     {
53         if( node != null )
54         {
55             name = node.name;
56             value = node.value;
57             attributes = node.attributes;
58             values = node.values;
59
60             if( node.child != null )
61                 this.child = ( XMLNode )node.child.clone();
62             if( node.next != null )
63                 this.next = ( XMLNode )node.next.clone();
64         }
65     }
66
67     public Object JavaDoc clone()
68     {
69         return new XMLNode( this );
70     }
71
72     public String JavaDoc toPostString()
73     {
74         return toPostString( "" );
75     }
76     public String JavaDoc toPostString( String JavaDoc indent )
77     {
78         if( next != null )
79             return this.toString( indent ) + next.toPostString( indent);
80         else
81             return this.toString( indent );
82     }
83
84     // returns the number of children
85
public int getNumberOfChildren()
86     {
87         int count = 0;
88         if( child != null )
89         {
90             XMLNode current = child;
91             while( current != null )
92             {
93                 current = current.next;
94                 count++;
95             }
96         }
97         return count;
98     }
99
100     // adds an attribute to an element
101
public XMLNode addAttribute( String JavaDoc attribute, String JavaDoc value )
102     {
103         // check if this attribute already exists
104
String JavaDoc[] tempAttributes = this.attributes;
105         String JavaDoc[] tempValues = this.values;
106         this.attributes = new String JavaDoc[ tempAttributes.length + 1 ];
107         this.values = new String JavaDoc[ tempValues.length + 1 ];
108         for( int i = 0; i < tempAttributes.length; i++ )
109         {
110             this.attributes[ i ] = tempAttributes[ i ];
111             if( tempValues.length > i )
112             {
113                 this.values[ i ] = tempValues[ i ];
114             }
115         }
116         this.attributes[ tempAttributes.length ] = attribute.trim();
117         this.values[ tempValues.length ] = value.trim();
118         return this;
119     }
120
121     // XML Printing and formatting
122
//
123
public String JavaDoc toString()
124     {
125         return toString( "" );
126     }
127     public String JavaDoc toString( String JavaDoc indent )
128     {
129         // <tag
130
StringBuffer JavaDoc beginTag = new StringBuffer JavaDoc( TAG_STRING_BUFFER );
131         StringBuffer JavaDoc endTag = new StringBuffer JavaDoc( TAG_STRING_BUFFER );
132         String JavaDoc xmlName = eliminateSpaces( name );
133
134         beginTag.append( "<" + xmlName );
135
136         if( attributes != null )
137         {
138             for( int i = 0; i < attributes.length; i++ )
139             {
140                 if( attributes[ i ].length() > 0 )
141                 {
142                     // <tag attr="
143
String JavaDoc attributeName = eliminateSpaces( attributes[ i ].toString().trim() );
144                     // TODO: attribute name should be one word! squish it?
145
beginTag.append( " " + attributeName + "=\"" );
146
147                     // <tag attr="val"
148
// if there is no value associated with this attribute,
149
// consider it a <hr NOSHADE> style attribute;
150
// use the default: <hr NOSHADE="NOSHADE">
151
if( values != null )
152                     {
153                         if( i < values.length )
154                         {
155                             beginTag.append( values[ i ].toString().trim() + "\"" );
156                         }
157                         else
158                         {
159                             beginTag.append( attributeName.trim() + "\"" );
160                         }
161                     }
162                 }
163             }
164         }
165
166         // <tag attr="val"...> or <tag attr="val".../>
167
// if there is no value in this element AND this element has no children, it can be a single tag <.../>
168
if( value.length() < 1 && child == null )
169         {
170             beginTag.append( " />\n" );
171             endTag.setLength( 0 );
172         }
173         else
174         {
175             beginTag.append( ">" );
176             endTag.append( "</" + xmlName + ">\n" );
177         }
178
179         //return ( prev.toString() + beginTag.toString() + value.toString() + child.toString() + endTag.toString() + next.toString() );
180
String JavaDoc returnStr = indent + beginTag.toString();
181         if( value.length() > 0 )
182             returnStr += value.toString();
183         if( child != null )
184             returnStr += "\n" + child.toPostString( indent + " " );
185         if( child != null )
186             returnStr += indent;
187         if( endTag.length() > 0 )
188             returnStr += endTag.toString();
189         return( returnStr );
190     }
191
192
193
194
195     // CONSTRUCTION ROUTINES
196
//
197
//
198

199
200     // insert element before the node here
201
public XMLNode insertElement( String JavaDoc name )
202     {
203         return insertElement( name, "", "", "" );
204     }
205     public XMLNode insertElement( String JavaDoc name, String JavaDoc value )
206     {
207         return insertElement( name, value, "", "" );
208     }
209     public XMLNode insertElement( String JavaDoc name, String JavaDoc value, String JavaDoc[] attributes )
210     {
211         return insertElement( name, value, attributes, null );
212     }
213     public XMLNode insertElement( String JavaDoc name, String JavaDoc[] attributes, String JavaDoc[] values )
214     {
215         return insertElement( name, "", attributes, values );
216     }
217     public XMLNode insertElement( String JavaDoc name, String JavaDoc value, String JavaDoc attribute, String JavaDoc attributeValue )
218     {
219         return insertElement( name, value, new String JavaDoc[] { attribute}, new String JavaDoc[] { attributeValue} );
220     }
221     public XMLNode insertElement( String JavaDoc name, String JavaDoc value, String JavaDoc[] attributes, String JavaDoc[] values )
222     {
223         XMLNode newnode = new XMLNode( name, value, attributes, values );
224
225         // check if this node is the first of a chain
226
if( this.parent != null )
227         {
228             if( this.parent.child.equals( this ) )
229             {
230                 this.parent.child = newnode;
231             }
232         }
233         // if it has no parent it might be a root's child
234
else
235         {
236             if( this.prev == null )
237             {
238                 this.root.child = newnode;
239             }
240         }
241
242         newnode.child = null;
243         newnode.parent = this.parent;
244         newnode.prev = this.prev;
245         if( newnode.prev != null )
246             newnode.prev.next = newnode;
247         this.prev = newnode;
248         newnode.next = this;
249         return newnode;
250     }
251
252
253     // add element to end of tree
254
public XMLNode addElement( String JavaDoc name )
255     {
256         return addElement( name, "", "", "" );
257     }
258     public XMLNode addElement( String JavaDoc name, String JavaDoc value )
259     {
260         return addElement( name, value, "", "" );
261     }
262     public XMLNode addElement( String JavaDoc name, String JavaDoc value, String JavaDoc[] attributes )
263     {
264         return addElement( name, value, attributes, null );
265     }
266     public XMLNode addElement( String JavaDoc name, String JavaDoc[] attributes, String JavaDoc[] values )
267     {
268         return addElement( name, "", attributes, values );
269     }
270     public XMLNode addElement( String JavaDoc name, String JavaDoc value, String JavaDoc attribute, String JavaDoc attributeValue )
271     {
272         return addElement( name, value, new String JavaDoc[] { attribute}, new String JavaDoc[] { attributeValue} );
273     }
274     public XMLNode addElement( String JavaDoc name, String JavaDoc value, String JavaDoc[] attributes, String JavaDoc[] values )
275     {
276         XMLNode newnode = new XMLNode( name, value, attributes, values);
277         return addElement( newnode );
278     }
279     public XMLNode addElement( XMLNode node )
280     {
281         XMLNode current = this;
282         while( current.next != null )
283         {
284             current = current.next;
285         }
286         current.next = node;
287         node.prev = current;
288         return node;
289     }
290
291     // add one level of children
292
public XMLNode addChildren( XMLNode children )
293     {
294         XMLNode current = children;
295         while( current != null )
296         {
297             current.parent = this;
298             current = current.next;
299         }
300
301         if( this.child == null )
302         {
303             this.child = children;
304         }
305         else
306         {
307             current = this.child;
308             while( current.next != null )
309             {
310                 current = current.next;
311             }
312             current.next = children;
313         }
314         return this;
315     }
316
317     // add element to end of tree
318
public XMLNode addChild( String JavaDoc name )
319     {
320         return addChild( name, "", "", "" );
321     }
322     public XMLNode addChild( String JavaDoc name, String JavaDoc value )
323     {
324         return addChild( name, value, "", "" );
325     }
326     public XMLNode addChild( String JavaDoc name, String JavaDoc value, String JavaDoc[] attributes )
327     {
328         return addChild( name, value, attributes, null );
329     }
330     public XMLNode addChild( String JavaDoc name, String JavaDoc[] attributes, String JavaDoc[] values )
331     {
332         return addChild( name, "", attributes, values );
333     }
334     public XMLNode addChild( String JavaDoc name, String JavaDoc value, String JavaDoc attribute, String JavaDoc attributeValue )
335     {
336         return addChild( name, value, new String JavaDoc[] { attribute}, new String JavaDoc[] { attributeValue} );
337     }
338     public XMLNode addChild( String JavaDoc name, String JavaDoc value, String JavaDoc[] attributes, String JavaDoc[] values )
339     {
340         XMLNode newnode = new XMLNode( name, value, attributes, values );
341         return addChild( newnode );
342     }
343     public XMLNode addChild( XMLNode node )
344     {
345         if( this.child == null )
346         {
347             this.child = node;
348             node.parent = this;
349         }
350         else
351         {
352             XMLNode current = this.child;
353             while( current.next != null )
354             {
355                 current = current.next;
356             }
357             current.next = node;
358             node.prev = current;
359             node.parent = this;
360         }
361         return node;
362     }
363
364     private String JavaDoc eliminateSpaces( String JavaDoc str )
365     {
366         return str.trim().replace( ' ', '_' );
367     }
368 }
369
370
371
Popular Tags