maven-doxia-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From vsive...@apache.org
Subject svn commit: r697523 [1/2] - in /maven/doxia/doxia-tools/trunk/doxia-converter: ./ src/main/java/org/apache/maven/doxia/ src/main/java/org/apache/maven/doxia/cli/ src/main/java/org/apache/maven/doxia/util/ src/main/java/org/apache/maven/doxia/wrapper/ s...
Date Sun, 21 Sep 2008 14:29:09 GMT
Author: vsiveton
Date: Sun Sep 21 07:29:09 2008
New Revision: 697523

URL: http://svn.apache.org/viewvc?rev=697523&view=rev
Log:
o refactoring to better autodetect encoding and format for input files
o added test cases
o updated doc

Added:
    maven/doxia/doxia-tools/trunk/doxia-converter/src/main/java/org/apache/maven/doxia/util/ConverterUtil.java   (with props)
    maven/doxia/doxia-tools/trunk/doxia-converter/src/test/resources/unit/apt/test.unknown
Removed:
    maven/doxia/doxia-tools/trunk/doxia-converter/src/main/java/org/apache/maven/doxia/util/FormatUtils.java
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
    maven/doxia/doxia-tools/trunk/doxia-converter/src/main/java/org/apache/maven/doxia/UnsupportedFormatException.java
    maven/doxia/doxia-tools/trunk/doxia-converter/src/main/java/org/apache/maven/doxia/cli/CLIManager.java
    maven/doxia/doxia-tools/trunk/doxia-converter/src/main/java/org/apache/maven/doxia/cli/ConverterCli.java
    maven/doxia/doxia-tools/trunk/doxia-converter/src/main/java/org/apache/maven/doxia/wrapper/AbstractFileWrapper.java
    maven/doxia/doxia-tools/trunk/doxia-converter/src/main/java/org/apache/maven/doxia/wrapper/AbstractWrapper.java
    maven/doxia/doxia-tools/trunk/doxia-converter/src/main/java/org/apache/maven/doxia/wrapper/InputFileWrapper.java
    maven/doxia/doxia-tools/trunk/doxia-converter/src/main/java/org/apache/maven/doxia/wrapper/InputReaderWrapper.java
    maven/doxia/doxia-tools/trunk/doxia-converter/src/main/java/org/apache/maven/doxia/wrapper/OutputFileWrapper.java
    maven/doxia/doxia-tools/trunk/doxia-converter/src/main/java/org/apache/maven/doxia/wrapper/OutputWriterWrapper.java
    maven/doxia/doxia-tools/trunk/doxia-converter/src/main/resources/META-INF/plexus/components.xml
    maven/doxia/doxia-tools/trunk/doxia-converter/src/site/apt/usage.apt
    maven/doxia/doxia-tools/trunk/doxia-converter/src/test/java/org/apache/maven/doxia/ConverterTest.java
    maven/doxia/doxia-tools/trunk/doxia-converter/src/test/resources/unit/confluence/test.confluence

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=697523&r1=697522&r2=697523&view=diff
==============================================================================
--- maven/doxia/doxia-tools/trunk/doxia-converter/pom.xml (original)
+++ maven/doxia/doxia-tools/trunk/doxia-converter/pom.xml Sun Sep 21 07:29:09 2008
@@ -123,6 +123,14 @@
       <groupId>org.codehaus.plexus</groupId>
       <artifactId>plexus-utils</artifactId>
     </dependency>
+
+    <!-- Test -->
+    <dependency>
+      <groupId>junit-addons</groupId>
+      <artifactId>junit-addons</artifactId>
+      <version>1.4</version>
+      <scope>test</scope>
+    </dependency>
   </dependencies>
 
   <properties>

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=697523&r1=697522&r2=697523&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 Sun Sep 21 07:29:09 2008
@@ -18,22 +18,29 @@
  */
 package org.apache.maven.doxia;
 
+import java.io.BufferedInputStream;
 import java.io.File;
 import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.FileReader;
 import java.io.IOException;
+import java.io.InputStream;
 import java.io.Reader;
 import java.io.Writer;
 import java.util.HashMap;
 import java.util.Iterator;
 import java.util.List;
+import java.util.Locale;
 import java.util.Map;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
 
 import org.apache.maven.doxia.logging.Log;
 import org.apache.maven.doxia.logging.SystemStreamLog;
 import org.apache.maven.doxia.parser.ParseException;
 import org.apache.maven.doxia.parser.Parser;
 import org.apache.maven.doxia.sink.Sink;
-import org.apache.maven.doxia.sink.SinkFactory;
+import org.apache.maven.doxia.util.ConverterUtil;
 import org.apache.maven.doxia.wrapper.InputFileWrapper;
 import org.apache.maven.doxia.wrapper.InputReaderWrapper;
 import org.apache.maven.doxia.wrapper.OutputFileWrapper;
@@ -50,6 +57,13 @@
 import org.codehaus.plexus.util.SelectorUtils;
 import org.codehaus.plexus.util.StringUtils;
 import org.codehaus.plexus.util.WriterFactory;
+import org.codehaus.plexus.util.xml.XmlStreamReader;
+import org.codehaus.plexus.util.xml.pull.MXParser;
+import org.codehaus.plexus.util.xml.pull.XmlPullParser;
+import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
+
+import com.ibm.icu.text.CharsetDetector;
+import com.ibm.icu.text.CharsetMatch;
 
 /**
  * Default implementation of <code>Converter</code>
@@ -162,25 +176,9 @@
 
         try
         {
-            Parser parser;
-            try
-            {
-                parser = getParser( plexus, input.getFormat() );
-                parser.enableLogging( log );
-            }
-            catch ( ComponentLookupException e )
-            {
-                throw new ConverterException( "ComponentLookupException: " + e.getMessage(), e );
-            }
-
-            if ( getLog().isDebugEnabled() )
-            {
-                getLog().debug( "Parser used: " + parser.getClass().getName() );
-            }
-
             if ( input.getFile().isFile() )
             {
-                parse( input.getFile(), input.getEncoding(), output, parser );
+                parse( input.getFile(), input.getEncoding(), input.getFormat(), output );
             }
             else
             {
@@ -195,12 +193,16 @@
                 {
                     throw new ConverterException( "IOException: " + e.getMessage(), e );
                 }
+                catch ( IllegalStateException e )
+                {
+                    throw new ConverterException( "IllegalStateException: " + e.getMessage(), e );
+                }
 
                 for ( Iterator it = files.iterator(); it.hasNext(); )
                 {
                     File f = (File) it.next();
 
-                    parse( f, input.getEncoding(), output, parser );
+                    parse( f, input.getEncoding(), input.getFormat(), output );
                 }
             }
         }
@@ -237,7 +239,7 @@
             Parser parser;
             try
             {
-                parser = getParser( plexus, input.getFormat() );
+                parser = ConverterUtil.getParser( plexus, input.getFormat(), SUPPORTED_FROM_FORMAT );
                 parser.enableLogging( log );
             }
             catch ( ComponentLookupException e )
@@ -253,7 +255,7 @@
             Sink sink;
             try
             {
-                sink = getSink( plexus, output.getFormat(), output.getWriter() );
+                sink = ConverterUtil.getSink( plexus, output.getFormat(), output.getWriter(), SUPPORTED_TO_FORMAT );
             }
             catch ( ComponentLookupException e )
             {
@@ -293,96 +295,51 @@
     // ----------------------------------------------------------------------
 
     /**
-     * @param plexus
-     * @param format
-     * @return an instance of <code>Parser</code> depending the format.
-     * @throws ComponentLookupException if any
-     * @throws IllegalArgumentException if any
+     * @param inputFile a not null existing file.
+     * @param inputEncoding a not null supported encoding or {@link InputFileWrapper#AUTO_ENCODING}
+     * @param inputFormat  a not null supported format or {@link InputFileWrapper#AUTO_FORMAT}
+     * @param output not null OutputFileWrapper object
+     * @throws ConverterException if any
+     * @throws UnsupportedFormatException if any
      */
