Fix problems with overlong directory names: phase #1
[mono.git] / mcs / class / Novell.Directory.Ldap / Novell.Directory.Ldap.Asn1 / Asn1Object.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.Asn1.Asn1Object.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
34 namespace Novell.Directory.Ldap.Asn1
35 {
36         
37         /// <summary> This is the base class for all other Asn1 types.</summary>
38         [Serializable]
39         public abstract class Asn1Object : System.Runtime.Serialization.ISerializable
40         {
41                 
42                 private Asn1Identifier id;
43                 
44                 public Asn1Object(Asn1Identifier id)
45                 {
46                         this.id = id;
47                         return ;
48                 }
49                 
50                 public void GetObjectData(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context)
51                 {
52                 }
53                 
54                 /// <summary> Abstract method that must be implemented by each child
55                 /// class to encode itself ( an Asn1Object) directly intto 
56                 /// a output stream.
57                 /// 
58                 /// </summary>
59                 /// <param name="out">The output stream onto which the encoded 
60                 /// Asn1Object will be placed.
61                 /// </param>
62                 abstract public void  encode(Asn1Encoder enc, System.IO.Stream out_Renamed);
63                 
64                 /// <summary> Returns the identifier for this Asn1Object as an Asn1Identifier. 
65                 /// This Asn1Identifier object will include the CLASS, FORM and TAG
66                 /// for this Asn1Object.
67                 /// </summary>
68                 public virtual Asn1Identifier getIdentifier()
69                 {
70                         return id;
71                 }
72                 
73                 /// <summary> Sets the identifier for this Asn1Object. This is helpful when 
74                 /// creating implicit Asn1Tagged types.
75                 /// 
76                 /// </summary>
77                 /// <param name="id">An Asn1Identifier object representing the CLASS, 
78                 /// FORM and TAG)
79                 /// </param>
80                 public virtual void  setIdentifier(Asn1Identifier id)
81                 {
82                         this.id = id;
83                         return ;
84                 }
85                 
86                 /// <summary> This method returns a byte array representing the encoded
87                 /// Asn1Object.  It in turn calls the encode method that is 
88                 /// defined in Asn1Object but will usually be implemented
89                 /// in the child Asn1 classses.
90                 /// </summary>
91                 [CLSCompliantAttribute(false)]
92                 public sbyte[] getEncoding(Asn1Encoder enc)
93                 {
94                         System.IO.MemoryStream out_Renamed = new System.IO.MemoryStream();
95                         try
96                         {
97                                 encode(enc, out_Renamed);
98                         }
99                         catch (System.IO.IOException e)
100                         {
101                                 // Should never happen - the current Asn1Object does not have
102                                 // a encode method. 
103                                 throw new System.SystemException("IOException while encoding to byte array: " + e.ToString());
104                         }
105                         return SupportClass.ToSByteArray(out_Renamed.ToArray());
106                 }
107                 
108                 /// <summary> Return a String representation of this Asn1Object.</summary>
109                 [CLSCompliantAttribute(false)]
110                 public override System.String ToString()
111                 {
112                         System.String[] classTypes = new System.String[]{"[UNIVERSAL ", "[APPLICATION ", "[CONTEXT ", "[PRIVATE "};
113                         
114                         System.Text.StringBuilder sb = new System.Text.StringBuilder();
115                         Asn1Identifier id = getIdentifier(); // could be overridden.
116                         
117                         sb.Append(classTypes[id.Asn1Class]).Append(id.Tag).Append("] ");
118                         
119                         return sb.ToString();
120                 }
121         }
122 }