📄 mrulefactory.java
字号:
name = null ;
type = null;
patternStr = null;
conditionStr = null;
caseSensitiveFlagStr = null;
logFlagStr = null;
action = null;
command = null ;
pluginList = null;
pluginArgList = null;
}
else if( line.indexOf( "name=" ) == 0 )
{
if( name != null )
{
throwParseError( currentFileName, lineNumber );
}
name = getParam( line, "name=" );
/*
// check " " and "+"
if( name.indexOf( " " ) > -1 )
{
throwParseError( "Blank space is not allowed to use in the name field.", lineNumber );
}
if( name.indexOf( "+" ) > -1 )
{
throwParseError( "Plus sign is not allowed to use in the name field.", lineNumber );
}
*/
}
else if( line.indexOf( "id=" ) == 0 )
{
if( id != null )
{
throwParseError( currentFileName, lineNumber );
}
id = getParam( line, "id=" );
if( !id.matches( "^[-_a-zA-Z0-9]{1,}$" ) )
{
throwParseError( currentFileName, lineNumber );
}
}
else if( line.indexOf( "revision=" ) == 0 )
{
if( revision != null )
{
throwParseError( currentFileName, lineNumber );
}
revision = getParam( line, "revision=" );
if( !revision.matches( "^[0-9]{1,}$" ) )
{
throwParseError( currentFileName, lineNumber );
}
revisionInt = Integer.parseInt( revision );
}
else if( line.indexOf( "type=" ) == 0 )
{
if( type != null )
{
throwParseError( currentFileName, lineNumber );
}
type = getParam( line, "type=" );
}
else if( line.indexOf( "pattern=" ) == 0 )
{
if( patternStr != null )
{
throwParseError( currentFileName, lineNumber );
}
patternStr = getParam( line, "pattern=" );
}
else if( line.indexOf( "condition=" ) == 0 )
{
if( conditionStr != null )
{
throwParseError( currentFileName, lineNumber );
}
conditionStr = getParam( line, "condition=" );
}
else if( line.indexOf( "case_sensitive=" ) == 0 )
{
if( caseSensitiveFlagStr != null )
{
throwParseError( currentFileName, lineNumber );
}
caseSensitiveFlagStr = getParam( line, "case_sensitive=" );
}
else if( line.indexOf( "log=" ) == 0 )
{
if( logFlagStr != null )
{
throwParseError( currentFileName, lineNumber );
}
logFlagStr = getParam( line, "log=" );
}
else if( line.indexOf( "action=" ) == 0 )
{
if( action != null )
{
throwParseError( currentFileName, lineNumber );
}
action = getParam( line, "action=" );
}
else if( line.indexOf( "command=" ) == 0 )
{
if( command != null )
{
throwParseError( currentFileName, lineNumber );
}
command = getParam( line, "command=" );
}
else if( line.indexOf( "plugin=" ) == 0 )
{
if( pluginList == null )
{
pluginList = new ArrayList();
}
String pluginStr = getParam( line, "plugin=" );
String pluginClassName = "";
String pluginArg = "";
int index = pluginStr.indexOf( " " );
if( index == -1 )
{
pluginClassName = pluginStr;
}
else
{
pluginClassName = pluginStr.substring( 0, index );
pluginArg = pluginStr.substring( index + 1 );
}
MGuardianPlugin plugin;
try
{
pluginList.add( MPluginManager.getInstance().getPlugin( pluginClassName ) );
}
catch( Exception e )
{
e.printStackTrace();
throwParseError( currentFileName, lineNumber );
}
if( pluginArgList == null )
{
pluginArgList = new ArrayList();
}
pluginArgList.add( pluginArg );
}
else if( MStringUtil.isComment( line ) )
{
}
else
{
throwParseError( currentFileName, lineNumber );
}
}
resolveVariables( requestRuleList, varSet );
resolveVariables( responseRuleList, varSet );
//startup plugins
//MPluginManager.getInstance().startup();
}
//--------------------------------------------------------------------------------
private void resolveVariables( List ruleList, Set varSet )
throws IOException
{
// command
Iterator p = ruleList.iterator();
while( p.hasNext() )
{
MAbstractRule rule = ( MAbstractRule )p.next();
String command = rule.getCommand();
int count = 0;
for( ;; ++count )
{
boolean found = false;
Iterator q = varSet.iterator();
while( q.hasNext() )
{
MVariable var = ( MVariable )q.next();
String name = var.getName();
String expression = var.getExpression();
if( command.indexOf( name ) > -1 )
{
found = true;
command = command.replaceAll( name, expression );
}
}
if( !found )
{
rule.setCommand( command );
break;
}
if( count > 100 )
{
// detect loop :(
throw new IOException( "variable error : " + command );
}
}
}
// action
p = ruleList.iterator();
while( p.hasNext() )
{
MAbstractRule rule = ( MAbstractRule )p.next();
String action = rule.getAction();
int count = 0;
for( ;; ++count )
{
boolean found = false;
Iterator q = varSet.iterator();
while( q.hasNext() )
{
MVariable var = ( MVariable )q.next();
String name = var.getName();
String expression = var.getExpression();
if( action.indexOf( name ) > -1 )
{
found = true;
action = action.replaceAll( name, expression );
}
}
if( !found )
{
if( action.equalsIgnoreCase( MAbstractRule.ACTION_BLOCK )
|| action.equalsIgnoreCase( MAbstractRule.ACTION_PASS_ALL )
|| action.equalsIgnoreCase( MAbstractRule.ACTION_NONE )
)
{
rule.setAction( action );
}
else if( action.indexOf( MAbstractRule.ACTION_PASS_RULE ) == 0 )
{
String[] array = action.split( "\\s{1,}" );
if( array.length == 1 )
{
throw new IOException( "Invalid action : " + action );
}
List passRuleList = new ArrayList( array.length - 1 );
for( int i = 1; i < array.length; ++i )
{
passRuleList.add( array[ i ] );
}
rule.setPassRuleList( passRuleList );
rule.setAction( MAbstractRule.ACTION_PASS_RULE );
}
else
{
throw new IOException( "Invalid action : " + action );
}
break;
}
if( count > 100 )
{
// detect loop :(
throw new IOException( "variable error : " + action );
}
}
}
}
//--------------------------------------------------------------------------------------
private void throwParseError( String ruleFileName, int lineNumber )
throws IOException
{
throw new IOException( "Parse error. " + ruleFileName + " line: " + lineNumber );
}
//--------------------------------------------------------------------------------------
private String getParam( String line, String paramName )
{
return line.substring( paramName.length() );
}
//--------------------------------------------------------------------------------------
public List getRequestRuleList()
{
return requestRuleList;
}
//--------------------------------------------------------------------------------------
public List getResponseRuleList()
{
return responseRuleList;
}
//--------------------------------------------------------------------------------------
private MAbstractRule getRuleGroupInstance( String patternStr )
{
MAbstractRule ruleGroup;
boolean containsResponseRule = false;
String[] ruleIdList = patternStr.split( " {0,}\\+ {0,}" );
int ruleCount = ruleIdList.length;
ArrayList ruleList = new ArrayList( ruleCount );
for( int i = 0; i < ruleCount; ++i )
{
MAbstractRule rule = ( MAbstractRule )( idRuleMap.get( ruleIdList[ i ] ) );
ruleList.add( rule );
if( rule.getRuleType() == MAbstractRule.RESPONSE )
{
containsResponseRule = true;
}
}
if( containsResponseRule )
{
ruleGroup = new MRuleGroup( ruleList );
}
else
{
ruleGroup = new MRequestRuleGroup( ruleList );
}
return ruleGroup;
}
//--------------------------------------------------------------------------------------
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -