1 20 21 package org.apache.directory.ldapstudio.valueeditors; 22 23 24 import org.apache.directory.ldapstudio.browser.core.events.EventRegistry; 25 import org.apache.directory.ldapstudio.browser.core.internal.model.Attribute; 26 import org.apache.directory.ldapstudio.browser.core.jobs.CreateValuesJob; 27 import org.apache.directory.ldapstudio.browser.core.jobs.DeleteAttributesValueJob; 28 import org.apache.directory.ldapstudio.browser.core.jobs.ModifyValueJob; 29 import org.apache.directory.ldapstudio.browser.core.model.AttributeHierarchy; 30 import org.apache.directory.ldapstudio.browser.core.model.IAttribute; 31 import org.apache.directory.ldapstudio.browser.core.model.IConnection; 32 import org.apache.directory.ldapstudio.browser.core.model.IEntry; 33 import org.apache.directory.ldapstudio.browser.core.model.IValue; 34 import org.apache.directory.ldapstudio.browser.core.model.ModelModificationException; 35 import org.apache.directory.ldapstudio.browser.core.utils.LdifUtils; 36 import org.apache.directory.ldapstudio.browser.core.utils.Utils; 37 38 39 47 public abstract class AbstractDialogBinaryValueEditor extends AbstractDialogValueEditor 48 { 49 50 53 protected AbstractDialogBinaryValueEditor() 54 { 55 } 56 57 58 64 public String getDisplayValue( IValue value ) 65 { 66 if ( showRawValues() ) 67 { 68 return getPrintableString( value ); 69 } 70 else 71 { 72 if ( value == null ) 73 { 74 return "NULL"; 75 } 76 else if ( value.isBinary() ) 77 { 78 byte[] data = value.getBinaryValue(); 79 return "Binary Data (" + data.length + " Bytes)"; 80 } 81 else 82 { 83 return "Invalid Data"; 84 } 85 } 86 } 87 88 89 97 public static String getPrintableString( IValue value ) 98 { 99 if ( value == null ) 100 { 101 return "NULL"; 102 } 103 else if ( value.isBinary() ) 104 { 105 byte[] data = value.getBinaryValue(); 106 StringBuffer sb = new StringBuffer (); 107 for ( int i = 0; data != null && i < data.length && i < 128; i++ ) 108 { 109 if ( data[i] > 32 && data[i] < 127 ) 110 sb.append( ( char ) data[i] ); 111 else 112 sb.append( '.' ); 113 } 114 return sb.toString(); 115 } 116 else if ( value.isString() ) 117 { 118 return value.getStringValue(); 119 } 120 else 121 { 122 return "NULL"; 123 } 124 } 125 126 127 133 protected Object getEmptyRawValue( IAttribute attribute ) 134 { 135 if ( attribute.isBinary() ) 136 { 137 return IValue.EMPTY_BINARY_VALUE; 138 } 139 else 140 { 141 return null; 142 } 143 } 144 145 146 152 public Object getRawValue( IValue value ) 153 { 154 if ( value == null ) 155 { 156 return null; 157 } 158 else if ( value.isString() ) 159 { 160 return value.getBinaryValue(); 161 } 162 else if ( value.isBinary() ) 163 { 164 return value.getBinaryValue(); 165 } 166 else 167 { 168 return null; 169 } 170 } 171 172 173 180 public Object getRawValue( IConnection connection, Object value ) 181 { 182 if ( value == null ) 183 { 184 return null; 185 } 186 else if ( value instanceof String ) 187 { 188 return LdifUtils.utf8encode( ( String ) value ); 189 } 190 else if ( value instanceof byte[] ) 191 { 192 return value; 193 } 194 else 195 { 196 return null; 197 } 198 } 199 200 201 207 public Object getStringOrBinaryValue( Object rawValue ) 208 { 209 if ( rawValue == null ) 210 { 211 return null; 212 } 213 else if ( rawValue instanceof byte[] ) 214 { 215 return rawValue; 216 } 217 else 218 { 219 return null; 220 } 221 } 222 223 224 227 public final void createValue( IEntry entry, String attributeDescription, Object newRawValue ) 228 throws ModelModificationException 229 { 230 if ( entry != null && attributeDescription != null && newRawValue != null && newRawValue instanceof byte[] ) 231 { 232 if ( entry.getAttribute( attributeDescription ) != null ) 233 { 234 this.modify( entry.getAttribute( attributeDescription ), newRawValue ); 235 } 236 else 237 { 238 EventRegistry.suspendEventFireingInCurrentThread(); 239 IAttribute attribute = new Attribute( entry, attributeDescription ); 240 entry.addAttribute( attribute ); 241 EventRegistry.resumeEventFireingInCurrentThread(); 242 243 Object newValue; 244 if ( entry.getConnection().getSchema().getAttributeTypeDescription( attributeDescription ) 245 .getSyntaxDescription().isString() ) 246 { 247 newValue = LdifUtils.utf8decode( ( byte[] ) newRawValue ); 248 } 249 else 250 { 251 newValue = ( byte[] ) newRawValue; 252 } 253 254 new CreateValuesJob( attribute, newValue ).execute(); 255 } 256 } 257 } 258 259 260 private final void modify( IAttribute attribute, Object newRawValue ) throws ModelModificationException 261 { 262 if ( attribute != null && newRawValue != null && newRawValue instanceof byte[] ) 263 { 264 if ( attribute.getValueSize() == 0 ) 265 { 266 byte[] newValue = ( byte[] ) newRawValue; 267 new CreateValuesJob( attribute, newValue ).execute(); 268 } 269 else if ( attribute.getValueSize() == 1 ) 270 { 271 this.modifyValue( attribute.getValues()[0], newRawValue ); 272 } 273 } 274 } 275 276 277 280 public final void modifyValue( IValue oldValue, Object newRawValue ) throws ModelModificationException 281 { 282 if ( oldValue != null && newRawValue != null && newRawValue instanceof byte[] ) 283 { 284 byte[] newValue = ( byte[] ) newRawValue; 285 IAttribute attribute = oldValue.getAttribute(); 286 if ( !Utils.equals( oldValue.getBinaryValue(), newValue ) ) 287 { 288 if ( oldValue.isEmpty() ) 289 { 290 EventRegistry.suspendEventFireingInCurrentThread(); 291 attribute.deleteEmptyValue(); 292 EventRegistry.resumeEventFireingInCurrentThread(); 293 new CreateValuesJob( attribute, newValue ).execute(); 294 } 295 else 296 { 297 new ModifyValueJob( attribute, oldValue, newValue ).execute(); 298 } 299 } 300 } 301 } 302 303 304 307 public final void deleteAttribute( AttributeHierarchy ah ) throws ModelModificationException 308 { 309 if ( ah != null ) 310 { 311 new DeleteAttributesValueJob( ah ).execute(); 312 } 313 } 314 315 316 319 public final void deleteValue( IValue oldValue ) throws ModelModificationException 320 { 321 if ( oldValue != null ) 322 { 323 new DeleteAttributesValueJob( oldValue ).execute(); 324 } 325 } 326 327 } 328 | Popular Tags |