001    /*
002     *  Licensed to the Apache Software Foundation (ASF) under one
003     *  or more contributor license agreements.  See the NOTICE file
004     *  distributed with this work for additional information
005     *  regarding copyright ownership.  The ASF licenses this file
006     *  to you under the Apache License, Version 2.0 (the
007     *  "License"); you may not use this file except in compliance
008     *  with the License.  You may obtain a copy of the License at
009     *  
010     *    http://www.apache.org/licenses/LICENSE-2.0
011     *  
012     *  Unless required by applicable law or agreed to in writing,
013     *  software distributed under the License is distributed on an
014     *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015     *  KIND, either express or implied.  See the License for the
016     *  specific language governing permissions and limitations
017     *  under the License. 
018     *  
019     */
020    package org.apache.directory.shared.ldap.codec.search;
021    
022    
023    import java.nio.BufferOverflowException;
024    import java.nio.ByteBuffer;
025    
026    import org.apache.directory.shared.asn1.ber.tlv.TLV;
027    import org.apache.directory.shared.asn1.codec.DecoderException;
028    import org.apache.directory.shared.asn1.codec.EncoderException;
029    import org.apache.directory.shared.ldap.codec.LdapConstants;
030    
031    
032    /**
033     * Not Filter Object to store the Not filter.
034     * 
035     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
036     * @version $Rev: 664290 $, $Date: 2008-06-07 08:28:06 +0200 (Sam, 07 jui 2008) $, 
037     */
038    public class NotFilter extends ConnectorFilter
039    {
040        // ~ Methods
041        // ------------------------------------------------------------------------------------
042    
043        /**
044         * The constructor.
045         */
046        public NotFilter( int tlvId )
047        {
048            super( tlvId );
049        }
050        
051        /**
052         * The constructor.
053         */
054        public NotFilter()
055        {
056            super();
057        }
058    
059    
060        /**
061         * Subclass the addFilterMethod, as this is specific for a NotFilter (we
062         * cannot have more than one elements).
063         * 
064         * @param filter The Filter to add
065         */
066        public void addFilter( Filter filter ) throws DecoderException
067        {
068            if ( filterSet != null )
069            {
070                throw new DecoderException( "Cannot have more than one Filter within a Not Filter" );
071            }
072    
073            super.addFilter( filter );
074        }
075    
076    
077        /**
078         * Get the NotFilter
079         * 
080         * @return Returns the notFilter.
081         */
082        public Filter getNotFilter()
083        {
084            return filterSet.get( 0 );
085        }
086    
087    
088        /**
089         * Set the NotFilter
090         * 
091         * @param notFilter The notFilter to set.
092         */
093        public void setNotFilter( Filter notFilter ) throws DecoderException
094        {
095            if ( filterSet != null )
096            {
097                throw new DecoderException( "Cannot have more than one Filter within a Not Filter" );
098            }
099    
100            super.addFilter( notFilter );
101        }
102    
103    
104        /**
105         * Compute the NotFilter length 
106         * NotFilter : 
107         * 0xA2 L1 super.computeLength()
108         * 
109         * Length(NotFilter) = Length(0xA2) + Length(super.computeLength()) +
110         *      super.computeLength()
111         */
112        public int computeLength()
113        {
114            filtersLength = super.computeLength();
115    
116            return 1 + TLV.getNbBytes( filtersLength ) + filtersLength;
117        }
118    
119    
120        /**
121         * Encode the NotFilter message to a PDU. 
122         * NotFilter : 
123         * 0xA2 LL filter.encode()
124         * 
125         * @param buffer The buffer where to put the PDU
126         * @return The PDU.
127         */
128        public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
129        {
130            if ( buffer == null )
131            {
132                throw new EncoderException( "Cannot put a PDU in a null buffer !" );
133            }
134    
135            try
136            {
137                // The NotFilter Tag
138                buffer.put( ( byte ) LdapConstants.NOT_FILTER_TAG );
139                buffer.put( TLV.getBytes( filtersLength ) );
140            }
141            catch ( BufferOverflowException boe )
142            {
143                throw new EncoderException( "The PDU buffer size is too small !" );
144            }
145    
146            super.encode( buffer );
147    
148            return buffer;
149        }
150    
151    
152        /**
153         * Return a string compliant with RFC 2254 representing a NOT filter
154         * 
155         * @return The NOT filter string
156         */
157        public String toString()
158        {
159            StringBuffer sb = new StringBuffer();
160    
161            sb.append( '!' ).append( super.toString() );
162    
163            return sb.toString();
164        }
165    }