001 /** 002 * Copyright 2010-2013 The Kuali Foundation 003 * 004 * Licensed under the Educational Community License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.opensource.org/licenses/ecl2.php 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 package org.codehaus.mojo.license.header; 017 018 import org.codehaus.mojo.license.header.transformer.FileHeaderTransformer; 019 import org.nuiton.processor.Processor; 020 021 import java.io.File; 022 import java.io.FileReader; 023 import java.io.FileWriter; 024 import java.io.IOException; 025 026 /** 027 * File header processor. 028 * 029 * @author tchemit <chemit@codelutin.com> 030 * @plexus.component role="org.nuiton.processor.Processor" role-hint="file-header" 031 * @since 1.0 032 */ 033 public class FileHeaderProcessor 034 extends Processor 035 { 036 037 /** 038 * processor configuration 039 */ 040 protected FileHeaderProcessorConfiguration configuration; 041 042 /** 043 * internal file header filter 044 */ 045 protected FileHeaderFilter filter; 046 047 public FileHeaderProcessor() 048 { 049 } 050 051 public FileHeaderProcessorConfiguration getConfiguration() 052 { 053 return configuration; 054 } 055 056 public FileHeaderFilter getFilter() 057 { 058 return filter; 059 } 060 061 /** 062 * @return {@code true} if processed file was touched (says the header was 063 * fully found), {@code false} otherwise 064 * @see FileHeaderFilter#isTouched() 065 */ 066 public boolean isTouched() 067 { 068 return getFilter() != null && getFilter().isTouched(); 069 } 070 071 /** 072 * @return {@code true} if processed file was modified (says the header was 073 * fully found and content changed), {@code false} otherwise 074 * @see FileHeaderFilter#isModified() 075 */ 076 public boolean isModified() 077 { 078 return getFilter() != null && getFilter().isModified(); 079 } 080 081 /** 082 * @return {@code true} if header of header was detected 083 * @see FileHeaderFilter#isDetectHeader() 084 */ 085 public boolean isDetectHeader() 086 { 087 return getFilter() != null && getFilter().isDetectHeader(); 088 } 089 090 public void process( File filein, File fileout ) 091 throws IOException, IllegalStateException 092 { 093 094 checkInit(); 095 reset(); 096 097 FileReader input = new FileReader( filein ); 098 try 099 { 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 }