Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issues with unicode ANSI skipping #31

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand All @@ -69,6 +71,8 @@ public void processGroupEnd()
{
handleEvent(GROUP_END);
state = stack.pop();
// Clear any remaining skippable bytes on group end.
skipBytes = 0;
}

/**
Expand All @@ -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;
}
}

Expand Down Expand Up @@ -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));
}

Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}

}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?xml version="1.0" encoding="UTF-8"?><rtf><group><command name="rtf" parameter="1"/><chars>axyz</chars></group></rtf>
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?xml version="1.0" encoding="UTF-8"?><rtf><group><command name="rtf" parameter="1"/><chars>a</chars><group><command name="b"/><chars>foo</chars></group><chars>aar a</chars><group><command name="b"/><chars>baz</chars></group></group></rtf>
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?xml version="1.0" encoding="UTF-8"?><rtf><group><command name="rtf" parameter="1"/><chars>ac</chars></group></rtf>
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?xml version="1.0" encoding="UTF-8"?><rtf><group><command name="rtf" parameter="1"/><group><chars>a</chars></group><chars>foo</chars></group></rtf>
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?xml version="1.0" encoding="UTF-8"?><rtf><group><command name="rtf" parameter="1"/><chars>a</chars><group><command name="b"/><chars>foo</chars></group></group></rtf>