Coverage Report - org.apache.maven.doxia.wrapper.AbstractFileWrapper
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractFileWrapper
0%
0/46
0%
0/32
2,444
 
 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  0
         super( format, supportedFormat );
 61  
 
 62  0
         if ( StringUtils.isEmpty( absolutePath ) )
 63  
         {
 64  0
             throw new IllegalArgumentException( "absolutePath is required" );
 65  
         }
 66  
 
 67  0
         File filetoset = new File( absolutePath );
 68  0
         if ( !filetoset.isAbsolute() )
 69  
         {
 70  0
             filetoset = new File( new File( "" ).getAbsolutePath(), absolutePath );
 71  
         }
 72  0
         this.file = filetoset;
 73  
 
 74  0
         if ( StringUtils.isNotEmpty( encoding ) && !encoding.equalsIgnoreCase( encoding )
 75  
             && !validateEncoding( encoding ) )
 76  
         {
 77  0
             StringBuffer msg = new StringBuffer();
 78  0
             msg.append( "The encoding '" + encoding + "' is not a valid one. The supported charsets are: " );
 79  0
             msg.append( StringUtils.join( CharsetDetector.getAllDetectableCharsets(), ", " ) );
 80  0
             throw new UnsupportedEncodingException( msg.toString() );
 81  
         }
 82  0
         this.encoding = ( StringUtils.isNotEmpty( encoding ) ? encoding : AUTO_ENCODING );
 83  0
     }
 84  
 
 85  
     /**
 86  
      * @return the file
 87  
      */
 88  
     public File getFile()
 89  
     {
 90  0
         return file;
 91  
     }
 92  
 
 93  
     /**
 94  
      * @param file new file.
 95  
      */
 96  
     void setFile( File file )
 97  
     {
 98  0
         this.file = file;
 99  0
     }
 100  
 
 101  
     /**
 102  
      * @return the encoding used for the file or <code>null</code> if not specified.
 103  
      */
 104  
     public String getEncoding()
 105  
     {
 106  0
         return encoding;
 107  
     }
 108  
 
 109  
     /**
 110  
      * @param encoding new encoding.
 111  
      */
 112  
     void setEncoding( String encoding )
 113  
     {
 114  0
         this.encoding = encoding;
 115  0
     }
 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  0
         if ( StringUtils.isEmpty( charsetName ) )
 126  
         {
 127  0
             return false;
 128  
         }
 129  
 
 130  0
         OutputStream ost = new ByteArrayOutputStream();
 131  0
         OutputStreamWriter osw = null;
 132  
         try
 133  
         {
 134  0
             osw = new OutputStreamWriter( ost, charsetName );
 135  
         }
 136  0
         catch ( UnsupportedEncodingException exc )
 137  
         {
 138  0
             return false;
 139  
         }
 140  
         finally
 141  
         {
 142  0
             IOUtil.close( osw );
 143  0
         }
 144  0
         return true;
 145  
     }
 146  
 
 147  
     /** {@inheritDoc} */
 148  
     public boolean equals( Object other )
 149  
     {
 150  0
         if ( this == other )
 151  
         {
 152  0
             return true;
 153  
         }
 154  
 
 155  0
         if ( !( other instanceof AbstractFileWrapper ) )
 156  
         {
 157  0
             return false;
 158  
         }
 159  
 
 160  0
         AbstractFileWrapper that = (AbstractFileWrapper) other;
 161  0
         boolean result = true;
 162  0
         result = result && super.equals( other );
 163  0
         result = result && ( getFile() == null ? that.getFile() == null : getFile().equals( that.getFile() ) );
 164  0
         return result;
 165  
     }
 166  
 
 167  
     /** {@inheritDoc} */
 168  
     public int hashCode()
 169  
     {
 170  0
         final int result = super.hashCode();
 171  0
         final int hash = 37;
 172  
 
 173  0
         return hash * result + ( getFile() != null ? getFile().hashCode() : 0 );
 174  
     }
 175  
 
 176  
     /** {@inheritDoc} */
 177  
     public java.lang.String toString()
 178  
     {
 179  0
         StringBuffer buf = new StringBuffer( super.toString() + "\n" );
 180  0
         buf.append( "file= '" );
 181  0
         buf.append( getFile() + "'" );
 182  0
         return buf.toString();
 183  
     }
 184  
 }