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       * @param object The Value to dump
61       * @return A string representing the object
62       */
63      public static String dumpObject( Object object )
64      {
65          if ( object != null )
66          {
67              if ( object instanceof String )
68              {
69                  return ( String ) object;
70              }
71              else if ( object instanceof byte[] )
72              {
73                  return Strings.dumpBytes( ( byte[] ) object );
74              }
75              else if ( object instanceof StringValue )
76              {
77                  return ( ( StringValue ) object ).getValue();
78              }
79              else if ( object instanceof BinaryValue )
80              {
81                  return Strings.dumpBytes( ( ( BinaryValue ) object ).getValue() );
82              }
83              else
84              {
85                  return "<unknown type>";
86              }
87          }
88          else
89          {
90              return "";
91          }
92      }
93  
94  
95      // ~ Methods
96      // ------------------------------------------------------------------------------------
97  
98      /**
99       * Get the assertion value
100      * 
101      * @return Returns the assertionValue.
102      */
103     public Value<?> getAssertionValue()
104     {
105         return assertionValue;
106     }
107 
108 
109     /**
110      * Set the assertion value
111      * 
112      * @param assertionValue The assertionValue to set.
113      */
114     public void setAssertionValue( Value<?> assertionValue )
115     {
116         this.assertionValue = assertionValue;
117     }
118 
119 
120     /**
121      * Get the attribute description
122      * 
123      * @return Returns the attributeDesc.
124      */
125     public String getAttributeDesc()
126     {
127         return attributeDesc;
128     }
129 
130 
131     /**
132      * Set the attribute description
133      * 
134      * @param attributeDesc The attributeDesc to set.
135      */
136     public void setAttributeDesc( String attributeDesc )
137     {
138         this.attributeDesc = attributeDesc;
139     }
140 
141 
142     /**
143      * Get a String representation of an AttributeValueAssertion
144      * 
145      * @param tabs The spacing to be put before the string
146      * @return An AttributeValueAssertion String
147      */
148     public String toString( String tabs )
149     {
150         StringBuffer sb = new StringBuffer();
151 
152         sb.append( tabs ).append( "AttributeValueAssertion\n" );
153         sb.append( tabs ).append( "    Assertion description : '" );
154         sb.append( attributeDesc != null ? attributeDesc : "null" );
155         sb.append( "'\n" );
156         sb.append( tabs ).append( "    Assertion value : '" ).append( dumpObject( assertionValue ) ).append( "'\n" );
157 
158         return sb.toString();
159     }
160 
161 
162     /**
163      * Get a String representation of an AttributeValueAssertion, as of RFC
164      * 2254.
165      * 
166      * @param filterType The filter type
167      * @return An AttributeValueAssertion String
168      */
169     public String toStringRFC2254( int filterType )
170     {
171         StringBuffer sb = new StringBuffer();
172 
173         sb.append( attributeDesc );
174 
175         switch ( filterType )
176         {
177             case LdapCodecConstants.EQUALITY_MATCH_FILTER:
178                 sb.append( '=' );
179                 break;
180 
181             case LdapCodecConstants.LESS_OR_EQUAL_FILTER:
182                 sb.append( "<=" );
183                 break;
184 
185             case LdapCodecConstants.GREATER_OR_EQUAL_FILTER:
186                 sb.append( ">=" );
187                 break;
188 
189             case LdapCodecConstants.APPROX_MATCH_FILTER:
190                 sb.append( "~=" );
191                 break;
192 
193             default:
194                 throw new IllegalStateException( "Unexpected filter type " + filterType );
195         }
196 
197         sb.append( dumpObject( assertionValue ) );
198 
199         return sb.toString();
200     }
201 
202 
203     /**
204      * Get a String representation of an AttributeValueAssertion
205      * 
206      * @return An AttributeValueAssertion String
207      */
208     public String toString()
209     {
210         return toString( "" );
211     }
212 }