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