You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@xmlbeans.apache.org by ra...@apache.org on 2005/05/13 22:23:15 UTC

svn commit: r170080 - in /xmlbeans/trunk/src/typeimpl/org/apache/xmlbeans/impl: schema/SchemaTypeCodePrinter.java util/FilerImpl.java

Author: radup
Date: Fri May 13 13:23:13 2005
New Revision: 170080

URL: http://svn.apache.org/viewcvs?rev=170080&view=rev
Log:
Fix for XMLBEANS-111.

Modified:
    xmlbeans/trunk/src/typeimpl/org/apache/xmlbeans/impl/schema/SchemaTypeCodePrinter.java
    xmlbeans/trunk/src/typeimpl/org/apache/xmlbeans/impl/util/FilerImpl.java

Modified: xmlbeans/trunk/src/typeimpl/org/apache/xmlbeans/impl/schema/SchemaTypeCodePrinter.java
URL: http://svn.apache.org/viewcvs/xmlbeans/trunk/src/typeimpl/org/apache/xmlbeans/impl/schema/SchemaTypeCodePrinter.java?rev=170080&r1=170079&r2=170080&view=diff
==============================================================================
--- xmlbeans/trunk/src/typeimpl/org/apache/xmlbeans/impl/schema/SchemaTypeCodePrinter.java (original)
+++ xmlbeans/trunk/src/typeimpl/org/apache/xmlbeans/impl/schema/SchemaTypeCodePrinter.java Fri May 13 13:23:13 2005
@@ -17,6 +17,9 @@
 
 import java.io.Writer;
 import java.io.IOException;
+import java.nio.charset.CharacterCodingException;
+import java.nio.charset.Charset;
+import java.nio.charset.CharsetEncoder;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Map;
@@ -171,11 +174,58 @@
             indent = MAX_SPACES.length();
         
         _writer.write(MAX_SPACES.substring(0, indent));
-        _writer.write(s);
+        try
+        {
+            _writer.write(s);
+        }
+        catch (CharacterCodingException cce)
+        {
+            _writer.write(makeSafe(s));
+        }
         _writer.write(LINE_SEPARATOR);
         
         // System.out.print(MAX_SPACES.substring(0, indent));
         // System.out.println(s);
+    }
+
+    private static String makeSafe(String s)
+    {
+        Charset charset = Charset.forName(System.getProperty("file.encoding"));
+        if (charset == null)
+            throw new IllegalStateException("Default character set is null!");
+        CharsetEncoder cEncoder = charset.newEncoder();
+        StringBuffer result = new StringBuffer();
+        int i;
+        for (i = 0; i < s.length(); i++)
+        {
+            char c = s.charAt(i);
+            if (!cEncoder.canEncode(c))
+                break;
+        }
+        for (; i < s.length(); i++)
+        {
+            char c = s.charAt(i);
+            if (cEncoder.canEncode(c))
+                result.append(c);
+            else
+            {
+                String hexValue = Integer.toHexString((int) c);
+                switch (hexValue.length())
+                {
+                case 1:
+                    result.append("\\u000").append(hexValue); break;
+                case 2:
+                    result.append("\\u00").append(hexValue); break;
+                case 3:
+                    result.append("\\u0").append(hexValue); break;
+                case 4:
+                    result.append("\\u").append(hexValue); break;
+                default:
+                    throw new IllegalStateException();
+                }
+            }
+        }
+        return result.toString();
     }
 
     public void printType(Writer writer, SchemaType sType) throws IOException

Modified: xmlbeans/trunk/src/typeimpl/org/apache/xmlbeans/impl/util/FilerImpl.java
URL: http://svn.apache.org/viewcvs/xmlbeans/trunk/src/typeimpl/org/apache/xmlbeans/impl/util/FilerImpl.java?rev=170080&r1=170079&r2=170080&view=diff
==============================================================================
--- xmlbeans/trunk/src/typeimpl/org/apache/xmlbeans/impl/util/FilerImpl.java (original)
+++ xmlbeans/trunk/src/typeimpl/org/apache/xmlbeans/impl/util/FilerImpl.java Fri May 13 13:23:13 2005
@@ -17,15 +17,19 @@
 
 import org.apache.xmlbeans.Filer;
 
-import java.io.OutputStream;
 import java.io.IOException;
 import java.io.File;
 import java.io.Writer;
 import java.io.FileReader;
+import java.io.FileOutputStream;
 import java.io.FileWriter;
+import java.io.OutputStream;
+import java.io.OutputStreamWriter;
 import java.io.StringReader;
 import java.io.StringWriter;
-import java.io.FileOutputStream;
+import java.nio.charset.Charset;
+import java.nio.charset.CharsetEncoder;
+import java.nio.charset.CodingErrorAction;
 import java.util.Set;
 import java.util.HashSet;
 import java.util.List;
@@ -45,6 +49,18 @@
     private List sourceFiles;
     private boolean incrSrcGen;
     private Set seenTypes;
+    private static final Charset CHARSET;
+
+    static
+    {
+        Charset temp = null;
+        try
+        {
+            temp = Charset.forName(System.getProperty("file.encoding"));
+        }
+        catch (Exception e) {}
+        CHARSET = temp;
+    }
 
     public FilerImpl(File classdir, File srcdir, Repackager repackager, boolean verbose, boolean incrSrcGen)
     {
@@ -114,7 +130,7 @@
         else
         {
             return repackager == null ?
-                (Writer) new FileWriter( sourcefile ) :
+                (Writer) writerForFile( sourcefile ) :
                 (Writer) new RepackagingWriter( sourcefile, repackager );
         }
     }
@@ -129,6 +145,17 @@
         return repackager;
     }
 
+    private static final Writer writerForFile(File f) throws IOException
+    {
+        if (CHARSET == null)
+            return new FileWriter(f);
+
+        FileOutputStream fileStream = new FileOutputStream(f);
+        CharsetEncoder ce = CHARSET.newEncoder();
+        ce.onUnmappableCharacter(CodingErrorAction.REPORT);
+        return new OutputStreamWriter(fileStream, ce);
+    }
+
     static class IncrFileWriter extends StringWriter
     {
         private File _file;
@@ -168,7 +195,7 @@
             {
                 // Diffs encountered, replace the file on disk with text from
                 // the buffer
-                FileWriter fw = new FileWriter(_file);
+                Writer fw = writerForFile(_file);
                 try
                 {   fw.write(str); }
                 finally
@@ -191,7 +218,7 @@
         {
             super.close();
 
-            FileWriter fw = new FileWriter( _file );
+            Writer fw = writerForFile( _file );
             try
             { fw.write( _repackager.repackage( getBuffer() ).toString() ); }
             finally



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@xmlbeans.apache.org
For additional commands, e-mail: commits-help@xmlbeans.apache.org