2 * Copyright 2011 BitMeister Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package jp.bitmeister.asn1.type;
18 import java.util.HashMap;
20 import java.util.Map.Entry;
23 * Manager of all ASN.1 modules.
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.
31 * @author WATANABE, Jun. <jwat at bitmeister.jp>
35 public class ASN1Modules {
38 * The manager of ASN.1 modules.
40 private static ASN1Modules instance;
43 instance = new ASN1Modules();
47 * Instantiates an ASN.1 data specified by the ASN.1 tag and the tag class.
53 * @return An instance of ASN.1 data.
55 public static ASN1Type instantiate(ASN1TagClass tagClass, int tagNumber) {
58 return instance.universalModule.instantiate(tagClass, tagNumber);
61 for (Entry<Class<? extends ASN1Module>, ASN1Module> e : instance.definedModules
63 ASN1Type data = e.getValue().instantiate(tagClass, tagNumber);
70 return new UnknownType(tagClass, tagNumber);
74 * Instantiates a built-in ASN.1 data specified by the type identifier.
76 * @param typeIdentifier
77 * The identifier of a type.
78 * @return An instance of built-in ASN.1 data.
80 public static ASN1Type instanciate(String typeIdentifier) {
81 return instance.universalModule.instantiate(typeIdentifier);
85 * Instantiates an ASN.1 data specified by the identifier of module and
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.
94 public static ASN1Type instantiate(String moduleIdentifier,
95 String typeIdentifier) {
96 for (Entry<Class<? extends ASN1Module>, ASN1Module> e : instance.definedModules
98 if (e.getValue().identifier().equals(moduleIdentifier)) {
99 return e.getValue().instantiate(typeIdentifier);
106 * Registers the ASN.1 module to the manager.
109 * The ASN.1 module to be registered.
111 public static void using(ASN1Module module) {
112 instance.definedModules.put(module.getClass(), module);
115 private ASN1Module universalModule = new BuiltInModule();
117 private Map<Class<? extends ASN1Module>, ASN1Module> definedModules = new HashMap<Class<? extends ASN1Module>, ASN1Module>();
120 * Instantiates an {@code ASN1Modules}.
122 private ASN1Modules() {