1 19 20 25 26 27 package soot.xml; 28 29 30 31 public class XMLNode extends XMLRoot 32 { 33 public static final int TAG_STRING_BUFFER = 4096; 35 36 public XMLNode next = null; public XMLNode prev = null; public XMLNode parent = null; public XMLNode child = null; public XMLRoot root = null; 43 public XMLNode( String in_name, String in_value, String [] in_attributes, String [] 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 clone() 68 { 69 return new XMLNode( this ); 70 } 71 72 public String toPostString() 73 { 74 return toPostString( "" ); 75 } 76 public String toPostString( String indent ) 77 { 78 if( next != null ) 79 return this.toString( indent ) + next.toPostString( indent); 80 else 81 return this.toString( indent ); 82 } 83 84 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 public XMLNode addAttribute( String attribute, String value ) 102 { 103 String [] tempAttributes = this.attributes; 105 String [] tempValues = this.values; 106 this.attributes = new String [ tempAttributes.length + 1 ]; 107 this.values = new String [ 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 public String toString() 124 { 125 return toString( "" ); 126 } 127 public String toString( String indent ) 128 { 129 StringBuffer beginTag = new StringBuffer ( TAG_STRING_BUFFER ); 131 StringBuffer endTag = new StringBuffer ( TAG_STRING_BUFFER ); 132 String 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 String attributeName = eliminateSpaces( attributes[ i ].toString().trim() ); 144 beginTag.append( " " + attributeName + "=\"" ); 146 147 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 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 String 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 199 200 public XMLNode insertElement( String name ) 202 { 203 return insertElement( name, "", "", "" ); 204 } 205 public XMLNode insertElement( String name, String value ) 206 { 207 return insertElement( name, value, "", "" ); 208 } 209 public XMLNode insertElement( String name, String value, String [] attributes ) 210 { 211 return insertElement( name, value, attributes, null ); 212 } 213 public XMLNode insertElement( String name, String [] attributes, String [] values ) 214 { 215 return insertElement( name, "", attributes, values ); 216 } 217 public XMLNode insertElement( String name, String value, String attribute, String attributeValue ) 218 { 219 return insertElement( name, value, new String [] { attribute}, new String [] { attributeValue} ); 220 } 221 public XMLNode insertElement( String name, String value, String [] attributes, String [] values ) 222 { 223 XMLNode newnode = new XMLNode( name, value, attributes, values ); 224 225 if( this.parent != null ) 227 { 228 if( this.parent.child.equals( this ) ) 229 { 230 this.parent.child = newnode; 231 } 232 } 233 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 public XMLNode addElement( String name ) 255 { 256 return addElement( name, "", "", "" ); 257 } 258 public XMLNode addElement( String name, String value ) 259 { 260 return addElement( name, value, "", "" ); 261 } 262 public XMLNode addElement( String name, String value, String [] attributes ) 263 { 264 return addElement( name, value, attributes, null ); 265 } 266 public XMLNode addElement( String name, String [] attributes, String [] values ) 267 { 268 return addElement( name, "", attributes, values ); 269 } 270 public XMLNode addElement( String name, String value, String attribute, String attributeValue ) 271 { 272 return addElement( name, value, new String [] { attribute}, new String [] { attributeValue} ); 273 } 274 public XMLNode addElement( String name, String value, String [] attributes, String [] 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 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 public XMLNode addChild( String name ) 319 { 320 return addChild( name, "", "", "" ); 321 } 322 public XMLNode addChild( String name, String value ) 323 { 324 return addChild( name, value, "", "" ); 325 } 326 public XMLNode addChild( String name, String value, String [] attributes ) 327 { 328 return addChild( name, value, attributes, null ); 329 } 330 public XMLNode addChild( String name, String [] attributes, String [] values ) 331 { 332 return addChild( name, "", attributes, values ); 333 } 334 public XMLNode addChild( String name, String value, String attribute, String attributeValue ) 335 { 336 return addChild( name, value, new String [] { attribute}, new String [] { attributeValue} ); 337 } 338 public XMLNode addChild( String name, String value, String [] attributes, String [] 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 eliminateSpaces( String str ) 365 { 366 return str.trim().replace( ' ', '_' ); 367 } 368 } 369 370 371 | Popular Tags |