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