Author: vsiveton
Date: Tue Sep  9 20:40:59 2008
New Revision: 693693

URL: http://svn.apache.org/viewvc?rev=693693&view=rev
Log:
o i/o encoding issue

Modified:
    maven/doxia/doxia-tools/trunk/doxia-converter/pom.xml
    
maven/doxia/doxia-tools/trunk/doxia-converter/src/main/java/org/apache/maven/doxia/DefaultConverter.java

Modified: maven/doxia/doxia-tools/trunk/doxia-converter/pom.xml
URL: 
http://svn.apache.org/viewvc/maven/doxia/doxia-tools/trunk/doxia-converter/pom.xml?rev=693693&r1=693692&r2=693693&view=diff
==============================================================================
--- maven/doxia/doxia-tools/trunk/doxia-converter/pom.xml (original)
+++ maven/doxia/doxia-tools/trunk/doxia-converter/pom.xml Tue Sep  9 20:40:59 
2008
@@ -117,6 +117,11 @@
       <artifactId>commons-lang</artifactId>
       <version>2.3</version>
     </dependency>
+    <dependency>
+      <groupId>com.ibm.icu</groupId>
+      <artifactId>icu4j</artifactId>
+      <version>3.8</version>
+    </dependency>
 
     <!-- Plexus -->
     <dependency>

Modified: 
maven/doxia/doxia-tools/trunk/doxia-converter/src/main/java/org/apache/maven/doxia/DefaultConverter.java
URL: 
http://svn.apache.org/viewvc/maven/doxia/doxia-tools/trunk/doxia-converter/src/main/java/org/apache/maven/doxia/DefaultConverter.java?rev=693693&r1=693692&r2=693693&view=diff
==============================================================================
--- 
maven/doxia/doxia-tools/trunk/doxia-converter/src/main/java/org/apache/maven/doxia/DefaultConverter.java
 (original)
+++ 
maven/doxia/doxia-tools/trunk/doxia-converter/src/main/java/org/apache/maven/doxia/DefaultConverter.java
 Tue Sep  9 20:40:59 2008
@@ -19,11 +19,10 @@
 package org.apache.maven.doxia;
 
 import java.io.File;
-import java.io.FileNotFoundException;
-import java.io.FileReader;
-import java.io.FileWriter;
+import java.io.FileInputStream;
 import java.io.IOException;
 import java.io.Reader;
+import java.io.StringWriter;
 import java.io.Writer;
 import java.util.HashMap;
 import java.util.Iterator;
@@ -64,8 +63,13 @@
 import 
org.codehaus.plexus.component.repository.exception.ComponentLookupException;
 import org.codehaus.plexus.util.FileUtils;
 import org.codehaus.plexus.util.IOUtil;
+import org.codehaus.plexus.util.ReaderFactory;
 import org.codehaus.plexus.util.SelectorUtils;
 import org.codehaus.plexus.util.StringUtils;
+import org.codehaus.plexus.util.WriterFactory;
+
+import com.ibm.icu.text.CharsetDetector;
+import com.ibm.icu.text.CharsetMatch;
 
 /**
  * Default implementation of <code>Converter</code>
@@ -456,10 +460,37 @@
             }
         }
 
+        String detectedInputEncoding;
+        Reader reader;
+        try
+        {
+            detectedInputEncoding = getEncoding( inputFile );
+
+            if ( detectedInputEncoding != null )
+            {
+                reader = ReaderFactory.newReader( inputFile, 
detectedInputEncoding );
+            }
+            else
+            {
+                reader = ReaderFactory.newPlatformReader( inputFile );
+            }
+        }
+        catch ( IOException e )
+        {
+            throw new ConverterException( "IOException: " + e.getMessage(), e 
);
+        }
+
         Writer writer;
         try
         {
-            writer = new FileWriter( outputFile );
+            if ( parser.getType() == Parser.XML_TYPE )
+            {
+                writer = WriterFactory.newXmlWriter( outputFile );
+            }
+            else
+            {
+                writer = WriterFactory.newPlatformWriter( outputFile );
+            }
         }
         catch ( IOException e )
         {
@@ -473,16 +504,6 @@
             getLog().debug( "Sink used: " + sink.getClass().getName() );
         }
 
-        Reader reader;
-        try
-        {
-            reader = new FileReader( inputFile );
-        }
-        catch ( FileNotFoundException e )
-        {
-            throw new ConverterException( "IOException: " + e.getMessage(), e 
);
-        }
-
         try
         {
             parser.parse( reader, sink );
@@ -526,4 +547,33 @@
     {
         plexus.dispose();
     }
+
+    /**
+     * @param f not null and should exist
+     * @return the detected encoding from f
+     * @throws IOException if any
+     */
+    private String getEncoding( File f )
+        throws IOException
+    {
+        FileInputStream is = null;
+        try
+        {
+            is = new FileInputStream( f );
+
+            StringWriter w = new StringWriter();
+            IOUtil.copy( is, w );
+            String content = w.toString();
+
+            CharsetDetector detector = new CharsetDetector();
+            detector.setText( content.getBytes() );
+            CharsetMatch match = detector.detect();
+
+            return match.getName().toUpperCase( Locale.ENGLISH );
+        }
+        finally
+        {
+            IOUtil.close( is );
+        }
+    }
 }


Reply via email to