felix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From akaras...@apache.org
Subject svn commit: r500116 [2/4] - in /incubator/felix/sandbox/akarasulu/bundledb: ./ src/main/antlr/ src/main/java/org/apache/felix/bundledb/filter/
Date Fri, 26 Jan 2007 02:48:30 GMT
Added: incubator/felix/sandbox/akarasulu/bundledb/src/main/java/org/apache/felix/bundledb/filter/FilterParserImpl.java
URL: http://svn.apache.org/viewvc/incubator/felix/sandbox/akarasulu/bundledb/src/main/java/org/apache/felix/bundledb/filter/FilterParserImpl.java?view=auto&rev=500116
==============================================================================
--- incubator/felix/sandbox/akarasulu/bundledb/src/main/java/org/apache/felix/bundledb/filter/FilterParserImpl.java
(added)
+++ incubator/felix/sandbox/akarasulu/bundledb/src/main/java/org/apache/felix/bundledb/filter/FilterParserImpl.java
Thu Jan 25 18:48:29 2007
@@ -0,0 +1,175 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you 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.apache.felix.bundledb.filter;
+
+
+import java.io.PipedOutputStream;
+import java.io.PipedInputStream;
+import java.io.IOException;
+import java.text.ParseException;
+
+import org.apache.felix.bundledb.filter.AntlrFilterLexer;
+import org.apache.felix.bundledb.filter.AntlrFilterParser;
+import org.apache.felix.bundledb.filter.AntlrFilterValueLexer;
+import org.apache.felix.bundledb.filter.AntlrFilterValueParser;
+import org.apache.felix.bundledb.filter.StringTools;
+
+import antlr.LexerSharedInputState;
+import antlr.RecognitionException;
+import antlr.TokenStreamException;
+import antlr.TokenStreamSelector;
+
+
+public class FilterParserImpl implements FilterParser
+{
+    private AntlrFilterParser parser;
+
+    private PipedOutputStream parserPipe;
+
+    private AntlrFilterLexer lexer;
+
+    private TokenStreamSelector selector;
+
+    private LexerSharedInputState state;
+
+    private AntlrFilterValueLexer valueLexer;
+
+    private AntlrFilterValueParser valueParser;
+
+
+    /**
+     * Creates a filter parser implementation.
+     */
+    public FilterParserImpl()
+    {
+        init();
+    }
+
+
+    /**
+     * Initializes the filter parser.
+     */
+    private synchronized void init()
+    {
+        // build the pipe used to feed the parser data and reusing it
+        this.parserPipe = new PipedOutputStream();
+        PipedInputStream pipeTail = new PipedInputStream();
+
+        try
+        {
+            this.parserPipe.connect( pipeTail );
+        }
+        catch ( IOException e )
+        {
+            // this never blows chuncks and if it does we report!
+            throw new InternalError();
+        }
+
+        this.state = new LexerSharedInputState( pipeTail );
+        this.lexer = new AntlrFilterLexer( state );
+        this.valueLexer = new AntlrFilterValueLexer( state );
+
+        this.selector = new TokenStreamSelector();
+        this.selector.addInputStream( this.lexer, AntlrFilterLexer.SELECTOR_KEY );
+        this.selector.addInputStream( this.valueLexer, AntlrFilterValueLexer.SELECTOR_KEY
);
+        this.selector.select( this.lexer );
+
+        this.parser = new AntlrFilterParser( this.selector );
+        this.parser.setSelector( this.selector );
+        this.parser.setValueLexer( this.valueLexer );
+        this.parser.setValueParser( this.valueParser );
+
+        this.valueParser = new AntlrFilterValueParser( this.selector );
+        this.valueParser.setSelector( this.selector );
+        this.valueParser.setLexer( this.lexer );
+
+        this.parser.setValueParser( this.valueParser );
+    }
+
+
+    public synchronized ExprNode parse( String filter ) throws ParseException, IOException
+    {
+        ExprNode root = null;
+
+        if ( filter == null || filter.trim().equals( "" ) )
+        {
+            return null;
+        }
+
+        if ( filter.indexOf( "**" ) > -1 )
+        {
+            filter = StringTools.trimConsecutiveToOne( filter, '*' );
+        }
+        
+        // check balanced parenthesis
+        int balanced = 0;
+        for(int i=0; i<filter.length(); i++)
+        {
+            if(filter.charAt( i ) == '(')
+            {
+                balanced++;
+            }
+            else if(filter.charAt( i ) == ')')
+            {
+                balanced--;
+            }
+        }
+        if(balanced != 0)
+        {
+            throw new ParseException( "Unbalanced parenthesis", 0 );
+        }
+
+        this.parserPipe.write( filter.getBytes() );
+        this.parserPipe.write( '\n' );
+        this.parserPipe.flush();
+
+        try
+        {
+            root = this.parser.filter();
+            this.state.reset();
+            this.selector.select( this.lexer );
+        }
+        catch ( RecognitionException e )
+        {
+            // @todo either use ExceptionUtils here or switch to throwing a
+            // naming exception instead.
+            String msg = "Parser failure on filter:\n\t" + filter;
+            msg += "\nAntlr exception trace:\n" + e.getMessage();
+            init();
+            throw new ParseException( msg, e.getColumn() );
+        }
+        catch ( TokenStreamException e2 )
+        {
+            String msg = "Parser failure on filter:\n\t" + filter;
+            msg += "\nAntlr exception trace:\n" + e2.getMessage();
+            init();
+            throw new ParseException( msg, 0 );
+        }
+
+        return root;
+    }
+
+
+    public void setFilterParserMonitor( FilterParserMonitor monitor )
+    {
+        this.parser.setFilterParserMonitor( monitor );
+        this.valueParser.setFilterParserMonitor( monitor );
+    }
+}

