2005-01-31 Zoltan Varga <vargaz@freemail.hu>
[mono.git] / mcs / class / Novell.Directory.Ldap / Novell.Directory.Ldap / LdapNameFormSchema.cs
1 /******************************************************************************
2 * The MIT License
3 * Copyright (c) 2003 Novell Inc.  www.novell.com
4
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:
11
12 * The above copyright notice and this permission notice shall be included in 
13 * all copies or substantial portions of the Software.
14
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
21 * SOFTWARE.
22 *******************************************************************************/
23 //
24 // Novell.Directory.Ldap.LdapAttributeSchema.cs
25 //
26 // Author:
27 //   Sunil Kumar (Sunilk@novell.com)
28 //
29 // (C) 2003 Novell, Inc (http://www.novell.com)
30 //
31
32 using System;
33 using SchemaParser = Novell.Directory.Ldap.Utilclass.SchemaParser;
34 using AttributeQualifier = Novell.Directory.Ldap.Utilclass.AttributeQualifier;
35
36 namespace Novell.Directory.Ldap
37 {
38         
39         /// <summary> A specific a name form in the directory schema.
40         /// 
41         /// The LdapNameFormSchema class represents the definition of a Name Form.  It
42         /// is used to discover or modify the allowed naming attributes for a particular
43         /// object class.
44         /// 
45         /// </summary>
46         /// <seealso cref="LdapSchemaElement">
47         /// </seealso>
48         /// <seealso cref="LdapSchema">
49         /// </seealso>
50         
51         public class LdapNameFormSchema:LdapSchemaElement
52         {
53                 /// <summary> Returns the name of the object class which this name form applies to.
54                 /// 
55                 /// </summary>
56                 /// <returns> The name of the object class.
57                 /// </returns>
58                 virtual public System.String ObjectClass
59                 {
60                         get
61                         {
62                                 return objectClass;
63                         }
64                         
65                 }
66                 /// <summary> Returns the list of required naming attributes for an entry
67                 /// controlled by this name form.
68                 /// 
69                 /// </summary>
70                 /// <returns> The list of required naming attributes.
71                 /// </returns>
72                 virtual public System.String[] RequiredNamingAttributes
73                 {
74                         get
75                         {
76                                 return required;
77                         }
78                         
79                 }
80                 /// <summary> Returns the list of optional naming attributes for an entry
81                 /// controlled by this content rule.
82                 /// 
83                 /// </summary>
84                 /// <returns> The list of the optional naming attributes.
85                 /// </returns>
86                 virtual public System.String[] OptionalNamingAttributes
87                 {
88                         get
89                         {
90                                 return optional;
91                         }
92                         
93                 }
94                 private System.String objectClass;
95                 private System.String[] required;
96                 private System.String[] optional;
97                 
98                 /// <summary> Constructs a name form for adding to or deleting from the schema.
99                 /// 
100                 /// </summary>
101                 /// <param name="names">      The name(s) of the name form.
102                 /// 
103                 /// </param>
104                 /// <param name="oid">        The unique object identifier of the name form - in
105                 /// dotted numerical format.
106                 /// 
107                 /// </param>
108                 /// <param name="description">An optional description of the name form.
109                 /// 
110                 /// </param>
111                 /// <param name="obsolete">   True if the name form is obsolete.
112                 /// 
113                 /// </param>
114                 /// <param name="objectClass">The object to which this name form applies.
115                 /// This may be specified by either name or
116                 /// numeric oid.
117                 /// 
118                 /// </param>
119                 /// <param name="required">   A list of the attributes that must be present
120                 /// in the RDN of an entry that this name form
121                 /// controls. These attributes may be specified by
122                 /// either name or numeric oid.
123                 /// 
124                 /// </param>
125                 /// <param name="optional">   A list of the attributes that may be present
126                 /// in the RDN of an entry that this name form
127                 /// controls. These attributes may be specified by
128                 /// either name or numeric oid.
129                 /// </param>
130                 public LdapNameFormSchema(System.String[] names, System.String oid, System.String description, bool obsolete, System.String objectClass, System.String[] required, System.String[] optional):base(LdapSchema.schemaTypeNames[LdapSchema.NAME_FORM])
131                 {
132                         base.names = new System.String[names.Length];
133                         names.CopyTo(base.names, 0);
134                         base.oid = oid;
135                         base.description = description;
136                         base.obsolete = obsolete;
137                         this.objectClass = objectClass;
138                         this.required = new System.String[required.Length];
139                         required.CopyTo(this.required, 0);
140                         this.optional = new System.String[optional.Length];
141                         optional.CopyTo(this.optional, 0);
142                         base.Value = formatString();
143                         return ;
144                 }
145                 
146                 /*
147                 }
148                 
149                 /**
150                 * Constructs a Name Form from the raw string value returned on a
151                 * schema query for nameForms.
152                 *
153                 * @param raw        The raw string value returned on a schema
154                 *                   query for nameForms.
155                 */
156                 public LdapNameFormSchema(System.String raw):base(LdapSchema.schemaTypeNames[LdapSchema.NAME_FORM])
157                 {
158                         base.obsolete = false;
159                         try
160                         {
161                                 SchemaParser parser = new SchemaParser(raw);
162                                 
163                                 if (parser.Names != null)
164                                 {
165                                         base.names = new System.String[parser.Names.Length];
166                                         parser.Names.CopyTo(base.names, 0);
167                                 }
168                                 if ((System.Object) parser.ID != null)
169                                         base.oid = new System.Text.StringBuilder(parser.ID).ToString();
170                                 if ((System.Object) parser.Description != null)
171                                         base.description = new System.Text.StringBuilder(parser.Description).ToString();
172                                 if (parser.Required != null)
173                                 {
174                                         required = new System.String[parser.Required.Length];
175                                         parser.Required.CopyTo(required, 0);
176                                 }
177                                 if (parser.Optional != null)
178                                 {
179                                         optional = new System.String[parser.Optional.Length];
180                                         parser.Optional.CopyTo(optional, 0);
181                                 }
182                                 if ((System.Object) parser.ObjectClass != null)
183                                         objectClass = parser.ObjectClass;
184                                 base.obsolete = parser.Obsolete;
185                                 System.Collections.IEnumerator qualifiers = parser.Qualifiers;
186                                 AttributeQualifier attrQualifier;
187                                 while (qualifiers.MoveNext())
188                                 {
189                                         attrQualifier = (AttributeQualifier) qualifiers.Current;
190                                         setQualifier(attrQualifier.Name, attrQualifier.Values);
191                                 }
192                                 base.Value = formatString();
193                         }
194                         catch (System.IO.IOException e)
195                         {
196                         }
197                         return ;
198                 }
199                 
200                 /// <summary> Returns a string in a format suitable for directly adding to a
201                 /// directory, as a value of the particular schema element class.
202                 /// 
203                 /// </summary>
204                 /// <returns> A string representation of the class' definition.
205                 /// </returns>
206                 protected internal override System.String formatString()
207                 {
208                         System.Text.StringBuilder valueBuffer = new System.Text.StringBuilder("( ");
209                         System.String token;
210                         System.String[] strArray;
211                         
212                         if ((System.Object) (token = ID) != null)
213                         {
214                                 valueBuffer.Append(token);
215                         }
216                         strArray = Names;
217                         if (strArray != null)
218                         {
219                                 valueBuffer.Append(" NAME ");
220                                 if (strArray.Length == 1)
221                                 {
222                                         valueBuffer.Append("'" + strArray[0] + "'");
223                                 }
224                                 else
225                                 {
226                                         valueBuffer.Append("( ");
227                                         
228                                         for (int i = 0; i < strArray.Length; i++)
229                                         {
230                                                 valueBuffer.Append(" '" + strArray[i] + "'");
231                                         }
232                                         valueBuffer.Append(" )");
233                                 }
234                         }
235                         if ((System.Object) (token = Description) != null)
236                         {
237                                 valueBuffer.Append(" DESC ");
238                                 valueBuffer.Append("'" + token + "'");
239                         }
240                         if (Obsolete)
241                         {
242                                 valueBuffer.Append(" OBSOLETE");
243                         }
244                         if ((System.Object) (token = ObjectClass) != null)
245                         {
246                                 valueBuffer.Append(" OC ");
247                                 valueBuffer.Append("'" + token + "'");
248                         }
249                         if ((strArray = RequiredNamingAttributes) != null)
250                         {
251                                 valueBuffer.Append(" MUST ");
252                                 if (strArray.Length > 1)
253                                         valueBuffer.Append("( ");
254                                 for (int i = 0; i < strArray.Length; i++)
255                                 {
256                                         if (i > 0)
257                                                 valueBuffer.Append(" $ ");
258                                         valueBuffer.Append(strArray[i]);
259                                 }
260                                 if (strArray.Length > 1)
261                                         valueBuffer.Append(" )");
262                         }
263                         if ((strArray = OptionalNamingAttributes) != null)
264                         {
265                                 valueBuffer.Append(" MAY ");
266                                 if (strArray.Length > 1)
267                                         valueBuffer.Append("( ");
268                                 for (int i = 0; i < strArray.Length; i++)
269                                 {
270                                         if (i > 0)
271                                                 valueBuffer.Append(" $ ");
272                                         valueBuffer.Append(strArray[i]);
273                                 }
274                                 if (strArray.Length > 1)
275                                         valueBuffer.Append(" )");
276                         }
277                         System.Collections.IEnumerator en;
278                         if ((en = QualifierNames) != null)
279                         {
280                                 System.String qualName;
281                                 System.String[] qualValue;
282                                 while (en.MoveNext())
283                                 {
284                                         qualName = ((System.String) en.Current);
285                                         valueBuffer.Append(" " + qualName + " ");
286                                         if ((qualValue = getQualifier(qualName)) != null)
287                                         {
288                                                 if (qualValue.Length > 1)
289                                                         valueBuffer.Append("( ");
290                                                 for (int i = 0; i < qualValue.Length; i++)
291                                                 {
292                                                         if (i > 0)
293                                                                 valueBuffer.Append(" ");
294                                                         valueBuffer.Append("'" + qualValue[i] + "'");
295                                                 }
296                                                 if (qualValue.Length > 1)
297                                                         valueBuffer.Append(" )");
298                                         }
299                                 }
300                         }
301                         valueBuffer.Append(" )");
302                         return valueBuffer.ToString();
303                 }
304         }
305 }