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.dsmlv2.request;
21  
22  
23  import org.apache.directory.api.ldap.codec.api.LdapCodecConstants;
24  import org.apache.directory.api.ldap.model.entry.BinaryValue;
25  import org.apache.directory.api.ldap.model.entry.StringValue;
26  import org.apache.directory.api.ldap.model.entry.Value;
27  import org.apache.directory.api.util.Strings;
28  
29  
30  /**
31   * A class to store an attribute value assertion. 
32   * The grammar is :
33   * 
34   * AttributeValueAssertion ::= SEQUENCE {
35   *           attributeDesc   AttributeDescription,
36   *           assertionValue  AssertionValue }
37   *
38   * AttributeDescription ::= LDAPString
39   * 
40   * AssertionValue ::= OCTET STRING
41   * 
42   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
43   */
44  public class AttributeValueAssertion
45  {
46      // ~ Instance fields
47      // ----------------------------------------------------------------------------
48  
49      /** The attribute description */
50      private String attributeDesc;
51  
52      /** The assertion value */
53      private Value<?> assertionValue;
54  
55  
56      /**
57       *
58       * Helper method to render an object which can be a String or a byte[]
59       *
60       * @return A string representing the object
61       */
62      public static String dumpObject( Object object )
63      {
64          if ( object != null )
65          {
66              if ( object instanceof String )
67              {
68                  return ( String ) object;
69              }
70              else if ( object instanceof byte[] )
71              {
72                  return Strings.dumpBytes( ( byte[] ) object );
73              }
74              else if ( object instanceof StringValue )
75              {
76                  return ( ( StringValue ) object ).getValue();
77              }
78              else if ( object instanceof BinaryValue )
79              {
80                  return Strings.dumpBytes( ( ( BinaryValue ) object ).getValue() );
81              }
82              else
83              {
84                  return "<unknown type>";
85              }
86          }
87          else
88          {
89              return "";
90          }
91      }
92  
93  
94      // ~ Methods
95      // ------------------------------------------------------------------------------------
96  
97      /**
98       * Get the assertion value
99       * 
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 }