View Javadoc
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *  
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *  
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License. 
18   *  
19   */
20  package org.apache.directory.api.ldap.codec;
21  
22  
23  import org.apache.directory.api.ldap.codec.api.LdapConstants;
24  import org.apache.directory.api.ldap.model.entry.*;
25  import org.apache.directory.api.util.Strings;
26  
27  
28  /**
29   * A class to store an attribute value assertion. 
30   * The grammar is :
31   * 
32   * AttributeValueAssertion ::= SEQUENCE {
33   *           attributeDesc   AttributeDescription,
34   *           assertionValue  AssertionValue }
35   *
36   * AttributeDescription ::= LDAPString
37   * 
38   * AssertionValue ::= OCTET STRING
39   * 
40   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
41   */
42  public class AttributeValueAssertion
43  {
44      // ~ Instance fields
45      // ----------------------------------------------------------------------------
46  
47      /** The attribute description */
48      private String attributeDesc;
49  
50      /** The assertion value */
51      private Value<?> assertionValue;
52  
53  
54      /**
55       *
56       * Helper method to render an object which can be a String or a byte[]
57       *
58       * @return A string representing the object
59       */
60      public static String dumpObject( Object object )
61      {
62          if ( object != null )
63          {
64              if ( object instanceof String )
65              {
66                  return ( String ) object;
67              }
68              else if ( object instanceof byte[] )
69              {
70                  return Strings.dumpBytes( ( byte[] ) object );
71              }
72              else if ( object instanceof StringValue )
73              {
74                  return ( ( StringValue ) object ).getValue();
75              }
76              else if ( object instanceof BinaryValue )
77              {
78                  return Strings.dumpBytes( ( ( BinaryValue ) object ).getValue() );
79              }
80              else
81              {
82                  return "<unknown type>";
83              }
84          }
85          else
86          {
87              return "";
88          }
89      }
90  
91  
92      // ~ Methods
93      // ------------------------------------------------------------------------------------
94  
95      /**
96       * Get the assertion value
97       * 
98       * @return Returns the assertionValue.
99       */
100     public Value<?> getAssertionValue()
101     {
102         return assertionValue;
103     }
104 
105 
106     /**
107      * Set the assertion value
108      * 
109      * @param assertionValue The assertionValue to set.
110      */
111     public void setAssertionValue( Value<?> assertionValue )
112     {
113         this.assertionValue = assertionValue;
114     }
115 
116 
117     /**
118      * Get the attribute description
119      * 
120      * @return Returns the attributeDesc.
121      */
122     public String getAttributeDesc()
123     {
124         return attributeDesc;
125     }
126 
127 
128     /**
129      * Set the attribute description
130      * 
131      * @param attributeDesc The attributeDesc to set.
132      */
133     public void setAttributeDesc( String attributeDesc )
134     {
135         this.attributeDesc = attributeDesc;
136     }
137 
138 
139     /**
140      * Get a String representation of an AttributeValueAssertion
141      * 
142      * @param tabs The spacing to be put before the string
143      * @return An AttributeValueAssertion String
144      */
145     public String toString( String tabs )
146     {
147         StringBuffer sb = new StringBuffer();
148 
149         sb.append( tabs ).append( "AttributeValueAssertion\n" );
150         sb.append( tabs ).append( "    Assertion description : '" );
151         sb.append( attributeDesc != null ? attributeDesc : "null" );
152         sb.append( "'\n" );
153         sb.append( tabs ).append( "    Assertion value : '" ).append( dumpObject( assertionValue ) ).append( "'\n" );
154 
155         return sb.toString();
156     }
157 
158 
159     /**
160      * Get a String representation of an AttributeValueAssertion, as of RFC
161      * 2254.
162      * 
163      * @param filterType The filter type
164      * @return An AttributeValueAssertion String
165      */
166     public String toStringRFC2254( int filterType )
167     {
168         StringBuffer sb = new StringBuffer();
169 
170         sb.append( attributeDesc );
171 
172         switch ( filterType )
173         {
174             case LdapConstants.EQUALITY_MATCH_FILTER:
175                 sb.append( '=' );
176                 break;
177 
178             case LdapConstants.LESS_OR_EQUAL_FILTER:
179                 sb.append( "<=" );
180                 break;
181 
182             case LdapConstants.GREATER_OR_EQUAL_FILTER:
183                 sb.append( ">=" );
184                 break;
185 
186             case LdapConstants.APPROX_MATCH_FILTER:
187                 sb.append( "~=" );
188                 break;
189         }
190 
191         sb.append( dumpObject( assertionValue ) );
192 
193         return sb.toString();
194     }
195 
196 
197     /**
198      * Get a String representation of an AttributeValueAssertion
199      * 
200      * @return An AttributeValueAssertion String
201      */
202     public String toString()
203     {
204         return toString( "" );
205     }
206 }