View Javadoc

1   /**
2    * Copyright 2010-2012 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * File header processor.
28   *
29   * @author tchemit <chemit@codelutin.com>
30   * @plexus.component role="org.nuiton.processor.Processor" role-hint="file-header"
31   * @since 1.0
32   */
33  public class FileHeaderProcessor
34      extends Processor
35  {
36  
37      /**
38       * processor configuration
39       */
40      protected FileHeaderProcessorConfiguration configuration;
41  
42      /**
43       * internal file header filter
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       * @return {@code true} if processed file was touched (says the header was
63       *         fully found), {@code false} otherwise
64       * @see FileHeaderFilter#isTouched()
65       */
66      public boolean isTouched()
67      {
68          return getFilter() != null && getFilter().isTouched();
69      }
70  
71      /**
72       * @return {@code true} if processed file was modified (says the header was
73       *         fully found and content changed), {@code false} otherwise
74       * @see FileHeaderFilter#isModified()
75       */
76      public boolean isModified()
77      {
78          return getFilter() != null && getFilter().isModified();
79      }
80  
81      /**
82       * @return {@code true} if header of header was detected
83       * @see FileHeaderFilter#isDetectHeader()
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             // change file header
127 
128             filter.setFileHeader( fileHeader );
129             change = true;
130         }
131         FileHeaderTransformer transformer = getConfiguration().getTransformer();
132         if ( !transformer.equals( filter.getTransformer() ) )
133         {
134 
135             // change file transformer
136 
137             filter.setTransformer( transformer );
138             change = true;
139         }
140         if ( change )
141         {
142 
143             // something has changed, must reset content cache
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 }