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.dsmlv2.request; 021 022 023import org.apache.directory.api.ldap.codec.api.LdapCodecConstants; 024import org.apache.directory.api.ldap.model.entry.BinaryValue; 025import org.apache.directory.api.ldap.model.entry.StringValue; 026import org.apache.directory.api.ldap.model.entry.Value; 027import org.apache.directory.api.util.Strings; 028 029 030/** 031 * A class to store an attribute value assertion. 032 * The grammar is : 033 * 034 * AttributeValueAssertion ::= SEQUENCE { 035 * attributeDesc AttributeDescription, 036 * assertionValue AssertionValue } 037 * 038 * AttributeDescription ::= LDAPString 039 * 040 * AssertionValue ::= OCTET STRING 041 * 042 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 043 */ 044public class AttributeValueAssertion 045{ 046 // ~ Instance fields 047 // ---------------------------------------------------------------------------- 048 049 /** The attribute description */ 050 private String attributeDesc; 051 052 /** The assertion value */ 053 private Value<?> assertionValue; 054 055 056 /** 057 * 058 * Helper method to render an object which can be a String or a byte[] 059 * 060 * @param object The Value to dump 061 * @return A string representing the object 062 */ 063 public static String dumpObject( Object object ) 064 { 065 if ( object != null ) 066 { 067 if ( object instanceof String ) 068 { 069 return ( String ) object; 070 } 071 else if ( object instanceof byte[] ) 072 { 073 return Strings.dumpBytes( ( byte[] ) object ); 074 } 075 else if ( object instanceof StringValue ) 076 { 077 return ( ( StringValue ) object ).getValue(); 078 } 079 else if ( object instanceof BinaryValue ) 080 { 081 return Strings.dumpBytes( ( ( BinaryValue ) object ).getValue() ); 082 } 083 else 084 { 085 return "<unknown type>"; 086 } 087 } 088 else 089 { 090 return ""; 091 } 092 } 093 094 095 // ~ Methods 096 // ------------------------------------------------------------------------------------ 097 098 /** 099 * 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}