1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 package org.codehaus.mojo.license.header;
27
28 import org.codehaus.mojo.license.header.transformer.FileHeaderTransformer;
29 import org.nuiton.processor.Processor;
30
31 import java.io.File;
32 import java.io.FileReader;
33 import java.io.FileWriter;
34 import java.io.IOException;
35
36
37
38
39
40
41
42
43 public class FileHeaderProcessor
44 extends Processor
45 {
46
47
48
49
50 protected FileHeaderProcessorConfiguration configuration;
51
52
53
54
55 protected FileHeaderFilter filter;
56
57 public FileHeaderProcessor()
58 {
59 }
60
61 public FileHeaderProcessorConfiguration getConfiguration()
62 {
63 return configuration;
64 }
65
66 public FileHeaderFilter getFilter()
67 {
68 return filter;
69 }
70
71
72
73
74
75
76 public boolean isTouched()
77 {
78 return getFilter() != null && getFilter().isTouched();
79 }
80
81
82
83
84
85
86 public boolean isModified()
87 {
88 return getFilter() != null && getFilter().isModified();
89 }
90
91
92
93
94
95 public boolean isDetectHeader()
96 {
97 return getFilter() != null && getFilter().isDetectHeader();
98 }
99
100 public void process( File filein, File fileout )
101 throws IOException, IllegalStateException
102 {
103
104 checkInit();
105 reset();
106
107 FileReader input = new FileReader( filein );
108 try
109 {
110 FileWriter output = new FileWriter( fileout );
111 try
112 {
113 process( input, output );
114 }
115 finally
116 {
117 output.close();
118 }
119 }
120 finally
121 {
122 input.close();
123 }
124 }
125
126 public void populateFilter()
127 {
128 FileHeader fileHeader = getConfiguration().getFileHeader();
129 boolean change = false;
130
131 FileHeaderFilter filter = getFilter();
132
133 if ( !fileHeader.equals( filter.getFileHeader() ) )
134 {
135
136
137
138 filter.setFileHeader( fileHeader );
139 change = true;
140 }
141 FileHeaderTransformer transformer = getConfiguration().getTransformer();
142 if ( !transformer.equals( filter.getTransformer() ) )
143 {
144
145
146
147 filter.setTransformer( transformer );
148 change = true;
149 }
150 if ( change )
151 {
152
153
154 filter.resetContent();
155 }
156 }
157
158 public void setConfiguration( FileHeaderProcessorConfiguration configuration )
159 {
160 this.configuration = configuration;
161 }
162
163 public void setFilter( FileHeaderFilter filter )
164 {
165 this.filter = filter;
166 setInputFilter( filter );
167 }
168
169 public void reset()
170 {
171 if ( filter != null )
172 {
173 filter.reset();
174 }
175 }
176
177 protected FileHeader getFileHeader()
178 {
179 return getConfiguration().getFileHeader();
180 }
181
182 protected FileHeaderTransformer getTransformer()
183 {
184 return getConfiguration().getTransformer();
185 }
186
187 protected void checkInit()
188 throws IllegalStateException
189 {
190 if ( getConfiguration() == null )
191 {
192 throw new IllegalStateException( "no configuration set." );
193 }
194 if ( getFileHeader() == null )
195 {
196 throw new IllegalStateException( "no file header set." );
197 }
198 if ( getTransformer() == null )
199 {
200 throw new IllegalStateException( "no file header transformer set." );
201 }
202 if ( getFilter() == null )
203 {
204 throw new IllegalStateException( "no file header filter set." );
205 }
206 }
207 }