Added: incubator/felix/sandbox/akarasulu/bundledb/src/main/java/org/apache/felix/bundledb/filter/FilterParserMonitor.java
URL: http://svn.apache.org/viewvc/incubator/felix/sandbox/akarasulu/bundledb/src/main/java/org/apache/felix/bundledb/filter/FilterParserMonitor.java?view=auto&rev=500116
==============================================================================
--- incubator/felix/sandbox/akarasulu/bundledb/src/main/java/org/apache/felix/bundledb/filter/FilterParserMonitor.java
(added)
+++ incubator/felix/sandbox/akarasulu/bundledb/src/main/java/org/apache/felix/bundledb/filter/FilterParserMonitor.java
Thu Jan 25 18:48:29 2007
@@ -0,0 +1,38 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you 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.apache.felix.bundledb.filter;
+
+
+/**
+ * A monitor for FilterParsers.
+ * 
+ * @author <a href="mailto:dev@directory.apache.org"> Apache Directory Project</a>
+ * @version $Rev: 437007 $
+ */
+public interface FilterParserMonitor
+{
+    /**
+     * Monitors FilterParser events where it matches a production.
+     * 
+     * @param production
+     *            the name of the production matched
+     */
+    void matchedProduction( String production );
+}

Added: incubator/felix/sandbox/akarasulu/bundledb/src/main/java/org/apache/felix/bundledb/filter/FilterParserMonitorAdapter.java
URL: http://svn.apache.org/viewvc/incubator/felix/sandbox/akarasulu/bundledb/src/main/java/org/apache/felix/bundledb/filter/FilterParserMonitorAdapter.java?view=auto&rev=500116
==============================================================================
--- incubator/felix/sandbox/akarasulu/bundledb/src/main/java/org/apache/felix/bundledb/filter/FilterParserMonitorAdapter.java
(added)
+++ incubator/felix/sandbox/akarasulu/bundledb/src/main/java/org/apache/felix/bundledb/filter/FilterParserMonitorAdapter.java
Thu Jan 25 18:48:29 2007
@@ -0,0 +1,40 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you 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.apache.felix.bundledb.filter;
+
+
+/**
+ * Adapter for the FilterParser monitor.
+ * 
+ * @author <a href="mailto:dev@directory.apache.org"> Apache Directory Project</a>
+ * @version $Rev: 437007 $
+ */
+public class FilterParserMonitorAdapter implements FilterParserMonitor
+{
+    /*
+     * (non-Javadoc)
+     * 
+     * @see org.apache.ldap.common.filter.FilterParserMonitor#
+     *      matchedProduction(java.lang.String)
+     */
+    public void matchedProduction( String production )
+    {
+    }
+}

