Skip to content

Commit

Permalink
hapifhir#7: fix problem with encoding \r
Browse files Browse the repository at this point in the history
  • Loading branch information
ohr committed Nov 6, 2017
1 parent e03603a commit 5e0dbe8
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 8 deletions.
14 changes: 7 additions & 7 deletions hapi-base/src/main/java/ca/uhn/hl7v2/parser/DefaultEscaping.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public class DefaultEscaping implements Escaping {
* limits the size of variousEncChars to 1000, can be overridden by system property.
*/
private static Map<EncodingCharacters, EncLookup> variousEncChars = Collections.synchronizedMap(new LinkedHashMap
<EncodingCharacters, EncLookup>(5, 0.75f, true) {
<EncodingCharacters, EncLookup>(6, 0.75f, true) {

private static final long serialVersionUID = 1L;
final int maxSize = new Integer(System.getProperty(Escape.class.getName() + ".maxSize", "1000"));
Expand All @@ -75,7 +75,7 @@ public String escape(String text, EncodingCharacters encChars) {
char c = text.charAt(i);

FORENCCHARS:
for (int j = 0; j < 6; j++) {
for (int j = 0; j < esc.characters.length; j++) {
if (text.charAt(i) == esc.characters[j]) {

// Formatting escape sequences such as \.br\ should be left alone
Expand Down Expand Up @@ -260,7 +260,8 @@ private static EncLookup getEscapeSequences(EncodingCharacters encChars) {
*/
private static class EncLookup {

char[] characters = new char[7];
private static final char[] CODES = {'F', 'S', 'T', 'R', 'E', 'L'};
private char[] characters = new char[7];
String[] encodings = new String[7];

EncLookup(EncodingCharacters ec) {
Expand All @@ -269,14 +270,13 @@ private static class EncLookup {
characters[2] = ec.getSubcomponentSeparator();
characters[3] = ec.getRepetitionSeparator();
characters[4] = ec.getEscapeCharacter();

characters[5] = ec.getTruncationCharacter();
characters[6] = '\r';
char[] codes = {'F', 'S', 'T', 'R', 'E', 'L'};
for (int i = 0; i < codes.length; i++) {

for (int i = 0; i < CODES.length; i++) {
StringBuilder seq = new StringBuilder();
seq.append(ec.getEscapeCharacter());
seq.append(codes[i]);
seq.append(CODES[i]);
seq.append(ec.getEscapeCharacter());
encodings[i] = seq.toString();
}
Expand Down
9 changes: 8 additions & 1 deletion hapi-test/src/test/java/ca/uhn/hl7v2/parser/EscapeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,14 @@ public void testUnescape() throws Exception {
String uu = getUuencodedEscapedString();
esc.unescape(uu, enc);
}



@Test
public void escapeLinebreakWithHapi() {
String escape = esc.escape("\r", enc);
assertEquals("\\X000d\\", escape);
}

@Test
public void testSimpleEscape() {
String actual = esc.escape("GLUCOSE^1H POST 75 G GLUCOSE PO:SCNC:PT:SER/PLAS:QN", enc);
Expand Down
3 changes: 3 additions & 0 deletions src/changes/changes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
</properties>
<body>
<release version="2.4" date="TBD">
<action type="fix" dev="Christian Ohr" issue="7">
Fix encoding of line breaks as \X000d\
</action>
<action type="fix">
When calling Parser#parse(Message, String), the parser now sets itself
as the parser associated with the message. This causes the parser's
Expand Down

0 comments on commit 5e0dbe8

Please sign in to comment.