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;
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    /** The attribute description */
047    private String attributeDesc;
048
049    /** The assertion value */
050    private Value<?> assertionValue;
051
052
053    /**
054     * Helper method to render an object which can be a String or a byte[]
055     *
056     * @param object the Object to render
057     * @return A string representing the object
058     */
059    public static String dumpObject( Object object )
060    {
061        if ( object != null )
062        {
063            if ( object instanceof String )
064            {
065                return ( String ) object;
066            }
067            else if ( object instanceof byte[] )
068            {
069                return Strings.dumpBytes( ( byte[] ) object );
070            }
071            else if ( object instanceof StringValue )
072            {
073                return ( ( StringValue ) object ).getValue();
074            }
075            else if ( object instanceof BinaryValue )
076            {
077                return Strings.dumpBytes( ( ( BinaryValue ) object ).getValue() );
078            }
079            else
080            {
081                return "<unknown type>";
082            }
083        }
084        else
085        {
086            return "";
087        }
088    }
089
090
091    /**
092     * Get the assertion value
093     * 
094     * @return Returns the assertionValue.
095     */
096    public Value<?> getAssertionValue()
097    {
098        return assertionValue;
099    }
100
101
102    /**
103     * Set the assertion value
104     * 
105     * @param assertionValue The assertionValue to set.
106     */
107    public void setAssertionValue( Value<?> assertionValue )
108    {
109        this.assertionValue = assertionValue;
110    }
111
112
113    /**
114     * Get the attribute description
115     * 
116     * @return Returns the attributeDesc.
117     */
118    public String getAttributeDesc()
119    {
120        return attributeDesc;
121    }
122
123
124    /**
125     * Set the attribute description
126     * 
127     * @param attributeDesc The attributeDesc to set.
128     */
129    public void setAttributeDesc( String attributeDesc )
130    {
131        this.attributeDesc = attributeDesc;
132    }
133
134
135    /**
136     * Get a String representation of an AttributeValueAssertion
137     * 
138     * @param tabs The spacing to be put before the string
139     * @return An AttributeValueAssertion String
140     */
141    public String toString( String tabs )
142    {
143        StringBuilder sb = new StringBuilder();
144
145        sb.append( tabs ).append( "AttributeValueAssertion\n" );
146        sb.append( tabs ).append( "    Assertion description : '" );
147        sb.append( attributeDesc != null ? attributeDesc : "null" );
148        sb.append( "'\n" );
149        sb.append( tabs ).append( "    Assertion value : '" ).append( dumpObject( assertionValue ) ).append( "'\n" );
150
151        return sb.toString();
152    }
153
154
155    /**
156     * Get a String representation of an AttributeValueAssertion, as of RFC
157     * 2254.
158     * 
159     * @param filterType The filter type
160     * @return An AttributeValueAssertion String
161     */
162    public String toStringRFC2254( int filterType )
163    {
164        StringBuilder sb = new StringBuilder();
165
166        sb.append( attributeDesc );
167
168        switch ( filterType )
169        {
170            case LdapCodecConstants.EQUALITY_MATCH_FILTER:
171                sb.append( '=' );
172                break;
173
174            case LdapCodecConstants.LESS_OR_EQUAL_FILTER:
175                sb.append( "<=" );
176                break;
177
178            case LdapCodecConstants.GREATER_OR_EQUAL_FILTER:
179                sb.append( ">=" );
180                break;
181
182            case LdapCodecConstants.APPROX_MATCH_FILTER:
183                sb.append( "~=" );
184                break;
185
186            default:
187                throw new IllegalArgumentException( "Unexpected filter type: " + filterType );
188        }
189
190        sb.append( dumpObject( assertionValue ) );
191
192        return sb.toString();
193    }
194
195
196    /**
197     * Get a String representation of an AttributeValueAssertion
198     * 
199     * @return An AttributeValueAssertion String
200     */
201    @Override
202    public String toString()
203    {
204        return toString( "" );
205    }
206}