View Javadoc
1   package org.apache.maven.plugins.enforcer;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.IOException;
23  import java.io.InputStream;
24  import java.io.Reader;
25  import java.nio.charset.Charset;
26  import java.nio.file.Files;
27  
28  import org.apache.commons.io.input.ReaderInputStream;
29  import org.apache.commons.lang3.StringUtils;
30  import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
31  import org.apache.maven.enforcer.rule.api.EnforcerRuleHelper;
32  import org.apache.maven.plugins.enforcer.utils.NormalizeLineSeparatorReader;
33  import org.apache.maven.plugins.enforcer.utils.NormalizeLineSeparatorReader.LineSeparator;
34  import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException;
35  
36  /**
37   * Rule to validate a text file to match the specified checksum.
38   *
39   * @author Konrad Windszus
40   * @see RequireFileChecksum
41   */
42  public class RequireTextFileChecksum
43      extends RequireFileChecksum
44  {
45      private NormalizeLineSeparatorReader.LineSeparator normalizeLineSeparatorTo = LineSeparator.UNIX;
46  
47      Charset encoding;
48  
49      public void setNormalizeLineSeparatorTo( NormalizeLineSeparatorReader.LineSeparator normalizeLineSeparatorTo )
50      {
51          this.normalizeLineSeparatorTo = normalizeLineSeparatorTo;
52      }
53  
54      public void setEncoding( String encoding )
55      {
56          this.encoding = Charset.forName( encoding );
57      }
58  
59      @Override
60      public void execute( EnforcerRuleHelper helper )
61          throws EnforcerRuleException
62      {
63          // set defaults
64          if ( encoding == null )
65          {
66              // https://maven.apache.org/plugins/maven-resources-plugin/examples/encoding.html
67              try
68              {
69                  String encoding = (String) helper.evaluate( "${project.build.sourceEncoding}" );
70                  if ( StringUtils.isBlank( encoding ) )
71                  {
72                      encoding = System.getProperty( "file.encoding" );
73                      helper.getLog().warn( "File encoding has not been set, using platform encoding " + encoding
74                          + ". Build is platform dependent!" );
75                  }
76                  this.encoding = Charset.forName( encoding );
77              }
78              catch ( ExpressionEvaluationException e )
79              {
80                  throw new EnforcerRuleException( "Unable to retrieve the project's build source encoding "
81                      + "(${project.build.sourceEncoding}): ", e );
82              }
83          }
84          super.execute( helper );
85      }
86  
87      @Override
88      protected String calculateChecksum()
89          throws EnforcerRuleException
90      {
91          try ( Reader reader = new NormalizeLineSeparatorReader( Files.newBufferedReader( file.toPath(), encoding ),
92                                                                  normalizeLineSeparatorTo );
93                          InputStream inputStream = new ReaderInputStream( reader, encoding ) )
94          {
95              return super.calculateChecksum( inputStream );
96          }
97          catch ( IOException e )
98          {
99              throw new EnforcerRuleException( "Unable to calculate checksum (with normalized line separators)", e );
100         }
101     }
102 }