1 20 21 package org.apache.directory.ldapstudio.browser.core.model.ldif.lines; 22 23 24 import org.apache.directory.ldapstudio.browser.core.BrowserCoreConstants; 25 import org.apache.directory.ldapstudio.browser.core.utils.LdifUtils; 26 27 28 public class LdifControlLine extends LdifValueLineBase 29 { 30 31 private static final long serialVersionUID = -3961159214439218610L; 32 33 private String rawCriticality; 34 35 private String rawControlValueType; 36 37 private String rawControlValue; 38 39 40 protected LdifControlLine() 41 { 42 } 43 44 45 public LdifControlLine( int offset, String rawControlSpec, String rawControlType, String rawOid, 46 String rawCriticality, String rawControlValueType, String rawControlValue, String rawNewLine ) 47 { 48 super( offset, rawControlSpec, rawControlType, rawOid, rawNewLine ); 49 this.rawCriticality = rawCriticality; 50 this.rawControlValueType = rawControlValueType; 51 this.rawControlValue = rawControlValue; 52 } 53 54 55 public String getRawControlSpec() 56 { 57 return super.getRawLineStart(); 58 } 59 60 61 public String getUnfoldedControlSpec() 62 { 63 return super.getUnfoldedLineStart(); 64 } 65 66 67 public String getRawControlType() 68 { 69 return super.getRawValueType(); 70 } 71 72 73 public String getUnfoldedControlType() 74 { 75 return super.getUnfoldedValueType(); 76 } 77 78 79 public String getRawOid() 80 { 81 return super.getRawValue(); 82 } 83 84 85 public String getUnfoldedOid() 86 { 87 return super.getUnfoldedValue(); 88 } 89 90 91 public String getRawCriticality() 92 { 93 return getNonNull( this.rawCriticality ); 94 } 95 96 97 public String getUnfoldedCriticality() 98 { 99 return unfold( this.getRawCriticality() ); 100 } 101 102 103 public boolean isCritical() 104 { 105 return this.getUnfoldedCriticality().endsWith( "true" ); 106 } 107 108 109 public String getRawControlValueType() 110 { 111 return getNonNull( this.rawControlValueType ); 112 } 113 114 115 public String getUnfoldedControlValueType() 116 { 117 return unfold( this.getRawControlValueType() ); 118 } 119 120 121 public String getRawControlValue() 122 { 123 return getNonNull( this.rawControlValue ); 124 } 125 126 127 public String getUnfoldedControlValue() 128 { 129 return unfold( this.getRawControlValue() ); 130 } 131 132 133 public String toRawString() 134 { 135 return this.getRawControlSpec() + this.getRawControlType() + this.getRawOid() + this.getRawCriticality() 136 + this.getRawControlValueType() + this.getRawControlValue() + this.getRawNewLine(); 137 } 138 139 140 public boolean isValid() 141 { 142 return this.getUnfoldedControlSpec().length() > 0 143 && this.getUnfoldedControlType().length() > 0 144 && this.getUnfoldedOid().length() > 0 145 && ( this.rawCriticality == null || this.getUnfoldedCriticality().endsWith( "true" ) || this 146 .getUnfoldedCriticality().endsWith( "false" ) ) 147 && ( ( this.rawControlValueType == null && this.rawControlValue == null ) || ( this.rawControlValueType != null && this.rawControlValue != null ) ) 148 && this.getUnfoldedNewLine().length() > 0; 149 } 150 151 152 public String getInvalidString() 153 { 154 if ( this.getUnfoldedControlSpec().length() == 0 ) 155 { 156 return "Missing 'control'"; 157 } 158 else if ( this.getUnfoldedOid().length() == 0 ) 159 { 160 return "Missing OID"; 161 } 162 else if ( ( this.rawCriticality != null && !this.getUnfoldedCriticality().endsWith( "true" ) && !this 163 .getUnfoldedCriticality().endsWith( "false" ) ) ) 164 { 165 return "Invalid criticality, must be 'true' or 'false'"; 166 } 167 else 168 { 169 return super.getInvalidString(); 170 } 171 } 172 173 174 178 public final byte[] getControlValueAsBinary() 179 { 180 Object o = getControlValueAsObject(); 181 if ( o instanceof String ) 182 { 183 return LdifUtils.utf8encode( ( String ) o ); 184 } 185 else if ( o instanceof byte[] ) 186 { 187 return ( byte[] ) o; 188 } 189 else 190 { 191 return new byte[0]; 192 } 193 } 194 195 196 public final Object getControlValueAsObject() 197 { 198 if ( this.isControlValueTypeSafe() ) 199 { 200 return this.getUnfoldedControlValue(); 201 } 202 else if ( this.isControlValueTypeBase64() ) 203 { 204 return LdifUtils.base64decodeToByteArray( this.getUnfoldedControlValue() ); 205 } 206 else 207 { 208 return null; 209 } 210 } 211 212 213 public boolean isControlValueTypeBase64() 214 { 215 return this.getUnfoldedControlValueType().startsWith( "::" ); 216 } 217 218 219 public boolean isControlValueTypeSafe() 220 { 221 return this.getUnfoldedControlValueType().startsWith( ":" ) && !this.isControlValueTypeBase64(); 222 } 223 224 225 public static LdifControlLine create( String oid, String criticality, String controlValue ) 226 { 227 if ( LdifUtils.mustEncode( controlValue ) ) 228 { 229 return create( oid, criticality, LdifUtils.utf8encode( controlValue ) ); 230 } 231 else 232 { 233 LdifControlLine controlLine = new LdifControlLine( 0, "control", ":", oid, criticality, 234 controlValue != null ? ":" : null, controlValue != null ? controlValue : null, 235 BrowserCoreConstants.LINE_SEPARATOR ); 236 return controlLine; 237 } 238 } 239 240 241 public static LdifControlLine create( String oid, String criticality, byte[] controlValue ) 242 { 243 LdifControlLine controlLine = new LdifControlLine( 0, "control", ":", oid, criticality, controlValue != null 244 && controlValue.length > 0 ? "::" : null, controlValue != null && controlValue.length > 0 ? LdifUtils 245 .base64encode( controlValue ) : null, BrowserCoreConstants.LINE_SEPARATOR ); 246 return controlLine; 247 } 248 249 } 250 | Popular Tags |