Added: incubator/felix/sandbox/akarasulu/bundledb/src/main/java/org/apache/felix/bundledb/filter/FilterVisitor.java
URL: http://svn.apache.org/viewvc/incubator/felix/sandbox/akarasulu/bundledb/src/main/java/org/apache/felix/bundledb/filter/FilterVisitor.java?view=auto&rev=500116
==============================================================================
--- incubator/felix/sandbox/akarasulu/bundledb/src/main/java/org/apache/felix/bundledb/filter/FilterVisitor.java
(added)
+++ incubator/felix/sandbox/akarasulu/bundledb/src/main/java/org/apache/felix/bundledb/filter/FilterVisitor.java
Thu Jan 25 18:48:29 2007
@@ -0,0 +1,87 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you 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.apache.felix.bundledb.filter;
+
+
+import java.util.List;
+
+
+/**
+ * Filter expression tree node visitor interface. Note that this is a variation
+ * of the extrinsic visitor variation. It has the following advantages over the
+ * standard visitor pattern:
+ * <ul>
+ * <li>Visitor takes responsibility that a visitor can visit a node</li>
+ * <li>Each visitor knows which types of concrete classes it can visit</li>
+ * <li>New visitors can be created without changing the node class</li>
+ * <li>New node classes can be added without having to change old visitors</li>
+ * <li>Visitation order can be controled in every respect:</li>
+ * <ul>
+ * <li>Visitation rejection with canVisit() and/or getOrder()</li>
+ * <li>Recursive visitation ordering with isPrefix()</li>
+ * <li>Child visitation ordering with getOrder()</li>
+ * </ul>
+ * </ul>
+ * 
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $Revision: 487713 $
+ */
+public interface FilterVisitor
+{
+    /**
+     * Visits a filter expression AST using a specific visitation order.
+     * 
+     * @param node
+     *            the node to visit
+     */
+    void visit( ExprNode node );
+
+
+    /**
+     * Checks to see if a node can be visited.
+     * 
+     * @param node
+     *            the node to be visited
+     * @return whether or node the node should be visited
+     */
+    boolean canVisit( ExprNode node );
+
+
+    /**
+     * Determines whether the visitation order is prefix or postfix.
+     * 
+     * @return true if the visitation is in prefix order, false otherwise.
+     */
+    boolean isPrefix();
+
+
+    /**
+     * Get the array of children to visit sequentially to determine the order of
+     * child visitations. Some children may not be returned at all if canVisit()
+     * returns false on them.
+     * 
+     * @param node
+     *            the parent branch node
+     * @param a_children
+     *            the child node array
+     * @return the new reordered array of children
+     */
+    List<ExprNode> getOrder( BranchNode node, List<ExprNode> children );
+}

Added: incubator/felix/sandbox/akarasulu/bundledb/src/main/java/org/apache/felix/bundledb/filter/LeafNode.java
URL: http://svn.apache.org/viewvc/incubator/felix/sandbox/akarasulu/bundledb/src/main/java/org/apache/felix/bundledb/filter/LeafNode.java?view=auto&rev=500116
==============================================================================
--- incubator/felix/sandbox/akarasulu/bundledb/src/main/java/org/apache/felix/bundledb/filter/LeafNode.java
(added)
+++ incubator/felix/sandbox/akarasulu/bundledb/src/main/java/org/apache/felix/bundledb/filter/LeafNode.java
Thu Jan 25 18:48:29 2007
@@ -0,0 +1,110 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you 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.apache.felix.bundledb.filter;
+
+
+/**
+ * Abstract base class for leaf nodes within the expression filter tree.
+ * 
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $Rev: 487713 $
+ */
+public abstract class LeafNode extends AbstractExprNode
+{
+    /** attribute on which this leaf is based */
+    private String attribute;
+
+
+    /**
+     * Creates a leaf node.
+     * 
+     * @param attribute the attribute this node is based on
+     * @param type the type of this leaf node
+     */
+    protected LeafNode( String attribute, AssertionEnum type )
+    {
+        super( type );
+        this.attribute = attribute;
+    }
+
+
+    /**
+     * Gets whether this node is a leaf - the answer is always true here.
+     * 
+     * @return true always
+     */
+    public final boolean isLeaf()
+    {
+        return true;
+    }
+
+
+    /**
+     * Gets the attribute this leaf node is based on.
+     * 
+     * @return the attribute asserted
+     */
+    public final String getAttribute()
+    {
+        return attribute;
+    }
+    
+    
+    /**
+     * Sets the attribute this leaf node is based on.
+     * 
+     * @param attribute the attribute that is asserted by this filter node
+     */
+    public void setAttribute( String attribute )
+    {
+        this.attribute = attribute;
+    }
+
+
+    /*
+     * (non-Javadoc)
+     * 
+     * @see java.lang.Object#equals(java.lang.Object)
+     */
+    public boolean equals( Object other )
+    {
+        if ( null == other )
+        {
+            return false;
+        }
+
+        if ( this == other )
+        {
+            return true;
+        }
+
+        if ( !( other instanceof LeafNode ) )
+        {
+            return false;
+        }
+
+        if ( !super.equals( other ) )
+        {
+            return false;
+        }
+
+        return attribute.equals( ( ( LeafNode ) other ).getAttribute() );
+    }
+}

Added: incubator/felix/sandbox/akarasulu/bundledb/src/main/java/org/apache/felix/bundledb/filter/Position.java
URL: http://svn.apache.org/viewvc/incubator/felix/sandbox/akarasulu/bundledb/src/main/java/org/apache/felix/bundledb/filter/Position.java?view=auto&rev=500116
==============================================================================
--- incubator/felix/sandbox/akarasulu/bundledb/src/main/java/org/apache/felix/bundledb/filter/Position.java
(added)
+++ incubator/felix/sandbox/akarasulu/bundledb/src/main/java/org/apache/felix/bundledb/filter/Position.java
Thu Jan 25 18:48:29 2007
@@ -0,0 +1,40 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you 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.apache.felix.bundledb.filter;
+
+/**
+ *
+ * This class is used to store the position of a token in a string. It's very
+ * usefull while parsing a DN. 
+ * 
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ *
+ */
+public class Position
+{
+    public int start = 0;
+    public int length =0;
+    public int end = 0;
+    
+    public String toString()
+    {
+        return "[" + start + ", " + end + ", " + length + "]";
+    }
+}

Added: incubator/felix/sandbox/akarasulu/bundledb/src/main/java/org/apache/felix/bundledb/filter/PresenceNode.java
URL: http://svn.apache.org/viewvc/incubator/felix/sandbox/akarasulu/bundledb/src/main/java/org/apache/felix/bundledb/filter/PresenceNode.java?view=auto&rev=500116
==============================================================================
--- incubator/felix/sandbox/akarasulu/bundledb/src/main/java/org/apache/felix/bundledb/filter/PresenceNode.java
(added)
+++ incubator/felix/sandbox/akarasulu/bundledb/src/main/java/org/apache/felix/bundledb/filter/PresenceNode.java
Thu Jan 25 18:48:29 2007
@@ -0,0 +1,100 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you 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.apache.felix.bundledb.filter;
+
+
+/**
+ * Filter expression tree node representing a filter attribute value assertion
+ * for presence.
+ * 
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $Revision: 490418 $
+ */
+public final class PresenceNode extends LeafNode
+{
+    /**
+     * Creates a PresenceNode object based on an attribute.
+     * 
+     * @param attribute
+     *            the attribute to assert the presence of
+     */
+    public PresenceNode( String attribute )
+    {
+        super( attribute, AssertionEnum.PRESENCE );
+    }
+
+
+    /**
+     * @see org.apache.directory.shared.ldap.filter.ExprNode#printToBuffer(java.lang.StringBuffer)
+     */
+    public StringBuffer printToBuffer( StringBuffer buf )
+    {
+        buf.append( '(' ).append( getAttribute() ).append( "=*" );
+
+        buf.append( ')' );
+
+        if ( ( null != getAnnotations() ) && getAnnotations().containsKey( "count"
) )
+        {
+            buf.append( '[' );
+            buf.append( getAnnotations().get( "count" ).toString() );
+            buf.append( "] " );
+        }
+        else
+        {
+            buf.append( ' ' );
+        }
+
+        return buf;
+    }
+
+
+    /**
+     * @see java.lang.Object#toString()
+     */
+    public String toString()
+    {
+        StringBuffer buf = new StringBuffer();
+        printToBuffer( buf );
+        
+        return ( buf.toString() );
+    }
+
+    
+    /**
+     * @see ExprNode#printRefinementToBuffer(StringBuffer)
+     */
+    public StringBuffer printRefinementToBuffer( StringBuffer a_buf ) throws UnsupportedOperationException
+    {
+        throw new UnsupportedOperationException( "PresenceNode can't be part of a refinement"
);
+    }
+
+
+    /**
+     * @see org.apache.directory.shared.ldap.filter.ExprNode#accept(
+     *      org.apache.directory.shared.ldap.filter.FilterVisitor)
+     */
+    public void accept( FilterVisitor visitor )
+    {
+        if ( visitor.canVisit( this ) )
+        {
+            visitor.visit( this );
+        }
+    }
+}

