Skip to content

Commit

Permalink
Escape string literals.
Browse files Browse the repository at this point in the history
  • Loading branch information
Atsushi Eno committed May 21, 2012
1 parent 474a821 commit b44fd5f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
22 changes: 19 additions & 3 deletions JavaClass.java
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,6 @@ void appendField (Field field, FieldNode asmField, Document doc, Element parent)
if (asmField == null)
// this happens to couple of fields on java.awt.font.TextAttribute, java.lang.Double/Float and so on.
System.err.println ("!!!!! WARNING!!! null ASM FieldNode for " + field);
//else if (field.isEnumConstant ())
// e.setAttribute ("value", field.getName ());
else if (asmField.value != null) {
String type = e.getAttribute ("type");
boolean isPublic = Modifier.isPublic (mods);
Expand Down Expand Up @@ -238,7 +236,7 @@ else if (type == "boolean")
else if (type == "java.lang.String") {
String value = (String) asmField.value;
if (value != null)
e.setAttribute ("value", "\"" + value.replace ("\\", "\\\\") + "\"");
e.setAttribute ("value", "\"" + escapeLiteral (value.replace ("\\", "\\\\")) + "\"");
}
else if (Modifier.isStatic (mods) && e.getAttribute ("type").endsWith ("[]"))
e.setAttribute ("value", "null");
Expand All @@ -252,6 +250,24 @@ else if (!Modifier.isStatic (mods) && e.getAttribute ("type").endsWith ("[]"))
parent.appendChild (e);
}

String escapeLiteral (String s)
{
for (int i = 0; i < s.length (); i++)
if (s.charAt (i) < 0x20 || 0xFF <= s.charAt (i))
return doEscapeLiteral (new StringBuilder (s), i);
return s;
}

String doEscapeLiteral (StringBuilder s, int i)
{
s.replace (i, i + 1, String.format ("\\u%1$x04" + (int) s.charAt (i)));
i += 4;
for (;i < s.length (); i++)
if (s.charAt (i) < 0x20 || 0xFF <= s.charAt (i))
return doEscapeLiteral (s, i);
return s.toString ();
}

void appendMethod (Method method, Document doc, Element parent)
{
int mods = method.getModifiers ();
Expand Down
2 changes: 1 addition & 1 deletion Start.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public static void main (String[] args)
List<String> jar_paths = new ArrayList<String> ();
String out_path = null;
List<String> additional_jar_paths = new ArrayList<String> ();
String usage = "Usage: jar2xml --jar=<jarfile> --out=<file> [--javadocpath=<javadoc>] [--droiddocpath=<droiddoc>] [--annotations=<xmlfile>]";
String usage = "Usage: jar2xml --jar=<jarfile> [--ref=<jarfile>] --out=<file> [--javadocpath=<javadoc>] [--droiddocpath=<droiddoc>] [--annotations=<xmlfile>]";

for (String arg : args) {
if (arg.startsWith ("--javadocpath=")) {
Expand Down

0 comments on commit b44fd5f

Please sign in to comment.