All Downloads are FREE. Search and download functionalities are using the official Maven repository.

ngmf.util.FilenameUtils Maven / Gradle / Ivy

Go to download

Object Modeling System (OMS) is a pure Java object-oriented framework. OMS v3.+ is a highly interoperable and lightweight modeling framework for component-based model and simulation development on multiple platforms.

There is a newer version: 3.5.12
Show newest version
/*
 * $Id: FilenameUtils.java 50798ee5e25c 2013-01-09 [email protected] $
 * 
 * This file is part of the Object Modeling System (OMS),
 * 2007-2012, Olaf David and others, Colorado State University.
 *
 * OMS 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, version 2.1.
 *
 * OMS 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with OMS.  If not, see .
 */
package ngmf.util;

import java.io.File;
import java.util.ArrayList;
import java.util.Stack;

public class FilenameUtils {

    /**
     * The extension separator character.
     */
    public static final char EXTENSION_SEPARATOR = '.';

    /**
     * The extension separator String.
     */
    public static final String EXTENSION_SEPARATOR_STR = (new Character(EXTENSION_SEPARATOR)).toString();

    /**
     * The Unix separator character.
     */
    private static final char UNIX_SEPARATOR = '/';

    /**
     * The Windows separator character.
     */
    private static final char WINDOWS_SEPARATOR = '\\';

    /**
     * The system separator character.
     */
    private static final char SYSTEM_SEPARATOR = File.separatorChar;

    /**
     * The separator character that is the opposite of the system separator.
     */
    private static final char OTHER_SEPARATOR;
    
    static {
        if (isSystemWindows()) {
            OTHER_SEPARATOR = UNIX_SEPARATOR;
        } else {
            OTHER_SEPARATOR = WINDOWS_SEPARATOR;
        }
    }

    /**
     * Instances should NOT be constructed in standard programming.
     */
    static boolean isSystemWindows() {
        return SYSTEM_SEPARATOR == WINDOWS_SEPARATOR;
    }

    /**
     * Checks a filename to see if it matches the specified wildcard matcher
     * allowing control over case-sensitivity.
     * 

* The wildcard matcher uses the characters '?' and '*' to represent a * single or multiple wildcard characters. * * @param filename the filename to match on * @param wildcardMatcher the wildcard string to match against * @param caseSensitivity what case sensitivity rule to use, null means case-sensitive * @return true if the filename matches the wilcard string */ public static boolean wildcardMatch(String filename, String wildcardMatcher, IOCase caseSensitivity) { if (filename == null && wildcardMatcher == null) { return true; } if (filename == null || wildcardMatcher == null) { return false; } if (caseSensitivity == null) { caseSensitivity = IOCase.SENSITIVE; } filename = caseSensitivity.convertCase(filename); wildcardMatcher = caseSensitivity.convertCase(wildcardMatcher); String[] wcs = splitOnTokens(wildcardMatcher); boolean anyChars = false; int textIdx = 0; int wcsIdx = 0; Stack backtrack = new Stack(); // loop around a backtrack stack, to handle complex * matching do { if (backtrack.size() > 0) { int[] array = (int[]) backtrack.pop(); wcsIdx = array[0]; textIdx = array[1]; anyChars = true; } // loop whilst tokens and text left to process while (wcsIdx < wcs.length) { if (wcs[wcsIdx].equals("?")) { // ? so move to next text char textIdx++; anyChars = false; } else if (wcs[wcsIdx].equals("*")) { // set any chars status anyChars = true; if (wcsIdx == wcs.length - 1) { textIdx = filename.length(); } } else { // matching text token if (anyChars) { // any chars then try to locate text token textIdx = filename.indexOf(wcs[wcsIdx], textIdx); if (textIdx == -1) { // token not found break; } int repeat = filename.indexOf(wcs[wcsIdx], textIdx + 1); if (repeat >= 0) { backtrack.push(new int[] {wcsIdx, repeat}); } } else { // matching from current position if (!filename.startsWith(wcs[wcsIdx], textIdx)) { // couldnt match token break; } } // matched text token, move text index to end of matched token textIdx += wcs[wcsIdx].length(); anyChars = false; } wcsIdx++; } // full match if (wcsIdx == wcs.length && textIdx == filename.length()) { return true; } } while (backtrack.size() > 0); return false; } /** * Splits a string into a number of tokens. * * @param text the text to split * @return the tokens, never null */ static String[] splitOnTokens(String text) { // used by wildcardMatch // package level so a unit test may run on this if (text.indexOf("?") == -1 && text.indexOf("*") == -1) { return new String[] { text }; } char[] array = text.toCharArray(); ArrayList list = new ArrayList(); StringBuffer buffer = new StringBuffer(); for (int i = 0; i < array.length; i++) { if (array[i] == '?' || array[i] == '*') { if (buffer.length() != 0) { list.add(buffer.toString()); buffer.setLength(0); } if (array[i] == '?') { list.add("?"); } else if (list.size() == 0 || (i > 0 && list.get(list.size() - 1).equals("*") == false)) { list.add("*"); } } else { buffer.append(array[i]); } } if (buffer.length() != 0) { list.add(buffer.toString()); } return (String[]) list.toArray( new String[ list.size() ] ); } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy