📄 regexputil.java
字号:
package de.spieleck.app.jacson.util;
import java.util.Iterator;
import org.apache.oro.text.regex.*;
import de.spieleck.config.ConfigNode;
import de.spieleck.config.ConfigVerify.Acceptor;
import de.spieleck.app.jacson.JacsonNames;
import de.spieleck.app.jacson.JacsonRegistry;
import de.spieleck.app.jacson.JacsonConfigException;
/**
* A utility class to avoid creating of many oro-Objects.
* Since Jacson works entirely singlethreaded a single instance
* of an PatternMatcher and and PatternCompiler is enough.
* <p>
* This class is also responsible for processing regexp initializations
* from the <i>Jacson</i> configuration.
* </p>
* @author fsn
*/
public class RegExpUtil
implements Acceptor, JacsonNames
{
public final static String REGEXP_NODE = "regexp";
public final static String ICASE_NODE = "ignorecase";
protected static RegExpUtil me = null;
/** oro-Regexp-Matcher */
protected PatternMatcher matcher;
/** oro-RegExp-Pattern-Compiler */
protected PatternCompiler pc;
public RegExpUtil()
{
matcher = new Perl5Matcher();
pc = new Perl5Compiler();
}
public PatternCompiler getPC()
{
return pc;
}
public PatternMatcher getMatcher()
{
return matcher;
}
public Pattern compile(String rxStr, int options)
throws MalformedPatternException
{
return getPC().compile(rxStr, options);
}
public Pattern obtainPattern(String name, ConfigNode node,
JacsonRegistry reg)
throws JacsonConfigException
{
return obtainPattern(name, node, reg, REGEXP_NODE);
}
public Pattern obtainPattern(String name, ConfigNode node,
JacsonRegistry reg, String rNodeName)
throws JacsonConfigException
{
Iterator iter = reg.scanFor(rNodeName, node);
if ( !iter.hasNext() )
ConfigUtil.exception(name+" needs "+rNodeName+"-setup", node);
ConfigNode config = (ConfigNode) iter.next();
if ( iter.hasNext() )
ConfigUtil.exception(name+" needs exactly one "+rNodeName
+"-setup", node);
ConfigUtil.verify(config, new Acceptor()
{
public boolean accept(ConfigNode cn)
{
String n = cn.getName();
return n.equals(ICASE_NODE)
|| JacsonRegistry.acceptId(cn);
}
}
);
String rxStr = config.getString();
if ( rxStr == null )
throw new JacsonConfigException(name+" needs regexp-setup");
//
// Look for RegExp options
boolean h = config.getBoolean(ICASE_NODE, true );
int options = Perl5Compiler.DEFAULT_MASK;
if ( h )
options |= Perl5Compiler.CASE_INSENSITIVE_MASK;
options |= Perl5Compiler.SINGLELINE_MASK;
try
{
return compile(rxStr, options);
}
catch ( MalformedPatternException e )
{
ConfigUtil.exception(name+" needs valid regexp-setup", node);
return null; // We never get here!
}
}
public boolean accept(ConfigNode node)
{
String name = node.getName();
return name.equals(REGEXP_NODE)
|| name.equals(ICASE_NODE)
|| JacsonRegistry.acceptId(node);
}
public static boolean acceptOptions(ConfigNode node)
{
String name = node.getName();
return name.equals(ICASE_NODE)
|| JacsonRegistry.acceptId(node);
}
}
//
// Jacson - Text Filtering with Java.
// Copyright (C) 2002 Frank S. Nestel (nestefan -at- users.sourceforge.net)
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library 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
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -