View Javadoc

1   package org.apache.maven.doxia.wrapper;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.ByteArrayOutputStream;
23  import java.io.File;
24  import java.io.OutputStream;
25  import java.io.OutputStreamWriter;
26  import java.io.UnsupportedEncodingException;
27  
28  import org.codehaus.plexus.util.IOUtil;
29  import org.codehaus.plexus.util.StringUtils;
30  
31  import com.ibm.icu.text.CharsetDetector;
32  
33  /**
34   * Abstract File wrapper for Doxia converter.
35   *
36   * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
37   * @version $Id: AbstractFileWrapper.java 786981 2009-06-21 10:01:58Z ltheussl $
38   */
39  abstract class AbstractFileWrapper
40      extends AbstractWrapper
41  {
42      public static final String AUTO_ENCODING = "auto";
43  
44      private File file;
45  
46      private String encoding;
47  
48      /**
49       *
50       * @param absolutePath not null
51       * @param format could be null
52       * @param encoding could be null
53       * @param supportedFormat not null
54       * @throws UnsupportedEncodingException if the encoding is unsupported.
55       * @throws IllegalArgumentException if any
56       */
57      AbstractFileWrapper( String absolutePath, String format, String encoding, String[] supportedFormat )
58          throws UnsupportedEncodingException
59      {
60          super( format, supportedFormat );
61  
62          if ( StringUtils.isEmpty( absolutePath ) )
63          {
64              throw new IllegalArgumentException( "absolutePath is required" );
65          }
66  
67          File filetoset = new File( absolutePath );
68          if ( !filetoset.isAbsolute() )
69          {
70              filetoset = new File( new File( "" ).getAbsolutePath(), absolutePath );
71          }
72          this.file = filetoset;
73  
74          if ( StringUtils.isNotEmpty( encoding ) && !encoding.equalsIgnoreCase( encoding )
75              && !validateEncoding( encoding ) )
76          {
77              StringBuffer msg = new StringBuffer();
78              msg.append( "The encoding '" + encoding + "' is not a valid one. The supported charsets are: " );
79              msg.append( StringUtils.join( CharsetDetector.getAllDetectableCharsets(), ", " ) );
80              throw new UnsupportedEncodingException( msg.toString() );
81          }
82          this.encoding = ( StringUtils.isNotEmpty( encoding ) ? encoding : AUTO_ENCODING );
83      }
84  
85      /**
86       * @return the file
87       */
88      public File getFile()
89      {
90          return file;
91      }
92  
93      /**
94       * @param file new file.
95       */
96      void setFile( File file )
97      {
98          this.file = file;
99      }
100 
101     /**
102      * @return the encoding used for the file or <code>null</code> if not specified.
103      */
104     public String getEncoding()
105     {
106         return encoding;
107     }
108 
109     /**
110      * @param encoding new encoding.
111      */
112     void setEncoding( String encoding )
113     {
114         this.encoding = encoding;
115     }
116 
117     /**
118      * Validate if a charset is supported on this platform.
119      *
120      * @param charsetName the charsetName to be checked.
121      * @return <code>true</code> if the charset is supported by the JVM, <code>false</code> otherwise.
122      */
123     static boolean validateEncoding( String charsetName )
124     {
125         if ( StringUtils.isEmpty( charsetName ) )
126         {
127             return false;
128         }
129 
130         OutputStream ost = new ByteArrayOutputStream();
131         OutputStreamWriter osw = null;
132         try
133         {
134             osw = new OutputStreamWriter( ost, charsetName );
135         }
136         catch ( UnsupportedEncodingException exc )
137         {
138             return false;
139         }
140         finally
141         {
142             IOUtil.close( osw );
143         }
144         return true;
145     }
146 
147     /** {@inheritDoc} */
148     public boolean equals( Object other )
149     {
150         if ( this == other )
151         {
152             return true;
153         }
154 
155         if ( !( other instanceof AbstractFileWrapper ) )
156         {
157             return false;
158         }
159 
160         AbstractFileWrapper that = (AbstractFileWrapper) other;
161         boolean result = true;
162         result = result && super.equals( other );
163         result = result && ( getFile() == null ? that.getFile() == null : getFile().equals( that.getFile() ) );
164         return result;
165     }
166 
167     /** {@inheritDoc} */
168     public int hashCode()
169     {
170         final int result = super.hashCode();
171         final int hash = 37;
172 
173         return hash * result + ( getFile() != null ? getFile().hashCode() : 0 );
174     }
175 
176     /** {@inheritDoc} */
177     public java.lang.String toString()
178     {
179         StringBuffer buf = new StringBuffer( super.toString() + "\n" );
180         buf.append( "file= '" );
181         buf.append( getFile() + "'" );
182         return buf.toString();
183     }
184 }