de.fhg.iais.asc.workflow.AbstractScan.java Source code

Java tutorial

Introduction

Here is the source code for de.fhg.iais.asc.workflow.AbstractScan.java

Source

package de.fhg.iais.asc.workflow;

/******************************************************************************
 * Copyright 2011 (c) Fraunhofer IAIS Netmedia  http://www.iais.fraunhofer.de *
 * ************************************************************************** *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may    *
 * not use this file except in compliance with the License.                   *
 * You may obtain a copy of the License at                                    *
 * http://www.apache.org/licenses/LICENSE-2.0                                 *
 * Unless required by applicable law or agreed to in writing,                 *
 * software distributed under the License is distributed on an "AS IS" BASIS, *
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   *
 * See the License for the specific language governing permissions and        *
 * limitations under the License.                                             *
 ******************************************************************************/

import java.io.File;
import java.util.Locale;

import org.apache.commons.lang.StringUtils;

import de.fhg.iais.asc.commons.AscConfiguration;
import de.fhg.iais.commons.fs.FileEventListener;
import de.fhg.iais.commons.fs.FolderEventListener;

public abstract class AbstractScan implements FileEventListener, FolderEventListener {

    // private static final Logger LOG = LoggerFactory.getLogger(AbstractScan.class);
    protected AscConfiguration config = null;
    protected String source;
    protected String target;
    protected int counter = 0;
    protected int maxfiles = 0;

    //    protected ASCState ascstate;
    //    protected int totalProcessedSize;
    //    private int expectedFiles = -1; // -1 means 'undefined'; counter for the number of files that are expected

    public AbstractScan(AscConfiguration config) {
        this.config = config;
        //        this.ascstate = ctx.getASCState();
        this.maxfiles = this.config.get(AscConfiguration.MAX_FILES_EACH, 0);
    }

    protected AbstractScan() {
        // empty default c'tor
    }

    @Override
    public void expect(int expectcount) {
        //        this.expectedFiles = expectcount;
    }

    //    @Override
    //    public int expected() {
    //        return this.expectedFiles;
    //    }

    @Override
    public final boolean acceptFile(File file) {

        if (this.config == null) {
            return true;
        }

        StringBuffer pathBuffer = new StringBuffer(File.separator);

        String sourceFolder = this.config.get(AscConfiguration.SOURCE_FOLDER,
                this.config.get(AscConfiguration.FORMAT, ""));
        if (!StringUtils.isEmpty(sourceFolder)) {
            pathBuffer.append(sourceFolder).append(File.separator);
        }

        String event = this.config.get(AscConfiguration.INGEST_EVENT, "");
        if (!StringUtils.isEmpty(event) && !event.contains("*")) {
            pathBuffer.append(event).append(File.separator);
        }

        String pathToTest = pathBuffer.toString();

        String absolutePath = file.getAbsolutePath();
        boolean result = absolutePath.contains(pathToTest) && !file.getName().startsWith("toc_")
                && absolutePath.toLowerCase(Locale.GERMAN).endsWith(".xml");

        // Ensure maxfiles handling
        if (result && this.maxfiles > 0 && this.counter >= this.maxfiles) {
            return false;
        }
        this.counter++;

        return result;
    }

    /**
     * folder change
     */
    @Override
    public final boolean nextFolder(File file) {
        this.counter = 0;
        return false;
    }

    /**
     * change up
     */
    @Override
    public final void changeUp(File file) {
        // intentionally empty (supposed (cw))
    }

    /**
     * change down
     */
    @Override
    public final void changeDown(File file) {
        // intentionally empty (supposed (cw))
    }

    //    /**
    //     * determines the set from the file-objects file path
    //     *
    //     * @param file file with attached path
    //     * @return the name of the set
    //     */
    //    public String determineSetName(File file) {
    //        String lastFolder = (new File(file.getParentFile().getPath()).getName());
    //        String setName;
    //        try {
    //            Integer.parseInt(lastFolder);
    //            setName = "unknown"; // lastFolder seems to be an int. Set is unknown
    //        } catch ( NumberFormatException ex ) {
    //            // it's a non-int. So we got the setname in lastfolder
    //            setName = lastFolder;
    //        }
    //        return setName;
    //    }
    //
    //    protected String determineIngestIdOfSource(File file) {
    //        String lastFolder = (new File(file.getParentFile().getPath()).getName());
    //        try {
    //            Integer.parseInt(lastFolder);
    //            return lastFolder;
    //
    //        } catch ( NumberFormatException ex ) {
    //            // it's a non-int. So we got the setname in lastfolder
    //            // we need to move one level up
    //            lastFolder = (new File(file.getParentFile().getParentFile().getPath()).getName());
    //            return lastFolder;
    //        }
    //    }

    /**
     * Determine the target directory where the scanner should put data.
     * 
     * @return the directory location
     */
    public String getTarget() {
        return this.target;
    }

    /**
     * the source
     * 
     * @return the directory location
     */
    public String getSource() {
        return this.source;
    }

    public File getTargetFile(File sourceFile) {
        String sourceRoot = getSource();
        if (!sourceRoot.endsWith(File.separator)) {
            sourceRoot += File.separator;
        }

        String sourcePath = sourceFile.getPath();
        if (!StringUtils.startsWith(sourcePath, sourceRoot)) {
            return null; // sourceDir is not equal or child of sourceRoot
        }

        int index = sourceRoot.length(), length = sourcePath.length();
        while ((index < length) && (isSeparator(sourcePath, index))) {
            ++index;
        }

        String subPath = sourcePath.substring(index);
        return StringUtils.isEmpty(subPath) ? null : new File(getTarget(), subPath);
    }

    private static boolean isSeparator(String sourceDir, int index) {
        return sourceDir.charAt(index) == File.separatorChar;
    }
}