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 37 38 46 public abstract class AbstractDialogStringValueEditor extends AbstractDialogValueEditor 47 { 48 49 52 protected AbstractDialogStringValueEditor() 53 { 54 super(); 55 } 56 57 58 63 public String getDisplayValue( IValue value ) 64 { 65 Object obj = this.getRawValue( value ); 66 return obj == null ? "NULL" : obj.toString(); 67 } 68 69 70 76 protected Object getEmptyRawValue( IAttribute attribute ) 77 { 78 if ( attribute.isString() ) 79 { 80 return IValue.EMPTY_STRING_VALUE; 81 } 82 else 83 { 84 return IValue.EMPTY_BINARY_VALUE; 85 } 86 } 87 88 89 95 public Object getRawValue( IValue value ) 96 { 97 if ( value == null ) 98 { 99 return null; 100 } 101 else if ( value.isString() ) 102 { 103 return value.getStringValue(); 104 } 105 else if ( value.isBinary() ) 106 { 107 return isEditable( value.getBinaryValue() ) ? value.getStringValue() : null; 108 } 109 else 110 { 111 return null; 112 } 113 } 114 115 116 123 public Object getRawValue( IConnection connection, Object value ) 124 { 125 if ( value == null ) 126 { 127 return null; 128 } 129 else if ( value instanceof String ) 130 { 131 return value; 132 } 133 else if ( value instanceof byte[] ) 134 { 135 String s = LdifUtils.utf8decode( ( byte[] ) value ); 136 for ( int i = 0; i < s.length(); i++ ) 137 { 138 if ( Character.isISOControl( s.charAt( i ) ) && s.charAt( i ) != '\n' && s.charAt( i ) != '\r' ) 139 { 140 return null; 141 } 142 } 143 return s; 144 } 145 else 146 { 147 return null; 148 } 149 } 150 151 152 155 private boolean isEditable( byte[] b ) 156 { 157 if ( b == null ) 158 { 159 return false; 160 } 161 162 for ( int i = 0; i < b.length; i++ ) 163 { 164 if ( !( b[i] == '\n' || b[i] == '\r' || ( b[i] >= '\u0020' && b[i] <= '\u007F' ) ) ) 165 { 166 return false; 167 } 168 } 169 170 return true; 171 } 172 173 174 180 public Object getStringOrBinaryValue( Object rawValue ) 181 { 182 if ( rawValue == null ) 183 { 184 return null; 185 } 186 else if ( rawValue instanceof String ) 187 { 188 return rawValue; 189 } 190 else 191 { 192 return null; 193 } 194 } 195 196 197 200 public final void createValue( IEntry entry, String attributeDescription, Object newRawValue ) 201 throws ModelModificationException 202 { 203 if ( entry != null && attributeDescription != null && newRawValue != null && newRawValue instanceof String ) 204 { 205 if ( entry.getAttribute( attributeDescription ) != null ) 206 { 207 this.modify( entry.getAttribute( attributeDescription ), newRawValue ); 208 } 209 else 210 { 211 EventRegistry.suspendEventFireingInCurrentThread(); 212 IAttribute attribute = new Attribute( entry, attributeDescription ); 213 entry.addAttribute( attribute ); 214 EventRegistry.resumeEventFireingInCurrentThread(); 215 216 Object newValue; 217 if ( entry.getConnection().getSchema().getAttributeTypeDescription( attributeDescription ) 218 .getSyntaxDescription().isString() ) 219 { 220 newValue = ( String ) newRawValue; 221 } 222 else 223 { 224 newValue = LdifUtils.utf8encode( ( String ) newRawValue ); 225 } 226 227 new CreateValuesJob( attribute, newValue ).execute(); 228 } 229 } 230 } 231 232 233 private final void modify( IAttribute attribute, Object newRawValue ) throws ModelModificationException 234 { 235 if ( attribute != null && newRawValue != null && newRawValue instanceof String ) 236 { 237 if ( attribute.getValueSize() == 0 ) 238 { 239 String newValue = ( String ) newRawValue; 240 new CreateValuesJob( attribute, newValue ).execute(); 241 } 242 else if ( attribute.getValueSize() == 1 ) 243 { 244 this.modifyValue( attribute.getValues()[0], newRawValue ); 245 } 246 } 247 } 248 249 250 253 public final void modifyValue( IValue oldValue, Object newRawValue ) throws ModelModificationException 254 { 255 if ( oldValue != null && newRawValue != null && newRawValue instanceof String ) 256 { 257 258 String newValue = ( String ) newRawValue; 259 IAttribute attribute = oldValue.getAttribute(); 260 if ( !oldValue.getStringValue().equals( newValue ) ) 261 { 262 if ( oldValue.isEmpty() ) 263 { 264 EventRegistry.suspendEventFireingInCurrentThread(); 265 attribute.deleteEmptyValue(); 266 EventRegistry.resumeEventFireingInCurrentThread(); 267 new CreateValuesJob( attribute, newValue ).execute(); 268 } 269 else 270 { 271 new ModifyValueJob( attribute, oldValue, newValue ).execute(); 272 } 273 } 274 } 275 } 276 277 278 281 public final void deleteAttribute( AttributeHierarchy ah ) throws ModelModificationException 282 { 283 if ( ah != null ) 284 { 285 new DeleteAttributesValueJob( ah ).execute(); 286 } 287 } 288 289 290 293 public final void deleteValue( IValue oldValue ) throws ModelModificationException 294 { 295 if ( oldValue != null ) 296 { 297 new DeleteAttributesValueJob( oldValue ).execute(); 298 } 299 } 300 301 } 302 | Popular Tags |