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.i18n.I18n;
030    import org.apache.directory.shared.ldap.codec.LdapConstants;
031    
032    
033    /**
034     * Not Filter Object to store the Not filter.
035     * 
036     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
037     * @version $Rev: 912399 $, $Date: 2010-02-21 21:52:31 +0100 (Sun, 21 Feb 2010) $, 
038     */
039    public class NotFilter extends ConnectorFilter
040    {
041        // ~ Methods
042        // ------------------------------------------------------------------------------------
043    
044        /**
045         * The constructor.
046         */
047        public NotFilter( int tlvId )
048        {
049            super( tlvId );
050        }
051        
052        /**
053         * The constructor.
054         */
055        public NotFilter()
056        {
057            super();
058        }
059    
060    
061        /**
062         * Subclass the addFilterMethod, as this is specific for a NotFilter (we
063         * cannot have more than one elements).
064         * 
065         * @param filter The Filter to add
066         */
067        public void addFilter( Filter filter ) throws DecoderException
068        {
069            if ( filterSet != null )
070            {
071                throw new DecoderException( I18n.err( I18n.ERR_04057 ) );
072            }
073    
074            super.addFilter( filter );
075        }
076    
077    
078        /**
079         * Get the NotFilter
080         * 
081         * @return Returns the notFilter.
082         */
083        public Filter getNotFilter()
084        {
085            return filterSet.get( 0 );
086        }
087    
088    
089        /**
090         * Set the NotFilter
091         * 
092         * @param notFilter The notFilter to set.
093         */
094        public void setNotFilter( Filter notFilter ) throws DecoderException
095        {
096            if ( filterSet != null )
097            {
098                throw new DecoderException( I18n.err( I18n.ERR_04057 ) );
099            }
100    
101            super.addFilter( notFilter );
102        }
103    
104    
105        /**
106         * Compute the NotFilter length 
107         * NotFilter : 
108         * 0xA2 L1 super.computeLength()
109         * 
110         * Length(NotFilter) = Length(0xA2) + Length(super.computeLength()) +
111         *      super.computeLength()
112         */
113        public int computeLength()
114        {
115            filtersLength = super.computeLength();
116    
117            return 1 + TLV.getNbBytes( filtersLength ) + filtersLength;
118        }
119    
120    
121        /**
122         * Encode the NotFilter message to a PDU. 
123         * NotFilter : 
124         * 0xA2 LL filter.encode()
125         * 
126         * @param buffer The buffer where to put the PDU
127         * @return The PDU.
128         */
129        public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
130        {
131            if ( buffer == null )
132            {
133                throw new EncoderException( I18n.err( I18n.ERR_04023 ) );
134            }
135    
136            try
137            {
138                // The NotFilter Tag
139                buffer.put( ( byte ) LdapConstants.NOT_FILTER_TAG );
140                buffer.put( TLV.getBytes( filtersLength ) );
141            }
142            catch ( BufferOverflowException boe )
143            {
144                throw new EncoderException( I18n.err( I18n.ERR_04005 ) );
145            }
146    
147            super.encode( buffer );
148    
149            return buffer;
150        }
151    
152    
153        /**
154         * Return a string compliant with RFC 2254 representing a NOT filter
155         * 
156         * @return The NOT filter string
157         */
158        public String toString()
159        {
160            StringBuffer sb = new StringBuffer();
161    
162            sb.append( '!' ).append( super.toString() );
163    
164            return sb.toString();
165        }
166    }