Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

import java.io.File;
import java.io.FileFilter;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

/*
 *  Licensed to the Apache Software Foundation (ASF) under one
 *  or more contributor license agreements.  See the NOTICE file
 *  distributed with this work for additional information
 *  regarding copyright ownership.  The ASF licenses this file
 *  to you 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. 
 *  
 */

/**
 * Various string manipulation methods that are more efficient then chaining
 * string operations: all is done in the same buffer without creating a bunch of
 * string objects.
 * 
 * @author <a href="mailto:dev@labs.apache.org">Dungeon Project</a>
 */
public class Main {

    /**
     * Splits apart a OS separator delimited set of paths in a string into
     * multiple Strings. File component path strings are returned within a List
     * in the order they are found in the composite path string. Optionally, a
     * file filter can be used to filter out path strings to control the
     * components returned. If the filter is null all path components are
     * returned.
     * 
     * @param paths
     *            a set of paths delimited using the OS path separator
     * @param filter
     *            a FileFilter used to filter the return set
     * @return the filter accepted path component Strings in the order
     *         encountered
     */
    public static final List getPaths(String paths, FileFilter filter) {
        int start = 0;
        int stop = -1;
        String path = null;
        ArrayList<String> list = new ArrayList<String>();

        // Abandon with no values if paths string is null
        if (paths == null || paths.trim().equals("")) {
            return list;
        }

        final int max = paths.length() - 1;

        // Loop spliting string using OS path separator: terminate
        // when the start index is at the end of the paths string.
        while (start < max) {
            stop = paths.indexOf(File.pathSeparatorChar, start);

            // The is no file sep between the start and the end of the string
            if (stop == -1) {
                // If we have a trailing path remaining without ending separator
                if (start < max) {
                    // Last path is everything from start to the string's end
                    path = paths.substring(start);

                    // Protect against consecutive separators side by side
                    if (!path.trim().equals("")) {
                        // If filter is null add path, if it is not null add the
                        // path only if the filter accepts the path component.
                        if (filter == null || filter.accept(new File(path))) {
                            list.add(path);
                        }
                    }
                }

                break; // Exit loop no more path components left!
            }

            // There is a separator between start and the end if we got here!
            // start index is now at 0 or the index of last separator + 1
            // stop index is now at next separator in front of start index
            path = paths.substring(start, stop);

            // Protect against consecutive separators side by side
            if (!path.trim().equals("")) {
                // If filter is null add path, if it is not null add the path
                // only if the filter accepts the path component.
                if (filter == null || filter.accept(new File(path))) {
                    list.add(path);
                }
            }

            // Advance start index past separator to start of next path comp
            start = stop + 1;
        }

        return list;
    }

}