]> git.argeo.org Git - gpl/argeo-slc.git/blobdiff - runtime/org.argeo.slc.specs/src/main/java/org/argeo/slc/process/SlcExecutionStep.java
Deal with special characters in logs
[gpl/argeo-slc.git] / runtime / org.argeo.slc.specs / src / main / java / org / argeo / slc / process / SlcExecutionStep.java
index 5c54b2c6d9c5a0c8498a918fdeb4553917e0cad9..f3319bbe635fe3c33818f44a06e49c13234df3c4 100644 (file)
@@ -1,14 +1,27 @@
+/*\r
+ * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>\r
+ *\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ *         http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ */\r
+\r
 package org.argeo.slc.process;\r
 \r
-import java.io.IOException;\r
-import java.io.StringReader;\r
 import java.util.ArrayList;\r
 import java.util.Date;\r
 import java.util.List;\r
+import java.util.StringTokenizer;\r
 import java.util.UUID;\r
 \r
-import org.apache.commons.io.IOUtils;\r
-\r
 public class SlcExecutionStep {\r
        public final static String TYPE_START = "START";\r
        public final static String TYPE_END = "END";\r
@@ -68,17 +81,41 @@ public class SlcExecutionStep {
                this.logLines = logLines;\r
        }\r
 \r
-       @SuppressWarnings(value = { "unchecked" })\r
        public void addLog(String log) {\r
                if (log == null)\r
                        return;\r
 \r
-               try {\r
-                       List<String> lines = IOUtils.readLines(new StringReader(log));\r
-                       logLines.addAll(lines);\r
-               } catch (IOException e) {\r
-                       throw new RuntimeException("Cannot add log", e);\r
+               StringTokenizer st = new StringTokenizer(log, "\n");\r
+               while (st.hasMoreTokens())\r
+                       logLines.add(removeNonXmlChars(st.nextToken()));\r
+       }\r
+\r
+       /**\r
+        * Removes non XML compliant characters (from\r
+        * http://stackoverflow.com/questions\r
+        * /20762/how-do-you-remove-invalid-hexadecimal\r
+        * -characters-from-an-xml-based-data-source-pr)\r
+        */\r
+       private static String removeNonXmlChars(String inString) {\r
+               if (inString == null)\r
+                       return null;\r
+\r
+               StringBuilder newString = new StringBuilder();\r
+               char ch;\r
+\r
+               for (int i = 0; i < inString.length(); i++) {\r
+\r
+                       ch = inString.charAt(i);\r
+                       // remove any characters outside the valid UTF-8 range as well as\r
+                       // all control characters\r
+                       // except tabs and new lines\r
+                       if ((ch < 0x00FD && ch > 0x001F) || ch == '\t' || ch == '\n'\r
+                                       || ch == '\r') {\r
+                               newString.append(ch);\r
+                       }\r
                }\r
+               return newString.toString();\r
+\r
        }\r
 \r
        @Override\r