Skip to content

Commit

Permalink
MXParser tokenization fails when PI is before first tag (fixes #7) (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
gnodet authored May 14, 2023
1 parent 85fe71e commit 792f947
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 8 deletions.
11 changes: 3 additions & 8 deletions src/main/java/org/codehaus/plexus/util/xml/pull/MXParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -1822,13 +1822,9 @@ else if ( ch == '\uFFFD' )
{
// check if it is 'xml'
// deal with XMLDecl
boolean isXMLDecl = parsePI();
parsePI();
if ( tokenize )
{
if ( isXMLDecl )
{
return eventType = START_DOCUMENT;
}
return eventType = PROCESSING_INSTRUCTION;
}
}
Expand Down Expand Up @@ -3111,7 +3107,7 @@ else if ( ch == '\n' )
}
}

private boolean parsePI()
private void parsePI()
throws XmlPullParserException, IOException
{
// implements XML 1.0 Section 2.6 Processing Instructions
Expand Down Expand Up @@ -3213,7 +3209,7 @@ else if ( ch == '<' )
final int off = piTargetStart + 3;
final int len = pos - 2 - off;
xmlDeclContent = newString( buf, off, len );
return false;
return;
}
}
}
Expand Down Expand Up @@ -3291,7 +3287,6 @@ else if ( ch == '\n' )
--pcEnd;
}
}
return true;
}

// protected final static char[] VERSION = {'v','e','r','s','i','o','n'};
Expand Down
42 changes: 42 additions & 0 deletions src/test/java/org/codehaus/plexus/util/xml/pull/MXParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1622,4 +1622,46 @@ public void testUnicode() throws IOException {
fail( "should not raise exception: " + e );
}
}

@Test
public void testProcessingInstructionTokenizeBeforeFirstTag()
throws Exception
{
String input = "<?a?><test>nnn</test>";

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

assertEquals( XmlPullParser.START_DOCUMENT, parser.getEventType() );
assertEquals( XmlPullParser.PROCESSING_INSTRUCTION, parser.nextToken() );
assertEquals( "a", parser.getText() );
assertEquals( XmlPullParser.START_TAG, parser.nextToken() );
assertEquals( "test", parser.getName() );
assertEquals( XmlPullParser.TEXT, parser.nextToken() );
assertEquals( "nnn", parser.getText() );
assertEquals( XmlPullParser.END_TAG, parser.nextToken() );
assertEquals( XmlPullParser.END_DOCUMENT, parser.nextToken() );
}

@Test
public void testProcessingInstructionTokenizeAfterXMLDeclAndBeforeFirstTag()
throws Exception
{
String input = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><?a?><test>nnn</test>";

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

assertEquals( XmlPullParser.START_DOCUMENT, parser.getEventType() );
assertEquals( XmlPullParser.PROCESSING_INSTRUCTION, parser.nextToken() );
assertEquals( "xml version=\"1.0\" encoding=\"UTF-8\"", parser.getText() );
assertEquals( XmlPullParser.PROCESSING_INSTRUCTION, parser.nextToken() );
assertEquals( "a", parser.getText() );
assertEquals( XmlPullParser.START_TAG, parser.nextToken() );
assertEquals( "test", parser.getName() );
assertEquals( XmlPullParser.TEXT, parser.nextToken() );
assertEquals( "nnn", parser.getText() );
assertEquals( XmlPullParser.END_TAG, parser.nextToken() );
assertEquals( XmlPullParser.END_DOCUMENT, parser.nextToken() );
}
}

0 comments on commit 792f947

Please sign in to comment.