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.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
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 ( 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
86
87 public File getFile()
88 {
89 return file;
90 }
91
92
93
94
95 void setFile( File file )
96 {
97 this.file = file;
98 }
99
100
101
102
103 public String getEncoding()
104 {
105 return encoding;
106 }
107
108
109
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
142 @Override
143 public java.lang.String toString()
144 {
145 return super.toString() + "\n" + "file= '" + getFile() + "'";
146 }
147 }