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.File;
23  import java.io.UnsupportedEncodingException;
24  import java.nio.charset.Charset;
25  import java.util.Objects;
26  
27  import org.codehaus.plexus.util.StringUtils;
28  
29  import com.ibm.icu.text.CharsetDetector;
30  
31  import static org.codehaus.plexus.util.StringUtils.isEmpty;
32  import static org.codehaus.plexus.util.StringUtils.isNotEmpty;
33  
34  /**
35   * Abstract File wrapper for Doxia converter.
36   *
37   * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
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 ( 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 ( isNotEmpty( encoding ) && !encoding.equalsIgnoreCase( encoding )
75              && !Charset.isSupported( encoding ) )
76          {
77              throw new UnsupportedEncodingException( "The encoding '" + encoding
78                      + "' is not a valid one. The supported charsets are: "
79                      + StringUtils.join( CharsetDetector.getAllDetectableCharsets(), ", " ) );
80          }
81          this.encoding = ( isNotEmpty( encoding ) ? encoding : AUTO_ENCODING );
82      }
83  
84      /**
85       * @return the file
86       */
87      public File getFile()
88      {
89          return file;
90      }
91  
92      /**
93       * @param file new file.
94       */
95      void setFile( File file )
96      {
97          this.file = file;
98      }
99  
100     /**
101      * @return the encoding used for the file or <code>null</code> if not specified.
102      */
103     public String getEncoding()
104     {
105         return encoding;
106     }
107 
108     /**
109      * @param encoding new encoding.
110      */
111     void setEncoding( String encoding )
112     {
113         this.encoding = encoding;
114     }
115 
116     @Override
117     public boolean equals( Object o )
118     {
119         if ( this == o )
120         {
121             return true;
122         }
123         if ( o == null || getClass() != o.getClass() )
124         {
125             return false;
126         }
127         if ( !super.equals( o ) )
128         {
129             return false;
130         }
131         AbstractFileWrapper that = (AbstractFileWrapper) o;
132         return Objects.equals( getFile(), that.getFile() );
133     }
134 
135     @Override
136     public int hashCode()
137     {
138         return Objects.hash( super.hashCode(), getFile() );
139     }
140 
141     /** {@inheritDoc} */
142     @Override
143     public java.lang.String toString()
144     {
145         return super.toString() + "\n" + "file= '" + getFile() + "'";
146     }
147 }