001/*
002 * Minify Maven Plugin
003 * https://github.com/samaxes/minify-maven-plugin
004 *
005 * Copyright (c) 2009 samaxes.com
006 *
007 * Licensed under the Apache License, Version 2.0 (the "License");
008 * you may not use this file except in compliance with the License.
009 * You may obtain a copy of the License at
010 *
011 *      http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 */
019package com.samaxes.maven.minify.common;
020
021import org.apache.maven.plugin.logging.Log;
022
023import java.io.File;
024import java.io.FileInputStream;
025import java.io.FileNotFoundException;
026import java.io.InputStream;
027import java.util.Enumeration;
028import java.util.List;
029import java.util.NoSuchElementException;
030
031/**
032 * Used to initialize a {@code SequenceInputStream} with a {@code Enumeration<? extends InputStream>}. The input streams
033 * that are produced by the enumeration will be read, in order, to provide the bytes to be read from the
034 * {@code SequenceInputStream}.
035 */
036public class SourceFilesEnumeration implements Enumeration<InputStream> {
037
038    private List<File> files;
039
040    private int current = 0;
041
042    /**
043     * Enumeration public constructor.
044     *
045     * @param log     Maven plugin log
046     * @param files   list of files
047     * @param verbose show source file paths in log output
048     */
049    public SourceFilesEnumeration(Log log, List<File> files, boolean verbose) {
050        this.files = files;
051
052        for (File file : files) {
053            log.info("Processing source file [" + ((verbose) ? file.getPath() : file.getName()) + "].");
054        }
055    }
056
057    /**
058     * Tests if this enumeration contains more elements.
059     *
060     * @return {@code true} if and only if this enumeration object contains at least one more element to provide;
061     * {@code false} otherwise.
062     */
063    @Override
064    public boolean hasMoreElements() {
065        return (current < files.size());
066    }
067
068    /**
069     * Returns the next element of this enumeration if this enumeration object has at least one more element to provide.
070     *
071     * @return the next element of this enumeration.
072     * @throws NoSuchElementException if no more elements exist.
073     */
074    @Override
075    public InputStream nextElement() {
076        InputStream is;
077
078        if (!hasMoreElements()) {
079            throw new NoSuchElementException("No more files!");
080        } else {
081            File nextElement = files.get(current);
082            current++;
083
084            try {
085                is = new FileInputStream(nextElement);
086            } catch (FileNotFoundException e) {
087                throw new NoSuchElementException("The path [" + nextElement.getPath() + "] cannot be found.");
088            }
089        }
090
091        return is;
092    }
093}