1 20 21 package org.apache.directory.ldapstudio.browser.core.model.ldif; 22 23 24 import java.io.Serializable ; 25 import java.util.ArrayList ; 26 import java.util.Arrays ; 27 import java.util.Iterator ; 28 import java.util.List ; 29 30 import org.apache.directory.ldapstudio.browser.core.model.ldif.container.LdifChangeRecord; 31 import org.apache.directory.ldapstudio.browser.core.model.ldif.container.LdifContainer; 32 import org.apache.directory.ldapstudio.browser.core.model.ldif.container.LdifContentRecord; 33 import org.apache.directory.ldapstudio.browser.core.model.ldif.container.LdifModSpec; 34 import org.apache.directory.ldapstudio.browser.core.model.ldif.container.LdifRecord; 35 36 37 public class LdifFile implements Serializable  38 { 39 40 private static final long serialVersionUID = 846864138240517008L; 41 42 private List containerList; 43 44 45 public LdifFile() 46 { 47 super(); 48 this.containerList = new ArrayList ( 1 ); 49 } 50 51 52 public boolean isContentType() 53 { 54 for ( Iterator it = this.containerList.iterator(); it.hasNext(); ) 55 { 56 Object o = it.next(); 57 if ( o instanceof LdifRecord ) 58 { 59 return o instanceof LdifContentRecord; 60 } 61 } 62 return false; 63 } 64 65 66 public boolean isChangeType() 67 { 68 for ( Iterator it = this.containerList.iterator(); it.hasNext(); ) 69 { 70 Object o = it.next(); 71 if ( o instanceof LdifRecord ) 72 { 73 return o instanceof LdifChangeRecord; 74 } 75 } 76 return false; 77 } 78 79 80 public void addContainer( LdifContainer container ) 81 { 82 this.containerList.add( container ); 83 } 84 85 86 91 public LdifContainer[] getContainers() 92 { 93 return ( LdifContainer[] ) this.containerList.toArray( new LdifContainer[this.containerList.size()] ); 94 } 95 96 97 102 public LdifRecord[] getRecords() 103 { 104 List l = new ArrayList (); 105 for ( Iterator it = this.containerList.iterator(); it.hasNext(); ) 106 { 107 Object o = it.next(); 108 if ( o instanceof LdifRecord ) 109 { 110 l.add( o ); 111 } 112 } 113 return ( LdifRecord[] ) l.toArray( new LdifRecord[l.size()] ); 114 } 115 116 117 121 public LdifContainer getLastContainer() 122 { 123 if ( this.containerList.isEmpty() ) 124 { 125 return null; 126 } 127 else 128 { 129 return ( LdifContainer ) this.containerList.get( this.containerList.size() - 1 ); 130 } 131 } 132 133 134 public String toRawString() 135 { 136 StringBuffer sb = new StringBuffer (); 137 138 LdifContainer[] containers = this.getContainers(); 139 for ( int i = 0; i < containers.length; i++ ) 140 { 141 sb.append( containers[i].toRawString() ); 142 } 143 144 return sb.toString(); 145 } 146 147 148 public String toFormattedString() 149 { 150 StringBuffer sb = new StringBuffer (); 151 152 LdifContainer[] containers = this.getContainers(); 153 for ( int i = 0; i < containers.length; i++ ) 154 { 155 sb.append( containers[i].toFormattedString() ); 156 } 157 158 return sb.toString(); 159 } 160 161 162 public String toString() 163 { 164 StringBuffer sb = new StringBuffer (); 165 166 LdifContainer[] containers = this.getContainers(); 167 for ( int i = 0; i < containers.length; i++ ) 168 { 169 sb.append( containers[i].toString() ); 170 } 171 172 return sb.toString(); 173 } 174 175 176 public static LdifContainer getContainer( LdifFile model, int offset ) 177 { 178 if ( model == null || offset < 0 ) 179 return null; 180 181 LdifContainer container = null; 182 LdifContainer[] containers = model.getContainers(); 183 if ( containers.length > 0 ) 184 { 185 for ( int i = 0; i < containers.length; i++ ) 186 { 187 if ( containers[i].getOffset() <= offset 188 && offset < containers[i].getOffset() + containers[i].getLength() ) 189 { 190 container = containers[i]; 191 break; 192 } 193 } 194 } 195 return container; 196 } 197 198 199 public static LdifModSpec getInnerContainer( LdifContainer container, int offset ) 200 { 201 if ( container == null || offset < container.getOffset() 202 || offset > container.getOffset() + container.getLength() ) 203 return null; 204 205 LdifModSpec innerContainer = null; 206 LdifPart[] parts = container.getParts(); 207 if ( parts.length > 0 ) 208 { 209 int partIndex = -1; 210 211 for ( int i = 0; i < parts.length; i++ ) 212 { 213 int start = parts[i].getOffset(); 214 int end = parts[i].getOffset() + parts[i].getLength(); 215 if ( start <= offset && offset < end ) 216 { 217 partIndex = i; 218 break; 219 } 220 } 221 222 if ( partIndex > -1 ) 223 { 224 if ( parts[partIndex] instanceof LdifModSpec ) 225 { 226 innerContainer = ( LdifModSpec ) parts[partIndex]; 227 } 228 } 229 } 230 return innerContainer; 231 } 232 233 234 public static LdifContainer[] getContainers( LdifFile model, int offset, int length ) 235 { 236 if ( model == null || offset < 0 ) 237 return null; 238 239 ArrayList containerList = new ArrayList (); 240 241 LdifContainer[] containers = model.getContainers(); 242 if ( containers.length > 0 ) 243 { 244 for ( int i = 0; i < containers.length; i++ ) 245 { 246 if ( offset < containers[i].getOffset() + containers[i].getLength() 247 && offset + length > containers[i].getOffset() ) 248 { 249 containerList.add( containers[i] ); 250 } 251 } 252 } 253 254 return ( LdifContainer[] ) containerList.toArray( new LdifContainer[containerList.size()] ); 255 } 256 257 258 public static LdifPart[] getParts( LdifFile model, int offset, int length ) 259 { 260 if ( model == null || offset < 0 ) 261 return null; 262 263 LdifContainer[] containers = model.getContainers(); 264 return getParts( containers, offset, length ); 265 266 } 267 268 269 public static LdifPart[] getParts( LdifContainer[] containers, int offset, int length ) 270 { 271 if ( containers == null || offset < 0 ) 272 return null; 273 274 ArrayList partList = new ArrayList (); 275 276 for ( int i = 0; i < containers.length; i++ ) 277 { 278 if ( offset < containers[i].getOffset() + containers[i].getLength() 279 && offset + length >= containers[i].getOffset() ) 280 { 281 LdifPart[] parts = containers[i].getParts(); 282 if ( parts.length > 0 ) 283 { 284 for ( int p = 0; p < parts.length; p++ ) 285 { 286 if ( offset < parts[p].getOffset() + parts[p].getLength() 287 && offset + length >= parts[p].getOffset() ) 288 { 289 LdifPart part = parts[p]; 290 291 if ( part instanceof LdifModSpec ) 292 { 293 LdifModSpec spec = ( LdifModSpec ) part; 294 partList.addAll( Arrays.asList( getParts( new LdifContainer[] 295 { spec }, offset, length ) ) ); 296 } 297 else 298 { 299 if ( part instanceof LdifInvalidPart && p > 0 ) 300 { 301 part = parts[p - 1]; 302 } 303 304 partList.add( part ); 305 } 306 } 307 } 308 } 309 } 310 } 311 312 return ( LdifPart[] ) partList.toArray( new LdifPart[partList.size()] ); 313 } 314 315 316 public static LdifPart getContainerContent( LdifContainer container, int offset ) 317 { 318 if ( container == null || offset < container.getOffset() 319 || offset > container.getOffset() + container.getLength() ) 320 return null; 321 322 LdifPart part = null; 323 LdifPart[] parts = container.getParts(); 324 if ( parts.length > 0 ) 325 { 326 int partIndex = -1; 327 328 for ( int i = 0; i < parts.length; i++ ) 329 { 330 int start = parts[i].getOffset(); 331 int end = parts[i].getOffset() + parts[i].getLength(); 332 if ( start <= offset && offset < end ) 333 { 334 partIndex = i; 335 break; 336 } 337 } 338 339 if ( partIndex > -1 ) 340 { 341 part = parts[partIndex]; 342 if ( part instanceof LdifModSpec ) 343 { 344 part = getContainerContent( ( LdifModSpec ) part, offset ); 345 } 346 } 351 } 352 return part; 353 } 354 355 356 390 public void replace( LdifContainer[] oldContainers, LdifContainer[] newContainers ) 391 { 392 393 int index = 0; 395 if ( oldContainers.length > 0 ) 396 { 397 index = this.containerList.indexOf( oldContainers[0] ); 398 } 399 400 int removeLength = 0; 402 int removeOffset = 0; 403 if ( oldContainers.length > 0 ) 404 { 405 removeOffset = oldContainers[0].getOffset(); 406 for ( int i = 0; i < oldContainers.length; i++ ) 407 { 408 this.containerList.remove( index ); 409 removeLength += oldContainers[i].getLength(); 410 } 411 } 412 413 int insertLength = 0; 415 for ( int i = 0; i < newContainers.length; i++ ) 416 { 417 newContainers[i].adjustOffset( removeOffset ); 418 insertLength += newContainers[i].getLength(); 419 this.containerList.add( index + i, newContainers[i] ); 420 } 421 422 int adjust = insertLength - removeLength; 424 for ( int i = index + newContainers.length; i < this.containerList.size(); i++ ) 425 { 426 LdifContainer container = ( LdifContainer ) this.containerList.get( i ); 427 container.adjustOffset( adjust ); 428 } 429 430 } 431 432 } 433 | Popular Tags |