krysalis-update/src/java/org/krysalis/depot/update/util/regexp RegularExpressionHelper.java,NONE,1.1 RegularExpressionSelector.java,NONE,1.1 RegularExpression.java,NONE,1.1

Nick Chalko <[email protected]> Wed, 08 Dec 2004 09:06:34 +0000
Newsgroups gmane.comp.krysalis.cvs
Message-ID <[email protected]>
Update of /cvsroot/krysalis/krysalis-update/src/java/org/krysalis/depot/update/util/regexp
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv24726/src/java/org/krysalis/depot/update/util/regexp

Added Files:
	RegularExpressionHelper.java RegularExpressionSelector.java 
	RegularExpression.java 
Log Message:
Copied from the apache incubator.

--- NEW FILE: RegularExpressionHelper.java ---
/*
 * Copyright  2004 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.
 */

package org.krysalis.depot.update.util.regexp;


/**
 * @author arb_jack
 * 
 *	Wrapper in case we move to ORO  
 */
public class RegularExpressionHelper {
	public final static String START	=	"^";
	public final static String WILD		=	".*";
	public final static String END		=	"$";
	
	public static RegularExpression getIncludes(String pattern) {
		return new RegularExpression(START + WILD + pattern + WILD + END);
	}
	public static RegularExpression endsWith(String pattern) {
		return new RegularExpression(START + WILD +  pattern + END);
	}
	public static RegularExpression startsWith(String pattern) {
		return new RegularExpression(START + pattern + WILD +END);
	}
}

--- NEW FILE: RegularExpressionSelector.java ---
/*
 * Copyright  2004 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.
 */

package org.krysalis.depot.update.util.regexp;

import org.krysalis.depot.update.util.select.ISelectable;
import org.krysalis.depot.update.util.select.ISelector;


/**
 * @author arb_jack
 */
public class RegularExpressionSelector implements ISelector {
	private final RegularExpression m_regexp;

	public RegularExpressionSelector(String expression) {
		m_regexp = new RegularExpression(expression);
	}

	public RegularExpressionSelector(RegularExpression expression) {
		m_regexp = expression;
	}

	/* (non-Javadoc)
	 * @see org.krysalis.depot.update.util.Selector#select(java.lang.Object)
	 */
	public boolean select(Object o) {
		String data = null;

		if (o instanceof ISelectable)
			data = ((ISelectable) o).getSelectionObject().toString();
		else
			data = o.toString();

		return m_regexp.match(data);
	}
	
	public String toString() {
		return m_regexp.toString();
	}
}

--- NEW FILE: RegularExpression.java ---
/*
 * Copyright  2004 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.
 */

package org.krysalis.depot.update.util.regexp;

import java.util.HashMap;

import com.sun.org.apache.regexp.internal.RE;
import com.sun.org.apache.regexp.internal.RESyntaxException;

/**
 * @author arb_jack
 * 
 *	Wrapper in case we move to ORO  
 */
public class RegularExpression {
	private final String m_expression;
	private final RE m_regexp;

	private static HashMap m_cached = new HashMap();

	public RegularExpression(String expression) {
		try {
			m_expression = expression;
			if (m_cached.containsKey(expression)) {
				m_regexp = (RE) m_cached.get(expression);
			}
			else {
				m_regexp = new RE(expression);
				m_cached.put(expression, m_regexp);
			}
		}
		catch (RESyntaxException e) {
			System.err.println(e.toString());
			e.printStackTrace(System.err);

			throw new IllegalArgumentException(e.getLocalizedMessage());
		}
	}

	public RegularExpression(RE expression) {
		m_expression = expression.toString();
		m_regexp = expression;
	}

	public boolean match(String input) {
		return m_regexp.match(input);
	}

	public String toString() {
		return m_expression;
	}
}



-------------------------------------------------------
SF email is sponsored by - The IT Product Guide
Read honest & candid reviews on hundreds of IT Products from real users.
Discover which products truly live up to the hype. Start reading now. 
http://productguide.itmanagersjournal.com/