You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by ka...@apache.org on 2013/10/30 14:08:33 UTC
svn commit: r1537076 -
/directory/escimo/trunk/common/src/main/java/org/apache/directory/scim/search/FilterParser.java
Author: kayyagari
Date: Wed Oct 30 13:08:32 2013
New Revision: 1537076
URL: http://svn.apache.org/r1537076
Log:
code cleanup
Modified:
directory/escimo/trunk/common/src/main/java/org/apache/directory/scim/search/FilterParser.java
Modified: directory/escimo/trunk/common/src/main/java/org/apache/directory/scim/search/FilterParser.java
URL: http://svn.apache.org/viewvc/directory/escimo/trunk/common/src/main/java/org/apache/directory/scim/search/FilterParser.java?rev=1537076&r1=1537075&r2=1537076&view=diff
==============================================================================
--- directory/escimo/trunk/common/src/main/java/org/apache/directory/scim/search/FilterParser.java (original)
+++ directory/escimo/trunk/common/src/main/java/org/apache/directory/scim/search/FilterParser.java Wed Oct 30 13:08:32 2013
@@ -19,10 +19,12 @@
*/
package org.apache.directory.scim.search;
+
import static org.apache.directory.scim.search.Operator.*;
import java.util.Stack;
+
/**
* TODO FilterParser.
*
@@ -30,121 +32,128 @@ import java.util.Stack;
*/
public class FilterParser
{
-
+
private static class Position
{
int val;
-
+
+
private Position()
{
this( 0 );
}
-
+
+
private Position( int pos )
{
this.val = pos;
}
-
+
+
private void increment()
{
val++;
}
-
+
+
private void set( int pos )
{
this.val = pos;
}
-
+
+
private void decrement()
{
val--;
}
}
-
+
+
public static FilterNode parse( String filter )
{
Position pos = new Position( 0 );
-
+
int len = filter.length();
-
+
FilterNode node = null;
-
+
char prevChar = ' ';
-
- while( pos.val < len )
+
+ while ( pos.val < len )
{
char c = filter.charAt( pos.val );
-
+
FilterNode next = null;
-
- switch( c )
+
+ switch ( c )
{
case '(':
- //FIXME handle groupings
String group = getWithinParenthesis( pos, filter );
next = parse( group );
break;
-
+
default:
next = parseNode( pos, filter );
}
-
- if( next instanceof BranchNode )
+
+ if ( next instanceof BranchNode )
{
- if( node != null )
+ if ( node != null )
{
( ( BranchNode ) next ).addNode( node );
}
-
+
node = next;
}
else if ( node instanceof BranchNode )
{
( ( BranchNode ) node ).addNode( next );
}
- else if( next != null )
+ else if ( next != null )
{
node = next;
}
-
+
prevChar = c;
pos.increment();
}
-
+
return node;
}
-
+
+
private static FilterNode parseNode( Position pos, String filter )
{
FilterNode node = null;
String attribute = parseToken( pos, filter );
-
+
Operator branchOperator = Operator.getByName( attribute );
-
- if( branchOperator != UNKNOWN )
+
+ if ( branchOperator != UNKNOWN )
{
switch ( branchOperator )
{
case AND:
case OR:
- return new BranchNode( branchOperator );
+ return new BranchNode( branchOperator );
default:
- throw new IllegalArgumentException( "Invalid predicate in filter, expected an attribute or an operator token but found " + attribute );
- }
+ throw new IllegalArgumentException(
+ "Invalid predicate in filter, expected an attribute or an operator token but found "
+ + attribute );
+ }
}
-
-
+
Operator operator = Operator.getByName( parseToken( pos, filter ) );
-
+
int curPos = pos.val;
-
+
String value = null;
-
+
String valOrOperator = parseToken( pos, filter );
-
- if( Operator.getByName( valOrOperator ) != UNKNOWN )
+
+ if ( Operator.getByName( valOrOperator ) != UNKNOWN )
{
// move back
pos.set( curPos );
@@ -153,58 +162,56 @@ public class FilterParser
{
value = valOrOperator;
}
-
+
switch ( operator )
{
case AND:
case OR:
- //node = new BranchNode( operator );
- throw new IllegalArgumentException( "Invalid predicate in filter, expected a non branching operator but found " + operator );
+ //node = new BranchNode( operator );
+ throw new IllegalArgumentException(
+ "Invalid predicate in filter, expected a non branching operator but found " + operator );
default:
- TerminalNode tn= new TerminalNode( operator );
+ TerminalNode tn = new TerminalNode( operator );
tn.setAttribute( attribute );
tn.setValue( value );
node = tn;
break;
}
-
+
return node;
}
-
+
+
private static String parseToken( Position pos, String filter )
{
boolean foundNonSpace = false;
-
+
StringBuilder sb = new StringBuilder();
-
+
char prevChar = ' ';
-
+
boolean isEscaped = false;
-
-// boolean isEnclosed = false;
-
+
boolean startQuote = false;
-
+
boolean endQuote = false;
-
- while( pos.val < filter.length() )
+
+ while ( pos.val < filter.length() )
{
char c = filter.charAt( pos.val );
pos.increment();
-
- if( ( prevChar == '\\' ) && ( c != '\\') )
+
+ if ( ( prevChar == '\\' ) && ( c != '\\' ) )
{
isEscaped = true;
}
-
- if( c == '"' )
+
+ if ( c == '"' )
{
-// isEnclosed = true;
-
- if( !isEscaped )
+ if ( !isEscaped )
{
- if( startQuote )
+ if ( startQuote )
{
endQuote = true;
}
@@ -212,16 +219,16 @@ public class FilterParser
{
startQuote = true;
}
-
+
continue;
}
}
-
- switch( c )
+
+ switch ( c )
{
- case ' ' :
-
- if( !foundNonSpace || ( startQuote && !endQuote ) )
+ case ' ':
+
+ if ( !foundNonSpace || ( startQuote && !endQuote ) )
{
continue;
}
@@ -229,67 +236,67 @@ public class FilterParser
{
return sb.toString();
}
-
+
default:
sb.append( c );
foundNonSpace = true;
}
-
+
prevChar = c;
}
-
+
return sb.toString();
}
-
-
+
+
private static String getWithinParenthesis( Position pos, String filter )
{
int start = -1;
int end = -1;
Stack<Integer> stack = new Stack<Integer>();
-
+
char prevChar = ' ';
-
+
boolean startQuote = false;
-
+
boolean endQuote = false;
-
+
boolean stop = false;
-
- while( !stop && ( pos.val < filter.length() ) )
+
+ while ( !stop && ( pos.val < filter.length() ) )
{
char c = filter.charAt( pos.val );
-
- switch( c )
+
+ switch ( c )
{
case '"':
- if( startQuote && prevChar != '\\')
+ if ( startQuote && prevChar != '\\' )
{
endQuote = true;
}
- else if( !startQuote )
+ else if ( !startQuote )
{
startQuote = true;
}
break;
-
+
case '(':
- if( !startQuote )
+ if ( !startQuote )
{
- if( start == -1 )
+ if ( start == -1 )
{
start = pos.val + 1;
}
-
+
stack.push( pos.val );
}
break;
-
+
case ')':
- if( !startQuote )
+ if ( !startQuote )
{
- if( stack.size() == 1 )
+ if ( stack.size() == 1 )
{
end = pos.val;
stop = true;
@@ -301,30 +308,18 @@ public class FilterParser
}
break;
}
-
- if( endQuote )
+
+ if ( endQuote )
{
startQuote = false;
endQuote = false;
}
-
+
prevChar = c;
pos.increment();
}
-
+
return filter.substring( start, end );
}
-
-
- public static void main( String[] args )
- {
- String s = "((( x eq \"(y)\" ))) and (y eq \"x\\\"\" )";
-// Position pos = new Position( 0 );
-// System.out.println(getWithinParenthesis( pos, s ));
-
- String filter = "(x eq y) and userName eq \"bjensen\"";
-
- FilterNode node = parse( s );
- System.out.println( node );
- }
+
}