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 */
020package org.apache.directory.api.ldap.codec.search;
021
022
023import java.nio.BufferOverflowException;
024import java.nio.ByteBuffer;
025
026import org.apache.directory.api.asn1.DecoderException;
027import org.apache.directory.api.asn1.EncoderException;
028import org.apache.directory.api.asn1.ber.tlv.TLV;
029import org.apache.directory.api.i18n.I18n;
030import org.apache.directory.api.ldap.codec.api.LdapCodecConstants;
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 */
038public class NotFilter extends ConnectorFilter
039{
040    /**
041     * The constructor.
042     * 
043     * @param tlvId The TLV identifier
044     */
045    public NotFilter( int tlvId )
046    {
047        super( tlvId );
048    }
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    @Override
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     * @throws DecoderException If the NotFilter is already containing a filter
094     */
095    public void setNotFilter( Filter notFilter ) throws DecoderException
096    {
097        if ( filterSet != null )
098        {
099            throw new DecoderException( I18n.err( I18n.ERR_04057 ) );
100        }
101
102        super.addFilter( notFilter );
103    }
104
105
106    /**
107     * Compute the NotFilter length 
108     * <br>
109     * NotFilter :
110     * <pre> 
111     * 0xA2 L1 super.computeLength()
112     * 
113     * Length(NotFilter) = Length(0xA2) + Length(super.computeLength()) +
114     *      super.computeLength()
115     * </pre>
116     * 
117     * @return The encoded length
118     */
119    @Override
120    public int computeLength()
121    {
122        filtersLength = super.computeLength();
123
124        return 1 + TLV.getNbBytes( filtersLength ) + filtersLength;
125    }
126
127
128    /**
129     * Encode the NotFilter message to a PDU. 
130     * <br>
131     * NotFilter :
132     * <pre> 
133     * 0xA2 LL filter.encode()
134     * </pre>
135     * 
136     * @param buffer The buffer where to put the PDU
137     * @return The PDU.
138     */
139    @Override
140    public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
141    {
142        if ( buffer == null )
143        {
144            throw new EncoderException( I18n.err( I18n.ERR_04023 ) );
145        }
146
147        try
148        {
149            // The NotFilter Tag
150            buffer.put( ( byte ) LdapCodecConstants.NOT_FILTER_TAG );
151            buffer.put( TLV.getBytes( filtersLength ) );
152        }
153        catch ( BufferOverflowException boe )
154        {
155            throw new EncoderException( I18n.err( I18n.ERR_04005 ), boe );
156        }
157
158        super.encode( buffer );
159
160        return buffer;
161    }
162
163
164    /**
165     * Return a string compliant with RFC 2254 representing a NOT filter
166     * 
167     * @return The NOT filter string
168     */
169    @Override
170    public String toString()
171    {
172        StringBuilder sb = new StringBuilder();
173
174        sb.append( '!' ).append( super.toString() );
175
176        return sb.toString();
177    }
178}