Revision: 5266
http://sourceforge.net/p/jump-pilot/code/5266
Author: edso
Date: 2016-12-17 16:18:21 +0000 (Sat, 17 Dec 2016)
Log Message:
-----------
#447 fix illegal character writing in jml (XML 1.0) using apache common-lang3
StringEscapeUtils
Modified Paths:
--------------
core/trunk/src/com/vividsolutions/jump/io/GMLWriter.java
Modified: core/trunk/src/com/vividsolutions/jump/io/GMLWriter.java
===================================================================
--- core/trunk/src/com/vividsolutions/jump/io/GMLWriter.java 2016-12-17
14:47:12 UTC (rev 5265)
+++ core/trunk/src/com/vividsolutions/jump/io/GMLWriter.java 2016-12-17
16:18:21 UTC (rev 5266)
@@ -41,6 +41,8 @@
import java.util.Date;
import java.util.Iterator;
+import org.apache.commons.lang3.StringEscapeUtils;
+
import com.vividsolutions.jts.util.Assert;
import com.vividsolutions.jump.feature.AttributeType;
import com.vividsolutions.jump.feature.Feature;
@@ -50,6 +52,7 @@
/**
* GMLWriter is a {@link JUMPWriter} specialized to output GML.
+ * TODO: use a proper XML framework, switch to XML1.1 which supports much
broader range of unicode characters
*
* <p>
* DataProperties for the JCSWriter write(featureSchema,DataProperties)
interface:<br>
@@ -213,40 +216,46 @@
* Ie. convert "<" to "<"
*@param s string to safe-ify
*/
- private static String safeXML(String s) {
+ private static String escapeXML(String s) {
if (s == null) return null;
- StringBuilder sb = new StringBuilder(s);
- char c;
-
- for (int t = 0; t < sb.length(); t++) {
- c = sb.charAt(t);
-
- if (c == '<') {
- sb.replace(t, t + 1, "<");
- }
-
- if (c == '>') {
- sb.replace(t, t + 1, ">");
- }
-
- if (c == '&') {
- sb.replace(t, t + 1, "&");
- }
-
- if (c == '\'') {
- sb.replace(t, t + 1, "'");
- }
-
- if (c == '"') {
- sb.replace(t, t + 1, """);
- }
-
- if ((int)c < 20 && c != '\t' && c != '\n' && c != '\r') {
- sb.replace(t, t + 1, "");
- }
- }
-
- return sb.toString();
+
+ // this should take care of really _all_ XML1.0 invalid chars
+ // see
https://commons.apache.org/proper/commons-lang/javadocs/api-3.5/org/apache/commons/lang3/StringEscapeUtils.html#escapeXml10-java.lang.String-
+ return StringEscapeUtils.escapeXml10(s);
+
+ // kept for reference
+// StringBuilder sb = new StringBuilder(s);
+// char c;
+//
+// for (int t = 0; t < sb.length(); t++) {
+// c = sb.charAt(t);
+//
+// if (c == '<') {
+// sb.replace(t, t + 1, "<");
+// }
+//
+// if (c == '>') {
+// sb.replace(t, t + 1, ">");
+// }
+//
+// if (c == '&') {
+// sb.replace(t, t + 1, "&");
+// }
+//
+// if (c == '\'') {
+// sb.replace(t, t + 1, "'");
+// }
+//
+// if (c == '"') {
+// sb.replace(t, t + 1, """);
+// }
+//
+// if ((int)c < 20 && c != '\t' && c != '\n' && c != '\r') {
+// sb.replace(t, t + 1, "");
+// }
+// }
+//
+// return sb.toString();
}
/**
@@ -296,7 +305,7 @@
result = toString(f, column);
//need to ensure that the output is XML okay
- result = safeXML(result);
+ result = escapeXML(result);
return result;
} else if (cmd.equalsIgnoreCase("geometry")) {
@@ -347,7 +356,7 @@
result = toString(f, column);
//need to ensure that the output is XML okay
- result = safeXML(result);
+ result = escapeXML(result);
if (result == null) writer.append(pre.replaceAll(">$","
xsi:nil=\"true\">"));
else writer.append(pre).append(result);
//writer.append(result);
------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, SlashDot.org! http://sdm.link/slashdot
_______________________________________________
Jump-pilot-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/jump-pilot-devel