Para limpiar mi código decidí extraer mis cadenas a un archivo properties
.
El problema es que ahora me dan un error.
java.lang.IllegalArgumentException: can't parse argument number:
Matcher m = Pattern.compile(\"{0}\").matcher(\"{1}\"); int groupsNum = m.groupCount(); while (m.find()) { for (int i = 1; i <= groupsNum; i++) { try { String group = m.group(i); allGroups.add(group); } catch (IllegalStateException ise) { allGroups.add(\"No Matches\"); } } }
at java.text.MessageFormat.makeFormat(MessageFormat.java:1429)
Más abajo en la traza me sale la causa
Caused by: java.lang.NumberFormatException: For input string: "
Matcher m = Pattern.compile(\"{0}\").matcher(\"{1}\"); int groupsNum = m.groupCount(); while (m.find()) { for (int i = 1; i <= groupsNum; i++) { try { String group = m.group(i); allGroups.add(group); } catch (IllegalStateException ise) { allGroups.add(\"No Matches\"); } } } "
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
Cargo la cadena de esta manera
private static String generateJavaSearchingCode(String regexText, String testCase) {
return MessageFormat.format(
java.util.ResourceBundle.getBundle("codegeneration").getString("JAVA_SEARCHING")
, escapedRegexText(regexText), escapedRegexText(testCase));
}
En mi properties tengo la cadena de esta manera:
JAVA_SEARCHING=List<String> allGroups = new ArrayList<>();\ntry {\n Matcher m = Pattern.compile(\\"{0}\\").matcher(\\"{1}\\");\n int groupsNum = m.groupCount();\n while (m.find()) {\n for (int i = 1; i <= groupsNum; i++) {\n try {\n String group = m.group(i);\n allGroups.add(group);\n } catch (IllegalStateException ise) {\n allGroups.add(\\"No Matches\\");\n }\n }\n }\n} catch (PatternSyntaxException pse) {\n allGroups.add(pse.getMessage());\n}
Originalmente la cargaba como:
private static String generateJavaSearchingCode(String regexText, String testCase) {
return new StringBuilder("val allGroups = ArrayList()\n")
.append("try {\n")
.append(" val m = Pattern.compile(\"")
.append(escapedRegexText(regexText))
.append("\").matcher(\"")
.append(escapedRegexText(testCase))
.append("\")\n")
.append(" val groupsNum = m.groupCount()\n")
.append(" while (m.find()) {\n")
.append(" for (i in 1..groupsNum) {\n")
.append(" try {\n")
.append(" val group = m.group(i)\n")
.append(" allGroups.add(group)\n")
.append(" } catch (ise:IllegalStateException) {\n")
.append(" allGroups.add(\"No Matches\")\n")
.append(" }\n")
.append(" }\n")
.append(" }\n")
.append("} catch (pse:PatternSyntaxException) {\n")
.append(" allGroups.add(pse.getMessage())\n")
.append("}")
.toString();
}
Y nunca me dió ese tipo de problemas.
Se me hace muy curioso porque el refactor funcionó perfeto en otras cadenas, por ejemplo
JAVA_SPLIT=Pattern pattern = Pattern.compile(\\"{0}\\");\nString[] splitted= pattern.split(\\"{1}\\",0);