OSDN Git Service

version 0.2
[bm-asn1/bm-asn1.git] / jp / bitmeister / asn1 / type / ASN1Modules.java
1 /*
2  * Copyright 2011 BitMeister Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package jp.bitmeister.asn1.type;
17
18 import java.util.HashMap;
19 import java.util.Map;
20 import java.util.Map.Entry;
21
22 /**
23  * Manager of all ASN.1 modules.
24  * 
25  * <p>
26  * This class manages all of the ASN.1 modules which are sub-class of
27  * {@code ASN1Module} and provides {@code instantiate} method that instantiates
28  * an ASN.1 data from an ASN.1 tag or identifier.
29  * </p>
30  * 
31  * @author WATANABE, Jun. <jwat at bitmeister.jp>
32  * 
33  * @see ASN1Module
34  */
35 public class ASN1Modules {
36
37         /**
38          * The manager of ASN.1 modules.
39          */
40         private static ASN1Modules instance;
41
42         static {
43                 instance = new ASN1Modules();
44         }
45
46         /**
47          * Instantiates an ASN.1 data specified by the ASN.1 tag and the tag class.
48          * 
49          * @param tagClass
50          *            The tag class.
51          * @param tagNumber
52          *            The tag number.
53          * @return An instance of ASN.1 data.
54          */
55         public static ASN1Type instantiate(ASN1TagClass tagClass, int tagNumber) {
56                 switch (tagClass) {
57                 case UNIVERSAL:
58                         return instance.universalModule.instantiate(tagClass, tagNumber);
59                 case APPLICATION:
60                 case PRIVATE:
61                         for (Entry<Class<? extends ASN1Module>, ASN1Module> e : instance.definedModules
62                                         .entrySet()) {
63                                 ASN1Type data = e.getValue().instantiate(tagClass, tagNumber);
64                                 if (data != null) {
65                                         return data;
66                                 }
67                         }
68                         break;
69                 }
70                 return new UnknownType(tagClass, tagNumber);
71         }
72
73         /**
74          * Instantiates a built-in ASN.1 data specified by the type identifier.
75          * 
76          * @param typeIdentifier
77          *            The identifier of a type.
78          * @return An instance of built-in ASN.1 data.
79          */
80         public static ASN1Type instanciate(String typeIdentifier) {
81                 return instance.universalModule.instantiate(typeIdentifier);
82         }
83
84         /**
85          * Instantiates an ASN.1 data specified by the identifier of module and
86          * type.
87          * 
88          * @param moduleIdentifier
89          *            The identifier of a module.
90          * @param typeIdentifier
91          *            The identifier of a type.
92          * @return An instance of ASN.1 data.
93          */
94         public static ASN1Type instantiate(String moduleIdentifier,
95                         String typeIdentifier) {
96                 for (Entry<Class<? extends ASN1Module>, ASN1Module> e : instance.definedModules
97                                 .entrySet()) {
98                         if (e.getValue().identifier().equals(moduleIdentifier)) {
99                                 return e.getValue().instantiate(typeIdentifier);
100                         }
101                 }
102                 return null;
103         }
104
105         /**
106          * Registers the ASN.1 module to the manager.
107          * 
108          * @param module
109          *            The ASN.1 module to be registered.
110          */
111         public static void using(ASN1Module module) {
112                 instance.definedModules.put(module.getClass(), module);
113         }
114
115         private ASN1Module universalModule = new BuiltInModule();
116
117         private Map<Class<? extends ASN1Module>, ASN1Module> definedModules = new HashMap<Class<? extends ASN1Module>, ASN1Module>();
118
119         /**
120          * Instantiates an {@code ASN1Modules}.
121          */
122         private ASN1Modules() {
123         }
124
125 }