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.api.ldap.codec; 021 022 023import org.apache.directory.api.ldap.codec.api.LdapConstants; 024import org.apache.directory.api.ldap.model.entry.*; 025import org.apache.directory.api.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 * 056 * Helper method to render an object which can be a String or a byte[] 057 * 058 * @return A string representing the object 059 */ 060 public static String dumpObject( Object object ) 061 { 062 if ( object != null ) 063 { 064 if ( object instanceof String ) 065 { 066 return ( String ) object; 067 } 068 else if ( object instanceof byte[] ) 069 { 070 return Strings.dumpBytes( ( byte[] ) object ); 071 } 072 else if ( object instanceof StringValue ) 073 { 074 return ( ( StringValue ) object ).getValue(); 075 } 076 else if ( object instanceof BinaryValue ) 077 { 078 return Strings.dumpBytes( ( ( BinaryValue ) object ).getValue() ); 079 } 080 else 081 { 082 return "<unknown type>"; 083 } 084 } 085 else 086 { 087 return ""; 088 } 089 } 090 091 092 // ~ Methods 093 // ------------------------------------------------------------------------------------ 094 095 /** 096 * Get the assertion value 097 * 098 * @return Returns the assertionValue. 099 */ 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}