Copied remotely
[mono.git] / mcs / class / Novell.Directory.Ldap / Novell.Directory.Ldap / LdapMatchingRuleSchema.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.LdapMatchingRuleSchema.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
35 namespace Novell.Directory.Ldap
36 {
37         /// <summary>  The schematic definition of a particular matching rule
38         /// in a particular Directory Server.
39         /// 
40         /// The LdapMatchingRuleSchema class represents the definition of a mathcing
41         /// rule.  It is used to query matching rule syntax, and to add or delete a
42         /// matching rule definition in a directory.
43         /// 
44         /// Novell eDirectory does not currently allow matching rules to be added
45         /// or deleted from the schema.
46         /// 
47         /// </summary>
48         /// <seealso cref="LdapAttributeSchema">
49         /// </seealso>
50         /// <seealso cref="LdapSchemaElement">
51         /// </seealso>
52         /// <seealso cref="LdapSchema">
53         /// </seealso>
54         public class LdapMatchingRuleSchema:LdapSchemaElement
55         {
56                 /// <summary> Returns the OIDs of the attributes to which this rule applies.
57                 /// 
58                 /// </summary>
59                 /// <returns> The OIDs of the attributes to which this matching rule applies.
60                 /// </returns>
61                 virtual public System.String[] Attributes
62                 {
63                         get
64                         {
65                                 return attributes;
66                         }
67                         
68                 }
69                 /// <summary> Returns the OID of the syntax that this matching rule is valid for.
70                 /// 
71                 /// </summary>
72                 /// <returns> The OID of the syntax that this matching rule is valid for.
73                 /// </returns>
74                 virtual public System.String SyntaxString
75                 {
76                         get
77                         {
78                                 return syntaxString;
79                         }
80                         
81                 }
82                 private System.String syntaxString;
83                 private System.String[] attributes;
84                 /// <summary> Constructs a matching rule definition for adding to or deleting from
85                 /// a directory.
86                 /// 
87                 /// </summary>
88                 /// <param name="names">      The names of the attribute.
89                 /// 
90                 /// </param>
91                 /// <param name="oid">        Object Identifier of the attribute - in
92                 /// dotted-decimal format.
93                 /// 
94                 /// </param>
95                 /// <param name="description">  Optional description of the attribute.
96                 /// 
97                 /// </param>
98                 /// <param name="attributes">   The OIDs of attributes to which the rule applies.
99                 /// This parameter may be null. All attributes added to
100                 /// this array must use the same syntax.
101                 /// 
102                 /// </param>
103                 /// <param name="obsolete">     true if this matching rule is obsolete.
104                 /// 
105                 /// 
106                 /// </param>
107                 /// <param name="syntaxString">  The unique object identifer of the syntax of the
108                 /// attribute, in dotted numerical format.
109                 /// 
110                 /// </param>
111                 public LdapMatchingRuleSchema(System.String[] names, System.String oid, System.String description, System.String[] attributes, bool obsolete, System.String syntaxString):base(LdapSchema.schemaTypeNames[LdapSchema.MATCHING])
112                 {
113                         base.names = new System.String[names.Length];
114                         names.CopyTo(base.names, 0);
115                         base.oid = oid;
116                         base.description = description;
117                         base.obsolete = obsolete;
118                         this.attributes = new System.String[attributes.Length];
119                         attributes.CopyTo(this.attributes, 0);
120                         this.syntaxString = syntaxString;
121                         base.Value = formatString();
122                         return ;
123                 }
124                 
125                 
126                 /// <summary> Constructs a matching rule definition from the raw string values
127                 /// returned from a schema query for "matchingRule" and for
128                 /// "matchingRuleUse" for the same rule.
129                 /// 
130                 /// </summary>
131                 /// <param name="rawMatchingRule">   The raw string value returned on a directory
132                 /// query for "matchingRule".
133                 /// 
134                 /// </param>
135                 /// <param name="rawMatchingRuleUse"> The raw string value returned on a directory
136                 /// query for "matchingRuleUse".
137                 /// </param>
138                 public LdapMatchingRuleSchema(System.String rawMatchingRule, System.String rawMatchingRuleUse):base(LdapSchema.schemaTypeNames[LdapSchema.MATCHING])
139                 {
140                         try
141                         {
142                                 SchemaParser matchParser = new SchemaParser(rawMatchingRule);
143                                 base.names = new System.String[matchParser.Names.Length];
144                                 matchParser.Names.CopyTo(base.names, 0);
145                                 base.oid = matchParser.ID;
146                                 base.description = matchParser.Description;
147                                 base.obsolete = matchParser.Obsolete;
148                                 this.syntaxString = matchParser.Syntax;
149                                 if ((System.Object) rawMatchingRuleUse != null)
150                                 {
151                                         SchemaParser matchUseParser = new SchemaParser(rawMatchingRuleUse);
152                                         this.attributes = matchUseParser.Applies;
153                                 }
154                                 base.Value = formatString();
155                         }
156                         catch (System.IO.IOException e)
157                         {
158                         }
159                         return ;
160                 }
161                 
162                 /// <summary> Returns a string in a format suitable for directly adding to a
163                 /// directory, as a value of the particular schema element attribute.
164                 /// 
165                 /// </summary>
166                 /// <returns> A string representation of the attribute's definition.
167                 /// </returns>
168                 protected internal override System.String formatString()
169                 {
170                         
171                         System.Text.StringBuilder valueBuffer = new System.Text.StringBuilder("( ");
172                         System.String token;
173                         System.String[] strArray;
174                         
175                         if ((System.Object) (token = ID) != null)
176                         {
177                                 valueBuffer.Append(token);
178                         }
179                         strArray = Names;
180                         if (strArray != null)
181                         {
182                                 valueBuffer.Append(" NAME ");
183                                 if (strArray.Length == 1)
184                                 {
185                                         valueBuffer.Append("'" + strArray[0] + "'");
186                                 }
187                                 else
188                                 {
189                                         valueBuffer.Append("( ");
190                                         
191                                         for (int i = 0; i < strArray.Length; i++)
192                                         {
193                                                 valueBuffer.Append(" '" + strArray[i] + "'");
194                                         }
195                                         valueBuffer.Append(" )");
196                                 }
197                         }
198                         if ((System.Object) (token = Description) != null)
199                         {
200                                 valueBuffer.Append(" DESC ");
201                                 valueBuffer.Append("'" + token + "'");
202                         }
203                         if (Obsolete)
204                         {
205                                 valueBuffer.Append(" OBSOLETE");
206                         }
207                         if ((System.Object) (token = SyntaxString) != null)
208                         {
209                                 valueBuffer.Append(" SYNTAX ");
210                                 valueBuffer.Append(token);
211                         }
212                         valueBuffer.Append(" )");
213                         return valueBuffer.ToString();
214                 }
215         }
216 }