001package org.apache.maven.plugins.enforcer; 002 003/* 004 * Licensed to the Apache Software Foundation (ASF) under one 005 * or more contributor license agreements. See the NOTICE file 006 * distributed with this work for additional information 007 * regarding copyright ownership. The ASF licenses this file 008 * to you under the Apache License, Version 2.0 (the 009 * "License"); you may not use this file except in compliance 010 * with the License. You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, 015 * software distributed under the License is distributed on an 016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 017 * KIND, either express or implied. See the License for the 018 * specific language governing permissions and limitations 019 * under the License. 020 */ 021 022import java.io.IOException; 023import java.io.InputStream; 024import java.io.Reader; 025import java.nio.charset.Charset; 026import java.nio.file.Files; 027 028import org.apache.commons.io.input.ReaderInputStream; 029import org.apache.commons.lang3.StringUtils; 030import org.apache.maven.enforcer.rule.api.EnforcerRuleException; 031import org.apache.maven.enforcer.rule.api.EnforcerRuleHelper; 032import org.apache.maven.plugins.enforcer.utils.NormalizeLineSeparatorReader; 033import org.apache.maven.plugins.enforcer.utils.NormalizeLineSeparatorReader.LineSeparator; 034import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException; 035 036/** 037 * Rule to validate a text file to match the specified checksum. 038 * 039 * @author Konrad Windszus 040 * @see RequireFileChecksum 041 */ 042public class RequireTextFileChecksum 043 extends RequireFileChecksum 044{ 045 private NormalizeLineSeparatorReader.LineSeparator normalizeLineSeparatorTo = LineSeparator.UNIX; 046 047 Charset encoding; 048 049 public void setNormalizeLineSeparatorTo( NormalizeLineSeparatorReader.LineSeparator normalizeLineSeparatorTo ) 050 { 051 this.normalizeLineSeparatorTo = normalizeLineSeparatorTo; 052 } 053 054 public void setEncoding( String encoding ) 055 { 056 this.encoding = Charset.forName( encoding ); 057 } 058 059 @Override 060 public void execute( EnforcerRuleHelper helper ) 061 throws EnforcerRuleException 062 { 063 // set defaults 064 if ( encoding == null ) 065 { 066 // https://maven.apache.org/plugins/maven-resources-plugin/examples/encoding.html 067 try 068 { 069 String encoding = (String) helper.evaluate( "${project.build.sourceEncoding}" ); 070 if ( StringUtils.isBlank( encoding ) ) 071 { 072 encoding = System.getProperty( "file.encoding" ); 073 helper.getLog().warn( "File encoding has not been set, using platform encoding " + encoding 074 + ". Build is platform dependent!" ); 075 } 076 this.encoding = Charset.forName( encoding ); 077 } 078 catch ( ExpressionEvaluationException e ) 079 { 080 throw new EnforcerRuleException( "Unable to retrieve the project's build source encoding " 081 + "(${project.build.sourceEncoding}): ", e ); 082 } 083 } 084 super.execute( helper ); 085 } 086 087 @Override 088 protected String calculateChecksum() 089 throws EnforcerRuleException 090 { 091 try ( Reader reader = new NormalizeLineSeparatorReader( Files.newBufferedReader( file.toPath(), encoding ), 092 normalizeLineSeparatorTo ); 093 InputStream inputStream = new ReaderInputStream( reader, encoding ) ) 094 { 095 return super.calculateChecksum( inputStream ); 096 } 097 catch ( IOException e ) 098 { 099 throw new EnforcerRuleException( "Unable to calculate checksum (with normalized line separators)", e ); 100 } 101 } 102}