1 20 21 package org.apache.directory.ldapstudio.browser.core.model.ldif.container; 22 23 24 import java.io.Serializable ; 25 import java.util.ArrayList ; 26 import java.util.Iterator ; 27 import java.util.List ; 28 29 import org.apache.directory.ldapstudio.browser.core.BrowserCoreConstants; 30 import org.apache.directory.ldapstudio.browser.core.model.ldif.LdifInvalidPart; 31 import org.apache.directory.ldapstudio.browser.core.model.ldif.LdifPart; 32 import org.apache.directory.ldapstudio.browser.core.model.ldif.lines.LdifLineBase; 33 34 35 public abstract class LdifContainer implements Serializable 36 { 37 38 protected List parts; 39 40 41 protected LdifContainer() 42 { 43 } 44 45 46 protected LdifContainer( LdifPart part ) 47 { 48 this.parts = new ArrayList ( 1 ); 49 if ( part == null ) 50 throw new IllegalArgumentException ( "null argument" ); 51 this.parts.add( part ); 52 } 53 54 55 public final int getOffset() 56 { 57 return ( ( LdifPart ) this.parts.get( 0 ) ).getOffset(); 58 } 59 60 61 public final int getLength() 62 { 63 LdifPart lastPart = this.getLastPart(); 64 return lastPart.getOffset() + lastPart.getLength() - getOffset(); 65 } 66 67 68 public final void addInvalid( LdifInvalidPart invalid ) 69 { 70 if ( invalid == null ) 71 throw new IllegalArgumentException ( "null argument" ); 72 this.parts.add( invalid ); 73 } 74 75 76 public final LdifPart getLastPart() 77 { 78 return ( LdifPart ) parts.get( parts.size() - 1 ); 79 } 80 81 82 public final LdifPart[] getParts() 83 { 84 return ( LdifPart[] ) this.parts.toArray( new LdifPart[parts.size()] ); 85 } 86 87 88 public final String toString() 89 { 90 StringBuffer sb = new StringBuffer (); 91 92 sb.append( getClass().getName() ); 93 sb.append( ":" ); 94 sb.append( BrowserCoreConstants.LINE_SEPARATOR ); 95 96 LdifPart[] parts = this.getParts(); 97 for ( int i = 0; i < parts.length; i++ ) 98 { 99 sb.append( " " ); 100 sb.append( parts[i].toString() ); 101 sb.append( BrowserCoreConstants.LINE_SEPARATOR ); 102 } 103 104 return sb.toString(); 105 } 106 107 108 public final String toRawString() 109 { 110 StringBuffer sb = new StringBuffer (); 111 112 LdifPart[] parts = this.getParts(); 113 for ( int i = 0; i < parts.length; i++ ) 114 { 115 sb.append( parts[i].toRawString() ); 116 } 117 118 return sb.toString(); 119 } 120 121 122 public final String toFormattedString() 123 { 124 StringBuffer sb = new StringBuffer (); 125 126 LdifPart[] parts = this.getParts(); 127 for ( int i = 0; i < parts.length; i++ ) 128 { 129 sb.append( parts[i].toFormattedString() ); 130 } 131 132 return sb.toString(); 133 } 134 135 136 public abstract boolean isValid(); 137 138 139 147 protected boolean isAbstractValid() 148 { 149 if ( this.parts.isEmpty() ) 150 return false; 151 152 boolean containsLine = false; 153 LdifPart[] parts = this.getParts(); 154 for ( int i = 0; i < parts.length; i++ ) 155 { 156 if ( parts[i] instanceof LdifInvalidPart ) 157 { 158 return false; 159 } 160 if ( !parts[i].isValid() ) 161 { 162 return false; 163 } 164 if ( parts[i] instanceof LdifLineBase ) 165 { 166 containsLine = true; 167 } 168 } 169 return containsLine; 170 } 171 172 173 public String getInvalidString() 174 { 175 if ( this.parts.isEmpty() ) 176 return "Empty Container"; 177 178 LdifPart[] parts = this.getParts(); 179 for ( int i = 0; i < parts.length; i++ ) 180 { 181 if ( !parts[i].isValid() ) 182 { 183 return parts[i].getInvalidString(); 184 } 185 } 186 187 return null; 188 } 189 190 191 public final void adjustOffset( int adjust ) 192 { 193 for ( Iterator it = this.parts.iterator(); it.hasNext(); ) 194 { 195 LdifPart part = ( LdifPart ) it.next(); 196 part.adjustOffset( adjust ); 197 } 198 } 199 200 } 201 | Popular Tags |