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