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    public void addFilter( Filter filter ) throws DecoderException
067    {
068        if ( filterSet != null )
069        {
070            throw new DecoderException( I18n.err( I18n.ERR_04057 ) );
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     * @throws DecoderException If the NotFilter is already containing a filter
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     * <br>
108     * NotFilter :
109     * <pre> 
110     * 0xA2 L1 super.computeLength()
111     * 
112     * Length(NotFilter) = Length(0xA2) + Length(super.computeLength()) +
113     *      super.computeLength()
114     * </pre>
115     * 
116     * @return The encoded length
117     */
118    public int computeLength()
119    {
120        filtersLength = super.computeLength();
121
122        return 1 + TLV.getNbBytes( filtersLength ) + filtersLength;
123    }
124
125
126    /**
127     * Encode the NotFilter message to a PDU. 
128     * <br>
129     * NotFilter :
130     * <pre> 
131     * 0xA2 LL filter.encode()
132     * </pre>
133     * 
134     * @param buffer The buffer where to put the PDU
135     * @return The PDU.
136     */
137    public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
138    {
139        if ( buffer == null )
140        {
141            throw new EncoderException( I18n.err( I18n.ERR_04023 ) );
142        }
143
144        try
145        {
146            // The NotFilter Tag
147            buffer.put( ( byte ) LdapCodecConstants.NOT_FILTER_TAG );
148            buffer.put( TLV.getBytes( filtersLength ) );
149        }
150        catch ( BufferOverflowException boe )
151        {
152            throw new EncoderException( I18n.err( I18n.ERR_04005 ), boe );
153        }
154
155        super.encode( buffer );
156
157        return buffer;
158    }
159
160
161    /**
162     * Return a string compliant with RFC 2254 representing a NOT filter
163     * 
164     * @return The NOT filter string
165     */
166    public String toString()
167    {
168        StringBuffer sb = new StringBuffer();
169
170        sb.append( '!' ).append( super.toString() );
171
172        return sb.toString();
173    }
174}