001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * Portions Copyrighted 2007 Sun Microsystems, Inc.
027: *
028: * The Original Software is the Accelerators module.
029: * The Initial Developer of the Original Software is Andrei Badea.
030: * Portions Copyright 2005-2006 Andrei Badea.
031: * All Rights Reserved.
032: *
033: * If you wish your version of this file to be governed by only the CDDL
034: * or only the GPL Version 2, indicate your decision by adding
035: * "[Contributor] elects to include this software in this distribution
036: * under the [CDDL or GPL Version 2] license." If you do not indicate a
037: * single choice of license, a recipient has the option to distribute
038: * your version of this file under either the CDDL, the GPL Version 2 or
039: * to extend the choice of license to its licensees as provided above.
040: * However, if you add GPL Version 2 code and therefore, elected the GPL
041: * Version 2 license, then the option applies only if the new code is
042: * made subject to such option by the copyright holder.
043: *
044: */
045: package org.netbeans.modules.php.rt.providers.impl.local.apache;
046:
047: import java.io.File;
048: import java.io.FileFilter;
049: import java.io.FilenameFilter;
050: import java.util.regex.Matcher;
051: import java.util.regex.Pattern;
052:
053: /** Nice filter which based on the text finds out how to search.
054: * copied from org.netbeans.modules.jumpto.file.RegexpFileFilter
055: *
056: * @author phrebejk
057: */
058: public class RegexpFileFilter implements FileFilter, FilenameFilter {
059:
060: private String filterText;
061: private Pattern pattern;
062: private boolean caseSensitive;
063:
064: public RegexpFileFilter(String filterText) {
065: this (filterText, true);
066: }
067:
068: public RegexpFileFilter(String filterText, boolean caseSensitive) {
069:
070: // XXX Camel case
071:
072: this .caseSensitive = caseSensitive;
073:
074: if (!filterText.contains("?") && !filterText.contains("*")) {
075: this .filterText = caseSensitive ? filterText : filterText
076: .toLowerCase();
077: } else {
078: filterText = filterText.replace(".", "\\."); // NOI18N
079: filterText = filterText.replace("?", "."); // NOI18N
080: filterText = filterText.replace("*", ".*"); // NOI18N
081: this .filterText = filterText;
082: this .pattern = Pattern.compile(this .filterText,
083: caseSensitive ? 0 : Pattern.CASE_INSENSITIVE);
084: }
085: }
086:
087: public boolean accept(File file) {
088: return accept(null, file.getPath());
089: }
090:
091: public boolean accept(File dir, String name) {
092: if (pattern == null) {
093: return caseSensitive ? name.startsWith(filterText) : name
094: .toLowerCase().startsWith(filterText);
095: } else {
096: String testPath = dir == null ? name : new File(dir, name)
097: .getPath();
098: Matcher m = pattern.matcher(testPath);
099: return m.matches();
100: }
101:
102: }
103:
104: @Override
105: public String toString() {
106: return pattern.toString();
107: }
108:
109: public static int wildcardIndex(String s) {
110: int question = s.indexOf("?");
111: int asterisk = s.indexOf("*");
112: if (question != -1 && asterisk != -1)
113: return question < asterisk ? question : asterisk;
114: else if (asterisk == -1 && question == -1)
115: return -1;
116: else
117: return question != -1 ? question : asterisk;
118: }
119:
120: public static boolean onlyContainsWildcards(String s) {
121: for (int i = 0; i < s.length(); i++) {
122: char ch = s.charAt(i);
123: switch (ch) {
124: case '*': // NOI18N
125: case '?': // NOI18N
126: case '.': // NOI18N
127: continue;
128: default:
129: return false;
130: }
131: }
132: return true;
133: }
134:
135: }
|