1 20 21 package org.apache.directory.ldapstudio.browser.core.model; 22 23 24 import java.io.Serializable ; 25 import java.util.ArrayList ; 26 import java.util.LinkedHashSet ; 27 import java.util.List ; 28 import java.util.Set ; 29 30 import org.apache.directory.ldapstudio.browser.core.BrowserCoreMessages; 31 import org.apache.directory.ldapstudio.browser.core.model.schema.Schema; 32 33 34 40 public class RDN implements Serializable 41 { 42 43 44 private static final long serialVersionUID = -4165959915339033047L; 45 46 47 private RDNPart[] parts; 48 49 50 54 public RDN() 55 { 56 this.parts = new RDNPart[0]; 57 } 58 59 60 66 public RDN( String rdn ) throws NameException 67 { 68 if ( rdn == null ) 69 { 70 throw new IllegalArgumentException ( BrowserCoreMessages.model__empty_rdn ); 71 } 72 73 this.parseMultiRdn( rdn ); 75 } 76 77 78 83 public RDN( RDN rdn ) 84 { 85 if ( rdn == null ) 86 { 87 throw new IllegalArgumentException ( BrowserCoreMessages.model__empty_rdn ); 88 } 89 90 this.parts = new RDNPart[rdn.getParts().length]; 91 for ( int i = 0; i < this.parts.length; i++ ) 92 { 93 this.parts[i] = new RDNPart( rdn.getParts()[i] ); 94 } 95 } 96 97 98 106 public RDN( String type, String value, boolean isValueEncoded ) throws NameException 107 { 108 if ( type == null ) 109 { 110 throw new IllegalArgumentException ( BrowserCoreMessages.model__empty_attribute ); 111 } 112 if ( value == null ) 113 { 114 throw new IllegalArgumentException ( BrowserCoreMessages.model__empty_value ); 115 } 116 117 this.parts = new RDNPart[1]; 118 this.parts[0] = new RDNPart( type, value, isValueEncoded ); 119 } 120 121 122 130 public RDN( String [] types, String [] values, boolean areValuesEncoded ) throws NameException 131 { 132 133 if ( types == null || types.length < 1 ) 134 { 135 throw new IllegalArgumentException ( BrowserCoreMessages.model__empty_attribute ); 136 } 137 if ( values == null || values.length < 1 ) 138 { 139 throw new IllegalArgumentException ( BrowserCoreMessages.model__empty_value ); 140 } 141 if ( types.length != values.length ) 142 { 143 throw new IllegalArgumentException ( "Size of types and values is not equal" ); } 145 146 this.parts = new RDNPart[types.length]; 147 for ( int i = 0; i < this.parts.length; i++ ) 148 { 149 this.parts[i] = new RDNPart( types[i], values[i], areValuesEncoded ); 150 } 151 } 152 153 154 159 public boolean isMultivalued() 160 { 161 return this.parts.length > 1; 162 } 163 164 165 170 public String getType() 171 { 172 return this.parts.length > 0 ? this.parts[0].getType() : ""; } 174 175 176 181 public String getValue() 182 { 183 return this.parts.length > 0 ? this.parts[0].getValue() : ""; } 185 186 187 192 public RDNPart[] getParts() 193 { 194 return this.parts; 195 } 196 197 198 203 public void setParts( RDNPart[] parts ) 204 { 205 this.parts = parts; 206 } 207 208 209 214 public String [] getTypes() 215 { 216 if ( !isMultivalued() ) 217 { 218 return new String [] 219 { getType() }; 220 } 221 else 222 { 223 Set <String > typeSet = new LinkedHashSet <String >(); 224 for ( int i = 0; i < this.parts.length; i++ ) 225 { 226 RDNPart entry = this.parts[i]; 227 typeSet.add( entry.getType() ); 228 } 229 return typeSet.toArray( new String [typeSet.size()] ); 230 } 231 } 232 233 234 239 public String [] getValues() 240 { 241 if ( !isMultivalued() ) 242 { 243 return new String [] 244 { getValue() }; 245 } 246 else 247 { 248 Set <String > valueSet = new LinkedHashSet <String >(); 249 for ( int i = 0; i < this.parts.length; i++ ) 250 { 251 RDNPart entry = this.parts[i]; 252 valueSet.add( entry.getValue() ); 253 } 254 return valueSet.toArray( new String [valueSet.size()] ); 255 } 256 } 257 258 259 262 public int hashCode() 263 { 264 return this.toString().hashCode(); 265 } 266 267 268 271 public boolean equals( Object o ) 272 { 273 if ( o instanceof RDN ) 274 { 275 return this.toString().equals( ( ( RDN ) o ).toString() ); 276 } 277 return false; 278 } 279 280 281 285 public String toString() 286 { 287 StringBuffer sb = new StringBuffer (); 288 289 if ( isMultivalued() ) 290 { 291 for ( int i = 0; i < this.parts.length; i++ ) 292 { 293 RDNPart part = this.parts[i]; 294 sb.append( part.toString() ); 295 296 if ( i + 1 < this.parts.length ) 297 { 298 sb.append( "+" ); } 300 } 301 } 302 else if ( this.parts.length > 0 ) 303 { 304 RDNPart part = this.parts[0]; 305 sb.append( part.toString() ); 306 } 307 308 return sb.toString(); 309 } 310 311 312 319 public String toOidString( Schema schema ) 320 { 321 StringBuffer sb = new StringBuffer (); 322 323 if ( isMultivalued() ) 324 { 325 for ( int i = 0; i < this.parts.length; i++ ) 326 { 327 RDNPart part = this.parts[i]; 328 sb.append( part.toOidString( schema ) ); 329 330 if ( i + 1 < this.parts.length ) 331 { 332 sb.append( "+" ); } 334 } 335 } 336 else if ( this.parts.length > 0 ) 337 { 338 RDNPart part = this.parts[0]; 339 sb.append( part.toOidString( schema ) ); 340 } 341 342 return sb.toString(); 343 } 344 345 346 352 private void parseMultiRdn( String multirdn ) throws NameException 353 { 354 List <RDNPart> partList = new ArrayList <RDNPart>( 1 ); 355 356 boolean backslash = false; 357 int start = 0; 358 for ( int i = 0; i < multirdn.length(); i++ ) 359 { 360 if ( multirdn.charAt( i ) == '\\' && !backslash ) 361 { 362 backslash = true; 363 } 364 else 365 { 366 String rdn = null; 367 if ( multirdn.charAt( i ) == '+' && !backslash ) 368 { 369 rdn = multirdn.substring( start, i ); 370 } 371 else if ( i == multirdn.length() - 1 ) 372 { 373 rdn = multirdn.substring( start ); 374 } 375 if ( rdn != null ) 376 { 377 int index = rdn.indexOf( '=' ); 378 if ( index < 1 ) 379 { 380 throw new NameException( BrowserCoreMessages.model__invalid_rdn ); 381 } 382 String type = rdn.substring( 0, index ); 383 String value = rdn.substring( index + 1, rdn.length() ); 384 partList.add( new RDNPart( type, value, true ) ); 385 start = i + 1; 386 } 387 backslash = false; 388 } 389 } 390 391 if ( partList.isEmpty() ) 392 { 393 394 } 395 396 this.parts = partList.toArray( new RDNPart[partList.size()] ); 397 } 398 399 } 400 | Popular Tags |