1 20 21 package org.apache.directory.ldapstudio.browser.core.model; 22 23 24 import java.io.Serializable ; 25 26 import org.apache.directory.ldapstudio.browser.core.BrowserCoreMessages; 27 import org.apache.directory.ldapstudio.browser.core.model.schema.Schema; 28 29 30 36 public class RDNPart implements Serializable 37 { 38 39 private static final long serialVersionUID = 3250931604639940667L; 40 41 42 private String type; 43 44 45 private String value; 46 47 48 52 public RDNPart() 53 { 54 this.type = ""; this.value = ""; } 57 58 59 67 public RDNPart( String type, String value, boolean isValueEncoded ) throws NameException 68 { 69 if ( type == null || !type.matches( "([A-Za-z][A-Za-z0-9-]*)|([0-9]+(\\.[0-9]+)+)" ) ) { throw new NameException( BrowserCoreMessages.model__empty_attribute ); 71 } 72 if ( value == null || value.length() < 1 ) 73 { 74 throw new NameException( BrowserCoreMessages.model__empty_value ); 75 } 76 this.setType( type ); 79 if ( isValueEncoded ) 80 { 81 this.setValue( value ); 82 } 83 else 84 { 85 this.setUnencodedValue( value ); 86 } 87 } 88 89 90 95 public RDNPart( RDNPart rdnPart ) 96 { 97 this.type = rdnPart.type; 98 this.value = rdnPart.value; 99 } 100 101 102 107 public String getType() 108 { 109 return type; 110 } 111 112 113 118 public void setType( String type ) 119 { 120 this.type = type; 121 } 122 123 124 130 public String getUnencodedValue() 131 { 132 StringBuffer unencodedValue = new StringBuffer ( this.value ); 133 134 for ( int i = 0; i < unencodedValue.length(); i++ ) 135 { 136 if ( unencodedValue.charAt( i ) == '\\' ) 137 { 138 if ( i == 0 && unencodedValue.length() > i + 1 && unencodedValue.charAt( i + 1 ) == ' ' ) 139 { 140 unencodedValue.deleteCharAt( i ); 141 } 142 else if ( i == unencodedValue.length() - 2 && unencodedValue.length() > i + 1 143 && unencodedValue.charAt( i + 1 ) == ' ' ) 144 { 145 unencodedValue.deleteCharAt( i ); 146 } 147 else if ( i == 0 && unencodedValue.length() > i + 1 && unencodedValue.charAt( i + 1 ) == '#' ) 148 { 149 unencodedValue.deleteCharAt( i ); 150 } 151 else if ( unencodedValue.length() > i + 1 152 && ( unencodedValue.charAt( i + 1 ) == '+' || unencodedValue.charAt( i + 1 ) == ',' 153 || unencodedValue.charAt( i + 1 ) == ';' || unencodedValue.charAt( i + 1 ) == '<' 154 || unencodedValue.charAt( i + 1 ) == '>' || unencodedValue.charAt( i + 1 ) == '"' || unencodedValue 155 .charAt( i + 1 ) == '\\' ) ) 156 { 157 unencodedValue.deleteCharAt( i ); 158 } 159 } 160 } 161 162 return unencodedValue.toString(); 163 } 164 165 166 172 public void setUnencodedValue( String unencodedValue ) 173 { 174 unencodedValue = unencodedValue.replaceAll( "\\\\", "\\\\\\\\" ); unencodedValue = unencodedValue.replaceAll( "\\+", "\\\\+" ); unencodedValue = unencodedValue.replaceAll( ",", "\\\\," ); unencodedValue = unencodedValue.replaceAll( "\"", "\\\\\"" ); unencodedValue = unencodedValue.replaceAll( "<", "\\\\<" ); unencodedValue = unencodedValue.replaceAll( ">", "\\\\>" ); unencodedValue = unencodedValue.replaceAll( ";", "\\\\;" ); 182 if ( unencodedValue.startsWith( " " ) ) { 184 unencodedValue = "\\" + unencodedValue; } 186 else if ( unencodedValue.startsWith( "#" ) ) { 188 unencodedValue = "\\" + unencodedValue; } 190 191 if ( unencodedValue.endsWith( " " ) ) { 193 unencodedValue = unencodedValue.substring( 0, unencodedValue.length() - 1 ) + "\\ "; } 195 196 this.value = unencodedValue; 197 } 198 199 200 206 public String getValue() 207 { 208 return value; 209 } 210 211 212 218 public void setValue( String value ) 219 { 220 this.value = value; 221 } 222 223 224 227 public int hashCode() 228 { 229 return this.toString().hashCode(); 230 } 231 232 233 236 public boolean equals( Object o ) 237 { 238 if ( o instanceof RDNPart ) 239 { 240 return this.toString().equals( ( ( RDNPart ) o ).toString() ); 241 } 242 return false; 243 } 244 245 246 250 public String toString() 251 { 252 return getType() + "=" + getValue(); } 254 255 256 263 public String toOidString( Schema schema ) 264 { 265 String oid = schema != null ? schema.getAttributeTypeDescription( getType() ).getNumericOID() : getType(); 266 return oid.toLowerCase() + "=" + getValue().toLowerCase(); } 268 269 } 270 | Popular Tags |