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.dsmlv2.request;
021
022
023import org.apache.directory.api.ldap.codec.api.LdapCodecConstants;
024import org.apache.directory.api.ldap.model.entry.BinaryValue;
025import org.apache.directory.api.ldap.model.entry.StringValue;
026import org.apache.directory.api.ldap.model.entry.Value;
027import org.apache.directory.api.util.Strings;
028
029
030/**
031 * A class to store an attribute value assertion. 
032 * The grammar is :
033 * 
034 * AttributeValueAssertion ::= SEQUENCE {
035 *           attributeDesc   AttributeDescription,
036 *           assertionValue  AssertionValue }
037 *
038 * AttributeDescription ::= LDAPString
039 * 
040 * AssertionValue ::= OCTET STRING
041 * 
042 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
043 */
044public class AttributeValueAssertion
045{
046    // ~ Instance fields
047    // ----------------------------------------------------------------------------
048
049    /** The attribute description */
050    private String attributeDesc;
051
052    /** The assertion value */
053    private Value<?> assertionValue;
054
055
056    /**
057     *
058     * Helper method to render an object which can be a String or a byte[]
059     *
060     * @return A string representing the object
061     */
062    public static String dumpObject( Object object )
063    {
064        if ( object != null )
065        {
066            if ( object instanceof String )
067            {
068                return ( String ) object;
069            }
070            else if ( object instanceof byte[] )
071            {
072                return Strings.dumpBytes( ( byte[] ) object );
073            }
074            else if ( object instanceof StringValue )
075            {
076                return ( ( StringValue ) object ).getValue();
077            }
078            else if ( object instanceof BinaryValue )
079            {
080                return Strings.dumpBytes( ( ( BinaryValue ) object ).getValue() );
081            }
082            else
083            {
084                return "<unknown type>";
085            }
086        }
087        else
088        {
089            return "";
090        }
091    }
092
093
094    // ~ Methods
095    // ------------------------------------------------------------------------------------
096
097    /**
098     * Get the assertion value
099     * 
100     * @return Returns the assertionValue.
101     */
102    public Value<?> getAssertionValue()
103    {
104        return assertionValue;
105    }
106
107
108    /**
109     * Set the assertion value
110     * 
111     * @param assertionValue The assertionValue to set.
112     */
113    public void setAssertionValue( Value<?> assertionValue )
114    {
115        this.assertionValue = assertionValue;
116    }
117
118
119    /**
120     * Get the attribute description
121     * 
122     * @return Returns the attributeDesc.
123     */
124    public String getAttributeDesc()
125    {
126        return attributeDesc;
127    }
128
129
130    /**
131     * Set the attribute description
132     * 
133     * @param attributeDesc The attributeDesc to set.
134     */
135    public void setAttributeDesc( String attributeDesc )
136    {
137        this.attributeDesc = attributeDesc;
138    }
139
140
141    /**
142     * Get a String representation of an AttributeValueAssertion
143     * 
144     * @param tabs The spacing to be put before the string
145     * @return An AttributeValueAssertion String
146     */
147    public String toString( String tabs )
148    {
149        StringBuffer sb = new StringBuffer();
150
151        sb.append( tabs ).append( "AttributeValueAssertion\n" );
152        sb.append( tabs ).append( "    Assertion description : '" );
153        sb.append( attributeDesc != null ? attributeDesc : "null" );
154        sb.append( "'\n" );
155        sb.append( tabs ).append( "    Assertion value : '" ).append( dumpObject( assertionValue ) ).append( "'\n" );
156
157        return sb.toString();
158    }
159
160
161    /**
162     * Get a String representation of an AttributeValueAssertion, as of RFC
163     * 2254.
164     * 
165     * @param filterType The filter type
166     * @return An AttributeValueAssertion String
167     */
168    public String toStringRFC2254( int filterType )
169    {
170        StringBuffer sb = new StringBuffer();
171
172        sb.append( attributeDesc );
173
174        switch ( filterType )
175        {
176            case LdapCodecConstants.EQUALITY_MATCH_FILTER:
177                sb.append( '=' );
178                break;
179
180            case LdapCodecConstants.LESS_OR_EQUAL_FILTER:
181                sb.append( "<=" );
182                break;
183
184            case LdapCodecConstants.GREATER_OR_EQUAL_FILTER:
185                sb.append( ">=" );
186                break;
187
188            case LdapCodecConstants.APPROX_MATCH_FILTER:
189                sb.append( "~=" );
190                break;
191        }
192
193        sb.append( dumpObject( assertionValue ) );
194
195        return sb.toString();
196    }
197
198
199    /**
200     * Get a String representation of an AttributeValueAssertion
201     * 
202     * @return An AttributeValueAssertion String
203     */
204    public String toString()
205    {
206        return toString( "" );
207    }
208}