Added: incubator/felix/sandbox/akarasulu/bundledb/src/main/java/org/apache/felix/bundledb/filter/SimpleNode.java
URL: http://svn.apache.org/viewvc/incubator/felix/sandbox/akarasulu/bundledb/src/main/java/org/apache/felix/bundledb/filter/SimpleNode.java?view=auto&rev=500116
==============================================================================
--- incubator/felix/sandbox/akarasulu/bundledb/src/main/java/org/apache/felix/bundledb/filter/SimpleNode.java
(added)
+++ incubator/felix/sandbox/akarasulu/bundledb/src/main/java/org/apache/felix/bundledb/filter/SimpleNode.java
Thu Jan 25 18:48:29 2007
@@ -0,0 +1,279 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you 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.apache.felix.bundledb.filter;
+
+
+/**
+ * A simple assertion value node.
+ * 
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $Revision: 490418 $
+ */
+public class SimpleNode extends LeafNode
+{
+    /** the value */
+    private Object value;
+
+
+    /**
+     * Creates a new SimpleNode object.
+     * 
+     * @param attribute
+     *            the attribute name
+     * @param value
+     *            the value to test for
+     * @param type
+     *            the type of the assertion
+     */
+    public SimpleNode( String attribute, byte[] value, AssertionEnum type )
+    {
+//        this( attribute, StringTools.utf8ToString( value ), type );
+        super( attribute, type );
+        this.value = value;
+
+        switch ( type )
+        {
+            case APPROXIMATE :
+            case EQUALITY :
+            case GREATEREQ :
+            case LESSEQ :
+                break;
+
+            case EXTENSIBLE :
+                throw new IllegalArgumentException( "Assertion type supplied is "
+                    + "extensible.  Use ExtensibleNode instead." );
+
+            case PRESENCE :
+                throw new IllegalArgumentException( "Assertion type supplied is "
+                    + "presence.  Use PresenceNode instead." );
+
+            case SUBSTRING :
+                throw new IllegalArgumentException( "Assertion type supplied is "
+                    + "substring.  Use SubstringNode instead." );
+
+            default:
+                throw new IllegalArgumentException( "Attribute value assertion type is undefined."
);
+        }
+    }
+
+
+    /**
+     * Creates a new SimpleNode object.
+     * 
+     * @param attribute
+     *            the attribute name
+     * @param value
+     *            the value to test for
+     * @param type
+     *            the type of the assertion
+     */
+    public SimpleNode( String attribute, String value, AssertionEnum type )
+    {
+        super( attribute, type );
+        this.value = value;
+
+        switch ( type )
+        {
+            case APPROXIMATE :
+            case EQUALITY :
+            case GREATEREQ :
+            case LESSEQ :
+                break;
+
+            case EXTENSIBLE :
+                throw new IllegalArgumentException( "Assertion type supplied is "
+                    + "extensible.  Use ExtensibleNode instead." );
+
+            case PRESENCE :
+                throw new IllegalArgumentException( "Assertion type supplied is "
+                    + "presence.  Use PresenceNode instead." );
+
+            case SUBSTRING :
+                throw new IllegalArgumentException( "Assertion type supplied is "
+                    + "substring.  Use SubstringNode instead." );
+
+            default:
+                throw new IllegalArgumentException( "Attribute value assertion type is undefined."
);
+        }
+    }
+
+
+    /**
+     * Gets the value.
+     * 
+     * @return the value
+     */
+    public final Object getValue()
+    {
+        return value;
+    }
+
+
+    /**
+     * Sets the value of this node.
+     * 
+     * @param value the value for this node
+     */
+    public void setValue( Object value )
+    {
+        this.value = value;
+    }
+
+
+    /**
+     * @see org.apache.directory.shared.ldap.filter.ExprNode#printToBuffer(
+     *      java.lang.StringBuffer)
+     */
+    public StringBuffer printToBuffer( StringBuffer buf )
+    {
+        buf.append( '(' ).append( getAttribute() );
+
+        switch ( getAssertionType() )
+        {
+            case APPROXIMATE :
+                buf.append( "~=" );
+                break;
+
+            case EQUALITY :
+                buf.append( "=" );
+                break;
+
+            case GREATEREQ :
+                buf.append( ">=" );
+                break;
+
+            case LESSEQ :
+                buf.append( "<=" );
+                break;
+
+            default:
+                buf.append( "UNKNOWN" );
+        }
+
+        buf.append( value );
+        buf.append( ')' );
+
+        if ( ( null != getAnnotations() ) && getAnnotations().containsKey( "count"
) )
+        {
+            buf.append( '[' );
+            buf.append( getAnnotations().get( "count" ).toString() );
+            buf.append( "] " );
+        }
+        else
+        {
+            buf.append( ' ' );
+        }
+
+        return buf;
+    }
+
+    
+    /**
+     * @see ExprNode#printRefinementToBuffer(StringBuffer)
+     */
+    public StringBuffer printRefinementToBuffer( StringBuffer buf ) throws UnsupportedOperationException
+    {
+
+        switch ( getAssertionType() )
+        {
+            case EQUALITY:
+                break;
+
+            case APPROXIMATE:
+                throw new UnsupportedOperationException( "Approximate assertion isn't valid
for a refinement" );
+
+            case GREATEREQ:
+                throw new UnsupportedOperationException( "GreaterOrEqual assertion isn't
valid for a refinement" );
+
+            case LESSEQ:
+                throw new UnsupportedOperationException( "LessOrEqual assertion isn't valid
for a refinement" );
+
+            default:
+                throw new UnsupportedOperationException( "Undefined assertion for a refinement"
);
+        }
+
+        if ( getAttribute() == null || !"objectClass".equalsIgnoreCase( getAttribute() )
)
+        {
+            throw new UnsupportedOperationException( "Invalid attribute " + getAttribute()
+ " for a refinement" );
+        }
+
+        buf.append( "item" );
+        buf.append( ':' );
+        buf.append( ' ' );
+        buf.append( getValue() );
+
+        return buf;
+    }
+
+
+    /**
+     * @see java.lang.Object#toString()
+     */
+    public String toString()
+    {
+        StringBuffer buf = new StringBuffer();
+        printToBuffer( buf );
+        return ( buf.toString() );
+    }
+
+
+    /**
+     * @see org.apache.directory.shared.ldap.filter.ExprNode#accept(
+     *      org.apache.directory.shared.ldap.filter.FilterVisitor)
+     */
+    public void accept( FilterVisitor visitor )
+    {
+        if ( visitor.canVisit( this ) )
+        {
+            visitor.visit( this );
+        }
+    }
+
+
+    /*
+     * (non-Javadoc)
+     * 
+     * @see java.lang.Object#equals(java.lang.Object)
+     */
+    public boolean equals( Object other )
+    {
+        if ( null == other )
+        {
+            return false;
+        }
+
+        if ( this == other )
+        {
+            return true;
+        }
+
+        if ( !( other instanceof SimpleNode ) )
+        {
+            return false;
+        }
+
+        if ( !super.equals( other ) )
+        {
+            return false;
+        }
+
+        return value.equals( ( ( SimpleNode ) other ).getValue() );
+    }
+}



Mime
View raw message