View Javadoc
1   /*
2    * #%L
3    * License Maven Plugin
4    * 
5    * $Id: FileHeaderProcessor.java 13519 2011-02-05 09:32:50Z tchemit $
6    * $HeadURL: http://svn.codehaus.org/mojo/tags/license-maven-plugin-1.0/src/main/java/org/codehaus/mojo/license/header/FileHeaderProcessor.java $
7    * %%
8    * Copyright (C) 2008 - 2011 CodeLutin, Codehaus, Tony Chemit
9    * %%
10   * This program is free software: you can redistribute it and/or modify
11   * it under the terms of the GNU Lesser General Public License as 
12   * published by the Free Software Foundation, either version 3 of the 
13   * License, or (at your option) any later version.
14   * 
15   * This program is distributed in the hope that it will be useful,
16   * but WITHOUT ANY WARRANTY; without even the implied warranty of
17   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18   * GNU General Lesser Public License for more details.
19   * 
20   * You should have received a copy of the GNU General Lesser Public 
21   * License along with this program.  If not, see
22   * <http://www.gnu.org/licenses/lgpl-3.0.html>.
23   * #L%
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   * File header processor.
38   *
39   * @author tchemit <chemit@codelutin.com>
40   * @plexus.component role="org.nuiton.processor.Processor" role-hint="file-header"
41   * @since 1.0
42   */
43  public class FileHeaderProcessor
44      extends Processor
45  {
46  
47      /**
48       * processor configuration
49       */
50      protected FileHeaderProcessorConfiguration configuration;
51  
52      /**
53       * internal file header filter
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       * @return {@code true} if processed file was touched (says the header was
73       *         fully found), {@code false} otherwise
74       * @see FileHeaderFilter#isTouched()
75       */
76      public boolean isTouched()
77      {
78          return getFilter() != null && getFilter().isTouched();
79      }
80  
81      /**
82       * @return {@code true} if processed file was modified (says the header was
83       *         fully found and content changed), {@code false} otherwise
84       * @see FileHeaderFilter#isModified()
85       */
86      public boolean isModified()
87      {
88          return getFilter() != null && getFilter().isModified();
89      }
90  
91      /**
92       * @return {@code true} if header of header was detected
93       * @see FileHeaderFilter#isDetectHeader()
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             // change file header
137 
138             filter.setFileHeader( fileHeader );
139             change = true;
140         }
141         FileHeaderTransformer transformer = getConfiguration().getTransformer();
142         if ( !transformer.equals( filter.getTransformer() ) )
143         {
144 
145             // change file transformer
146 
147             filter.setTransformer( transformer );
148             change = true;
149         }
150         if ( change )
151         {
152 
153             // something has changed, must reset content cache
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 }