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