001    /*
002     * #%L
003     * License Maven Plugin
004     * 
005     * $Id: FileHeaderProcessor.java 13519 2011-02-05 09:32:50Z tchemit $
006     * $HeadURL: http://svn.codehaus.org/mojo/tags/license-maven-plugin-1.0/src/main/java/org/codehaus/mojo/license/header/FileHeaderProcessor.java $
007     * %%
008     * Copyright (C) 2008 - 2011 CodeLutin, Codehaus, Tony Chemit
009     * %%
010     * This program is free software: you can redistribute it and/or modify
011     * it under the terms of the GNU Lesser General Public License as 
012     * published by the Free Software Foundation, either version 3 of the 
013     * License, or (at your option) any later version.
014     * 
015     * This program is distributed in the hope that it will be useful,
016     * but WITHOUT ANY WARRANTY; without even the implied warranty of
017     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
018     * GNU General Lesser Public License for more details.
019     * 
020     * You should have received a copy of the GNU General Lesser Public 
021     * License along with this program.  If not, see
022     * <http://www.gnu.org/licenses/lgpl-3.0.html>.
023     * #L%
024     */
025    
026    package org.codehaus.mojo.license.header;
027    
028    import org.codehaus.mojo.license.header.transformer.FileHeaderTransformer;
029    import org.nuiton.processor.Processor;
030    
031    import java.io.File;
032    import java.io.FileReader;
033    import java.io.FileWriter;
034    import java.io.IOException;
035    
036    /**
037     * File header processor.
038     *
039     * @author tchemit <chemit@codelutin.com>
040     * @plexus.component role="org.nuiton.processor.Processor" role-hint="file-header"
041     * @since 1.0
042     */
043    public class FileHeaderProcessor
044        extends Processor
045    {
046    
047        /**
048         * processor configuration
049         */
050        protected FileHeaderProcessorConfiguration configuration;
051    
052        /**
053         * internal file header filter
054         */
055        protected FileHeaderFilter filter;
056    
057        public FileHeaderProcessor()
058        {
059        }
060    
061        public FileHeaderProcessorConfiguration getConfiguration()
062        {
063            return configuration;
064        }
065    
066        public FileHeaderFilter getFilter()
067        {
068            return filter;
069        }
070    
071        /**
072         * @return {@code true} if processed file was touched (says the header was
073         *         fully found), {@code false} otherwise
074         * @see FileHeaderFilter#isTouched()
075         */
076        public boolean isTouched()
077        {
078            return getFilter() != null && getFilter().isTouched();
079        }
080    
081        /**
082         * @return {@code true} if processed file was modified (says the header was
083         *         fully found and content changed), {@code false} otherwise
084         * @see FileHeaderFilter#isModified()
085         */
086        public boolean isModified()
087        {
088            return getFilter() != null && getFilter().isModified();
089        }
090    
091        /**
092         * @return {@code true} if header of header was detected
093         * @see FileHeaderFilter#isDetectHeader()
094         */
095        public boolean isDetectHeader()
096        {
097            return getFilter() != null && getFilter().isDetectHeader();
098        }
099    
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    }