]> gerrit.simantics Code Review - simantics/platform.git/commitdiff
Filter out CR in resource files generated by graph compiler. 26/326/2
authorHannu Niemistö <hannu.niemisto@semantum.fi>
Mon, 6 Feb 2017 11:01:11 +0000 (13:01 +0200)
committerHannu Niemistö <hannu.niemisto@semantum.fi>
Mon, 6 Feb 2017 11:05:54 +0000 (13:05 +0200)
Change-Id: I10e0dfe6a0726c001f63dd438c50256f4fc7cfe6

bundles/org.simantics.graph.compiler/src/org/simantics/graph/compiler/internal/resourceFiles/FilterCRWriter.java [new file with mode: 0644]
bundles/org.simantics.graph.compiler/src/org/simantics/graph/compiler/internal/resourceFiles/ResourceFile.java

diff --git a/bundles/org.simantics.graph.compiler/src/org/simantics/graph/compiler/internal/resourceFiles/FilterCRWriter.java b/bundles/org.simantics.graph.compiler/src/org/simantics/graph/compiler/internal/resourceFiles/FilterCRWriter.java
new file mode 100644 (file)
index 0000000..2e96675
--- /dev/null
@@ -0,0 +1,47 @@
+package org.simantics.graph.compiler.internal.resourceFiles;
+
+import java.io.FilterWriter;
+import java.io.IOException;
+import java.io.Writer;
+
+public class FilterCRWriter extends FilterWriter {
+
+    public FilterCRWriter(Writer out) {
+        super(out);
+    }
+    
+    @Override
+    public void write(int c) throws IOException {
+        if(c != '\r')
+            out.write(c);
+    }
+    
+    @Override
+    public void write(char[] cbuf, int off, int len) throws IOException {
+        int begin = 0;
+        for(int i=0;i<len;++i) {
+            if(cbuf[off+i] == '\r') {
+                if(i > begin)
+                    write(cbuf, off+begin, i-begin);
+                begin = i+1;
+            }
+        }
+        if(len > begin)
+            write(cbuf, off+begin, len-begin);
+    }
+    
+    @Override
+    public void write(String str, int off, int len) throws IOException {
+        int begin = 0;
+        for(int i=0;i<len;++i) {
+            if(str.charAt(off+i) == '\r') {
+                if(i > begin)
+                    write(str, off+begin, i-begin);
+                begin = i+1;
+            }
+        }
+        if(len > begin)
+            write(str, off+begin, len-begin);
+    }
+
+}
index c7e1f22b14ccf58b173ab2b323cad58a4955346b..d9d155037e1eadd3b624cca948bcd95394676afb 100644 (file)
@@ -6,6 +6,7 @@ import java.io.OutputStreamWriter;
 import java.io.StringWriter;
 import java.io.Writer;
 import java.net.URL;
+import java.nio.charset.Charset;
 import java.util.Arrays;
 import java.util.Collection;
 import java.util.Collections;
@@ -19,6 +20,8 @@ import freemarker.template.DefaultObjectWrapper;
 import freemarker.template.Template;
 
 public class ResourceFile implements IResourceFile {
+    private final static Charset UTF8 = Charset.forName("UTF-8");
+    
        String packageName;
        String className;
        List<ResourceRef> resources;
@@ -88,8 +91,8 @@ public class ResourceFile implements IResourceFile {
        @Override
        public InputStream getContent() {
                StringWriter writer = new StringWriter();
-               write(writer);
-               return new ByteArrayInputStream(writer.toString().replace("\r\n", "\n").getBytes());
+               write(new FilterCRWriter(writer));
+               return new ByteArrayInputStream(writer.toString().getBytes(UTF8));
        }
        
        /* (non-Javadoc)