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 | 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 | |
|
87 | |
|
88 | |
public File getFile() |
89 | |
{ |
90 | 0 | return file; |
91 | |
} |
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
void setFile( File file ) |
97 | |
{ |
98 | 0 | this.file = file; |
99 | 0 | } |
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
public String getEncoding() |
105 | |
{ |
106 | 0 | return encoding; |
107 | |
} |
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
void setEncoding( String encoding ) |
113 | |
{ |
114 | 0 | this.encoding = encoding; |
115 | 0 | } |
116 | |
|
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | |
|
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 | |
|
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 | |
|
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 | |
|
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 | |
} |