Skip to content

Commit cf52404

Browse files
author
Yoshito Umaoka
committed
Handle exception while parsing Java resource string
MessagePattern class throws an exception when a given string is not parsed as a valid message format pattern string. Added exception handling code in the code unescaping single quote in an input resource string. When an exception was thrown by MessagePattern class, we don’t need to unescape single quotes, because it’s not a message format pattern.
1 parent 8a787a2 commit cf52404

7 files changed

Lines changed: 33 additions & 3 deletions

File tree

gp-res-filter/src/main/java/com/ibm/g11n/pipeline/resfilter/impl/JavaPropertiesResource.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -627,7 +627,19 @@ private static String unescape(String str) {
627627
public static String ConvertDoubleSingleQuote(String inputStr){
628628
String outputStr = "";
629629
boolean needConvert = false;
630-
MessagePattern msgPat = new MessagePattern(inputStr);
630+
MessagePattern msgPat = null;
631+
try {
632+
msgPat = new MessagePattern(inputStr);
633+
} catch (IllegalArgumentException e) {
634+
// not a message format pattern - fall through
635+
} catch (IndexOutOfBoundsException e) {
636+
// might be a valid message format pattern, but cannot handle this - fall through
637+
}
638+
if (msgPat == null) {
639+
// if the string cannot be parsed as a MessageFormat pattern string,
640+
// just returns the input string.
641+
return inputStr;
642+
}
631643

632644
int numParts = msgPat.countParts();
633645

gp-res-filter/src/test/java/com/ibm/g11n/pipeline/resfilter/impl/JavaPropertiesResourceTest.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,13 @@ public class JavaPropertiesResourceTest {
9191

9292
lst.add(ResourceString.with("withTabs", "Tab1\tTab2\tTab3\t").sequenceNumber(9)
9393
.notes(Arrays.asList(" tabs")).build());
94-
94+
9595
lst.add(ResourceString.with("withQuote", "You're about to delete '{0}' rows in Mike's file {0}.")
9696
.sequenceNumber(10).notes(Arrays.asList(" Quote")).build());
97-
97+
98+
lst.add(ResourceString.with("non-param", "This {} is not a parameter.")
99+
.sequenceNumber(11).notes(Arrays.asList(" Not a Java MessageFormat param")).build());
100+
98101
Collections.sort(lst, new ResourceStringComparator());
99102
EXPECTED_INPUT_RES_LIST = lst;
100103
}
@@ -123,6 +126,8 @@ public class JavaPropertiesResourceTest {
123126
Arrays.asList(" tabs"));
124127
bundleBuilder.addResourceString("withQuote", "You're about to delete '{1}' rows in Mike's file {0}.", 10,
125128
Arrays.asList(" Quote"));
129+
bundleBuilder.addResourceString("non-param", "This {} is not a parameter.", 11,
130+
Arrays.asList(" Not a Java MessageFormat param"));
126131
bundleBuilder.addNotes(Arrays.asList(
127132
" You are reading the \".properties\" entry.",
128133
" The exclamation mark can also mark text as comments.",
@@ -145,6 +150,9 @@ public class JavaPropertiesResourceTest {
145150
EXPECTED_PROP_DEF_LIST.add(new PropDef("leadTabs", "leading tabs", PropSeparator.EQUAL));
146151
EXPECTED_PROP_DEF_LIST.add(new PropDef("trailSPs", "trailing SPs ", PropSeparator.EQUAL));
147152
EXPECTED_PROP_DEF_LIST.add(new PropDef("withTabs", "Tab1\tTab2\tTab3\t", PropSeparator.EQUAL));
153+
// PropDef does not detect message pattern - message pattern handling is done by the logic in JavaPropertiesResource class
154+
EXPECTED_PROP_DEF_LIST.add(new PropDef("withQuote", "You''re about to delete '{1}' rows in Mike''s file {0}.", PropSeparator.EQUAL));
155+
EXPECTED_PROP_DEF_LIST.add(new PropDef("non-param", "This {} is not a parameter.", PropSeparator.EQUAL));
148156
}
149157

150158
private static final JavaPropertiesResource res = new JavaPropertiesResource();

gp-res-filter/src/test/resource/resfilter/properties/input.properties

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,5 @@ trailSPs = trailing SPs
2323
withTabs = Tab1\tTab2\tTab3\t
2424
# Quote
2525
withQuote = You''re about to delete '{0}' rows in Mike''s file {0}.
26+
# Not a Java MessageFormat param
27+
non-param = This {} is not a parameter.

gp-res-filter/src/test/resource/resfilter/properties/merge-output.properties

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,5 @@ trailSPs = localized trailing SPs
2323
withTabs = localized Tab1\tTab2\tTab3\t
2424
# Quote
2525
withQuote = You''re about to delete '{1}' rows in Mike''s file {0}.
26+
# Not a Java MessageFormat param
27+
non-param = This {} is not a parameter.

gp-res-filter/src/test/resource/resfilter/properties/parse-test-input.properties

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,5 @@ tab : pick up the\u00A5 tab
77
leadTabs = leading tabs
88
trailSPs = trailing SPs
99
withTabs = Tab1\tTab2\tTab3\t
10+
withQuote = You''re about to delete '{1}' rows in Mike''s file {0}.
11+
non-param = This {} is not a parameter.

gp-res-filter/src/test/resource/resfilter/properties/parseline-test-input.properties

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,5 @@ leadSPs = leading SPs
77
leadTabs = leading tabs
88
trailSPs = trailing SPs
99
withTabs = Tab1\tTab2\tTab3\t
10+
withQuote = You''re about to delete '{1}' rows in Mike''s file {0}.
11+
non-param = This {} is not a parameter.

gp-res-filter/src/test/resource/resfilter/properties/write-output.properties

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,5 @@ trailSPs = localized trailing SPs
2424
withTabs = localized Tab1\tTab2\tTab3\t
2525
# Quote
2626
withQuote = You''re about to delete '{1}' rows in Mike''s file {0}.
27+
# Not a Java MessageFormat param
28+
non-param = This {} is not a parameter.

0 commit comments

Comments
 (0)