-    private static Parser getParser( PlexusContainer plexus, String format )
-        throws ComponentLookupException
+    private void parse( File inputFile, String inputEncoding, String inputFormat, OutputFileWrapper output )
+        throws ConverterException, UnsupportedFormatException
     {
-        Parser parser = null;
-        if ( format.equals( APT_PARSER ) )
-        {
-            parser = (Parser) plexus.lookup( Parser.ROLE, "apt" );
-        }
-        else if ( format.equals( CONFLUENCE_PARSER ) )
-        {
-            parser = (Parser) plexus.lookup( Parser.ROLE, "confluence" );
-        }
-        else if ( format.equals( DOCBOOK_PARSER ) )
-        {
-            parser = (Parser) plexus.lookup( Parser.ROLE, "doc-book" );
-        }
-        else if ( format.equals( FML_PARSER ) )
-        {
-            parser = (Parser) plexus.lookup( Parser.ROLE, "fml" );
-        }
-        else if ( format.equals( TWIKI_PARSER ) )
-        {
-            parser = (Parser) plexus.lookup( Parser.ROLE, "twiki" );
-        }
-        else if ( format.equals( XDOC_PARSER ) )
-        {
-            parser = (Parser) plexus.lookup( Parser.ROLE, "xdoc" );
-        }
-        else if ( format.equals( XHTML_PARSER ) )
+        if ( getLog().isDebugEnabled() )
         {
-            parser = (Parser) plexus.lookup( Parser.ROLE, "xhtml" );
+            getLog().debug(
+                            "Parsing file from '" + inputFile.getAbsolutePath() + "' with the encoding '"
+                                + inputEncoding + "' to '" + output.getFile().getAbsolutePath()
+                                + "' with the encoding '" + output.getEncoding() + "'" );
         }
 
-        if ( parser == null )
+        if ( inputEncoding.equals( InputFileWrapper.AUTO_ENCODING ) )
         {
-            throw new IllegalArgumentException( "Parser not found for: " + format );
+            inputEncoding = autoDetectEncoding( inputFile );
+            if ( getLog().isDebugEnabled() )
+            {
+                getLog().debug( "Auto detect encoding: " + inputEncoding );
+            }
         }
 
-        return parser;
-    }
-
-    /**
-     * @param format
-     * @param writer
-     * @return an instance of <code>Sink</code> depending the format.
-        throws ComponentLookupException if any
-     * @throws IllegalArgumentException if any
-     */
-    private static Sink getSink( PlexusContainer plexus, String format, Writer writer )
-        throws ComponentLookupException
-    {
-        SinkFactory factory = (SinkFactory) plexus.lookup( SinkFactory.ROLE, format );
-
-        if ( factory == null )
+        if ( inputFormat.equals( InputFileWrapper.AUTO_FORMAT ) )
         {
-            throw new IllegalArgumentException( "SinkFactory not found for: " + format );
+            inputFormat = autoDetectFormat( inputFile, inputEncoding );
+            if ( getLog().isDebugEnabled() )
+            {
+                getLog().debug( "Auto detect input format: " + inputFormat );
+            }
         }
 
-        Sink sink = factory.createSink( writer );
-
-        if ( sink == null )
+        Parser parser;
+        try
         {
-            throw new IllegalArgumentException( "Sink was not instanciated: " + format );
+            parser = ConverterUtil.getParser( plexus, inputFormat, SUPPORTED_FROM_FORMAT );
+            parser.enableLogging( log );
         }
-
-        return sink;
-    }
-
-    /**
-     * @param inputFile not null
-     * @param inputEncoding could be null
-     * @param output not null
-     * @param parser not null
-     * @throws ConverterException if any
-     */
-    private void parse( File inputFile, String inputEncoding, OutputFileWrapper output, Parser parser )
-        throws ConverterException
-    {
-        if ( getLog().isDebugEnabled() )
+        catch ( ComponentLookupException e )
         {
-            getLog().debug(
-                            "Parsing file from '" + inputFile.getAbsolutePath() + "' with the encoding '"
-                                + inputEncoding + "' to '" + output.getFile().getAbsolutePath()
-                                + "' with the encoding '" + output.getEncoding() + "'" );
+            throw new ConverterException( "ComponentLookupException: " + e.getMessage(), e );
         }
 
         File outputFile;
@@ -440,7 +397,8 @@
         try
         {
             String outputEncoding;
-            if ( StringUtils.isEmpty( output.getEncoding() ) )
+            if ( StringUtils.isEmpty( output.getEncoding() )
+                || output.getEncoding().equals( OutputFileWrapper.AUTO_ENCODING ) )
             {
                 outputEncoding = inputEncoding;
             }
@@ -459,7 +417,7 @@
         Sink sink;
         try
         {
-            sink = getSink( plexus, output.getFormat(), writer );
+            sink = ConverterUtil.getSink( plexus, output.getFormat(), writer, SUPPORTED_TO_FORMAT );
         }
         catch ( ComponentLookupException e )
         {
@@ -525,4 +483,225 @@
         plexus.dispose();
         plexus = null;
     }
+
+    /**
+     * @param f not null file
+     * @return the detected encoding for f or <code>null</code> if not able to detect it.
+     * @throws IllegalArgumentException if f is not a file.
+     * @throws UnsupportedOperationException if could not detect the file encoding.
+     * @see {@link XmlStreamReader#getEncoding()} for xml files
+     * @see {@link CharsetDetector#detect()} for text files
+     */
+    private static String autoDetectEncoding( File f )
+    {
+        if ( !f.isFile() )
+        {
+            throw new IllegalArgumentException( "The file '" + f.getAbsolutePath()
+                + "' is not a file, could not detect encoding." );
+        }
+
+        Reader reader = null;
+        InputStream is = null;
+        try
+        {
+            is = new BufferedInputStream( new FileInputStream( f ) );
+
+            if ( isXML( f ) )
+            {
+                reader = ReaderFactory.newXmlReader( f );
+                return ( (XmlStreamReader) reader ).getEncoding();
+            }
+
+            CharsetDetector detector = new CharsetDetector();
+            detector.setText( is );
+            CharsetMatch match = detector.detect();
+
+            return match.getName().toUpperCase( Locale.ENGLISH );
+        }
+        catch ( IOException e )
+        {
+            // nop
+        }
+        finally
+        {
+            IOUtil.close( reader );
+            IOUtil.close( is );
+        }
+
+        StringBuffer msg = new StringBuffer();
+        msg.append( "Could not detect the encoding for file: " );
+        msg.append( f.getAbsolutePath() );
+        msg.append( "\n Specify explicitly the encoding." );
+        throw new UnsupportedOperationException( msg.toString() );
+    }
+
+    /**
+     * Determines if a given File shall be handled as XML.
+     *
+     * @param f not null file
+     * @return <code>true</code> if the given file has XML content, <code>fale</code> otherwise.
+     */
+    private static boolean isXML( File f )
+    {
+        if ( !f.isFile() )
+        {
+            throw new IllegalArgumentException( "The file '" + f.getAbsolutePath() + "' is not a file." );
+        }
+
+        try
+        {
+            XmlPullParser parser = new MXParser();
+            parser.setInput( new FileReader( f ) );
+            parser.nextToken();
+
+            return true;
+        }
+        catch ( Exception e )
+        {
+            return false;
+        }
+    }
+
+    /**
+     * Auto detect Doxia format for the given file depending:
+     * <ul>
+     * <li>the file name for TextMarkup based Doxia files</li>
+     * <li>the file content for XMLMarkup based Doxia files</li>
+     * </ul>
+     *
+     * @param f not null file
+     * @param encoding a not null encoding.
+     * @return the detected encoding from f.
+     * @throws IllegalArgumentException if f is not a file.
+     * @throws UnsupportedOperationException if could not detect the Doxia format.
+     */
+    private static String autoDetectFormat( File f, String encoding )
+    {
+        if ( !f.isFile() )
+        {
+            throw new IllegalArgumentException( "The file '" + f.getAbsolutePath()
+                + "' is not a file, could not detect format." );
+        }
+
+        for ( int i = 0; i < SUPPORTED_FROM_FORMAT.length; i++ )
+        {
+            // Handle Doxia text files
+            if ( SUPPORTED_FROM_FORMAT[i].equalsIgnoreCase( APT_PARSER ) && isDoxiaFormat( f, SUPPORTED_FROM_FORMAT[i] ) )
+            {
+                return SUPPORTED_FROM_FORMAT[i];
+            }
+            else if ( SUPPORTED_FROM_FORMAT[i].equalsIgnoreCase( CONFLUENCE_PARSER )
+                && isDoxiaFormat( f, SUPPORTED_FROM_FORMAT[i] ) )
+            {
+                return SUPPORTED_FROM_FORMAT[i];
+            }
+            else if ( SUPPORTED_FROM_FORMAT[i].equalsIgnoreCase( TWIKI_PARSER )
+                && isDoxiaFormat( f, SUPPORTED_FROM_FORMAT[i] ) )
+            {
+                return SUPPORTED_FROM_FORMAT[i];
+            }
+
+            // Handle Doxia xml files
+            String firstTag = getFirstTag( f );
+            if ( firstTag == null )
+            {
+                continue;
+            }
+            else if ( firstTag.equalsIgnoreCase( "article" )
+                && SUPPORTED_FROM_FORMAT[i].equalsIgnoreCase( DOCBOOK_PARSER ) )
+            {
+                return SUPPORTED_FROM_FORMAT[i];
+            }
+            else if ( firstTag.equalsIgnoreCase( "faqs" )
+                && SUPPORTED_FROM_FORMAT[i].equalsIgnoreCase( FML_PARSER ) )
+            {
+                return SUPPORTED_FROM_FORMAT[i];
+            }
+            else if ( firstTag.equalsIgnoreCase( "document" )
+                && SUPPORTED_FROM_FORMAT[i].equalsIgnoreCase( XDOC_PARSER ) )
+            {
+                return SUPPORTED_FROM_FORMAT[i];
+            }
+            else if ( firstTag.equalsIgnoreCase( "html" )
+                && SUPPORTED_FROM_FORMAT[i].equalsIgnoreCase( XHTML_PARSER ) )
+            {
+                return SUPPORTED_FROM_FORMAT[i];
+            }
+        }
+
+        StringBuffer msg = new StringBuffer();
+        msg.append( "Could not detect the Doxia format for file: " );
+        msg.append( f.getAbsolutePath() );
+        msg.append( "\n Specify explicitly the Doxia format." );
+        throw new UnsupportedOperationException( msg.toString() );
+    }
+
+    /**
+     * @param f not null
+     * @param format could be null
+     * @return <code>true</code> if the file name computes the format.
+     */
+    private static boolean isDoxiaFormat( File f, String format )
+    {
+        if ( f == null )
+        {
+            throw new IllegalArgumentException( "f is required." );
+        }
+
+        Pattern pattern = Pattern.compile( "(.*?)\\." + format.toLowerCase( Locale.ENGLISH ) + "$" );
+        Matcher matcher = pattern.matcher( f.getName().toLowerCase( Locale.ENGLISH ) );
+
+        return matcher.matches();
+    }
+
+    /**
+     * @param xmlFile not null and should be a file.
+     * @return the first tag name if found, <code>null</code> in other case.
+     */
+    private static String getFirstTag( File xmlFile )
+    {
+        if ( xmlFile == null )
+        {
+            throw new IllegalArgumentException( "f is required." );
+        }
+        if ( !xmlFile.isFile() )
+        {
+            throw new IllegalArgumentException( "The file '" + xmlFile.getAbsolutePath() + "' is not a file." );
+        }
+
+        Reader reader = null;
+        try
+        {
+            reader = ReaderFactory.newXmlReader( xmlFile );
+            XmlPullParser parser = new MXParser();
+            parser.setInput( reader );
+            int eventType = parser.getEventType();
+            while ( eventType != XmlPullParser.END_DOCUMENT )
+            {
+                if ( eventType == XmlPullParser.START_TAG )
+                {
+                    return parser.getName();
+                }
+                eventType = parser.nextToken();
+            }
+        }
+        catch ( FileNotFoundException e )
+        {
+            return null;
+        }
+        catch ( XmlPullParserException e )
+        {
+            return null;
+        }
+        catch ( IOException e )
+        {
+            return null;
+        }
+        finally
+        {
+            IOUtil.close( reader );
+        }
+
+        return null;
+    }
 }

Modified: maven/doxia/doxia-tools/trunk/doxia-converter/src/main/java/org/apache/maven/doxia/UnsupportedFormatException.java
URL: http://svn.apache.org/viewvc/maven/doxia/doxia-tools/trunk/doxia-converter/src/main/java/org/apache/maven/doxia/UnsupportedFormatException.java?rev=697523&r1=697522&r2=697523&view=diff
==============================================================================
--- maven/doxia/doxia-tools/trunk/doxia-converter/src/main/java/org/apache/maven/doxia/UnsupportedFormatException.java (original)
+++ maven/doxia/doxia-tools/trunk/doxia-converter/src/main/java/org/apache/maven/doxia/UnsupportedFormatException.java Sun Sep 21 07:29:09 2008
@@ -1,7 +1,5 @@
 package org.apache.maven.doxia;
 
-import org.codehaus.plexus.util.StringUtils;
-
 /*
  * Licensed to the Apache Software Foundation (ASF) under one
  * or more contributor license agreements.  See the NOTICE file
@@ -21,6 +19,8 @@
  * under the License.
  */
 
+import org.codehaus.plexus.util.StringUtils;
+
 /**
  * Wrap an exception that occurs if a format is not supported.
  *
@@ -37,8 +37,8 @@
      * Constructs an UnsupportedFormatException with the specified
      * detail message.
      *
-     * @param format the unsupported format
-     * @param supportedFormat the supported formats
+     * @param format the unsupported format, not null
+     * @param supportedFormat the supported formats, not null
      */
     public UnsupportedFormatException( String format, String[] supportedFormat )
     {
@@ -50,8 +50,8 @@
      * Construct a new UnsupportedFormatException with the specified
      * detail message and cause.
      *
-     * @param format the unsupported format
-     * @param supportedFormat the supported formats
+     * @param format the unsupported format, not null
+     * @param supportedFormat the supported formats, not null
      * This can later be retrieved by the Throwable.getMessage() method.
      * @param cause the cause. This can be retrieved later by the
      * Throwable.getCause() method. (A null value is permitted, and indicates

Modified: maven/doxia/doxia-tools/trunk/doxia-converter/src/main/java/org/apache/maven/doxia/cli/CLIManager.java
URL: http://svn.apache.org/viewvc/maven/doxia/doxia-tools/trunk/doxia-converter/src/main/java/org/apache/maven/doxia/cli/CLIManager.java?rev=697523&r1=697522&r2=697523&view=diff
==============================================================================
--- maven/doxia/doxia-tools/trunk/doxia-converter/src/main/java/org/apache/maven/doxia/cli/CLIManager.java (original)
+++ maven/doxia/doxia-tools/trunk/doxia-converter/src/main/java/org/apache/maven/doxia/cli/CLIManager.java Sun Sep 21 07:29:09 2008
@@ -31,6 +31,8 @@
 import org.apache.maven.doxia.DefaultConverter;
 import org.codehaus.plexus.util.StringUtils;
 
+import com.ibm.icu.text.CharsetDetector;
+
 /**
  * Manager for Doxia converter CLI options.
  *
@@ -40,34 +42,34 @@
 class CLIManager
 {
     /** h character */
-    public static final char HELP = 'h';
+    static final char HELP = 'h';
 
     /** v character */
-    public static final char VERSION = 'v';
+    static final char VERSION = 'v';
 
     /** in String */
-    public static final String IN = "in";
+    static final String IN = "in";
 
     /** out String */
-    public static final String OUT = "out";
+    static final String OUT = "out";
 
     /** from String */
-    public static final String FROM = "from";
+    static final String FROM = "from";
 
     /** to String */
-    public static final String TO = "to";
+    static final String TO = "to";
 
     /** inEncoding String */
-    public static final String INENCODING = "inEncoding";
+    static final String INENCODING = "inEncoding";
 
     /** outEncoding String */
-    public static final String OUTENCODING = "outEncoding";
+    static final String OUTENCODING = "outEncoding";
 
     /** X character */
-    public static final char DEBUG = 'X';
+    static final char DEBUG = 'X';
 
     /** e character */
-    public static final char ERRORS = 'e';
+    static final char ERRORS = 'e';
 
     private static final Options options;
 
@@ -75,38 +77,49 @@
     {
         options = new Options();
 
-        options.addOption( OptionBuilder.withLongOpt( "help" ).withDescription( "Display help information" )
-            .create( HELP ) );
-        options.addOption( OptionBuilder.withLongOpt( "version" ).withDescription( "Display version information" )
-            .create( VERSION ) );
-
-        options.addOption( OptionBuilder.withLongOpt( "input" ).withDescription( "Input file or directory" ).hasArg()
-            .create( IN ) );
-        options.addOption( OptionBuilder.withLongOpt( "output" ).withDescription( "Output file or directory" ).hasArg()
-            .create( OUT ) );
-        options.addOption( OptionBuilder.withDescription( "From format" ).hasArg().create( FROM ) );
-        options.addOption( OptionBuilder.withDescription( "To format" ).hasArg().create( TO ) );
-        options.addOption( OptionBuilder.withLongOpt( "inputEncoding" ).withDescription( "Input file encoding. " +
-                "If not specified, autodetect the input encoding." )
-            .hasArg().create( INENCODING ) );
-        options.addOption( OptionBuilder.withLongOpt( "outputEncoding" ).withDescription( "Output file encoding. " +
-                "If not specified, use the input encoding (or detected)." )
-            .hasArg().create( OUTENCODING ) );
-
-        options.addOption( OptionBuilder.withLongOpt( "debug" ).withDescription( "Produce execution debug output" )
-            .create( DEBUG ) );
-        options.addOption( OptionBuilder.withLongOpt( "errors" ).withDescription( "Produce execution error messages" )
-            .create( ERRORS ) );
+        options.addOption( OptionBuilder.withLongOpt( "help" ).withDescription( "Display help information." )
+                                        .create( HELP ) );
+        options.addOption( OptionBuilder.withLongOpt( "version" ).withDescription( "Display version information." )
+                                        .create( VERSION ) );
+
+        options.addOption( OptionBuilder.withLongOpt( "input" ).withDescription( "Input file or directory." )
+                                        .hasArg().create( IN ) );
+        options.addOption( OptionBuilder.withLongOpt( "output" ).withDescription( "Output file or directory." )
+                                        .hasArg().create( OUT ) );
+        options.addOption( OptionBuilder.withDescription( "From format. If not specified, try to autodetect it." )
+                                        .hasArg().create( FROM ) );
+        options.addOption( OptionBuilder.withDescription( "To format." ).hasArg().create( TO ) );
+        options.addOption( OptionBuilder.withLongOpt( "inputEncoding" )
+                                        .withDescription(
+                                                          "Input file encoding. "
+                                                              + "If not specified, try to autodetect it." )
+                                        .hasArg().create( INENCODING ) );
+        options.addOption( OptionBuilder.withLongOpt( "outputEncoding" )
+                                        .withDescription(
+                                                          "Output file encoding. If not specified, use the "
+                                                              + "input encoding (or autodetected)." ).hasArg()
+                                        .create( OUTENCODING ) );
+
+        options.addOption( OptionBuilder.withLongOpt( "debug" )
+                                        .withDescription( "Produce execution debug output." ).create( DEBUG ) );
+        options.addOption( OptionBuilder.withLongOpt( "errors" )
+                                        .withDescription( "Produce execution error messages." ).create( ERRORS ) );
     }
 
     /**
-     * @param args
-     * @return
-     * @throws ParseException
+     * @param args not null.
+     * @return a not null command line.
+     * @throws ParseException if any
+     * @throws IllegalArgumentException is args is null
      */
-    public CommandLine parse( String[] args )
+    CommandLine parse( String[] args )
         throws ParseException
     {
+        if ( args == null )
+        {
+            throw new IllegalArgumentException( "args is required." );
+        }
+
         // We need to eat any quotes surrounding arguments...
         String[] cleanArgs = cleanArgs( args );
 
@@ -114,24 +127,30 @@
         return parser.parse( options, cleanArgs );
     }
 
-    protected static void displayHelp()
+    static void displayHelp()
     {
         System.out.println();
 
         HelpFormatter formatter = new HelpFormatter();
-        formatter.printHelp( "doxia [options] [-in <arg>] [-from <arg>] [-inEncoding <arg>] [-out <arg>] "
-            + "[-to <arg>] [-outEncoding <arg>]\n", "\nOptions:", options, getSupportedFormat() );
+        formatter.printHelp( "doxia [options] -in <arg> [-from <arg>] [-inEncoding <arg>] -out <arg> "
+            + "-to <arg> [-outEncoding <arg>]\n", "\nOptions:", options, getSupportedFormatAndEncoding() );
     }
 
-    protected static void displaySupportedFormat()
+    private static String getSupportedFormatAndEncoding()
     {
-        System.out.println( getSupportedFormat() );
+        return getSupportedFormat() + "\n" + getSupportedEncoding();
     }
 
     private static String getSupportedFormat()
     {
         return "\nSupported Formats:\n from: " + StringUtils.join( DefaultConverter.SUPPORTED_FROM_FORMAT, ", " )
-            + "\n out: " + StringUtils.join( DefaultConverter.SUPPORTED_TO_FORMAT, ", " ) + "\n";
+            + " or autodetect" + "\n out: " + StringUtils.join( DefaultConverter.SUPPORTED_TO_FORMAT, ", " )
+            + "\n";
+    }
+
+    private static String getSupportedEncoding()
+    {
+        return "\nSupported Encoding:\n " + StringUtils.join( CharsetDetector.getAllDetectableCharsets(), ", " );
     }
 
     private String[] cleanArgs( String[] args )

Modified: maven/doxia/doxia-tools/trunk/doxia-converter/src/main/java/org/apache/maven/doxia/cli/ConverterCli.java
URL: http://svn.apache.org/viewvc/maven/doxia/doxia-tools/trunk/doxia-converter/src/main/java/org/apache/maven/doxia/cli/ConverterCli.java?rev=697523&r1=697522&r2=697523&view=diff
==============================================================================
--- maven/doxia/doxia-tools/trunk/doxia-converter/src/main/java/org/apache/maven/doxia/cli/ConverterCli.java (original)
+++ maven/doxia/doxia-tools/trunk/doxia-converter/src/main/java/org/apache/maven/doxia/cli/ConverterCli.java Sun Sep 21 07:29:09 2008
@@ -18,8 +18,10 @@
  */
 package org.apache.maven.doxia.cli;
 
+import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.io.InputStream;
+import java.io.UnsupportedEncodingException;
 import java.util.Properties;
 
 import org.apache.commons.cli.CommandLine;
@@ -143,11 +145,15 @@
 
             return 1;
         }
-        catch ( UnsupportedFormatException e )
+        catch ( UnsupportedEncodingException e )
         {
-            showFatalError( "Unsupported format", e, showErrors );
+            showFatalError( e.getMessage(), e, showErrors );
 
-            CLIManager.displaySupportedFormat();
+            return 1;
+        }
+        catch ( FileNotFoundException e )
+        {
+            showFatalError( e.getMessage(), e, showErrors );
 
             return 1;
         }
@@ -158,9 +164,7 @@
         }
         catch ( UnsupportedFormatException e )
         {
-            showFatalError( "Unsupported format", e, showErrors );
-
-            CLIManager.displaySupportedFormat();
+            showFatalError( e.getMessage(), e, showErrors );
 
             return 1;
         }
@@ -193,7 +197,7 @@
         {
             Properties properties = new Properties();
             resourceAsStream = ConverterCli.class.getClassLoader()
-                .getResourceAsStream( "META-INF/maven/org.apache.maven.doxia/doxia-convertor/pom.properties" );
+                .getResourceAsStream( "META-INF/maven/org.apache.maven.doxia/doxia-converter/pom.properties" );
 
             if ( resourceAsStream != null )
             {
@@ -201,17 +205,17 @@
 
                 if ( properties.getProperty( "builtOn" ) != null )
                 {
-                    System.out.println( "Doxia Convertor version: " + properties.getProperty( "version", "unknown" )
+                    System.out.println( "Doxia Converter version: " + properties.getProperty( "version", "unknown" )
                         + " built on " + properties.getProperty( "builtOn" ) );
                 }
                 else
                 {
-                    System.out.println( "Doxia Convertor version: " + properties.getProperty( "version", "unknown" ) );
+                    System.out.println( "Doxia Converter version: " + properties.getProperty( "version", "unknown" ) );
                 }
             }
             else
             {
-                System.out.println( "Doxia Convertor version: " + properties.getProperty( "version", "unknown" ) );
+                System.out.println( "Doxia Converter version: " + properties.getProperty( "version", "unknown" ) );
             }
 
             System.out.println( "Java version: " + System.getProperty( "java.version", "<unknown java version>" ) );

Added: maven/doxia/doxia-tools/trunk/doxia-converter/src/main/java/org/apache/maven/doxia/util/ConverterUtil.java
URL: http://svn.apache.org/viewvc/maven/doxia/doxia-tools/trunk/doxia-converter/src/main/java/org/apache/maven/doxia/util/ConverterUtil.java?rev=697523&view=auto
==============================================================================
--- maven/doxia/doxia-tools/trunk/doxia-converter/src/main/java/org/apache/maven/doxia/util/ConverterUtil.java (added)
+++ maven/doxia/doxia-tools/trunk/doxia-converter/src/main/java/org/apache/maven/doxia/util/ConverterUtil.java Sun Sep 21 07:29:09 2008
@@ -0,0 +1,129 @@
+package org.apache.maven.doxia.util;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import java.io.Writer;
+
+import org.apache.maven.doxia.UnsupportedFormatException;
+import org.apache.maven.doxia.parser.Parser;
+import org.apache.maven.doxia.sink.Sink;
+import org.apache.maven.doxia.sink.SinkFactory;
+import org.codehaus.plexus.PlexusContainer;
+import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
+
+/**
+ * Utility class to play with Doxia objects.
+ *
+ * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
+ * @version $Id$
+ */
+public class ConverterUtil
+{
+    /**
+     * @param plexus not null
+     * @param format not null
+     * @param supportedFormats not null
+     * @return an instance of <code>Parser</code> depending the format.
+     * @throws ComponentLookupException if could not find the Parser for the given format.
+     * @throws UnsupportedFormatException if the found parser is not instantiated.
+     * @throws IllegalArgumentException if parameters are null
+     */
+    public static Parser getParser( PlexusContainer plexus, String format, String[] supportedFormats )
+        throws ComponentLookupException, UnsupportedFormatException
+    {
+        if ( plexus == null )
+        {
+            throw new IllegalArgumentException( "plexus is required" );
+        }
+        if ( format == null )
+        {
+            throw new IllegalArgumentException( "format is required" );
+        }
+        if ( supportedFormats == null )
+        {
+            throw new IllegalArgumentException( "supportedFormats is required" );
+        }
+
+        Parser parser = null;
+        for ( int i = 0; i < supportedFormats.length; i++ )
+        {
+            if ( format.equalsIgnoreCase( supportedFormats[i] ) )
+            {
+                parser = (Parser) plexus.lookup( Parser.ROLE, format );
+            }
+        }
+
+        if ( parser == null )
+        {
+            throw new UnsupportedFormatException( format, supportedFormats );
+        }
+
+        return parser;
+    }
+
+    /**
+     * @param plexus not null
+     * @param format not null
+     * @param writer could be null
+     * @param supportedFormats not null
+     * @return an instance of <code>Sink</code> depending the given format.
+     * @throws ComponentLookupException if could not find the SinkFactory for the given format.
+     * @throws UnsupportedFormatException if the found parser is not instantiated.
+     * @throws IllegalArgumentException if parameters are null
+     */
+    public static Sink getSink( PlexusContainer plexus, String format, Writer writer, String[] supportedFormats )
+        throws ComponentLookupException, UnsupportedFormatException
+    {
+        if ( plexus == null )
+        {
+            throw new IllegalArgumentException( "plexus is required" );
+        }
+        if ( format == null )
+        {
+            throw new IllegalArgumentException( "format is required" );
+        }
+        if ( supportedFormats == null )
+        {
+            throw new IllegalArgumentException( "supportedFormats is required" );
+        }
+
+        SinkFactory factory = null;
+        for ( int i = 0; i < supportedFormats.length; i++ )
+        {
+            if ( format.equalsIgnoreCase( supportedFormats[i] ) )
+            {
+                factory = (SinkFactory) plexus.lookup( SinkFactory.ROLE, format );
+            }
+        }
+
+        if ( factory == null )
+        {
+            throw new UnsupportedFormatException( format, supportedFormats );
+        }
+
+        Sink sink = factory.createSink( writer );
+        if ( sink == null )
+        {
+            throw new IllegalArgumentException( "Sink was not instanciated: " + format );
+        }
+
+        return sink;
+    }
+}

Propchange: maven/doxia/doxia-tools/trunk/doxia-converter/src/main/java/org/apache/maven/doxia/util/ConverterUtil.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: maven/doxia/doxia-tools/trunk/doxia-converter/src/main/java/org/apache/maven/doxia/util/ConverterUtil.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Modified: maven/doxia/doxia-tools/trunk/doxia-converter/src/main/java/org/apache/maven/doxia/wrapper/AbstractFileWrapper.java
URL: http://svn.apache.org/viewvc/maven/doxia/doxia-tools/trunk/doxia-converter/src/main/java/org/apache/maven/doxia/wrapper/AbstractFileWrapper.java?rev=697523&r1=697522&r2=697523&view=diff
==============================================================================
--- maven/doxia/doxia-tools/trunk/doxia-converter/src/main/java/org/apache/maven/doxia/wrapper/AbstractFileWrapper.java (original)
+++ maven/doxia/doxia-tools/trunk/doxia-converter/src/main/java/org/apache/maven/doxia/wrapper/AbstractFileWrapper.java Sun Sep 21 07:29:09 2008
@@ -21,22 +21,14 @@
 
 import java.io.ByteArrayOutputStream;
 import java.io.File;
-import java.io.FileInputStream;
-import java.io.IOException;
 import java.io.OutputStream;
 import java.io.OutputStreamWriter;
-import java.io.Reader;
-import java.io.StringWriter;
 import java.io.UnsupportedEncodingException;
-import java.util.Locale;
 
 import org.codehaus.plexus.util.IOUtil;
-import org.codehaus.plexus.util.ReaderFactory;
 import org.codehaus.plexus.util.StringUtils;
-import org.codehaus.plexus.util.xml.XmlStreamReader;
 
 import com.ibm.icu.text.CharsetDetector;
-import com.ibm.icu.text.CharsetMatch;
 
 /**
  * Abstract File wrapper for Doxia converter.
@@ -44,9 +36,11 @@
  * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
  * @version $Id$
  */
-public abstract class AbstractFileWrapper
+abstract class AbstractFileWrapper
     extends AbstractWrapper
 {
+    public static final String AUTO_ENCODING = "auto";
+
     /** serialVersionUID */
     static final long serialVersionUID = 6072685230076158590L;
 
@@ -55,6 +49,43 @@
     private String encoding;
 
     /**
+     *
+     * @param absolutePath not null
+     * @param format could be null
+     * @param encoding could be null
+     * @param supportedFormat not null
+     * @throws UnsupportedEncodingException if the encoding is unsupported.
+     * @throws IllegalArgumentException if any
+     */
+    AbstractFileWrapper( String absolutePath, String format, String encoding, String[] supportedFormat )
+        throws UnsupportedEncodingException
+    {
+        super( format, supportedFormat );
+
+        if ( StringUtils.isEmpty( absolutePath ) )
+        {
+            throw new IllegalArgumentException( "absolutePath is required" );
+        }
+
+        File file = new File( absolutePath );
+        if ( !file.isAbsolute() )
+        {
+            file = new File( new File( "" ).getAbsolutePath(), absolutePath );
+        }
+        this.file = file;
+
+        if ( StringUtils.isNotEmpty( encoding ) && !encoding.equalsIgnoreCase( encoding )
+            && !validateEncoding( encoding ) )
+        {
+            StringBuffer msg = new StringBuffer();
+            msg.append( "The encoding '" + encoding + "' is not a valid one. The supported charsets are: " );
+            msg.append( StringUtils.join( CharsetDetector.getAllDetectableCharsets(), ", " ) );
+            throw new UnsupportedEncodingException( msg.toString() );
+        }
+        this.encoding = ( StringUtils.isNotEmpty( encoding ) ? encoding : AUTO_ENCODING );
+    }
+
+    /**
      * @return the file
      */
     public File getFile()
@@ -116,45 +147,6 @@
         return true;
     }
 
-    /**
-     * @param f not null
-     * @return the detected encoding from f or <code>null</code> if an IOException occurred.
-     */
-    static String autoDetectEncoding( File f )
-    {
-        Reader reader = null;
-        FileInputStream is = null;
-        try
-        {
-            is = new FileInputStream( f );
-
-            StringWriter w = new StringWriter();
-            IOUtil.copy( is, w );
-            String content = w.toString();
-
-            if ( content.startsWith( "<?xml" ) )
-            {
-                reader = ReaderFactory.newXmlReader( f );
-                return ((XmlStreamReader)reader).getEncoding();
-            }
-
-            CharsetDetector detector = new CharsetDetector();
-            detector.setText( w.toString().getBytes() );
-            CharsetMatch match = detector.detect();
-
-            return match.getName().toUpperCase( Locale.ENGLISH );
-        }
-        catch ( IOException e )
-        {
-            return null;
-        }
-        finally
-        {
-            IOUtil.close( reader );
-            IOUtil.close( is );
-        }
-    }
-
     /** {@inheritDoc} */
     public boolean equals( Object other )
     {

Modified: maven/doxia/doxia-tools/trunk/doxia-converter/src/main/java/org/apache/maven/doxia/wrapper/AbstractWrapper.java
URL: http://svn.apache.org/viewvc/maven/doxia/doxia-tools/trunk/doxia-converter/src/main/java/org/apache/maven/doxia/wrapper/AbstractWrapper.java?rev=697523&r1=697522&r2=697523&view=diff
==============================================================================
--- maven/doxia/doxia-tools/trunk/doxia-converter/src/main/java/org/apache/maven/doxia/wrapper/AbstractWrapper.java (original)
+++ maven/doxia/doxia-tools/trunk/doxia-converter/src/main/java/org/apache/maven/doxia/wrapper/AbstractWrapper.java Sun Sep 21 07:29:09 2008
@@ -21,15 +21,19 @@
 
 import java.io.Serializable;
 
+import org.codehaus.plexus.util.StringUtils;
+
 /**
  * Abstract wrapper for Doxia converter.
  *
  * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
  * @version $Id$
  */
-public abstract class AbstractWrapper
+abstract class AbstractWrapper
     implements Serializable
 {
+    public static final String AUTO_FORMAT = "auto";
+
     /** serialVersionUID */
     static final long serialVersionUID = -1150854786311626411L;
 
@@ -38,6 +42,21 @@
     private String[] supportedFormat;
 
     /**
+     * @param format could be null.
+     * @param supportedFormat not null.
+     * @throws IllegalArgumentException if supportedFormat is null.
+     */
+    AbstractWrapper( String format, String[] supportedFormat )
+    {
+        this.format = ( StringUtils.isNotEmpty( format ) ? format : AUTO_FORMAT );
+        if ( supportedFormat == null )
+        {
+            throw new IllegalArgumentException( "supportedFormat is required" );
+        }
+        this.supportedFormat = supportedFormat;
+    }
+
+    /**
      * @return the wanted format.
      */
     public String getFormat()
@@ -84,7 +103,8 @@
 
         AbstractWrapper that = (AbstractWrapper) other;
         boolean result = true;
-        result = result && ( getFormat() == null ? that.getFormat() == null : getFormat().equals( that.getFormat() ) );
+        result =
+            result && ( getFormat() == null ? that.getFormat() == null : getFormat().equals( that.getFormat() ) );
         return result;
     }
 

Modified: maven/doxia/doxia-tools/trunk/doxia-converter/src/main/java/org/apache/maven/doxia/wrapper/InputFileWrapper.java
URL: http://svn.apache.org/viewvc/maven/doxia/doxia-tools/trunk/doxia-converter/src/main/java/org/apache/maven/doxia/wrapper/InputFileWrapper.java?rev=697523&r1=697522&r2=697523&view=diff
==============================================================================
--- maven/doxia/doxia-tools/trunk/doxia-converter/src/main/java/org/apache/maven/doxia/wrapper/InputFileWrapper.java (original)
+++ maven/doxia/doxia-tools/trunk/doxia-converter/src/main/java/org/apache/maven/doxia/wrapper/InputFileWrapper.java Sun Sep 21 07:29:09 2008
@@ -19,13 +19,8 @@
  * under the License.
  */
 
-import java.io.File;
-
-import org.apache.maven.doxia.UnsupportedFormatException;
-import org.apache.maven.doxia.util.FormatUtils;
-import org.codehaus.plexus.util.StringUtils;
-
-import com.ibm.icu.text.CharsetDetector;
+import java.io.FileNotFoundException;
+import java.io.UnsupportedEncodingException;
 
 /**
  * Wrapper for an input file.
@@ -43,60 +38,55 @@
      * Private constructor.
      *
      * @param file not null
-     * @param format not null
+     * @param format could be null
      * @param charsetName could be null
      * @param supportedFormat not null
+     * @throws IllegalArgumentException if the file doesn't exist.
+     * @throws UnsupportedEncodingException if the encoding is unsupported.
+     * @throws FileNotFoundException if the file for absolutePath is not found.
      */
-    private InputFileWrapper( File file, String format, String charsetName, String[] supportedFormat )
+    private InputFileWrapper( String absolutePath, String format, String charsetName, String[] supportedFormat )
+        throws IllegalArgumentException, UnsupportedEncodingException, FileNotFoundException
     {
-        setFile( file );
-        setFormat( format );
-        setEncoding( charsetName );
-        setSupportedFormat( supportedFormat );
+        super( absolutePath, format, charsetName, supportedFormat );
+
+        if ( !getFile().exists() )
+        {
+            throw new FileNotFoundException( "The file '" + getFile().getAbsolutePath() + "' doesn't exist." );
+        }
     }
 
     /**
-     * @param absolutePath not null
-     * @param format not null
+     * @param absolutePath for a file or a directory not null.
+     * @param format could be null
      * @param charsetName could be null
      * @param supportedFormat not null
      * @return a type safe input reader
-     * @throws IllegalArgumentException if any
-     * @throws UnsupportedFormatException if any
-     * @see FormatUtils#getSupportedFormat(String, String, String[])
+     * @throws IllegalArgumentException if the file doesn't exist.
+     * @throws UnsupportedEncodingException if the encoding is unsupported.
+     * @throws FileNotFoundException if the file for absolutePath is not found.
+     * @see #valueOf(String, String, String, String[]) using AUTO_FORMAT
      */
-    public static InputFileWrapper valueOf( String absolutePath, String format, String charsetName, String[] supportedFormat )
-        throws UnsupportedFormatException
+    public static InputFileWrapper valueOf( String absolutePath, String format, String[] supportedFormat )
+        throws IllegalArgumentException, UnsupportedEncodingException, FileNotFoundException
     {
-        if ( StringUtils.isEmpty( absolutePath ) )
-        {
-            throw new IllegalArgumentException( "absolutePath is required" );
-        }
-        if ( StringUtils.isNotEmpty( charsetName ) && !validateEncoding( charsetName ) )
-        {
-            StringBuffer msg = new StringBuffer();
-            msg.append( "The encoding '" + charsetName + "' is not a valid. The supported charsets are: " );
-            msg.append( StringUtils.join( CharsetDetector.getAllDetectableCharsets(), ", " ) );
-            throw new IllegalArgumentException( msg.toString() );
-        }
-
-        File file = new File( absolutePath );
-        if ( !file.isAbsolute() )
-        {
-            file = new File( new File( "" ).getAbsolutePath(), absolutePath );
-        }
-
-        String encoding;
-        if ( StringUtils.isNotEmpty( charsetName ) )
-        {
-            encoding = charsetName;
-        }
-        else
-        {
-            encoding = autoDetectEncoding( file );
-        }
+        return valueOf( absolutePath, format, AUTO_FORMAT, supportedFormat );
+    }
 
-        return new InputFileWrapper( file, FormatUtils.getSupportedFormat( file.getAbsolutePath(), format,
-                                                                           supportedFormat ), encoding, supportedFormat );
+    /**
+     * @param absolutePath for a wanted file or a wanted directory, not null.
+     * @param format could be null
+     * @param charsetName could be null
+     * @param supportedFormat not null
+     * @return a type safe input reader
+     * @throws IllegalArgumentException if the file doesn't exist.
+     * @throws UnsupportedEncodingException if the encoding is unsupported.
+     * @throws FileNotFoundException if the file for absolutePath is not found.
+     */
+    public static InputFileWrapper valueOf( String absolutePath, String format, String charsetName,
+                                            String[] supportedFormat )
+        throws IllegalArgumentException, UnsupportedEncodingException, FileNotFoundException
+    {
+        return new InputFileWrapper( absolutePath, format, charsetName, supportedFormat );
     }
 }

Modified: maven/doxia/doxia-tools/trunk/doxia-converter/src/main/java/org/apache/maven/doxia/wrapper/InputReaderWrapper.java
URL: http://svn.apache.org/viewvc/maven/doxia/doxia-tools/trunk/doxia-converter/src/main/java/org/apache/maven/doxia/wrapper/InputReaderWrapper.java?rev=697523&r1=697522&r2=697523&view=diff
==============================================================================
--- maven/doxia/doxia-tools/trunk/doxia-converter/src/main/java/org/apache/maven/doxia/wrapper/InputReaderWrapper.java (original)
+++ maven/doxia/doxia-tools/trunk/doxia-converter/src/main/java/org/apache/maven/doxia/wrapper/InputReaderWrapper.java Sun Sep 21 07:29:09 2008
@@ -21,9 +21,6 @@
 
 import java.io.Reader;
 
-import org.apache.maven.doxia.UnsupportedFormatException;
-import org.apache.maven.doxia.util.FormatUtils;
-
 /**
  * Wrapper for an input reader.
  *
@@ -31,7 +28,7 @@
  * @version $Id$
  */
 public class InputReaderWrapper
-    extends AbstractFileWrapper
+    extends AbstractWrapper
 {
     /** serialVersionUID */
     static final long serialVersionUID = 3260213754615748766L;
@@ -41,13 +38,24 @@
     /**
      * Private constructor.
      *
-     * @param format
-     * @param supportedFormat
+     * @param format not null
+     * @param supportedFormat not null
+     * @throws IllegalArgumentException if the format equals AUTO_FORMAT.
      */
-    private InputReaderWrapper( String format, String[] supportedFormat )
+    private InputReaderWrapper( Reader reader, String format, String[] supportedFormat )
     {
-        setFormat( format );
-        setSupportedFormat( supportedFormat );
+        super( format, supportedFormat );
+
+        if ( getFormat().equalsIgnoreCase( AUTO_FORMAT ) )
+        {
+            throw new IllegalArgumentException( "input format is required" );
+        }
+
+        if ( reader == null )
+        {
+            throw new IllegalArgumentException( "input reader is required" );
+        }
+        this.reader = reader;
     }
 
     /**
@@ -64,21 +72,10 @@
      * @param supportedFormat not null
      * @return a type safe input reader
      * @throws IllegalArgumentException if any
-     * @throws UnsupportedFormatException if any
-     * @see FormatUtils#getSupportedFormat(String, String[])
      */
     public static InputReaderWrapper valueOf( Reader reader, String format, String[] supportedFormat )
-        throws UnsupportedFormatException
+        throws IllegalArgumentException
     {
-        if ( reader == null )
-        {
-            throw new IllegalArgumentException( "reader is required" );
-        }
-
-        InputReaderWrapper input = new InputReaderWrapper( FormatUtils.getSupportedFormat( format, supportedFormat ),
-                                                           supportedFormat );
-        input.reader = reader;
-
-        return input;
+        return new InputReaderWrapper( reader, format, supportedFormat );
     }
 }

Modified: maven/doxia/doxia-tools/trunk/doxia-converter/src/main/java/org/apache/maven/doxia/wrapper/OutputFileWrapper.java
URL: http://svn.apache.org/viewvc/maven/doxia/doxia-tools/trunk/doxia-converter/src/main/java/org/apache/maven/doxia/wrapper/OutputFileWrapper.java?rev=697523&r1=697522&r2=697523&view=diff
==============================================================================
--- maven/doxia/doxia-tools/trunk/doxia-converter/src/main/java/org/apache/maven/doxia/wrapper/OutputFileWrapper.java (original)
+++ maven/doxia/doxia-tools/trunk/doxia-converter/src/main/java/org/apache/maven/doxia/wrapper/OutputFileWrapper.java Sun Sep 21 07:29:09 2008
@@ -19,13 +19,10 @@
  * under the License.
  */
 
-import java.io.File;
+import java.io.UnsupportedEncodingException;
 
-import org.apache.maven.doxia.UnsupportedFormatException;
-import org.apache.maven.doxia.util.FormatUtils;
 import org.codehaus.plexus.util.StringUtils;
-
-import com.ibm.icu.text.CharsetDetector;
+import org.codehaus.plexus.util.WriterFactory;
 
 /**
  * Wrapper for an output file.
@@ -42,17 +39,22 @@
     /**
      * Private constructor.
      *
-     * @param file
-     * @param format
+     * @param file not null
+     * @param format not null
      * @param charsetName could be null
-     * @param supportedFormat
+     * @param supportedFormat not null.
+     * @throws IllegalArgumentException if any.
+     * @throws UnsupportedEncodingException if the encoding is unsupported.
      */
-    private OutputFileWrapper( File file, String format, String charsetName, String[] supportedFormat )
+    private OutputFileWrapper( String absolutePath, String format, String charsetName, String[] supportedFormat )
+        throws UnsupportedEncodingException
     {
-        setFile( file );
-        setFormat( format );
-        setEncoding( charsetName );
-        setSupportedFormat( supportedFormat );
+        super( absolutePath, format, charsetName, supportedFormat );
+
+        if ( getFormat().equalsIgnoreCase( AUTO_FORMAT ) )
+        {
+            throw new IllegalArgumentException( "output format could not be " + AUTO_FORMAT );
+        }
     }
 
     /**
@@ -61,34 +63,32 @@
      * @param charsetName could be null
      * @param supportedFormat not null
      * @return a type safe output writer
-     * @throws IllegalArgumentException if any
-     * @throws UnsupportedFormatException if any
-     * @see FormatUtils#getSupportedFormat(String, String, String[])
+     * @throws IllegalArgumentException if any.
+     * @throws UnsupportedEncodingException if the encoding is unsupported.
      */
-    public static OutputFileWrapper valueOf( String absolutePath, String format, String charsetName, String[] supportedFormat )
-        throws UnsupportedFormatException
+    public static OutputFileWrapper valueOf( String absolutePath, String format, String[] supportedFormat )
+        throws IllegalArgumentException, UnsupportedEncodingException
     {
-        if ( StringUtils.isEmpty( absolutePath ) )
-        {
-            throw new IllegalArgumentException( "absolutePath is required" );
-        }
-        if ( StringUtils.isNotEmpty( charsetName ) && !validateEncoding( charsetName ) )
-        {
-            StringBuffer msg = new StringBuffer();
-            msg.append( "The encoding '" + charsetName + "' is not a valid. The supported charsets are: " );
-            msg.append( StringUtils.join( CharsetDetector.getAllDetectableCharsets(), ", " ) );
-            throw new IllegalArgumentException( msg.toString() );
-        }
-
-        File file = new File( absolutePath );
+        return valueOf( absolutePath, format, WriterFactory.UTF_8, supportedFormat );
+    }
 
-        if ( !file.isAbsolute() )
+    /**
+     * @param absolutePath not null
+     * @param format not null
+     * @param charsetName could be null
+     * @param supportedFormat not null
+     * @return a type safe output writer
+     * @throws IllegalArgumentException if any.
+     * @throws UnsupportedEncodingException if the encoding is unsupported.
+     */
+    public static OutputFileWrapper valueOf( String absolutePath, String format, String charsetName,
+                                             String[] supportedFormat )
+        throws IllegalArgumentException, UnsupportedEncodingException
+    {
+        if ( StringUtils.isEmpty( format ) )
         {
-            file = new File( new File( "" ).getAbsolutePath(), absolutePath );
+            throw new IllegalArgumentException( "output format is required" );
         }
-
-        return new OutputFileWrapper( file, FormatUtils.getSupportedFormat( file.getAbsolutePath(), format,
-                                                                            supportedFormat ), charsetName,
-                                      supportedFormat );
+        return new OutputFileWrapper( absolutePath, format, charsetName, supportedFormat );
     }
 }

Modified: maven/doxia/doxia-tools/trunk/doxia-converter/src/main/java/org/apache/maven/doxia/wrapper/OutputWriterWrapper.java
URL: http://svn.apache.org/viewvc/maven/doxia/doxia-tools/trunk/doxia-converter/src/main/java/org/apache/maven/doxia/wrapper/OutputWriterWrapper.java?rev=697523&r1=697522&r2=697523&view=diff
==============================================================================
--- maven/doxia/doxia-tools/trunk/doxia-converter/src/main/java/org/apache/maven/doxia/wrapper/OutputWriterWrapper.java (original)
+++ maven/doxia/doxia-tools/trunk/doxia-converter/src/main/java/org/apache/maven/doxia/wrapper/OutputWriterWrapper.java Sun Sep 21 07:29:09 2008
@@ -19,10 +19,10 @@
  * under the License.
  */
 
+import java.io.UnsupportedEncodingException;
 import java.io.Writer;
 
-import org.apache.maven.doxia.UnsupportedFormatException;
-import org.apache.maven.doxia.util.FormatUtils;
+import org.codehaus.plexus.util.StringUtils;
 
 /**
  * Wrapper for an output writer.
@@ -31,7 +31,7 @@
  * @version $Id$
  */
 public class OutputWriterWrapper
-    extends AbstractFileWrapper
+    extends AbstractWrapper
 {
     /** serialVersionUID */
     static final long serialVersionUID = 3329037527245430610L;
@@ -41,13 +41,20 @@
     /**
      * Private constructor.
      *
-     * @param format
-     * @param supportedFormat
+     * @param format not null
+     * @param supportedFormat not null
+     * @throws IllegalArgumentException if any.
      */
-    private OutputWriterWrapper( String format, String[] supportedFormat )
+    private OutputWriterWrapper( Writer writer, String format, String[] supportedFormat )
     {
-        setFormat( format );
-        setSupportedFormat( supportedFormat );
+        super( format, supportedFormat );
+
+        if ( getFormat().equalsIgnoreCase( AUTO_FORMAT ) )
+        {
+            throw new IllegalArgumentException( "output format is required" );
+        }
+
+        this.writer = writer;
     }
 
     /**
@@ -63,23 +70,21 @@
      * @param format not null
      * @param supportedFormat not null
      * @return a type safe output writer
-     * @throws IllegalArgumentException if any
-     * @throws UnsupportedFormatException if any
-     * @see FormatUtils#getSupportedFormat(String, String[])
+     * @throws IllegalArgumentException if any.
+     * @throws UnsupportedEncodingException if the encoding is unsupported.
      */
     public static OutputWriterWrapper valueOf( Writer writer, String format, String[] supportedFormat )
-        throws UnsupportedFormatException
+        throws IllegalArgumentException
     {
         if ( writer == null )
         {
-            throw new IllegalArgumentException( "writer is required" );
+            throw new IllegalArgumentException( "output writer is required" );
+        }
+        if ( StringUtils.isEmpty( format ) )
+        {
+            throw new IllegalArgumentException( "output format is required" );
         }
 
-        OutputWriterWrapper output = new OutputWriterWrapper(
-                                                              FormatUtils.getSupportedFormat( format, supportedFormat ),
-                                                              supportedFormat );
-        output.writer = writer;
-
-        return output;
+        return new OutputWriterWrapper( writer, format, supportedFormat );
     }
 }

Modified: maven/doxia/doxia-tools/trunk/doxia-converter/src/main/resources/META-INF/plexus/components.xml
URL: http://svn.apache.org/viewvc/maven/doxia/doxia-tools/trunk/doxia-converter/src/main/resources/META-INF/plexus/components.xml?rev=697523&r1=697522&r2=697523&view=diff
==============================================================================
--- maven/doxia/doxia-tools/trunk/doxia-converter/src/main/resources/META-INF/plexus/components.xml (original)
+++ maven/doxia/doxia-tools/trunk/doxia-converter/src/main/resources/META-INF/plexus/components.xml Sun Sep 21 07:29:09 2008
@@ -85,6 +85,12 @@
         </requirement>
       </requirements>
     </component>
+    <component>
+      <role>org.apache.maven.doxia.sink.SinkFactory</role>
+      <role-hint>apt</role-hint>
+      <implementation>org.apache.maven.doxia.module.apt.AptSinkFactory</implementation>
+      <description>APT implementation of the Sink factory.</description>
+    </component>
 
     <!-- Doxia confluence -->
     <component>
@@ -97,11 +103,17 @@
     <!-- Doxia docbook -->
     <component>
       <role>org.apache.maven.doxia.parser.Parser</role>
-      <role-hint>doc-book</role-hint>
+      <role-hint>docbook</role-hint>
       <implementation>org.apache.maven.doxia.module.docbook.DocBookParser</implementation>
       <description>Parse a &lt;code&gt;Docbook&lt;/code&gt; document and emit events
         into the specified doxia Sink.</description>
     </component>
+    <component>
+      <role>org.apache.maven.doxia.sink.SinkFactory</role>
+      <role-hint>docbook</role-hint>
+      <implementation>org.apache.maven.doxia.module.docbook.DocbookSinkFactory</implementation>
+      <description>Docbook implementation of the Sink factory.</description>
+    </component>
 
     <!-- Doxia fml -->
     <component>
@@ -111,6 +123,38 @@
       <description>Parse a fml model and emit events into the specified doxia Sink.</description>
     </component>
 
+    <!-- Doxia fo -->
+    <component>
+      <role>org.apache.maven.doxia.sink.SinkFactory</role>
+      <role-hint>fo</role-hint>
+      <implementation>org.apache.maven.doxia.module.fo.FoSinkFactory</implementation>
+      <description>FO implementation of the Sink factory.</description>
+    </component>
+
+    <!-- Doxia itext -->
+    <component>
+      <role>org.apache.maven.doxia.sink.SinkFactory</role>
+      <role-hint>itext</role-hint>
+      <implementation>org.apache.maven.doxia.module.itext.ITextSinkFactory</implementation>
+      <description>IText implementation of the Sink factory.</description>
+    </component>
+
+    <!-- Doxia latex -->
+    <component>
+      <role>org.apache.maven.doxia.sink.SinkFactory</role>
+      <role-hint>latex</role-hint>
+      <implementation>org.apache.maven.doxia.module.latex.LatexSinkFactory</implementation>
+      <description>Latex implementation of the Sink factory.</description>
+    </component>
+
+    <!-- Doxia rtf -->
+    <component>
+      <role>org.apache.maven.doxia.sink.SinkFactory</role>
+      <role-hint>rtf</role-hint>
+      <implementation>org.apache.maven.doxia.module.rtf.RtfSinkFactory</implementation>
+      <description>APT implementation of the Sink factory.</description>
+    </component>
+
     <!-- Doxia twiki -->
     <component>
       <role>org.apache.maven.doxia.parser.Parser</role>
@@ -132,6 +176,12 @@
         </requirement>
       </requirements>
     </component>
+    <component>
+      <role>org.apache.maven.doxia.sink.SinkFactory</role>
+      <role-hint>xdoc</role-hint>
+      <implementation>org.apache.maven.doxia.module.xdoc.XdocSinkFactory</implementation>
+      <description>Xdoc implementation of the Sink factory.</description>
+    </component>
 
     <!-- Doxia xhtml -->
     <component>
@@ -140,5 +190,11 @@
       <implementation>org.apache.maven.doxia.module.xhtml.XhtmlParser</implementation>
       <description>Parse an xhtml model and emit events into a Doxia Sink.</description>
     </component>
+    <component>
+      <role>org.apache.maven.doxia.sink.SinkFactory</role>
+      <role-hint>xhtml</role-hint>
+      <implementation>org.apache.maven.doxia.module.xhtml.XhtmlSinkFactory</implementation>
+      <description>Xhtml implementation of the Sink factory.</description>
+    </component>
   </components>
 </component-set>

Modified: maven/doxia/doxia-tools/trunk/doxia-converter/src/site/apt/usage.apt
URL: http://svn.apache.org/viewvc/maven/doxia/doxia-tools/trunk/doxia-converter/src/site/apt/usage.apt?rev=697523&r1=697522&r2=697523&view=diff
==============================================================================
--- maven/doxia/doxia-tools/trunk/doxia-converter/src/site/apt/usage.apt (original)
+++ maven/doxia/doxia-tools/trunk/doxia-converter/src/site/apt/usage.apt Sun Sep 21 07:29:09 2008
@@ -3,7 +3,7 @@
  ------
  Vincent Siveton
  ------
- March 2007
+ 2008-09-21
  ------
 
 ~~ Licensed to the Apache Software Foundation (ASF) under one
@@ -35,24 +35,35 @@
 +-----+
 # java -jar target/apache-doxia-1.0-SNAPSHOT-jar-with-dependencies.jar -h
 
-usage: doxia [options] [-in <arg>] [-from <arg>] [-out <arg>] [-to <arg>]
-
+usage: doxia [options] -in <arg> [-from <arg>] [-inEncoding <arg>] -out
+             <arg> -to <arg> [-outEncoding <arg>]
 
 Options:
- -e,--errors           Produce execution error messages
- -from <arg>           From format
- -h,--help             Display help information
- -in,--input <arg>     Input file or directory
- -out,--output <arg>   Output directory
- -to <arg>             To format
- -v,--version          Display version information
- -X,--debug            Produce execution debug output
+ -e,--errors                           Produce execution error messages
+ -from <arg>                           From format
+ -h,--help                             Display help information
+ -in,--input <arg>                     Input file or directory
+ -inEncoding,--inputEncoding <arg>     Input file encoding. If not
+                                       specified, autodetect the input encoding.
+ -out,--output <arg>                   Output file or directory
+ -outEncoding,--outputEncoding <arg>   Output file encoding. If not
+                                       specified, use the input encoding (or detected).
+ -to <arg>                             To format
+ -v,--version                          Display version information
+ -X,--debug                            Produce execution debug output
 
-Supported Format:
-from: apt, confluence, docbook, fml, twiki, xdoc, xhtml
+Supported Formats:
+from: apt, confluence, docbook, fml, twiki, xdoc, xhtml or autodetect
 out: apt, docbook, fo, itext, latex, rtf, xdoc, xhtml
+Supported Encoding:
+UTF-8, UTF-16BE, UTF-16LE, UTF-32BE, UTF-32LE, Shift_JIS, ISO-2022-JP,
+ISO-2022-CN, ISO-2022-KR, GB18030, EUC-JP, EUC-KR, Big5, ISO-8859-1,
+ISO-8859-2, ISO-8859-5, ISO-8859-6, ISO-8859-7, ISO-8859-8, windows-1251,
+windows-1256, KOI8-R, ISO-8859-9
 +-----+
 
+ <<Note>>: The input parameters (i.e. encoding and format) could be auto detected.
+
 * Command Line Execution
 
 +-----+



Mime
View raw message