remove svn:executable from .cs files
[mono.git] / mcs / class / Novell.Directory.Ldap / Novell.Directory.Ldap / LdapAttributeSchema.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 using System;
32 using Novell.Directory.Ldap.Utilclass;
33
34 namespace Novell.Directory.Ldap
35 {
36         
37         /// <summary> The definition of an attribute type in the schema.
38         /// 
39         /// LdapAttributeSchema is used to discover an attribute's
40         /// syntax, and add or delete an attribute definition.
41         /// RFC 2252, "Lightweight Directory Access Protocol (v3):
42         /// Attribute Syntax Definitions" contains a description
43         /// of the information on the Ldap representation of schema.
44         /// draft-sermerseim-nds-ldap-schema-02, "Ldap Schema for NDS"
45         /// defines the schema descriptions and non-standard syntaxes
46         /// used by Novell eDirectory.
47         /// 
48         /// </summary>
49         /// <seealso cref="LdapSchema">
50         /// </seealso>
51         public class LdapAttributeSchema:LdapSchemaElement
52         {
53                 private void  InitBlock()
54                 {
55                         usage = USER_APPLICATIONS;
56                 }
57                 /// <summary> Returns the object identifer of the syntax of the attribute, in
58                 /// dotted numerical format.
59                 /// 
60                 /// </summary>
61                 /// <returns> The object identifer of the attribute's syntax.
62                 /// </returns>
63                 virtual public System.String SyntaxString
64                 {
65                         get
66                         {
67                                 return syntaxString;
68                         }
69                         
70                 }
71                 /// <summary> Returns the name of the attribute type which this attribute derives
72                 /// from, or null if there is no superior attribute.
73                 /// 
74                 /// </summary>
75                 /// <returns> The attribute's superior attribute, or null if there is none.
76                 /// </returns>
77                 virtual public System.String Superior
78                 {
79                         get
80                         {
81                                 return superior;
82                         }
83                         
84                 }
85                 /// <summary> Returns true if the attribute is single-valued.
86                 /// 
87                 /// </summary>
88                 /// <returns> True if the attribute is single-valued; false if the attribute
89                 /// is multi-valued.
90                 /// </returns>
91                 virtual public bool SingleValued
92                 {
93                         get
94                         {
95                                 return single;
96                         }
97                         
98                 }
99                 /// <summary> Returns the matching rule for this attribute.
100                 /// 
101                 /// </summary>
102                 /// <returns> The attribute's equality matching rule; null if it has no equality
103                 /// matching rule.
104                 /// </returns>
105                 virtual public System.String EqualityMatchingRule
106                 {
107                         get
108                         {
109                                 return equality;
110                         }
111                         
112                 }
113                 /// <summary> Returns the ordering matching rule for this attribute.
114                 /// 
115                 /// </summary>
116                 /// <returns> The attribute's ordering matching rule; null if it has no ordering
117                 /// matching rule.
118                 /// </returns>
119                 virtual public System.String OrderingMatchingRule
120                 {
121                         get
122                         {
123                                 return ordering;
124                         }
125                         
126                 }
127                 /// <summary> Returns the substring matching rule for this attribute.
128                 /// 
129                 /// </summary>
130                 /// <returns> The attribute's substring matching rule; null if it has no substring
131                 /// matching rule.
132                 /// </returns>
133                 virtual public System.String SubstringMatchingRule
134                 {
135                         get
136                         {
137                                 return substring;
138                         }
139                         
140                 }
141                 /// <summary> Returns true if the attribute is a collective attribute.
142                 /// 
143                 /// </summary>
144                 /// <returns> True if the attribute is a collective; false if the attribute
145                 /// is not a collective attribute.
146                 /// </returns>
147                 virtual public bool Collective
148                 {
149                         get
150                         {
151                                 return collective;
152                         }
153                         
154                 }
155                 /// <summary> Returns false if the attribute is read-only.
156                 /// 
157                 /// </summary>
158                 /// <returns> False if the attribute is read-only; true if the attribute
159                 /// is read-write.
160                 /// </returns>
161                 virtual public bool UserModifiable
162                 {
163                         get
164                         {
165                                 return userMod;
166                         }
167                         
168                 }
169                 /// <summary> Returns the usage of the attribute.
170                 /// 
171                 /// </summary>
172                 /// <returns> One of the following values: USER_APPLICATIONS,
173                 /// DIRECTORY_OPERATION, DISTRIBUTED_OPERATION or
174                 /// DSA_OPERATION.
175                 /// </returns>
176                 virtual public int Usage
177                 {
178                         get
179                         {
180                                 return usage;
181                         }
182                         
183                 }
184                 
185                 private System.String syntaxString;
186                 private bool single = false;
187                 private System.String superior;
188                 private System.String equality;
189                 private System.String ordering;
190                 private System.String substring;
191                 private bool collective = false;
192                 private bool userMod = true;
193                 private int usage;
194                 
195                 /// <summary> Indicates that the attribute usage is for ordinary application
196                 /// or user data.
197                 /// </summary>
198                 public const int USER_APPLICATIONS = 0;
199                 /// <summary> Indicates that the attribute usage is for directory operations.
200                 /// Values are vendor specific.
201                 /// </summary>
202                 public const int DIRECTORY_OPERATION = 1;
203                 /// <summary> Indicates that the attribute usage is for distributed operational
204                 /// attributes. These hold server (DSA) information that is shared among
205                 /// servers holding replicas of the entry.
206                 /// </summary>
207                 public const int DISTRIBUTED_OPERATION = 2;
208                 /// <summary> Indicates that the attribute usage is for local operational attributes.
209                 /// These hold server (DSA) information that is local to a server.
210                 /// </summary>
211                 public const int DSA_OPERATION = 3;
212                 
213                 /// <summary> Constructs an attribute definition for adding to or deleting from a
214                 /// directory's schema.
215                 /// 
216                 /// </summary>
217                 /// <param name="names">Names of the attribute.
218                 /// 
219                 /// </param>
220                 /// <param name="oid">  Object identifer of the attribute, in
221                 /// dotted numerical format.
222                 /// 
223                 /// </param>
224                 /// <param name="description">  Optional description of the attribute.
225                 /// 
226                 /// </param>
227                 /// <param name="syntaxString"> Object identifer of the syntax of the
228                 /// attribute, in dotted numerical format.
229                 /// 
230                 /// </param>
231                 /// <param name="single">   True if the attribute is to be single-valued.
232                 /// 
233                 /// </param>
234                 /// <param name="superior"> Optional name of the attribute type which this
235                 /// attribute type derives from; null if there is no
236                 /// superior attribute type.
237                 /// 
238                 /// </param>
239                 /// <param name="obsolete"> True if the attribute is obsolete.
240                 /// 
241                 /// </param>
242                 /// <param name="equality"> Optional matching rule name; null if there is not
243                 /// an equality matching rule for this attribute.
244                 /// 
245                 /// </param>
246                 /// <param name="ordering">Optional matching rule name; null if there is not
247                 /// an ordering matching rule for this attribute.
248                 /// 
249                 /// </param>
250                 /// <param name="substring">   Optional matching rule name; null if there is not
251                 /// a substring matching rule for this attribute.
252                 /// 
253                 /// </param>
254                 /// <param name="collective">   True of this attribute is a collective attribute
255                 /// 
256                 /// </param>
257                 /// <param name="isUserModifiable"> False if this attribute is a read-only attribute
258                 /// 
259                 /// </param>
260                 /// <param name="usage">       Describes what the attribute is used for. Must be
261                 /// one of the following: USER_APPLICATIONS,
262                 /// DIRECTORY_OPERATION, DISTRIBUTED_OPERATION or
263                 /// DSA_OPERATION.
264                 /// </param>
265                 public LdapAttributeSchema(System.String[] names, System.String oid, System.String description, System.String syntaxString, bool single, System.String superior, bool obsolete, System.String equality, System.String ordering, System.String substring, bool collective, bool isUserModifiable, int usage):base(LdapSchema.schemaTypeNames[LdapSchema.ATTRIBUTE])
266                 {
267                         InitBlock();
268                         base.names = names;
269                         base.oid = oid;
270                         base.description = description;
271                         base.obsolete = obsolete;
272                         this.syntaxString = syntaxString;
273                         this.single = single;
274                         this.equality = equality;
275                         this.ordering = ordering;
276                         this.substring = substring;
277                         this.collective = collective;
278                         this.userMod = isUserModifiable;
279                         this.usage = usage;
280                         base.Value = formatString();
281                         return ;
282                 }
283                 
284                 
285                 
286                 /// <summary> Constructs an attribute definition from the raw string value returned
287                 /// on a directory query for "attributetypes".
288                 /// 
289                 /// </summary>
290                 /// <param name="raw">     The raw string value returned on a directory
291                 /// query for "attributetypes".
292                 /// </param>
293                 public LdapAttributeSchema(System.String raw):base(LdapSchema.schemaTypeNames[LdapSchema.ATTRIBUTE])
294                 {
295                         InitBlock();
296                         try
297                         {
298                                 SchemaParser parser = new SchemaParser(raw);
299                                 
300                                 if (parser.Names != null)
301                                         base.names = parser.Names;
302                                 if ((System.Object) parser.ID != null)
303                                         base.oid = parser.ID;
304                                 if ((System.Object) parser.Description != null)
305                                         base.description = parser.Description;
306                                 if ((System.Object) parser.Syntax != null)
307                                         syntaxString = parser.Syntax;
308                                 if ((System.Object) parser.Superior != null)
309                                         syntaxString = parser.Superior;
310                                 single = parser.Single;
311                                 base.obsolete = parser.Obsolete;
312                                 System.Collections.IEnumerator qualifiers = parser.Qualifiers;
313                                 AttributeQualifier attrQualifier;
314                                 while (qualifiers.MoveNext())
315                                 {
316                                         attrQualifier = (AttributeQualifier) qualifiers.Current;
317                                         setQualifier(attrQualifier.Name, attrQualifier.Values);
318                                 }
319                                 base.Value = formatString();
320                         }
321                         catch (System.IO.IOException e)
322                         {
323                                 throw new System.SystemException(e.ToString());
324                         }
325                         return ;
326                 }
327                 
328                 /// <summary> Returns a string in a format suitable for directly adding to a
329                 /// directory, as a value of the particular schema element attribute.
330                 /// 
331                 /// </summary>
332                 /// <returns> A string representation of the attribute's definition.
333                 /// </returns>
334                 protected internal override System.String formatString()
335                 {
336                         
337                         System.Text.StringBuilder valueBuffer = new System.Text.StringBuilder("( ");
338                         System.String token;
339                         System.String[] strArray;
340                         
341                         if ((System.Object) (token = ID) != null)
342                         {
343                                 valueBuffer.Append(token);
344                         }
345                         strArray = Names;
346                         if (strArray != null)
347                         {
348                                 valueBuffer.Append(" NAME ");
349                                 if (strArray.Length == 1)
350                                 {
351                                         valueBuffer.Append("'" + strArray[0] + "'");
352                                 }
353                                 else
354                                 {
355                                         valueBuffer.Append("( ");
356                                         
357                                         for (int i = 0; i < strArray.Length; i++)
358                                         {
359                                                 valueBuffer.Append(" '" + strArray[i] + "'");
360                                         }
361                                         valueBuffer.Append(" )");
362                                 }
363                         }
364                         if ((System.Object) (token = Description) != null)
365                         {
366                                 valueBuffer.Append(" DESC ");
367                                 valueBuffer.Append("'" + token + "'");
368                         }
369                         if (Obsolete)
370                         {
371                                 valueBuffer.Append(" OBSOLETE");
372                         }
373                         if ((System.Object) (token = Superior) != null)
374                         {
375                                 valueBuffer.Append(" SUP ");
376                                 valueBuffer.Append("'" + token + "'");
377                         }
378                         if ((System.Object) (token = EqualityMatchingRule) != null)
379                         {
380                                 valueBuffer.Append(" EQUALITY ");
381                                 valueBuffer.Append("'" + token + "'");
382                         }
383                         if ((System.Object) (token = OrderingMatchingRule) != null)
384                         {
385                                 valueBuffer.Append(" ORDERING ");
386                                 valueBuffer.Append("'" + token + "'");
387                         }
388                         if ((System.Object) (token = SubstringMatchingRule) != null)
389                         {
390                                 valueBuffer.Append(" SUBSTR ");
391                                 valueBuffer.Append("'" + token + "'");
392                         }
393                         if ((System.Object) (token = SyntaxString) != null)
394                         {
395                                 valueBuffer.Append(" SYNTAX ");
396                                 valueBuffer.Append(token);
397                         }
398                         if (SingleValued)
399                         {
400                                 valueBuffer.Append(" SINGLE-VALUE");
401                         }
402                         if (Collective)
403                         {
404                                 valueBuffer.Append(" COLLECTIVE");
405                         }
406                         if (UserModifiable == false)
407                         {
408                                 valueBuffer.Append(" NO-USER-MODIFICATION");
409                         }
410                         int useType;
411                         if ((useType = Usage) != USER_APPLICATIONS)
412                         {
413                                 switch (useType)
414                                 {
415                                         
416                                         case DIRECTORY_OPERATION: 
417                                                 valueBuffer.Append(" USAGE directoryOperation");
418                                                 break;
419                                         
420                                         case DISTRIBUTED_OPERATION: 
421                                                 valueBuffer.Append(" USAGE distributedOperation");
422                                                 break;
423                                         
424                                         case DSA_OPERATION: 
425                                                 valueBuffer.Append(" USAGE dSAOperation");
426                                                 break;
427                                         
428                                         default: 
429                                                 break;
430                                         
431                                 }
432                         }
433                         System.Collections.IEnumerator en = QualifierNames;
434
435                         while (en.MoveNext())
436                         {
437                                 token = ((System.String) en.Current);
438                                 if (((System.Object) token != null))
439                                 {
440                                         valueBuffer.Append(" " + token);
441                                         strArray = getQualifier(token);
442                                         if (strArray != null)
443                                         {
444                                                 if (strArray.Length > 1)
445                                                         valueBuffer.Append("(");
446                                                 for (int i = 0; i < strArray.Length; i++)
447                                                 {
448                                                         valueBuffer.Append(" '" + strArray[i] + "'");
449                                                 }
450                                                 if (strArray.Length > 1)
451                                                         valueBuffer.Append(" )");
452                                         }
453                                 }
454                         }
455                         valueBuffer.Append(" )");
456                         return valueBuffer.ToString();
457                 }
458         }
459 }