New test.
[mono.git] / mcs / class / Novell.Directory.Ldap / Novell.Directory.Ldap.Asn1 / Asn1Tagged.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.Asn1Tagged.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> The Asn1Tagged class can hold a base Asn1Object with a distinctive tag
38         /// describing the type of that base object. It also maintains a boolean value
39         /// indicating whether the value should be encoded by EXPLICIT or IMPLICIT
40         /// means. (Explicit is true by default.)
41         /// 
42         /// If the type is encoded IMPLICITLY, the base types form, length and content
43         /// will be encoded as usual along with the class type and tag specified in
44         /// the constructor of this Asn1Tagged class.
45         /// 
46         /// If the type is to be encoded EXPLICITLY, the base type will be encoded as
47         /// usual after the Asn1Tagged identifier has been encoded.
48         /// </summary>
49         public class Asn1Tagged:Asn1Object
50         {
51                 /// <summary> Sets the Asn1Object tagged value</summary>
52                 [CLSCompliantAttribute(false)]
53                 virtual public Asn1Object TaggedValue
54                 {
55                         set
56                         {
57                                 this.content = value;
58                                 if (!explicit_Renamed && value != null)
59                                 {
60                                         // replace object's id with new tag.
61                                         value.setIdentifier(this.getIdentifier());
62                                 }
63                         }
64                         
65                 }
66                 /// <summary> Returns a boolean value indicating if this object uses
67                 /// EXPLICIT tagging.
68                 /// </summary>
69                 virtual public bool Explicit
70                 {
71                         get
72                         {
73                                 return explicit_Renamed;
74                         }
75                         
76                 }
77                 
78                 private bool explicit_Renamed;
79                 private Asn1Object content;
80                 
81                 /* Constructors for Asn1Tagged
82                 */
83                 
84                 /// <summary> Constructs an Asn1Tagged object using the provided 
85                 /// AN1Identifier and the Asn1Object.
86                 /// 
87                 /// The explicit flag defaults to true as per the spec.
88                 /// </summary>
89                 public Asn1Tagged(Asn1Identifier identifier, Asn1Object object_Renamed):this(identifier, object_Renamed, true)
90                 {
91                         return ;
92                 }
93                 
94                 /// <summary> Constructs an Asn1Tagged object.</summary>
95                 public Asn1Tagged(Asn1Identifier identifier, Asn1Object object_Renamed, bool explicit_Renamed):base(identifier)
96                 {
97                         this.content = object_Renamed;
98                         this.explicit_Renamed = explicit_Renamed;
99                         
100                         if (!explicit_Renamed && content != null)
101                         {
102                                 // replace object's id with new tag.
103                                 content.setIdentifier(identifier);
104                         }
105                         return ;
106                 }
107                 
108                 /// <summary> Constructs an Asn1Tagged object by decoding data from an 
109                 /// input stream.
110                 /// 
111                 /// </summary>
112                 /// <param name="dec">The decoder object to use when decoding the
113                 /// input stream.  Sometimes a developer might want to pass
114                 /// in his/her own decoder object
115                 /// 
116                 /// </param>
117                 /// <param name="in">A byte stream that contains the encoded ASN.1
118                 /// 
119                 /// </param>
120                 [CLSCompliantAttribute(false)]
121                 public Asn1Tagged(Asn1Decoder dec, System.IO.Stream in_Renamed, int len, Asn1Identifier identifier):base(identifier)
122                 {
123                         
124                         // If we are decoding an implicit tag, there is no way to know at this
125                         // low level what the base type really is. We can place the content
126                         // into an Asn1OctetString type and pass it back to the application who
127                         // will be able to create the appropriate ASN.1 type for this tag.
128                         content = new Asn1OctetString(dec, in_Renamed, len);
129                         return ;
130                 }
131                 
132                 /* Asn1Object implementation
133                 */
134                 
135                 /// <summary> Call this method to encode the current instance into the 
136                 /// specified output stream using the specified encoder object.
137                 /// 
138                 /// </summary>
139                 /// <param name="enc">Encoder object to use when encoding self.
140                 /// 
141                 /// </param>
142                 /// <param name="out">The output stream onto which the encoded byte 
143                 /// stream is written.
144                 /// </param>
145                 public override void  encode(Asn1Encoder enc, System.IO.Stream out_Renamed)
146                 {
147                         enc.encode(this, out_Renamed);
148                         return ;
149                 }
150                 
151                 /* Asn1Tagged specific methods
152                 */
153                 
154                 /// <summary> Returns the Asn1Object stored in this Asn1Tagged object</summary>
155                 public Asn1Object taggedValue()
156                 {
157                         return content;
158                 }
159                 
160                 /// <summary> Return a String representation of this Asn1Object.</summary>
161                 public override System.String ToString()
162                 {
163                         if (explicit_Renamed)
164                         {
165                                 return base.ToString() + content.ToString();
166                         }
167                         // implicit tagging
168                         return content.ToString();
169                 }
170         }
171 }