Skip to content

Commit 6bfda82

Browse files
committed
Fix issues with unicode ANSI skipping
The current implementation of unicode ANSI replacement sequences is slightly wrong - according to https://www.biblioscape.com/rtf15_spec.htm * A scope delimiter (i.e. "{" or "}") should end the current skippable data * Control words or symbols should be considered a single skipped character (and in my testing with MS office, they're ignored) * Any binary data is also considered a single skipped character Resolves: https://everlaw.aha.io/features/EP-2307
1 parent 26c80c8 commit 6bfda82

12 files changed

Lines changed: 60 additions & 10 deletions

File tree

src/main/java/com/rtfparserkit/parser/standard/StandardRtfParser.java

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ public void processGroupStart()
5959
handleEvent(GROUP_START);
6060
stack.push(state);
6161
state = new ParserState(state);
62+
// Clear any remaining skippable bytes on group start.
63+
skipBytes = 0;
6264
}
6365

6466
/**
@@ -69,6 +71,8 @@ public void processGroupEnd()
6971
{
7072
handleEvent(GROUP_END);
7173
state = stack.pop();
74+
// Clear any remaining skippable bytes on group end.
75+
skipBytes = 0;
7276
}
7377

7478
/**
@@ -77,21 +81,21 @@ public void processGroupEnd()
7781
@Override
7882
public void processCharacterBytes(byte[] data)
7983
{
80-
try
84+
if (skipBytes < data.length)
8185
{
82-
if (data.length != 0)
86+
try
8387
{
84-
if (skipBytes < data.length)
85-
{
86-
handleEvent(new StringEvent(new String(data, skipBytes, data.length - skipBytes, currentEncoding())));
87-
}
88-
skipBytes = 0;
88+
handleEvent(new StringEvent(new String(data, skipBytes, data.length - skipBytes, currentEncoding())));
89+
}
90+
catch (UnsupportedEncodingException ex)
91+
{
92+
throw new RuntimeException(ex);
8993
}
94+
skipBytes = 0;
9095
}
91-
92-
catch (UnsupportedEncodingException ex)
96+
else
9397
{
94-
throw new RuntimeException(ex);
98+
skipBytes -= data.length;
9599
}
96100
}
97101

@@ -133,6 +137,11 @@ public void processDocumentEnd()
133137
@Override
134138
public void processBinaryBytes(byte[] data)
135139
{
140+
if (skipBytes > 0) {
141+
// Treat all binary data as a single skippable character.
142+
skipBytes--;
143+
return;
144+
}
136145
handleEvent(new BinaryBytesEvent(data));
137146
}
138147

@@ -151,6 +160,11 @@ public void processString(String string)
151160
@Override
152161
public void processCommand(Command command, int parameter, boolean hasParameter, boolean optional)
153162
{
163+
if (skipBytes > 0) {
164+
// A command should be treated as a single skippable character (and skipped).
165+
skipBytes--;
166+
return;
167+
}
154168
if (command.getCommandType() == CommandType.Encoding)
155169
{
156170
processEncoding(command, hasParameter, parameter);

src/test/java/com/rtfparserkit/parser/standard/StandardRtfParserTest.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,4 +154,35 @@ public void testNecCharacters() throws Exception
154154
{
155155
TestUtilities.assertRtfParserDumpMatches(this, new StandardRtfParser(), "testNecCharacters");
156156
}
157+
158+
@Test
159+
public void testSkipGroupStart() throws Exception
160+
{
161+
TestUtilities.assertRtfParserDumpMatches(this, new StandardRtfParser(), "testSkipGroupStart");
162+
}
163+
164+
@Test
165+
public void testSkipGroupEnd() throws Exception
166+
{
167+
TestUtilities.assertRtfParserDumpMatches(this, new StandardRtfParser(), "testSkipGroupEnd");
168+
}
169+
170+
@Test
171+
public void testSkipBinary() throws Exception
172+
{
173+
TestUtilities.assertRtfParserDumpMatches(this, new StandardRtfParser(), "testSkipBinary");
174+
}
175+
176+
@Test
177+
public void testSkipCommand() throws Exception
178+
{
179+
TestUtilities.assertRtfParserDumpMatches(this, new StandardRtfParser(), "testSkipCommand");
180+
}
181+
182+
@Test
183+
public void testSkipCombo() throws Exception
184+
{
185+
TestUtilities.assertRtfParserDumpMatches(this, new StandardRtfParser(), "testSkipCombo");
186+
}
187+
157188
}
Binary file not shown.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<?xml version="1.0" encoding="UTF-8"?><rtf><group><command name="rtf" parameter="1"/><chars>axyz</chars></group></rtf>
Binary file not shown.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
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.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<?xml version="1.0" encoding="UTF-8"?><rtf><group><command name="rtf" parameter="1"/><chars>ac</chars></group></rtf>
Binary file not shown.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
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>

0 commit comments

Comments
 (0)