KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > browser > core > model > RDNPart


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  *
19  */

20
21 package org.apache.directory.ldapstudio.browser.core.model;
22
23
24 import java.io.Serializable JavaDoc;
25
26 import org.apache.directory.ldapstudio.browser.core.BrowserCoreMessages;
27 import org.apache.directory.ldapstudio.browser.core.model.schema.Schema;
28
29
30 /**
31  * A RDNPart represents a attribute type-value-pair, used in RDN.
32  *
33  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
34  * @version $Rev$, $Date$
35  */

36 public class RDNPart implements Serializable JavaDoc
37 {
38     /** The generated serialVersionUID */
39     private static final long serialVersionUID = 3250931604639940667L;
40
41     /** The attribute type */
42     private String JavaDoc type;
43
44     /** The value */
45     private String JavaDoc value;
46
47
48     /**
49      * Creates a new instance of RDNPart with an empty type an value
50      *
51      */

52     public RDNPart()
53     {
54         this.type = ""; //$NON-NLS-1$
55
this.value = ""; //$NON-NLS-1$
56
}
57
58
59     /**
60      * Creates a new instance of RDNPart with the given type and value.
61      *
62      * @param type the attribute type
63      * @param value the value
64      * @param isValueEncoded true if the value is already encoded according RFC4514, Section 2.4
65      * @throws NameException if the type or value are invalid
66      */

67     public RDNPart( String JavaDoc type, String JavaDoc value, boolean isValueEncoded ) throws NameException
68     {
69         if ( type == null || !type.matches( "([A-Za-z][A-Za-z0-9-]*)|([0-9]+(\\.[0-9]+)+)" ) ) { //$NON-NLS-1$
70
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.type = type.trim();
77
// this.value = value.trim();
78
this.setType( type );
79         if ( isValueEncoded )
80         {
81             this.setValue( value );
82         }
83         else
84         {
85             this.setUnencodedValue( value );
86         }
87     }
88
89
90     /**
91      * Creates a clone of the given RDNPart.
92      *
93      * @param rdnPart the RDNPart.
94      */

95     public RDNPart( RDNPart rdnPart )
96     {
97         this.type = rdnPart.type;
98         this.value = rdnPart.value;
99     }
100
101
102     /**
103      * Gets the type.
104      *
105      * @return the type
106      */

107     public String JavaDoc getType()
108     {
109         return type;
110     }
111
112
113     /**
114      * Sets the type.
115      *
116      * @param type the type
117      */

118     public void setType( String JavaDoc type )
119     {
120         this.type = type;
121     }
122
123
124     /**
125      * Gets the unencoded value. All escaped characters are unescaped
126      * before returning the vaue.
127      *
128      * @return the unencoded value.
129      */

130     public String JavaDoc getUnencodedValue()
131     {
132         StringBuffer JavaDoc unencodedValue = new StringBuffer JavaDoc( 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     /**
167      * Sets the unencoded value. The unencoded value will be encoded
168      * according RFC4514, Section 2.4.
169      *
170      * @param unencodedValue the unencoded value
171      */

172     public void setUnencodedValue( String JavaDoc unencodedValue )
173     {
174         unencodedValue = unencodedValue.replaceAll( "\\\\", "\\\\\\\\" ); //$NON-NLS-1$ //$NON-NLS-2$
175
unencodedValue = unencodedValue.replaceAll( "\\+", "\\\\+" ); //$NON-NLS-1$ //$NON-NLS-2$
176
unencodedValue = unencodedValue.replaceAll( ",", "\\\\," ); //$NON-NLS-1$ //$NON-NLS-2$
177
unencodedValue = unencodedValue.replaceAll( "\"", "\\\\\"" ); //$NON-NLS-1$ //$NON-NLS-2$
178
unencodedValue = unencodedValue.replaceAll( "<", "\\\\<" ); //$NON-NLS-1$ //$NON-NLS-2$
179
unencodedValue = unencodedValue.replaceAll( ">", "\\\\>" ); //$NON-NLS-1$ //$NON-NLS-2$
180
unencodedValue = unencodedValue.replaceAll( ";", "\\\\;" ); //$NON-NLS-1$ //$NON-NLS-2$
181

182         if ( unencodedValue.startsWith( " " ) ) //$NON-NLS-1$
183
{
184             unencodedValue = "\\" + unencodedValue; //$NON-NLS-1$
185
}
186         else if ( unencodedValue.startsWith( "#" ) ) //$NON-NLS-1$
187
{
188             unencodedValue = "\\" + unencodedValue; //$NON-NLS-1$
189
}
190
191         if ( unencodedValue.endsWith( " " ) ) //$NON-NLS-1$
192
{
193             unencodedValue = unencodedValue.substring( 0, unencodedValue.length() - 1 ) + "\\ "; //$NON-NLS-1$
194
}
195
196         this.value = unencodedValue;
197     }
198
199
200     /**
201      * Gets the value. Note that the value is encoded
202      * according RFC 4514, Section 2.4.
203      *
204      * @return the value
205      */

206     public String JavaDoc getValue()
207     {
208         return value;
209     }
210
211
212     /**
213      * Sets the value. Note that the value must be encoded
214      * according RFC 4514, Section 2.4.
215      *
216      * @param value the value
217      */

218     public void setValue( String JavaDoc value )
219     {
220         this.value = value;
221     }
222
223
224     /**
225      * {@inheritDoc}
226      */

227     public int hashCode()
228     {
229         return this.toString().hashCode();
230     }
231
232
233     /**
234      * {@inheritDoc}
235      */

236     public boolean equals( Object JavaDoc o )
237     {
238         if ( o instanceof RDNPart )
239         {
240             return this.toString().equals( ( ( RDNPart ) o ).toString() );
241         }
242         return false;
243     }
244
245
246     /**
247      * Returns the string representation of this RDNPart, namely
248      * &lt;type&gt;=&lt;value&gt;
249      */

250     public String JavaDoc toString()
251     {
252         return getType() + "=" + getValue(); //$NON-NLS-1$
253
}
254
255
256     /**
257      * Returns the string representation of this RDNPart, but
258      * lowercased and with the numerid OID instead of the type.
259      *
260      * @param schema the schema
261      * @return the lowercased and OID-fizied string representation of this RDNPart
262      */

263     public String JavaDoc toOidString( Schema schema )
264     {
265         String JavaDoc oid = schema != null ? schema.getAttributeTypeDescription( getType() ).getNumericOID() : getType();
266         return oid.toLowerCase() + "=" + getValue().toLowerCase(); //$NON-NLS-1$
267
}
268
269 }
270
Popular Tags