/*
* This file is part of the WfMOpen project.
* Copyright (C) 2001-2006 Danet GmbH (www.danet.de), BU BTS.
* All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Based on code from the Apache ORO package, original copyright follows:
*
* Copyright 2000-2005 The Apache Software Foundation
*
* 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.
*
* $Id: Glob.java,v 1.2 2007/03/27 21:59:42 mlipp Exp $
*
* $Log: Glob.java,v $
* Revision 1.2 2007/03/27 21:59:42 mlipp
* Fixed lots of checkstyle warnings.
*
* Revision 1.1 2007/02/26 14:33:25 drmlipp
* Implemented filter.
*
*/
package de.danet.an.util;
/**
* This class provides a glob expression to regular expression converter.
*
* @author Michael Lipp
*/
public final class Glob {
private Glob() {
// Prevent construction.
}
private static boolean isRegExpMetaCharacter(char ch) {
return "*?+[]()|^$.{}\\".indexOf(ch) >= 0;
}
private static boolean isGlobMetaCharacter(char ch) {
return "*?[]".indexOf(ch) >= 0;
}
/**
* Verifies if the given expression is a glob expression or just a string
* constant.
* @param expression the expression to verify
* @return the result
*/
public static boolean isGlobExpression (String expression) {
char[] patbytes = expression.toCharArray();
for (int i = 0; i < patbytes.length; i++) {
if (isGlobMetaCharacter(patbytes[i])) {
return true;
}
}
return false;
}
/**
* This static method takes a glob expression in the form of a String
* and converts it into a String representation of a Perl5 pattern.
* <p>
* @param pattern A string representation of a Glob pattern.
* @return A String representation of a Perl5 pattern equivalent to the
* Glob pattern.
*/
public static String globToRegexp(String pattern) {
char[] patbytes = pattern.toCharArray();
boolean inCharSet;
int ch;
StringBuffer buffer;
buffer = new StringBuffer(2*patbytes.length);
inCharSet = false;
for(ch=0; ch < patbytes.length; ch++) {
switch(patbytes[ch]) {
case '*':
if(inCharSet) {
buffer.append('*');
} else {
buffer.append(".*");
}
break;
case '?':
if(inCharSet) {
buffer.append('?');
} else {
buffer.append('.');
}
break;
case '[':
inCharSet = true;
buffer.append(patbytes[ch]);
if(ch + 1 < patbytes.length) {
switch(patbytes[ch + 1]) {
case '!':
case '^':
buffer.append('^');
++ch;
continue;
case ']':
buffer.append(']');
++ch;
continue;
default:
break;
}
}
break;
case ']':
inCharSet = false;
buffer.append(patbytes[ch]);
break;
case '\\':
buffer.append('\\');
if(ch == patbytes.length - 1) {
buffer.append('\\');
} else if(isGlobMetaCharacter(patbytes[ch + 1])) {
buffer.append(patbytes[++ch]);
} else {
buffer.append('\\');
}
break;
default:
if(!inCharSet && isRegExpMetaCharacter(patbytes[ch])) {
buffer.append('\\');
}
buffer.append(patbytes[ch]);
break;
}
}
return buffer.toString();
}
}
|