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.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      /** The attribute description */
47      private String attributeDesc;
48  
49      /** The assertion value */
50      private Value<?> assertionValue;
51  
52  
53      /**
54       * Helper method to render an object which can be a String or a byte[]
55       *
56       * @param object the Object to render
57       * @return A string representing the object
58       */
59      public static String dumpObject( Object object )
60      {
61          if ( object != null )
62          {
63              if ( object instanceof String )
64              {
65                  return ( String ) object;
66              }
67              else if ( object instanceof byte[] )
68              {
69                  return Strings.dumpBytes( ( byte[] ) object );
70              }
71              else if ( object instanceof StringValue )
72              {
73                  return ( ( StringValue ) object ).getValue();
74              }
75              else if ( object instanceof BinaryValue )
76              {
77                  return Strings.dumpBytes( ( ( BinaryValue ) object ).getValue() );
78              }
79              else
80              {
81                  return "<unknown type>";
82              }
83          }
84          else
85          {
86              return "";
87          }
88      }
89  
90  
91      /**
92       * Get the assertion value
93       * 
94       * @return Returns the assertionValue.
95       */
96      public Value<?> getAssertionValue()
97      {
98          return assertionValue;
99      }
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 }