1 20 21 package org.apache.directory.ldapstudio.browser.core.utils; 22 23 24 27 import org.apache.directory.ldapstudio.browser.core.events.EventRegistry; 28 import org.apache.directory.ldapstudio.browser.core.internal.model.Attribute; 29 import org.apache.directory.ldapstudio.browser.core.internal.model.DummyEntry; 30 import org.apache.directory.ldapstudio.browser.core.internal.model.Value; 31 import org.apache.directory.ldapstudio.browser.core.model.DN; 32 import org.apache.directory.ldapstudio.browser.core.model.IAttribute; 33 import org.apache.directory.ldapstudio.browser.core.model.IConnection; 34 import org.apache.directory.ldapstudio.browser.core.model.IEntry; 35 import org.apache.directory.ldapstudio.browser.core.model.IValue; 36 import org.apache.directory.ldapstudio.browser.core.model.ModelModificationException; 37 import org.apache.directory.ldapstudio.browser.core.model.NameException; 38 import org.apache.directory.ldapstudio.browser.core.model.ldif.LdifPart; 39 import org.apache.directory.ldapstudio.browser.core.model.ldif.container.LdifChangeAddRecord; 40 import org.apache.directory.ldapstudio.browser.core.model.ldif.container.LdifChangeRecord; 41 import org.apache.directory.ldapstudio.browser.core.model.ldif.container.LdifContentRecord; 42 import org.apache.directory.ldapstudio.browser.core.model.ldif.container.LdifRecord; 43 import org.apache.directory.ldapstudio.browser.core.model.ldif.lines.LdifAttrValLine; 44 import org.apache.directory.ldapstudio.browser.core.model.ldif.lines.LdifChangeTypeLine; 45 import org.apache.directory.ldapstudio.browser.core.model.ldif.lines.LdifCommentLine; 46 import org.apache.directory.ldapstudio.browser.core.model.ldif.lines.LdifControlLine; 47 import org.apache.directory.ldapstudio.browser.core.model.ldif.lines.LdifDnLine; 48 import org.apache.directory.ldapstudio.browser.core.model.ldif.lines.LdifSepLine; 49 50 51 public class ModelConverter 52 { 53 54 55 66 public static DummyEntry ldifContentRecordToEntry( LdifContentRecord ldifContentRecord, IConnection connection ) 67 throws NameException, ModelModificationException 68 { 69 return createIntern( ldifContentRecord, connection ); 70 } 71 72 73 84 public static DummyEntry ldifChangeAddRecordToEntry( LdifChangeAddRecord ldifChangeAddRecord, IConnection connection ) 85 throws NameException, ModelModificationException 86 { 87 return createIntern( ldifChangeAddRecord, connection ); 88 } 89 90 91 102 private static DummyEntry createIntern( LdifRecord ldifRecord, IConnection connection ) throws NameException, 103 ModelModificationException 104 { 105 LdifPart[] parts = ldifRecord.getParts(); 106 107 EventRegistry.suspendEventFireingInCurrentThread(); 108 109 DummyEntry entry = new DummyEntry( new DN( ldifRecord.getDnLine().getValueAsString() ), connection ); 110 111 for ( int i = 0; i < parts.length; i++ ) 112 { 113 if ( parts[i] instanceof LdifAttrValLine ) 114 { 115 LdifAttrValLine line = ( LdifAttrValLine ) parts[i]; 116 String attributeName = line.getUnfoldedAttributeDescription(); 117 Object value = line.getValueAsObject(); 118 IAttribute attribute = entry.getAttribute( attributeName ); 119 if ( attribute == null ) 120 { 121 attribute = new Attribute( entry, attributeName ); 122 entry.addAttribute( attribute ); 123 } 124 attribute.addValue( new Value( attribute, value ) ); 125 } 126 else if ( !( parts[i] instanceof LdifDnLine ) && !( parts[i] instanceof LdifSepLine ) ) 127 { 128 String name = parts[i].toRawString(); 129 name = name.replaceAll( "\n", "" ); 130 name = name.replaceAll( "\r", "" ); 131 IAttribute attribute = new Attribute( entry, name ); 132 attribute.addValue( new Value( attribute, parts[i] ) ); 133 entry.addAttribute( attribute ); 134 } 141 } 142 143 EventRegistry.resumeEventFireingInCurrentThread(); 144 145 return entry; 146 } 147 148 149 public static LdifChangeAddRecord entryToLdifChangeAddRecord( IEntry entry ) 150 { 151 152 boolean mustCreateChangeTypeLine = true; 153 IAttribute[] attributes = entry.getAttributes(); 154 for ( int i = 0; i < attributes.length; i++ ) 155 { 156 IValue[] values = attributes[i].getValues(); 157 for ( int ii = 0; ii < values.length; ii++ ) 158 { 159 IValue value = values[ii]; 160 if ( value.getRawValue() instanceof LdifPart ) 161 { 162 mustCreateChangeTypeLine = false; 163 } 164 } 165 } 166 167 LdifChangeAddRecord record = new LdifChangeAddRecord( LdifDnLine.create( entry.getDn().toString() ) ); 170 if ( mustCreateChangeTypeLine ) 171 { 172 addControls( record, entry ); 173 record.setChangeType( LdifChangeTypeLine.createAdd() ); 174 } 175 176 for ( int i = 0; i < attributes.length; i++ ) 177 { 178 String name = attributes[i].getDescription(); 179 IValue[] values = attributes[i].getValues(); 180 for ( int ii = 0; ii < values.length; ii++ ) 181 { 182 IValue value = values[ii]; 183 if ( value.getRawValue() instanceof LdifPart ) 184 { 185 LdifPart part = ( LdifPart ) value.getRawValue(); 186 if ( part instanceof LdifChangeTypeLine ) 187 { 188 record.setChangeType( ( LdifChangeTypeLine ) part ); 189 } 190 else if ( part instanceof LdifCommentLine ) 191 { 192 record.addComment( ( LdifCommentLine ) part ); 193 } 194 else if ( part instanceof LdifControlLine ) 195 { 196 record.addControl( ( LdifControlLine ) part ); 197 } 198 } 199 else if ( value.isString() ) 200 { 201 record.addAttrVal( LdifAttrValLine.create( name, value.getStringValue() ) ); 202 } 203 else 204 { 205 record.addAttrVal( LdifAttrValLine.create( name, value.getBinaryValue() ) ); 206 } 207 } 208 } 209 210 record.finish( LdifSepLine.create() ); 211 212 return record; 213 } 214 215 216 public static LdifContentRecord entryToLdifContentRecord( IEntry entry ) 217 { 218 219 LdifContentRecord record = LdifContentRecord.create( entry.getDn().toString() ); 220 221 IAttribute[] attributes = entry.getAttributes(); 222 for ( int i = 0; i < attributes.length; i++ ) 223 { 224 String name = attributes[i].getDescription(); 225 IValue[] values = attributes[i].getValues(); 226 for ( int ii = 0; ii < values.length; ii++ ) 227 { 228 IValue value = values[ii]; 229 if ( value.getRawValue() instanceof LdifPart ) 230 { 231 LdifPart part = ( LdifPart ) value.getRawValue(); 232 if ( part instanceof LdifCommentLine ) 233 { 234 record.addComment( ( LdifCommentLine ) part ); 235 } 236 } 237 else if ( value.isString() ) 238 { 239 record.addAttrVal( LdifAttrValLine.create( name, value.getStringValue() ) ); 240 } 241 else 242 { 243 record.addAttrVal( LdifAttrValLine.create( name, value.getBinaryValue() ) ); 244 } 245 } 246 } 247 248 record.finish( LdifSepLine.create() ); 249 250 return record; 251 } 252 253 254 public static LdifAttrValLine valueToLdifAttrValLine( IValue value ) 255 { 256 257 LdifAttrValLine line; 258 if ( value.isString() ) 259 { 260 line = LdifAttrValLine.create( value.getAttribute().getDescription(), value.getStringValue() ); 261 } 262 else 263 { 264 line = LdifAttrValLine.create( value.getAttribute().getDescription(), value.getBinaryValue() ); 265 } 266 return line; 267 } 268 269 270 public static IValue ldifAttrValLineToValue( LdifAttrValLine line, IEntry entry ) 271 { 272 try 273 { 274 IAttribute attribute = new Attribute( entry, line.getUnfoldedAttributeDescription() ); 275 IValue value = new Value( attribute, line.getValueAsObject() ); 276 return value; 277 } 278 catch ( Exception e ) 279 { 280 return null; 281 } 282 } 283 284 285 public static LdifDnLine dnToLdifDnLine( DN dn ) 286 { 287 LdifDnLine line = LdifDnLine.create( dn.toString() ); 288 return line; 289 } 290 291 292 public static void addControls( LdifChangeRecord cr, IEntry entry ) 293 { 294 if ( entry.isReferral() ) 295 { 296 cr.addControl( LdifControlLine.create( IConnection.CONTROL_MANAGEDSAIT, null, ( String ) null ) ); 297 } 298 } 299 300 } 301 | Popular Tags |