1 20 21 package org.apache.directory.ldapstudio.browser.core.model.ldif.container; 22 23 24 import java.util.ArrayList ; 25 import java.util.Iterator ; 26 import java.util.List ; 27 28 import org.apache.directory.ldapstudio.browser.core.model.ldif.LdifPart; 29 import org.apache.directory.ldapstudio.browser.core.model.ldif.lines.LdifAttrValLine; 30 import org.apache.directory.ldapstudio.browser.core.model.ldif.lines.LdifModSpecSepLine; 31 import org.apache.directory.ldapstudio.browser.core.model.ldif.lines.LdifModSpecTypeLine; 32 33 34 public class LdifModSpec extends LdifContainer implements LdifPart 35 { 36 37 private static final long serialVersionUID = 6708749639253050273L; 38 39 40 protected LdifModSpec() 41 { 42 } 43 44 45 public LdifModSpec( LdifModSpecTypeLine modSpecTypeLine ) 46 { 47 super( modSpecTypeLine ); 48 } 49 50 51 public void addAttrVal( LdifAttrValLine attrVal ) 52 { 53 if ( attrVal == null ) 54 throw new IllegalArgumentException ( "null argument" ); 55 this.parts.add( attrVal ); 56 } 57 58 59 public void finish( LdifModSpecSepLine modSpecSepLine ) 60 { 61 if ( modSpecSepLine == null ) 62 throw new IllegalArgumentException ( "null argument" ); 63 this.parts.add( modSpecSepLine ); 64 } 65 66 67 public LdifModSpecTypeLine getModSpecType() 68 { 69 return ( LdifModSpecTypeLine ) this.parts.get( 0 ); 70 } 71 72 73 public LdifAttrValLine[] getAttrVals() 74 { 75 List l = new ArrayList (); 76 for ( Iterator it = this.parts.iterator(); it.hasNext(); ) 77 { 78 Object o = it.next(); 79 if ( o instanceof LdifAttrValLine ) 80 { 81 l.add( o ); 82 } 83 } 84 return ( LdifAttrValLine[] ) l.toArray( new LdifAttrValLine[l.size()] ); 85 } 86 87 88 public LdifModSpecSepLine getModSpecSep() 89 { 90 if ( getLastPart() instanceof LdifModSpecSepLine ) 91 { 92 return ( LdifModSpecSepLine ) getLastPart(); 93 } 94 else 95 { 96 return null; 97 } 98 } 99 100 101 public boolean isAdd() 102 { 103 return this.getModSpecType().isAdd(); 104 } 105 106 107 public boolean isReplace() 108 { 109 return this.getModSpecType().isReplace(); 110 } 111 112 113 public boolean isDelete() 114 { 115 return this.getModSpecType().isDelete(); 116 } 117 118 119 public static LdifModSpec createAdd( String attributeName ) 120 { 121 return new LdifModSpec( LdifModSpecTypeLine.createAdd( attributeName ) ); 122 } 123 124 125 public static LdifModSpec createReplace( String attributeName ) 126 { 127 return new LdifModSpec( LdifModSpecTypeLine.createReplace( attributeName ) ); 128 } 129 130 131 public static LdifModSpec createDelete( String attributeName ) 132 { 133 return new LdifModSpec( LdifModSpecTypeLine.createDelete( attributeName ) ); 134 } 135 136 137 public boolean isValid() 138 { 139 if ( !super.isAbstractValid() ) 140 { 141 return false; 142 } 143 144 if ( this.getModSpecType() == null ) 145 { 146 return false; 147 } 148 149 LdifAttrValLine[] attrVals = this.getAttrVals(); 150 if ( attrVals.length > 0 ) 151 { 152 String att = this.getModSpecType().getUnfoldedAttributeDescription(); 153 for ( int i = 0; i < attrVals.length; i++ ) 154 { 155 if ( !att.equals( attrVals[i].getUnfoldedAttributeDescription() ) ) 156 { 157 return false; 158 } 159 } 160 } 161 162 if ( isAdd() ) 163 { 164 return attrVals.length > 0; 165 } 166 else if ( isDelete() ) 167 { 168 return true; 169 } 170 else if ( isReplace() ) 171 { 172 return true; 173 } 174 else 175 { 176 return false; 177 } 178 } 179 180 181 public String getInvalidString() 182 { 183 if ( this.getModSpecType() == null ) 184 { 185 return "Missing mod spec line "; 186 } 187 else if ( isAdd() && this.getAttrVals().length == 0 ) 188 { 189 return "Modification must contain attribute value lines "; 190 } 191 192 LdifAttrValLine[] attrVals = this.getAttrVals(); 193 if ( attrVals.length > 0 ) 194 { 195 String att = this.getModSpecType().getUnfoldedAttributeDescription(); 196 for ( int i = 0; i < attrVals.length; i++ ) 197 { 198 if ( !att.equals( attrVals[i].getUnfoldedAttributeDescription() ) ) 199 { 200 return "Attribute descriptions don't match"; 201 } 202 } 203 } 204 205 return null; 206 } 207 208 } 209 | Popular Tags |