com.anrisoftware.globalpom.textmatch.match.DefaultMatchText.java Source code

Java tutorial

Introduction

Here is the source code for com.anrisoftware.globalpom.textmatch.match.DefaultMatchText.java

Source

/*
 * Copyright 2013-2014 Erwin Mller <erwin.mueller@deventm.org>
 *
 * This file is part of globalpomutils-core.
 *
 * globalpomutils-core is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by the
 * Free Software Foundation, either version 3 of the License, or (at your
 * option) any later version.
 *
 * globalpomutils-core is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with globalpomutils-core. If not, see <http://www.gnu.org/licenses/>.
 */
package com.anrisoftware.globalpom.textmatch.match;

import static com.anrisoftware.globalpom.charset.SerializableCharset.decorateSerializableCharset;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.regex.Pattern;

import javax.inject.Inject;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.builder.ToStringBuilder;

import com.anrisoftware.globalpom.charset.SerializableCharset;
import com.google.inject.assistedinject.Assisted;
import com.google.inject.assistedinject.AssistedInject;

/**
 * Compare the content of a text file to a given pattern.
 * 
 * @author Erwin Mueller, erwin.mueller@deventm.org
 * @since 2.0
 */
@SuppressWarnings("serial")
public class DefaultMatchText implements Serializable {

    private static final String CHARACTER_SET = "character set";

    private static final String PATTERN = "pattern";

    private final URI resource;

    private final Pattern pattern;

    private final SerializableCharset charset;

    @Inject
    private DefaultMatchTextWorkerLogger log;

    private transient boolean matches;

    /**
     * @see DefaultMatchTextFactory#create(File, Pattern, Charset)
     */
    @AssistedInject
    DefaultMatchText(@Assisted File file, @Assisted Pattern pattern, @Assisted Charset charset) {
        this(file.toURI(), pattern, charset);
    }

    /**
     * @see DefaultMatchTextFactory#create(URL, Pattern, Charset)
     */
    @AssistedInject
    DefaultMatchText(@Assisted URL resource, @Assisted Pattern pattern, @Assisted Charset charset)
            throws URISyntaxException {
        this(resource.toURI(), pattern, charset);
    }

    /**
     * @see DefaultMatchTextFactory#create(URI, Pattern, Charset)
     */
    @AssistedInject
    DefaultMatchText(@Assisted URI resource, @Assisted Pattern pattern, @Assisted Charset charset) {
        this.resource = resource;
        this.pattern = pattern;
        this.charset = decorateSerializableCharset(charset);
    }

    /**
     * Matches the resource text and replaces the found text.
     * 
     * @return this {@link DefaultMatchText}.
     * 
     * @throws MatchTextException
     *             if there was an error reading the resource.
     */
    public DefaultMatchText match() throws MatchTextException {
        String string = readFile();
        compareText(string);
        return this;
    }

    private void compareText(String string) {
        matches = pattern.matcher(string).find();
        if (matches) {
            log.textWasFound(this);
        } else {
            log.textWasNotFound(this);
        }
    }

    private String readFile() throws MatchTextException {
        try {
            Charset encoding = charset.getCharset();
            InputStream input = resource.toURL().openStream();
            return IOUtils.toString(input, encoding);
        } catch (IOException e) {
            throw log.readResourceError(this, e);
        }
    }

    /**
     * Returns if the comparison pattern matches or not.
     * 
     * @return {@code true} if it's matching, {@code false} if not.
     */
    public boolean isMatches() {
        return matches;
    }

    /**
     * Returns the text resource in which the text is compared to.
     * 
     * @return the {@link URI}.
     */
    public URI getResource() {
        return resource;
    }

    /**
     * Returns the comparison pattern.
     * 
     * @return the comparison {@link Pattern}.
     */
    public Pattern getPattern() {
        return pattern;
    }

    @Override
    public String toString() {
        return new ToStringBuilder(this).append(resource).append(PATTERN, pattern).append(CHARACTER_SET, charset)
                .toString();
    }
}