Skip to content

Commit 26cc2bf

Browse files
committed
#61 add input location support when building XPP3 DOM
1 parent 2b10af4 commit 26cc2bf

File tree

4 files changed

+109
-3
lines changed

4 files changed

+109
-3
lines changed

pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ limitations under the License.
2626
</parent>
2727

2828
<artifactId>plexus-utils</artifactId>
29-
<version>3.1.2-SNAPSHOT</version>
29+
<version>3.2.0-SNAPSHOT</version>
3030

3131
<name>Plexus Common Utilities</name>
3232
<description>A collection of various utility classes to ease working with strings, files, command lines, XML and

src/main/java/org/codehaus/plexus/util/xml/Xpp3Dom.java

+25
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ public class Xpp3Dom
4848

4949
protected Xpp3Dom parent;
5050

51+
/**
52+
* @since 3.2.0
53+
*/
54+
protected Object inputLocation;
55+
5156
private static final String[] EMPTY_STRING_ARRAY = new String[0];
5257

5358
private static final Xpp3Dom[] EMPTY_DOM_ARRAY = new Xpp3Dom[0];
@@ -278,6 +283,26 @@ public void setParent( Xpp3Dom parent )
278283
this.parent = parent;
279284
}
280285

286+
// ----------------------------------------------------------------------
287+
// Input location handling
288+
// ----------------------------------------------------------------------
289+
290+
/**
291+
* @since 3.2.0
292+
*/
293+
public Object getInputLocation()
294+
{
295+
return inputLocation;
296+
}
297+
298+
/**
299+
* @since 3.2.0
300+
*/
301+
public void setInputLocation( Object inputLocation )
302+
{
303+
this.inputLocation = inputLocation;
304+
}
305+
281306
// ----------------------------------------------------------------------
282307
// Helpers
283308
// ----------------------------------------------------------------------

src/main/java/org/codehaus/plexus/util/xml/Xpp3DomBuilder.java

+44-2
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,16 @@ public class Xpp3DomBuilder
3737
public static Xpp3Dom build( Reader reader )
3838
throws XmlPullParserException, IOException
3939
{
40-
return build( reader, DEFAULT_TRIM );
40+
return build( reader, null );
41+
}
42+
43+
/**
44+
* @since 3.2.0
45+
*/
46+
public static Xpp3Dom build( Reader reader, InputLocationBuilder locationBuilder )
47+
throws XmlPullParserException, IOException
48+
{
49+
return build( reader, DEFAULT_TRIM, locationBuilder );
4150
}
4251

4352
public static Xpp3Dom build( InputStream is, String encoding )
@@ -68,13 +77,22 @@ public static Xpp3Dom build( InputStream is, String encoding, boolean trim )
6877

6978
public static Xpp3Dom build( Reader reader, boolean trim )
7079
throws XmlPullParserException, IOException
80+
{
81+
return build( reader, trim, null );
82+
}
83+
84+
/**
85+
* @since 3.2.0
86+
*/
87+
public static Xpp3Dom build( Reader reader, boolean trim, InputLocationBuilder locationBuilder )
88+
throws XmlPullParserException, IOException
7189
{
7290
try
7391
{
7492
final XmlPullParser parser = new MXParser();
7593
parser.setInput( reader );
7694

77-
final Xpp3Dom xpp3Dom = build( parser, trim );
95+
final Xpp3Dom xpp3Dom = build( parser, trim, locationBuilder );
7896
reader.close();
7997
reader = null;
8098

@@ -94,6 +112,15 @@ public static Xpp3Dom build( XmlPullParser parser )
94112

95113
public static Xpp3Dom build( XmlPullParser parser, boolean trim )
96114
throws XmlPullParserException, IOException
115+
{
116+
return build( parser, trim, null );
117+
}
118+
119+
/**
120+
* @since 3.2.0
121+
*/
122+
public static Xpp3Dom build( XmlPullParser parser, boolean trim, InputLocationBuilder locationBuilder )
123+
throws XmlPullParserException, IOException
97124
{
98125
List<Xpp3Dom> elements = new ArrayList<Xpp3Dom>();
99126

@@ -113,6 +140,11 @@ public static Xpp3Dom build( XmlPullParser parser, boolean trim )
113140

114141
Xpp3Dom childConfiguration = new Xpp3Dom( rawName );
115142

143+
if ( locationBuilder != null )
144+
{
145+
childConfiguration.setInputLocation( locationBuilder.toInputLocation( parser ) );
146+
}
147+
116148
int depth = elements.size();
117149

118150
if ( depth > 0 )
@@ -194,4 +226,14 @@ else if ( eventType == XmlPullParser.END_TAG )
194226

195227
throw new IllegalStateException( "End of document found before returning to 0 depth" );
196228
}
229+
230+
/**
231+
* Input location builder interface, to be implemented to choose how to store data.
232+
*
233+
* @since 3.2.0
234+
*/
235+
public static interface InputLocationBuilder
236+
{
237+
Object toInputLocation( XmlPullParser parser );
238+
}
197239
}

src/test/java/org/codehaus/plexus/util/xml/Xpp3DomBuilderTest.java

+39
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,35 @@ public void testEscapingInAttributes()
175175
assertEquals( "Compare stringified DOMs", newString, s );
176176
}
177177

