//
// This file is part of the prose package.
//
// The contents of this file are subject to the Mozilla Public License
// Version 1.1 (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.mozilla.org/MPL/
//
// Software distributed under the License is distributed on an "AS IS" basis,
// WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
// for the specific language governing rights and limitations under the
// License.
//
// The Original Code is prose.
//
// The Initial Developer of the Original Code is Andrei Popovici. Portions
// created by Andrei Popovici are Copyright (C) 2002 Andrei Popovici.
// All Rights Reserved.
//
// Contributor(s):
// $Id: NameExpression.java,v 1.3 2008/11/18 11:38:01 anicoara Exp $
// =====================================================================
//
package ch.ethz.prose.filter;
// used packages
import java.io.Serializable;
import java.lang.reflect.Member;
import org.apache.regexp.RE;
import org.apache.regexp.RESyntaxException;
/**
* Class NameExpression defines (and encapsulates) some handy mathing routines.
*
* @version $Revision: 1.3 $
* @author Andrei Popovici
*/
class NameExpression implements Serializable {
private static final long serialVersionUID = 3258134669538243124L;
String regexp;
transient RE re = null;
NameExpression(String regexp) {
this.regexp = regexp;
try {
re = new RE(regexp);
}
catch (RESyntaxException e) {
throw new IllegalArgumentException("NameExpression: " + regexp + " is not a valid regexp." + e.toString());
}
}
private RE getRE() {
try {
if (re == null)
re = new RE(regexp);
}
catch (RESyntaxException cannotB) {
throw new Error("NameExpression.getRE: regezp was not verified");
}
return re;
}
boolean classMatchesRegexp(Class cls) {
//System.out.println(" ->>>>>>>>> NameExpression - classMatchesRegexp -> className(cls.getName()) = " + className(cls.getName())); //angy test
//System.out.println(" ->>>>>>>>> NameExpression - classMatchesRegexp -> regexp = " + regexp); //angy test
return getRE().match(className(cls.getName()));
}
boolean packageMatchesRegexp(Class cls) {
return getRE().match(packageName(cls.getName()));
}
boolean qualifiedClassMatchesRegexp(Class cls) {
return getRE().match(cls.getName());
}
boolean memberMatchesRegexp(Member m) {
return getRE().match(m.getName());
}
private String className(String qualifiedClassName) {
int lastDotIndex = qualifiedClassName.lastIndexOf('.');
return qualifiedClassName.substring(lastDotIndex+1);
}
private String packageName(String qualifiedClassName) {
int lastDotIndex = qualifiedClassName.lastIndexOf('.');
return (lastDotIndex != -1)?qualifiedClassName.substring(0,lastDotIndex):"";
}
public String toString() {
return regexp;
}
}