de.fhg.iais.asc.sipmaker.SipMakerFileSearcher.java Source code

Java tutorial

Introduction

Here is the source code for de.fhg.iais.asc.sipmaker.SipMakerFileSearcher.java

Source

package de.fhg.iais.asc.sipmaker;

/******************************************************************************
 * 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.io.FilenameFilter;

import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;

public final class SipMakerFileSearcher {
    private static final String SIPMAKERS_PATH_PREFIX = "transformers_edm/";
    private static final String SIPMAKERS_SPLITTING_CONFIG_SUBPATH = "splitting/";
    //    private static final String[] SIPMAKERS_EXCLUDES = new String[]{"common","md_mappings"};
    private static final String SUFFIX = ".properties";
    private static final int SUFFIX_LENGTH = SUFFIX.length();

    private static final String[] NO_STRINGS = new String[0];

    private final File baseDir;

    public static SipMakerFileSearcher fromBaseDir(File baseDir) {
        File sipmakersPath = new File(baseDir, SIPMAKERS_PATH_PREFIX);
        return sipmakersPath.isDirectory() ? new SipMakerFileSearcher(baseDir) : null;
    }

    private SipMakerFileSearcher(File baseDir) {
        this.baseDir = baseDir;
    }

    public File getBaseDir() {
        return this.baseDir;
    }

    public File getSipMakersRootDir() {
        return new File(this.baseDir, SIPMAKERS_PATH_PREFIX);
    }

    public File getPropertiesFile(SipMakerKey key, boolean fuzzyMatch) {
        return findPathOrFile(key, fuzzyMatch, true);
    }

    public File getVersionsPath(SipMakerKey key, boolean fuzzyMatch) {
        return findPathOrFile(key, fuzzyMatch, false);
    }

    public File findPathOrFile(SipMakerKey key, boolean fuzzyMatch, boolean findFile) {
        boolean tryDirAsVersion = StringUtils.isEmpty(key.getVersion());
        for (String directory : key.getDirectories()) {
            File dir = new File(this.baseDir, SIPMAKERS_PATH_PREFIX + directory);

            if (dir.isDirectory()) {
                return findFile ? getPropertiesFile(dir, key.getVersion(), fuzzyMatch) : dir;
            }

            if (tryDirAsVersion) {
                File file = new File(dir.getPath() + SUFFIX);
                if (file.isFile()) {
                    return findFile ? file : file.getParentFile();
                }

                tryDirAsVersion = false;
            }

            if (!fuzzyMatch) {
                return null;
            }
        }

        return null;
    }

    private File getPropertiesFile(File path, String version, boolean fuzzyMatch) {
        path = new File(path, SIPMAKERS_SPLITTING_CONFIG_SUBPATH);

        if (!StringUtils.isEmpty(version)) {
            File versionFile = new File(path, version + SUFFIX);

            if (versionFile.exists()) {
                return versionFile;
            } else if (!fuzzyMatch) {
                return null;
            }
        }

        return findHighestVersion(path);
    }

    private File findHighestVersion(File versionsPath) {
        File bestFile = null;
        int best = -2;
        for (File file : getVersionFiles(versionsPath)) {
            int v = NumberUtils.toInt(FilenameUtils.getBaseName(file.getName()), -1);
            if (v > best) {
                bestFile = file;
                best = v;
            }
        }

        return bestFile;
    }

    private File[] getVersionFiles(File versionsPath) {
        return versionsPath.listFiles(new FilenameFilter() {
            @Override
            public boolean accept(File dir, String name) {
                return name.endsWith(SUFFIX) && new File(dir, name).isFile();
            }
        });
    }

    public String[] getVersions(SipMakerKey description) {
        File path = getVersionsPath(description, false);
        if (path == null) {
            return NO_STRINGS;
        }

        File[] files = getVersionFiles(path);
        int count = files.length;

        String[] result = new String[count];
        for (int i = 0; i < count; ++i) {
            String name = files[i].getName();
            result[i] = name.substring(0, name.length() - SUFFIX_LENGTH);
        }

        return result;
    }
}