1 package org.apache.maven.doxia.wrapper;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
35
36
37
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
51
52
53
54
55
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
87
88 public File getFile()
89 {
90 return file;
91 }
92
93
94
95
96 void setFile( File file )
97 {
98 this.file = file;
99 }
100
101
102
103
104 public String getEncoding()
105 {
106 return encoding;
107 }
108
109
110
111
112 void setEncoding( String encoding )
113 {
114 this.encoding = encoding;
115 }
116
117
118
119
120
121
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
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
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
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 }