001: /*
002: * This file is part of the WfMOpen project.
003: * Copyright (C) 2001-2006 Danet GmbH (www.danet.de), BU BTS.
004: * All rights reserved.
005: *
006: * This program is free software; you can redistribute it and/or modify
007: * it under the terms of the GNU General Public License as published by
008: * the Free Software Foundation; either version 2 of the License, or
009: * (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: * Based on code from the Apache ORO package, original copyright follows:
021: *
022: * Copyright 2000-2005 The Apache Software Foundation
023: *
024: * Licensed under the Apache License, Version 2.0 (the "License");
025: * you may not use this file except in compliance with the License.
026: * You may obtain a copy of the License at
027: *
028: * http://www.apache.org/licenses/LICENSE-2.0
029: *
030: * Unless required by applicable law or agreed to in writing, software
031: * distributed under the License is distributed on an "AS IS" BASIS,
032: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
033: * See the License for the specific language governing permissions and
034: * limitations under the License.
035: *
036: * $Id: Glob.java,v 1.2 2007/03/27 21:59:42 mlipp Exp $
037: *
038: * $Log: Glob.java,v $
039: * Revision 1.2 2007/03/27 21:59:42 mlipp
040: * Fixed lots of checkstyle warnings.
041: *
042: * Revision 1.1 2007/02/26 14:33:25 drmlipp
043: * Implemented filter.
044: *
045: */
046: package de.danet.an.util;
047:
048: /**
049: * This class provides a glob expression to regular expression converter.
050: *
051: * @author Michael Lipp
052: */
053: public final class Glob {
054:
055: private Glob() {
056: // Prevent construction.
057: }
058:
059: private static boolean isRegExpMetaCharacter(char ch) {
060: return "*?+[]()|^$.{}\\".indexOf(ch) >= 0;
061: }
062:
063: private static boolean isGlobMetaCharacter(char ch) {
064: return "*?[]".indexOf(ch) >= 0;
065: }
066:
067: /**
068: * Verifies if the given expression is a glob expression or just a string
069: * constant.
070: * @param expression the expression to verify
071: * @return the result
072: */
073: public static boolean isGlobExpression(String expression) {
074: char[] patbytes = expression.toCharArray();
075: for (int i = 0; i < patbytes.length; i++) {
076: if (isGlobMetaCharacter(patbytes[i])) {
077: return true;
078: }
079: }
080: return false;
081: }
082:
083: /**
084: * This static method takes a glob expression in the form of a String
085: * and converts it into a String representation of a Perl5 pattern.
086: * <p>
087: * @param pattern A string representation of a Glob pattern.
088: * @return A String representation of a Perl5 pattern equivalent to the
089: * Glob pattern.
090: */
091: public static String globToRegexp(String pattern) {
092: char[] patbytes = pattern.toCharArray();
093: boolean inCharSet;
094: int ch;
095: StringBuffer buffer;
096:
097: buffer = new StringBuffer(2 * patbytes.length);
098: inCharSet = false;
099:
100: for (ch = 0; ch < patbytes.length; ch++) {
101: switch (patbytes[ch]) {
102: case '*':
103: if (inCharSet) {
104: buffer.append('*');
105: } else {
106: buffer.append(".*");
107: }
108: break;
109: case '?':
110: if (inCharSet) {
111: buffer.append('?');
112: } else {
113: buffer.append('.');
114: }
115: break;
116: case '[':
117: inCharSet = true;
118: buffer.append(patbytes[ch]);
119:
120: if (ch + 1 < patbytes.length) {
121: switch (patbytes[ch + 1]) {
122: case '!':
123: case '^':
124: buffer.append('^');
125: ++ch;
126: continue;
127: case ']':
128: buffer.append(']');
129: ++ch;
130: continue;
131: default:
132: break;
133: }
134: }
135: break;
136: case ']':
137: inCharSet = false;
138: buffer.append(patbytes[ch]);
139: break;
140: case '\\':
141: buffer.append('\\');
142: if (ch == patbytes.length - 1) {
143: buffer.append('\\');
144: } else if (isGlobMetaCharacter(patbytes[ch + 1])) {
145: buffer.append(patbytes[++ch]);
146: } else {
147: buffer.append('\\');
148: }
149: break;
150: default:
151: if (!inCharSet && isRegExpMetaCharacter(patbytes[ch])) {
152: buffer.append('\\');
153: }
154: buffer.append(patbytes[ch]);
155: break;
156: }
157: }
158:
159: return buffer.toString();
160: }
161:
162: }
|