Skip to content

Commit

Permalink
Fix position on MXParser (#121)
Browse files Browse the repository at this point in the history
This closes #121
  • Loading branch information
joehni authored and michael-o committed May 17, 2022
1 parent 407a93c commit 89b8ef5
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/main/java/org/codehaus/plexus/util/xml/pull/MXParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,7 @@ private void reset()
// System.out.println("reset() called");
location = null;
lineNumber = 1;
columnNumber = 0;
columnNumber = 1;
seenRoot = false;
reachedEnd = false;
eventType = START_DOCUMENT;
Expand Down Expand Up @@ -3156,7 +3156,7 @@ else if ( !seenInnerTag )
{
// seenPITarget && !seenQ
throw new XmlPullParserException( "processing instruction started on line " + curLine
+ " and column " + curColumn + " was not closed", this, null );
+ " and column " + (curColumn -2) + " was not closed", this, null );
}
}
else if ( ch == '<' )
Expand Down
45 changes: 40 additions & 5 deletions src/test/java/org/codehaus/plexus/util/xml/pull/MXParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,35 @@ public void testValidCharacterReferenceDecimal()
*
* @throws java.lang.Exception if any.
*/
@Test
public void testParserPosition()
throws Exception
{
String input = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><!-- A --> \n <!-- B --><test>\tnnn</test>\n<!-- C\nC -->";

MXParser parser = new MXParser();
parser.setInput( new StringReader( input ) );

assertEquals( XmlPullParser.PROCESSING_INSTRUCTION, parser.nextToken() );
assertPosition( 1, 39, parser );
assertEquals( XmlPullParser.COMMENT, parser.nextToken() );
assertPosition( 1, 49, parser );
assertEquals( XmlPullParser.IGNORABLE_WHITESPACE, parser.nextToken() );
assertPosition( 2, 3, parser ); // end when next token starts
assertEquals( XmlPullParser.COMMENT, parser.nextToken() );
assertPosition( 2, 12, parser );
assertEquals( XmlPullParser.START_TAG, parser.nextToken() );
assertPosition( 2, 18, parser );
assertEquals( XmlPullParser.TEXT, parser.nextToken() );
assertPosition( 2, 23, parser ); // end when next token starts
assertEquals( XmlPullParser.END_TAG, parser.nextToken() );
assertPosition( 2, 29, parser );
assertEquals( XmlPullParser.IGNORABLE_WHITESPACE, parser.nextToken() );
assertPosition( 3, 2, parser ); // end when next token starts
assertEquals( XmlPullParser.COMMENT, parser.nextToken() );
assertPosition( 4, 6, parser );
}

@Test
public void testProcessingInstruction()
throws Exception
Expand Down Expand Up @@ -624,7 +653,7 @@ public void testMalformedProcessingInstructionNoClosingQuestionMark()
}
catch ( XmlPullParserException ex )
{
assertTrue( ex.getMessage().contains( "processing instruction started on line 1 and column 2 was not closed" ) );
assertTrue( ex.getMessage().contains( "processing instruction started on line 1 and column 1 was not closed" ) );
}
}

Expand Down Expand Up @@ -657,7 +686,7 @@ public void testSubsequentMalformedProcessingInstructionNoClosingQuestionMark()
}
catch ( XmlPullParserException ex )
{
assertTrue( ex.getMessage().contains( "processing instruction started on line 1 and column 13 was not closed" ) );
assertTrue( ex.getMessage().contains( "processing instruction started on line 1 and column 12 was not closed" ) );
}
}

Expand Down Expand Up @@ -900,6 +929,12 @@ public void testEncodingISO_8859_1_setInputStream()
}
}

private static void assertPosition( int row, int col, MXParser parser )
{
assertEquals( "Current line", row, parser.getLineNumber() );
assertEquals( "Current column", col, parser.getColumnNumber() );
}

/**
* Issue 163: https://github.com/codehaus-plexus/plexus-utils/issues/163
*
Expand Down Expand Up @@ -958,7 +993,7 @@ public void testCustomEntityNotFoundInText()
}
catch ( XmlPullParserException e )
{
assertTrue( e.getMessage().contains( "could not resolve entity named 'otherentity' (position: START_TAG seen <root>&otherentity;... @1:19)" ) );
assertTrue( e.getMessage().contains( "could not resolve entity named 'otherentity' (position: START_TAG seen <root>&otherentity;... @1:20)" ) );
assertEquals( XmlPullParser.START_TAG, parser.getEventType() ); // not an ENTITY_REF
assertEquals( "otherentity", parser.getText() );
}
Expand Down Expand Up @@ -1025,7 +1060,7 @@ public void testCustomEntityNotFoundInAttr()
}
catch ( XmlPullParserException e )
{
assertTrue( e.getMessage().contains( "could not resolve entity named 'otherentity' (position: START_DOCUMENT seen <root name=\"&otherentity;... @1:25)" ) );
assertTrue( e.getMessage().contains( "could not resolve entity named 'otherentity' (position: START_DOCUMENT seen <root name=\"&otherentity;... @1:26)" ) );
assertEquals( XmlPullParser.START_DOCUMENT, parser.getEventType() ); // not an ENTITY_REF
assertNull( parser.getText() );
}
Expand Down Expand Up @@ -1060,7 +1095,7 @@ public void testCustomEntityNotFoundInAttrTokenize() throws Exception
}
catch ( XmlPullParserException e )
{
assertTrue( e.getMessage().contains( "could not resolve entity named 'otherentity' (position: START_DOCUMENT seen <root name=\"&otherentity;... @1:25)" ) );
assertTrue( e.getMessage().contains( "could not resolve entity named 'otherentity' (position: START_DOCUMENT seen <root name=\"&otherentity;... @1:26)" ) );
assertEquals( XmlPullParser.START_DOCUMENT, parser.getEventType() ); // not an ENTITY_REF
assertNull( parser.getText() );
}
Expand Down

0 comments on commit 89b8ef5

Please sign in to comment.