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 * https://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.i18n.I18n; 024import org.apache.directory.api.ldap.codec.api.LdapCodecConstants; 025import org.apache.directory.api.ldap.model.entry.Value; 026import org.apache.directory.api.util.Strings; 027 028 029/** 030 * A class to store an attribute value assertion. 031 * The grammar is : 032 * 033 * AttributeValueAssertion ::= SEQUENCE { 034 * attributeDesc AttributeDescription, 035 * assertionValue AssertionValue } 036 * 037 * AttributeDescription ::= LDAPString 038 * 039 * AssertionValue ::= OCTET STRING 040 * 041 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 042 */ 043public class AttributeValueAssertion 044{ 045 // ~ Instance fields 046 // ---------------------------------------------------------------------------- 047 048 /** The attribute description */ 049 private String attributeDesc; 050 051 /** The assertion value */ 052 private Value assertionValue; 053 054 055 /** 056 * 057 * Helper method to render an object which can be a String or a byte[] 058 * 059 * @param object The Value to dump 060 * @return A string representing the object 061 */ 062 public static String dumpObject( Object object ) 063 { 064 if ( object != null ) 065 { 066 if ( object instanceof String ) 067 { 068 return ( String ) object; 069 } 070 else if ( object instanceof byte[] ) 071 { 072 return Strings.dumpBytes( ( byte[] ) object ); 073 } 074 else if ( object instanceof Value ) 075 { 076 return ( ( Value ) object ).getString(); 077 } 078 else 079 { 080 return "<unknown type>"; 081 } 082 } 083 else 084 { 085 return Strings.EMPTY_STRING; 086 } 087 } 088 089 090 // ~ Methods 091 // ------------------------------------------------------------------------------------ 092 093 /** 094 * Get the assertion value 095 * 096 * @return Returns the assertionValue. 097 */ 098 public Value getAssertionValue() 099 { 100 return assertionValue; 101 } 102 103 104 /** 105 * Set the assertion value 106 * 107 * @param assertionValue The assertionValue to set. 108 */ 109 public void setAssertionValue( Value assertionValue ) 110 { 111 this.assertionValue = assertionValue; 112 } 113 114 115 /** 116 * Get the attribute description 117 * 118 * @return Returns the attributeDesc. 119 */ 120 public String getAttributeDesc() 121 { 122 return attributeDesc; 123 } 124 125 126 /** 127 * Set the attribute description 128 * 129 * @param attributeDesc The attributeDesc to set. 130 */ 131 public void setAttributeDesc( String attributeDesc ) 132 { 133 this.attributeDesc = attributeDesc; 134 } 135 136 137 /** 138 * Get a String representation of an AttributeValueAssertion 139 * 140 * @param tabs The spacing to be put before the string 141 * @return An AttributeValueAssertion String 142 */ 143 public String toString( String tabs ) 144 { 145 StringBuilder sb = new StringBuilder(); 146 147 sb.append( tabs ).append( "AttributeValueAssertion\n" ); 148 sb.append( tabs ).append( " Assertion description : '" ); 149 sb.append( attributeDesc != null ? attributeDesc : "null" ); 150 sb.append( "'\n" ); 151 sb.append( tabs ).append( " Assertion value : '" ).append( dumpObject( assertionValue ) ).append( "'\n" ); 152 153 return sb.toString(); 154 } 155 156 157 /** 158 * Get a String representation of an AttributeValueAssertion, as of RFC 159 * 2254. 160 * 161 * @param filterType The filter type 162 * @return An AttributeValueAssertion String 163 */ 164 public String toStringRFC2254( int filterType ) 165 { 166 StringBuilder sb = new StringBuilder(); 167 168 sb.append( attributeDesc ); 169 170 switch ( filterType ) 171 { 172 case LdapCodecConstants.EQUALITY_MATCH_FILTER: 173 sb.append( '=' ); 174 break; 175 176 case LdapCodecConstants.LESS_OR_EQUAL_FILTER: 177 sb.append( "<=" ); 178 break; 179 180 case LdapCodecConstants.GREATER_OR_EQUAL_FILTER: 181 sb.append( ">=" ); 182 break; 183 184 case LdapCodecConstants.APPROX_MATCH_FILTER: 185 sb.append( "~=" ); 186 break; 187 188 default: 189 throw new IllegalStateException( I18n.err( I18n.ERR_03037_UNEXPECTED_FILTER_TYPE, filterType ) ); 190 } 191 192 sb.append( dumpObject( assertionValue ) ); 193 194 return sb.toString(); 195 } 196 197 198 /** 199 * Get a String representation of an AttributeValueAssertion 200 * 201 * @return An AttributeValueAssertion String 202 */ 203 @Override 204 public String toString() 205 { 206 return toString( Strings.EMPTY_STRING ); 207 } 208}