1 /******************************************************************************
3 * Copyright (c) 2003 Novell Inc. www.novell.com
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the Software), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 *******************************************************************************/
24 // Novell.Directory.Ldap.Utilclass.RDN.cs
27 // Sunil Kumar (Sunilk@novell.com)
29 // (C) 2003 Novell, Inc (http://www.novell.com)
34 namespace Novell
.Directory
.Ldap
.Utilclass
37 /// <summary> A RDN encapsulates a single object's name of a Distinguished Name(DN).
38 /// The object name represented by this class contains no context. Thus a
39 /// Relative Distinguished Name (RDN) could be relative to anywhere in the
42 /// For example, of following DN, 'cn=admin, ou=marketing, o=corporation', all
43 /// possible RDNs are 'cn=admin', 'ou=marketing', and 'o=corporation'.
45 /// Multivalued attributes are encapsulated in this class. For example the
46 /// following could be represented by an RDN: 'cn=john + l=US', or
50 /// <seealso cref="DN">
54 public class RDN
:System
.Object
56 /// <summary> Returns the actually Raw String before Normalization
59 /// <returns> The raw string
61 virtual protected internal System
.String RawValue
69 /// <summary> Returns the type of this RDN. This method assumes that only one value
70 /// is used, If multivalues attributes are used only the first Type is
71 /// returned. Use GetTypes.
73 /// <returns> Type of attribute
75 virtual public System
.String Type
79 return (System
.String
) types
[0];
83 /// <summary> Returns all the types for this RDN.</summary>
84 /// <returns> list of types
86 virtual public System
.String
[] Types
90 System
.String
[] toReturn
= new System
.String
[types
.Count
];
91 for (int i
= 0; i
< types
.Count
; i
++)
92 toReturn
[i
] = ((System
.String
) types
[i
]);
97 /// <summary> Returns the values of this RDN. If multivalues attributes are used only
98 /// the first Type is returned. Use GetTypes.
101 /// <returns> Type of attribute
103 virtual public System
.String Value
107 return (System
.String
) values
[0];
111 /// <summary> Returns all the types for this RDN.</summary>
112 /// <returns> list of types
114 virtual public System
.String
[] Values
118 System
.String
[] toReturn
= new System
.String
[values
.Count
];
119 for (int i
= 0; i
< values
.Count
; i
++)
120 toReturn
[i
] = ((System
.String
) values
[i
]);
125 /// <summary> Determines if this RDN is multivalued or not</summary>
126 /// <returns> true if this RDN is multivalued
128 virtual public bool Multivalued
132 return (values
.Count
> 1)?true:false;
136 private System
.Collections
.ArrayList types
; //list of Type strings
137 private System
.Collections
.ArrayList values
; //list of Value strings
138 private System
.String rawValue
; //the unnormalized value
140 /// <summary> Creates an RDN object from the DN component specified in the string RDN
143 /// <param name="rdn">the DN component
145 public RDN(System
.String rdn
)
149 System
.Collections
.ArrayList rdns
= dn
.RDNs
;
150 //there should only be one rdn
152 throw new System
.ArgumentException("Invalid RDN: see API " + "documentation");
153 RDN thisRDN
= (RDN
) (rdns
[0]);
154 this.types
= thisRDN
.types
;
155 this.values
= thisRDN
.values
;
156 this.rawValue
= thisRDN
.rawValue
;
162 types
= new System
.Collections
.ArrayList();
163 values
= new System
.Collections
.ArrayList();
168 /// <summary> Compares the RDN to the rdn passed. Note: If an there exist any
169 /// mulivalues in one RDN they must all be present in the other.
172 /// <param name="rdn">the RDN to compare to
174 /// @throws IllegalArgumentException if the application compares a name
177 [CLSCompliantAttribute(false)]
178 public virtual bool equals(RDN rdn
)
180 if (this.values
.Count
!= rdn
.values
.Count
)
185 for (i
= 0; i
< this.values
.Count
; i
++)
187 //verify that the current value and type exists in the other list
189 //May need a more intellegent compare
190 while (j
< values
.Count
&& (!((System
.String
) this.values
[i
]).ToUpper().Equals(((System
.String
) rdn
.values
[j
]).ToUpper()) || !equalAttrType((System
.String
) this.types
[i
], (System
.String
) rdn
.types
[j
])))
194 if (j
>= rdn
.values
.Count
)
195 //couldn't find first value
201 /// <summary> Internal function used by equal to compare Attribute types. Because
202 /// attribute types could either be an OID or a name. There needs to be a
203 /// Translation mechanism. This function will absract this functionality.
205 /// Currently if types differ (Oid and number) then UnsupportedOperation is
206 /// thrown, either one or the other must used. In the future an OID to name
207 /// translation can be used.
211 private bool equalAttrType(System
.String attr1
, System
.String attr2
)
213 if (System
.Char
.IsDigit(attr1
[0]) ^ System
.Char
.IsDigit(attr2
[0]))
214 //isDigit tests if it is an OID
215 throw new System
.ArgumentException("OID numbers are not " + "currently compared to attribute names");
217 return attr1
.ToUpper().Equals(attr2
.ToUpper());
220 /// <summary> Adds another value to the RDN. Only one attribute type is allowed for
223 /// <param name="attrType">Attribute type, could be an OID or String
225 /// <param name="attrValue">Attribute Value, must be normalized and escaped
227 /// <param name="rawValue">or text before normalization, can be Null
229 public virtual void add(System
.String attrType
, System
.String attrValue
, System
.String rawValue
)
232 values
.Add(attrValue
);
233 this.rawValue
+= rawValue
;
236 /// <summary> Creates a string that represents this RDN, according to RFC 2253
239 /// <returns> An RDN string
241 public override System
.String
ToString()
243 return toString(false);
246 /// <summary> Creates a string that represents this RDN.
248 /// If noTypes is true then Atribute types will be ommited.
251 /// <param name="noTypes">true if attribute types will be omitted.
254 /// <returns> An RDN string
256 [CLSCompliantAttribute(false)]
257 public virtual System
.String
toString(bool noTypes
)
259 int length
= types
.Count
;
260 System
.String toReturn
= "";
265 toReturn
= types
[0] + "=";
267 toReturn
+= values
[0];
269 for (int i
= 1; i
< length
; i
++)
274 toReturn
+= (types
[i
] + "=");
276 toReturn
+= values
[i
];
281 /// <summary> Returns each multivalued name in the current RDN as an array of Strings.
284 /// <param name="noTypes">Specifies whether Attribute types are included. The attribute
285 /// type names will be ommitted if the parameter noTypes is true.
288 /// <returns> List of multivalued Attributes
290 public virtual System
.String
[] explodeRDN(bool noTypes
)
292 int length
= types
.Count
;
295 System
.String
[] toReturn
= new System
.String
[types
.Count
];
299 toReturn
[0] = types
[0] + "=";
301 toReturn
[0] += values
[0];
303 for (int i
= 1; i
< length
; i
++)
307 toReturn
[i
] += (types
[i
] + "=");
309 toReturn
[i
] += values
[i
];