KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > util > asn1 > BERTaggedObject


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.geronimo.util.asn1;
19
20 import java.io.IOException JavaDoc;
21 import java.util.Enumeration JavaDoc;
22
23 /**
24  * BER TaggedObject - in ASN.1 nottation this is any object proceeded by
25  * a [n] where n is some number - these are assume to follow the construction
26  * rules (as with sequences).
27  */

28 public class BERTaggedObject
29     extends DERTaggedObject
30 {
31     /**
32      * @param tagNo the tag number for this object.
33      * @param obj the tagged object.
34      */

35     public BERTaggedObject(
36         int tagNo,
37         DEREncodable obj)
38     {
39         super(tagNo, obj);
40     }
41
42     /**
43      * @param explicit true if an explicitly tagged object.
44      * @param tagNo the tag number for this object.
45      * @param obj the tagged object.
46      */

47     public BERTaggedObject(
48         boolean explicit,
49         int tagNo,
50         DEREncodable obj)
51     {
52         super(explicit, tagNo, obj);
53     }
54
55     /**
56      * create an implicitly tagged object that contains a zero
57      * length sequence.
58      */

59     public BERTaggedObject(
60         int tagNo)
61     {
62         super(false, tagNo, new BERSequence());
63     }
64
65     void encode(
66         DEROutputStream out)
67         throws IOException JavaDoc
68     {
69         if (out instanceof ASN1OutputStream || out instanceof BEROutputStream)
70         {
71             out.write(CONSTRUCTED | TAGGED | tagNo);
72             out.write(0x80);
73
74             if (!empty)
75             {
76                 if (!explicit)
77                 {
78                     if (obj instanceof ASN1OctetString)
79                     {
80                         Enumeration JavaDoc e;
81
82                         if (obj instanceof BERConstructedOctetString)
83                         {
84                             e = ((BERConstructedOctetString)obj).getObjects();
85                         }
86                         else
87                         {
88                             ASN1OctetString octs = (ASN1OctetString)obj;
89                             BERConstructedOctetString berO = new BERConstructedOctetString(octs.getOctets());
90
91                             e = berO.getObjects();
92                         }
93
94                         while (e.hasMoreElements())
95                         {
96                             out.writeObject(e.nextElement());
97                         }
98                     }
99                     else if (obj instanceof ASN1Sequence)
100                     {
101                         Enumeration JavaDoc e = ((ASN1Sequence)obj).getObjects();
102
103                         while (e.hasMoreElements())
104                         {
105                             out.writeObject(e.nextElement());
106                         }
107                     }
108                     else if (obj instanceof ASN1Set)
109                     {
110                         Enumeration JavaDoc e = ((ASN1Set)obj).getObjects();
111
112                         while (e.hasMoreElements())
113                         {
114                             out.writeObject(e.nextElement());
115                         }
116                     }
117                     else
118                     {
119                         throw new RuntimeException JavaDoc("not implemented: " + obj.getClass().getName());
120                     }
121                 }
122                 else
123                 {
124                     out.writeObject(obj);
125                 }
126             }
127
128             out.write(0x00);
129             out.write(0x00);
130         }
131         else
132         {
133             super.encode(out);
134         }
135     }
136 }
137
Popular Tags