diff --git a/src/main/java/com/rtfparserkit/parser/standard/StandardRtfParser.java b/src/main/java/com/rtfparserkit/parser/standard/StandardRtfParser.java index 4f320e6..b55eca2 100644 --- a/src/main/java/com/rtfparserkit/parser/standard/StandardRtfParser.java +++ b/src/main/java/com/rtfparserkit/parser/standard/StandardRtfParser.java @@ -59,6 +59,8 @@ public void processGroupStart() handleEvent(GROUP_START); stack.push(state); state = new ParserState(state); + // Clear any remaining skippable bytes on group start. + skipBytes = 0; } /** @@ -69,6 +71,8 @@ public void processGroupEnd() { handleEvent(GROUP_END); state = stack.pop(); + // Clear any remaining skippable bytes on group end. + skipBytes = 0; } /** @@ -77,21 +81,21 @@ public void processGroupEnd() @Override public void processCharacterBytes(byte[] data) { - try + if (skipBytes < data.length) { - if (data.length != 0) + try { - if (skipBytes < data.length) - { - handleEvent(new StringEvent(new String(data, skipBytes, data.length - skipBytes, currentEncoding()))); - } - skipBytes = 0; + handleEvent(new StringEvent(new String(data, skipBytes, data.length - skipBytes, currentEncoding()))); + } + catch (UnsupportedEncodingException ex) + { + throw new RuntimeException(ex); } + skipBytes = 0; } - - catch (UnsupportedEncodingException ex) + else { - throw new RuntimeException(ex); + skipBytes -= data.length; } } @@ -133,6 +137,11 @@ public void processDocumentEnd() @Override public void processBinaryBytes(byte[] data) { + if (skipBytes > 0) { + // Treat all binary data as a single skippable character. + skipBytes--; + return; + } handleEvent(new BinaryBytesEvent(data)); } @@ -151,6 +160,11 @@ public void processString(String string) @Override public void processCommand(Command command, int parameter, boolean hasParameter, boolean optional) { + if (skipBytes > 0) { + // A command should be treated as a single skippable character (and skipped). + skipBytes--; + return; + } if (command.getCommandType() == CommandType.Encoding) { processEncoding(command, hasParameter, parameter); diff --git a/src/test/java/com/rtfparserkit/parser/standard/StandardRtfParserTest.java b/src/test/java/com/rtfparserkit/parser/standard/StandardRtfParserTest.java index 014f3c9..39ce4f8 100644 --- a/src/test/java/com/rtfparserkit/parser/standard/StandardRtfParserTest.java +++ b/src/test/java/com/rtfparserkit/parser/standard/StandardRtfParserTest.java @@ -154,4 +154,35 @@ public void testNecCharacters() throws Exception { TestUtilities.assertRtfParserDumpMatches(this, new StandardRtfParser(), "testNecCharacters"); } + + @Test + public void testSkipGroupStart() throws Exception + { + TestUtilities.assertRtfParserDumpMatches(this, new StandardRtfParser(), "testSkipGroupStart"); + } + + @Test + public void testSkipGroupEnd() throws Exception + { + TestUtilities.assertRtfParserDumpMatches(this, new StandardRtfParser(), "testSkipGroupEnd"); + } + + @Test + public void testSkipBinary() throws Exception + { + TestUtilities.assertRtfParserDumpMatches(this, new StandardRtfParser(), "testSkipBinary"); + } + + @Test + public void testSkipCommand() throws Exception + { + TestUtilities.assertRtfParserDumpMatches(this, new StandardRtfParser(), "testSkipCommand"); + } + + @Test + public void testSkipCombo() throws Exception + { + TestUtilities.assertRtfParserDumpMatches(this, new StandardRtfParser(), "testSkipCombo"); + } + } diff --git a/src/test/resources/com/rtfparserkit/parser/standard/data/testSkipBinary.rtf b/src/test/resources/com/rtfparserkit/parser/standard/data/testSkipBinary.rtf new file mode 100644 index 0000000..3f8e326 Binary files /dev/null and b/src/test/resources/com/rtfparserkit/parser/standard/data/testSkipBinary.rtf differ diff --git a/src/test/resources/com/rtfparserkit/parser/standard/data/testSkipBinary.xml b/src/test/resources/com/rtfparserkit/parser/standard/data/testSkipBinary.xml new file mode 100644 index 0000000..083094a --- /dev/null +++ b/src/test/resources/com/rtfparserkit/parser/standard/data/testSkipBinary.xml @@ -0,0 +1 @@ +axyz \ No newline at end of file diff --git a/src/test/resources/com/rtfparserkit/parser/standard/data/testSkipCombo.rtf b/src/test/resources/com/rtfparserkit/parser/standard/data/testSkipCombo.rtf new file mode 100644 index 0000000..57c2c89 Binary files /dev/null and b/src/test/resources/com/rtfparserkit/parser/standard/data/testSkipCombo.rtf differ diff --git a/src/test/resources/com/rtfparserkit/parser/standard/data/testSkipCombo.xml b/src/test/resources/com/rtfparserkit/parser/standard/data/testSkipCombo.xml new file mode 100644 index 0000000..8624a34 --- /dev/null +++ b/src/test/resources/com/rtfparserkit/parser/standard/data/testSkipCombo.xml @@ -0,0 +1 @@ +afooaar abaz \ No newline at end of file diff --git a/src/test/resources/com/rtfparserkit/parser/standard/data/testSkipCommand.rtf b/src/test/resources/com/rtfparserkit/parser/standard/data/testSkipCommand.rtf new file mode 100644 index 0000000..51c106b Binary files /dev/null and b/src/test/resources/com/rtfparserkit/parser/standard/data/testSkipCommand.rtf differ diff --git a/src/test/resources/com/rtfparserkit/parser/standard/data/testSkipCommand.xml b/src/test/resources/com/rtfparserkit/parser/standard/data/testSkipCommand.xml new file mode 100644 index 0000000..baf10d4 --- /dev/null +++ b/src/test/resources/com/rtfparserkit/parser/standard/data/testSkipCommand.xml @@ -0,0 +1 @@ +ac \ No newline at end of file diff --git a/src/test/resources/com/rtfparserkit/parser/standard/data/testSkipGroupEnd.rtf b/src/test/resources/com/rtfparserkit/parser/standard/data/testSkipGroupEnd.rtf new file mode 100644 index 0000000..6c12c55 Binary files /dev/null and b/src/test/resources/com/rtfparserkit/parser/standard/data/testSkipGroupEnd.rtf differ diff --git a/src/test/resources/com/rtfparserkit/parser/standard/data/testSkipGroupEnd.xml b/src/test/resources/com/rtfparserkit/parser/standard/data/testSkipGroupEnd.xml new file mode 100644 index 0000000..33a2ae5 --- /dev/null +++ b/src/test/resources/com/rtfparserkit/parser/standard/data/testSkipGroupEnd.xml @@ -0,0 +1 @@ +afoo \ No newline at end of file diff --git a/src/test/resources/com/rtfparserkit/parser/standard/data/testSkipGroupStart.rtf b/src/test/resources/com/rtfparserkit/parser/standard/data/testSkipGroupStart.rtf new file mode 100644 index 0000000..33b6b5f Binary files /dev/null and b/src/test/resources/com/rtfparserkit/parser/standard/data/testSkipGroupStart.rtf differ diff --git a/src/test/resources/com/rtfparserkit/parser/standard/data/testSkipGroupStart.xml b/src/test/resources/com/rtfparserkit/parser/standard/data/testSkipGroupStart.xml new file mode 100644 index 0000000..d4931be --- /dev/null +++ b/src/test/resources/com/rtfparserkit/parser/standard/data/testSkipGroupStart.xml @@ -0,0 +1 @@ +afoo \ No newline at end of file