1 20 21 package org.apache.directory.ldapstudio.browser.core.internal.model; 22 23 24 import org.apache.directory.ldapstudio.browser.core.BrowserCoreMessages; 25 import org.apache.directory.ldapstudio.browser.core.internal.search.LdapSearchPageScoreComputer; 26 import org.apache.directory.ldapstudio.browser.core.model.IAttribute; 27 import org.apache.directory.ldapstudio.browser.core.model.IConnection; 28 import org.apache.directory.ldapstudio.browser.core.model.IEntry; 29 import org.apache.directory.ldapstudio.browser.core.model.IValue; 30 import org.apache.directory.ldapstudio.browser.core.model.ModelModificationException; 31 import org.apache.directory.ldapstudio.browser.core.model.RDNPart; 32 import org.apache.directory.ldapstudio.browser.core.utils.LdifUtils; 33 import org.apache.directory.ldapstudio.browser.core.utils.Utils; 34 import org.eclipse.search.ui.ISearchPageScoreComputer; 35 36 37 43 public class Value implements IValue 44 { 45 46 47 private static final long serialVersionUID = -9039209604742682740L; 48 49 50 private IAttribute attribute; 51 52 53 private Object rawValue; 54 55 56 63 public Value( IAttribute attribute, Object rawValue ) throws ModelModificationException 64 { 65 this.init( attribute, rawValue ); 66 67 if ( rawValue == null ) 68 { 69 throw new ModelModificationException( BrowserCoreMessages.model__empty_value ); 70 } 71 } 72 73 74 80 public Value( IAttribute attribute ) throws ModelModificationException 81 { 82 this.init( attribute, null ); 83 } 84 85 86 93 private void init( IAttribute attribute, Object rawValue ) throws ModelModificationException 94 { 95 if ( attribute == null ) 96 { 97 throw new ModelModificationException( BrowserCoreMessages.model__empty_attribute ); 98 } 99 100 this.attribute = attribute; 101 102 if ( rawValue == null ) 103 { 104 if ( attribute.isString() ) 105 { 106 this.rawValue = IValue.EMPTY_STRING_VALUE; 107 } 108 else 109 { 110 this.rawValue = IValue.EMPTY_BINARY_VALUE; 111 } 112 } 113 else 114 { 115 this.rawValue = rawValue; 116 } 117 } 118 119 120 123 public IAttribute getAttribute() 124 { 125 return this.attribute; 126 } 127 128 129 132 public Object getRawValue() 133 { 134 return this.rawValue; 135 } 136 137 138 141 public String getStringValue() 142 { 143 144 if ( this.rawValue == EMPTY_STRING_VALUE ) 145 { 146 return EMPTY_STRING_VALUE.getStringValue(); 147 } 148 else if ( this.rawValue == EMPTY_BINARY_VALUE ) 149 { 150 return EMPTY_BINARY_VALUE.getStringValue(); 151 } 152 else if ( this.rawValue instanceof String ) 153 { 154 return ( String ) this.rawValue; 155 } 156 else if ( this.rawValue instanceof byte[] ) 157 { 158 return LdifUtils.utf8decode( ( byte[] ) this.rawValue ); 159 } 160 else 161 { 162 return "UNKNOWN"; 163 } 164 } 165 166 167 170 public byte[] getBinaryValue() 171 { 172 if ( this.rawValue == EMPTY_STRING_VALUE ) 173 { 174 return EMPTY_STRING_VALUE.getBinaryValue(); 175 } 176 else if ( this.rawValue == EMPTY_BINARY_VALUE ) 177 { 178 return EMPTY_BINARY_VALUE.getBinaryValue(); 179 } 180 else if ( this.rawValue instanceof byte[] ) 181 { 182 return ( byte[] ) this.rawValue; 183 } 184 else if ( this.rawValue instanceof String ) 185 { 186 return LdifUtils.utf8encode( ( String ) this.rawValue ); 187 } 188 else 189 { 190 return LdifUtils.utf8encode( "UNKNOWN" ); 191 } 192 } 193 194 195 198 public boolean isString() 199 { 200 return this.rawValue == EMPTY_STRING_VALUE || this.attribute.isString(); 201 } 202 203 204 207 public boolean isBinary() 208 { 209 return this.rawValue == EMPTY_BINARY_VALUE || this.attribute.isBinary(); 210 } 211 212 213 216 public boolean isEmpty() 217 { 218 return this.rawValue == EMPTY_STRING_VALUE || this.rawValue == EMPTY_BINARY_VALUE; 219 } 220 221 222 225 public boolean equals( Object o ) 226 { 227 if ( o == null || !( o instanceof IValue ) ) 229 { 230 return false; 231 } 232 IValue vc = ( IValue ) o; 233 234 if ( !vc.getAttribute().equals( this.getAttribute() ) ) 236 { 237 return false; 238 } 239 240 if ( this.isEmpty() && vc.isEmpty() ) 242 { 243 return true; 244 } 245 else if ( this.isBinary() && vc.isBinary() ) 246 { 247 return Utils.equals( this.getBinaryValue(), vc.getBinaryValue() ); 248 } 249 else if ( this.isString() && vc.isString() ) 250 { 251 return ( this.getStringValue().equals( vc.getStringValue() ) ); 252 } 253 else 254 { 255 return false; 256 } 257 } 258 259 260 263 public int hashCode() 264 { 265 return rawValue.hashCode(); 266 } 267 268 269 272 public String toString() 273 { 274 return attribute + ":" + ( this.isString() ? this.getStringValue() : "BINARY" ); } 276 277 278 281 public Object getAdapter( Class adapter ) 282 { 283 Class <?> clazz = ( Class <?> ) adapter; 284 if ( clazz.isAssignableFrom( ISearchPageScoreComputer.class ) ) 285 { 286 return new LdapSearchPageScoreComputer(); 287 } 288 if ( clazz.isAssignableFrom( IConnection.class ) ) 289 { 290 return getAttribute().getEntry().getConnection(); 291 } 292 if ( clazz.isAssignableFrom( IEntry.class ) ) 293 { 294 return getAttribute().getEntry(); 295 } 296 if ( clazz.isAssignableFrom( IAttribute.class ) ) 297 { 298 return getAttribute(); 299 } 300 if ( clazz.isAssignableFrom( IValue.class ) ) 301 { 302 return this; 303 } 304 return null; 305 } 306 307 308 311 public boolean isRdnPart() 312 { 313 RDNPart[] parts = getAttribute().getEntry().getRdn().getParts(); 314 for ( int p = 0; p < parts.length; p++ ) 315 { 316 if ( getAttribute().getDescription().equals( parts[p].getType() ) 317 && getStringValue().equals( parts[p].getValue() ) ) 318 { 319 return true; 320 } 321 } 322 return false; 323 } 324 325 } 326 | Popular Tags |