178+
@Test
179+
public void testInputLocationTracking()
180+
throws IOException, XmlPullParserException
181+
{
182+
Xpp3DomBuilder.InputLocationBuilder ilb = new Xpp3DomBuilder.InputLocationBuilder() {
183+
public Object toInputLocation( XmlPullParser parser )
184+
{
185+
return parser.getLineNumber(); // store only line number as a simple Integer
186+
}
187+
188+
};
189+
Xpp3Dom dom = Xpp3DomBuilder.build( new StringReader( createDomString() ), true, ilb );
190+
Xpp3Dom expectedDom = createExpectedDom();
191+
assertEquals( "root input location", expectedDom.getInputLocation(), dom.getInputLocation() );
192+
for( int i = 0; i < dom.getChildCount(); i++ )
193+
{
194+
Xpp3Dom elt = dom.getChild( i );
195+
Xpp3Dom expectedElt = expectedDom.getChild( i );
196+
assertEquals( elt.getName() + " input location", expectedElt.getInputLocation(), elt.getInputLocation() );
197+
198+
if ( "el2".equals( elt.getName() ) )
199+
{
200+
Xpp3Dom el3 = elt.getChild( 0 );
201+
Xpp3Dom expectedEl3 = expectedElt.getChild( 0 );
202+
assertEquals( el3.getName() + " input location", expectedEl3.getInputLocation(), el3.getInputLocation() );
203+
}
204+
}
205+
}
206+
178207
private static String getAttributeEncodedString()
179208
{
180209
StringBuilder domString = new StringBuilder();
@@ -237,23 +266,33 @@ private static String createDomString()
237266

238267
private static Xpp3Dom createExpectedDom()
239268
{
269+
int line = 1;
240270
Xpp3Dom expectedDom = new Xpp3Dom( "root" );
271+
expectedDom.setInputLocation( line );
241272
Xpp3Dom el1 = new Xpp3Dom( "el1" );
273+
el1.setInputLocation( ++line );
242274
el1.setValue( "element1" );
243275
expectedDom.addChild( el1 );
276+
++line; // newline trimmed in Xpp3Dom but not in source
244277
Xpp3Dom el2 = new Xpp3Dom( "el2" );
278+
el2.setInputLocation( ++line );
245279
el2.setAttribute( "att2", "attribute2\nnextline" );
246280
expectedDom.addChild( el2 );
247281
Xpp3Dom el3 = new Xpp3Dom( "el3" );
282+
el3.setInputLocation( ++line );
248283
el3.setAttribute( "att3", "attribute3" );
249284
el3.setValue( "element3" );
250285
el2.addChild( el3 );
286+
++line;
251287
Xpp3Dom el4 = new Xpp3Dom( "el4" );
288+
el4.setInputLocation( ++line );
252289
el4.setValue( "" );
253290
expectedDom.addChild( el4 );
254291
Xpp3Dom el5 = new Xpp3Dom( "el5" );
292+
el5.setInputLocation( ++line );
255293
expectedDom.addChild( el5 );
256294
Xpp3Dom el6 = new Xpp3Dom( "el6" );
295+
el6.setInputLocation( ++line );
257296
el6.setAttribute( "xml:space", "preserve" );
258297
el6.setValue( " do not trim " );
259298
expectedDom.addChild( el6 );

0 commit comments

Comments
 (0)