23
23
import java .io .StringWriter ;
24
24
import java .util .ArrayList ;
25
25
import java .util .Arrays ;
26
+ import java .util .Collections ;
26
27
import java .util .HashMap ;
27
28
import java .util .Iterator ;
28
29
import java .util .List ;
30
+ import java .util .ListIterator ;
29
31
import java .util .Map ;
30
32
31
33
/**
@@ -44,8 +46,6 @@ public class Xpp3Dom
44
46
45
47
protected final List <Xpp3Dom > childList ;
46
48
47
- protected final Map <String , Xpp3Dom > childMap ;
48
-
49
49
protected Xpp3Dom parent ;
50
50
51
51
/**
@@ -88,7 +88,6 @@ public Xpp3Dom( String name )
88
88
{
89
89
this .name = name ;
90
90
childList = new ArrayList <Xpp3Dom >();
91
- childMap = new HashMap <String , Xpp3Dom >();
92
91
}
93
92
94
93
/**
@@ -119,7 +118,6 @@ public Xpp3Dom( Xpp3Dom src, String name )
119
118
int childCount = src .getChildCount ();
120
119
121
120
childList = new ArrayList <Xpp3Dom >( childCount );
122
- childMap = new HashMap <String , Xpp3Dom >( childCount << 1 );
123
121
124
122
setValue ( src .getValue () );
125
123
@@ -170,13 +168,13 @@ public String[] getAttributeNames()
170
168
}
171
169
else
172
170
{
173
- return ( String []) attributes .keySet ().toArray ( new String [ attributes . size ()] );
171
+ return attributes .keySet ().toArray ( EMPTY_STRING_ARRAY );
174
172
}
175
173
}
176
174
177
175
public String getAttribute ( String name )
178
176
{
179
- return ( null != attributes ) ? ( String ) attributes .get ( name ) : null ;
177
+ return ( null != attributes ) ? attributes .get ( name ) : null ;
180
178
}
181
179
182
180
/**
@@ -209,19 +207,30 @@ public void setAttribute( String name, String value )
209
207
210
208
public Xpp3Dom getChild ( int i )
211
209
{
212
- return ( Xpp3Dom ) childList .get ( i );
210
+ return childList .get ( i );
213
211
}
214
212
215
213
public Xpp3Dom getChild ( String name )
216
214
{
217
- return (Xpp3Dom ) childMap .get ( name );
215
+ if ( name != null )
216
+ {
217
+ ListIterator <Xpp3Dom > it = childList .listIterator ( childList .size () );
218
+ while ( it .hasPrevious () )
219
+ {
220
+ Xpp3Dom child = it .previous ();
221
+ if ( name .equals ( child .getName () ) )
222
+ {
223
+ return child ;
224
+ }
225
+ }
226
+ }
227
+ return null ;
218
228
}
219
229
220
230
public void addChild ( Xpp3Dom xpp3Dom )
221
231
{
222
232
xpp3Dom .setParent ( this );
223
233
childList .add ( xpp3Dom );
224
- childMap .put ( xpp3Dom .getName (), xpp3Dom );
225
234
}
226
235
227
236
public Xpp3Dom [] getChildren ()
@@ -232,31 +241,45 @@ public Xpp3Dom[] getChildren()
232
241
}
233
242
else
234
243
{
235
- return ( Xpp3Dom []) childList .toArray ( new Xpp3Dom [ childList . size ()] );
244
+ return childList .toArray ( EMPTY_DOM_ARRAY );
236
245
}
237
246
}
238
247
239
248
public Xpp3Dom [] getChildren ( String name )
249
+ {
250
+ return getChildrenAsList ( name ).toArray ( EMPTY_DOM_ARRAY );
251
+ }
252
+
253
+ private List <Xpp3Dom > getChildrenAsList ( String name )
240
254
{
241
255
if ( null == childList )
242
256
{
243
- return EMPTY_DOM_ARRAY ;
257
+ return Collections . emptyList () ;
244
258
}
245
259
else
246
260
{
247
- ArrayList <Xpp3Dom > children = new ArrayList <Xpp3Dom >();
248
- int size = childList .size ();
261
+ ArrayList <Xpp3Dom > children = null ;
249
262
250
- for ( Xpp3Dom aChildList : childList )
263
+ for ( Xpp3Dom configuration : childList )
251
264
{
252
- Xpp3Dom configuration = (Xpp3Dom ) aChildList ;
253
265
if ( name .equals ( configuration .getName () ) )
254
266
{
267
+ if ( children == null )
268
+ {
269
+ children = new ArrayList <Xpp3Dom >();
270
+ }
255
271
children .add ( configuration );
256
272
}
257
273
}
258
274
259
- return (Xpp3Dom []) children .toArray ( new Xpp3Dom [children .size ()] );
275
+ if ( children != null )
276
+ {
277
+ return children ;
278
+ }
279
+ else
280
+ {
281
+ return Collections .emptyList ();
282
+ }
260
283
}
261
284
}
262
285
@@ -273,7 +296,6 @@ public int getChildCount()
273
296
public void removeChild ( int i )
274
297
{
275
298
Xpp3Dom child = getChild ( i );
276
- childMap .values ().remove ( child );
277
299
childList .remove ( i );
278
300
// In case of any dangling references
279
301
child .setParent ( null );
@@ -392,12 +414,14 @@ private static void mergeIntoXpp3Dom( Xpp3Dom dominant, Xpp3Dom recessive, Boole
392
414
dominant .setInputLocation ( recessive .getInputLocation () );
393
415
}
394
416
395
- String [] recessiveAttrs = recessive .getAttributeNames ();
396
- for ( String attr : recessiveAttrs )
417
+ if ( recessive .attributes != null )
397
418
{
398
- if ( isEmpty ( dominant . getAttribute ( attr ) ) )
419
+ for ( String attr : recessive . attributes . keySet ( ) )
399
420
{
400
- dominant .setAttribute ( attr , recessive .getAttribute ( attr ) );
421
+ if ( isEmpty ( dominant .getAttribute ( attr ) ) )
422
+ {
423
+ dominant .setAttribute ( attr , recessive .getAttribute ( attr ) );
424
+ }
401
425
}
402
426
}
403
427
@@ -441,12 +465,16 @@ private static void mergeIntoXpp3Dom( Xpp3Dom dominant, Xpp3Dom recessive, Boole
441
465
{
442
466
Map <String , Iterator <Xpp3Dom >> commonChildren = new HashMap <String , Iterator <Xpp3Dom >>();
443
467
444
- for ( String childName : recessive .childMap . keySet () )
468
+ for ( Xpp3Dom recChild : recessive .childList )
445
469
{
446
- Xpp3Dom [] dominantChildren = dominant .getChildren ( childName );
447
- if ( dominantChildren .length > 0 )
470
+ if ( commonChildren .containsKey ( recChild .name ) )
471
+ {
472
+ continue ;
473
+ }
474
+ List <Xpp3Dom > dominantChildren = dominant .getChildrenAsList ( recChild .name );
475
+ if ( dominantChildren .size () > 0 )
448
476
{
449
- commonChildren .put ( childName , Arrays . asList ( dominantChildren ) .iterator () );
477
+ commonChildren .put ( recChild . name , dominantChildren .iterator () );
450
478
}
451
479
}
452
480
0 commit comments