OSDN Git Service

version 0.2
[bm-asn1/bm-asn1.git] / jp / bitmeister / asn1 / type / builtin / OBJECT_IDENTIFIER.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.builtin;
17
18 import java.util.ArrayList;
19 import java.util.HashMap;
20 import java.util.List;
21 import java.util.Map;
22
23 import jp.bitmeister.asn1.annotation.ASN1BuiltIn;
24 import jp.bitmeister.asn1.annotation.ASN1Identifier;
25 import jp.bitmeister.asn1.annotation.ASN1Tag;
26 import jp.bitmeister.asn1.annotation.ASN1XmlTypeName;
27 import jp.bitmeister.asn1.exception.ASN1IllegalArgument;
28 import jp.bitmeister.asn1.processor.ASN1Visitor;
29 import jp.bitmeister.asn1.type.ASN1TagClass;
30 import jp.bitmeister.asn1.type.ASN1TagMode;
31 import jp.bitmeister.asn1.type.PrimitiveType;
32
33 /**
34  * Represents ASN.1 'OBJECT IDENTIFIER' type.
35  * 
36  * <p>
37  * An instance of this class represents an 'OBJECT IDENTIFIER' type data and
38  * contains a {@code List} of {@code Integer}. Each element of the {@code List}
39  * represents an object id component.
40  * </p>
41  * 
42  * @author WATANABE, Jun. <jwat at bitmeister.jp>
43  */
44 @ASN1BuiltIn
45 @ASN1Identifier("OBJECT IDENTIFIER")
46 @ASN1XmlTypeName("OBJECT_IDENTIFIER")
47 @ASN1Tag(tagClass = ASN1TagClass.UNIVERSAL, value = 6, tagMode = ASN1TagMode.IMPLICIT)
48 public class OBJECT_IDENTIFIER extends PrimitiveType<List<Integer>> {
49         
50         private static Map<String, Integer> NAMES_ROOT = new HashMap<String, Integer>();
51         private static Map<String, Integer> NAMES_ITU_T = new HashMap<String, Integer>();
52         private static Map<String, Integer> NAMES_ISO = new HashMap<String, Integer>();
53         private static Map<String, Integer> NAMES_ITU_T_REC = new HashMap<String, Integer>();
54         
55         static {
56                 // top level
57                 NAMES_ROOT.put("itu-t", 0);
58                 NAMES_ROOT.put("ccitt", 0);
59                 NAMES_ROOT.put("iso", 1);
60                 NAMES_ROOT.put("joint-iso-itu-t", 2);
61                 NAMES_ROOT.put("joint-iso-ccitt", 2);
62                 // itu-t/ccitt
63                 NAMES_ITU_T.put("recommendation", 0);
64                 NAMES_ITU_T.put("question", 1);
65                 NAMES_ITU_T.put("administration", 2);
66                 NAMES_ITU_T.put("network-operator", 3);
67                 NAMES_ITU_T.put("identified-organization", 4);
68                 NAMES_ITU_T.put("r-recommendation", 5);
69                 // iso
70                 NAMES_ISO.put("standard", 0);
71                 NAMES_ISO.put("registration-authority", 1);
72                 NAMES_ISO.put("member-body", 2);
73                 NAMES_ISO.put("identified-organization", 3);
74                 // itu-t recommendation
75                 for (char c = 'a'; c <= 'z'; c++) {
76                         NAMES_ITU_T_REC.put(String.valueOf(c), c - 'a');
77                 } 
78         }
79         
80         public static int nameFormToInt(List<Integer> list, String nameForm) {
81                 Integer number = null;
82                 if (list.size() == 0) {
83                         number = NAMES_ROOT.get(nameForm);
84                 }
85                 else if (list.size() == 1) {
86                         if (list.get(0) == 0) {
87                                 number = NAMES_ITU_T.get(nameForm);
88                         }
89                         else if (list.get(0) == 1) {
90                                 number = NAMES_ISO.get(nameForm);
91                         }
92                 }
93                 else if (list.size() == 2 && list.get(0) == 0 && list.get(1) == 0) {
94                         number = NAMES_ITU_T_REC.get(nameForm);
95                 }
96                 if (number == null) {
97                         ASN1IllegalArgument e = new ASN1IllegalArgument();
98                         e.setMessage("Invalid OID name form '" + nameForm + "'.", null, OBJECT_IDENTIFIER.class, null, null);
99                         throw e;
100                 }
101                 return number;
102         }
103         
104         /**
105          * Instantiates an empty {@code OBJECT_IDENTIFIER}.
106          */
107         public OBJECT_IDENTIFIER() {
108         }
109
110         /**
111          * Instantiates an {@code OBJECT_IDENTIFIER} and initialize it with the
112          * {@code List<Integer>} value.
113          * 
114          * @param value
115          *            the {@code List} of {@code Integer} represents an object
116          *            identifier.
117          */
118         public OBJECT_IDENTIFIER(List<Integer> value) {
119                 set(value);
120         }
121
122         /**
123          * Instantiates an {@code OBJECT_IDENTIFIER} and initialize it with the
124          * array of {@code int} value.
125          * 
126          * @param value
127          *            the array of {@code int} represents an object identifier.
128          */
129         public OBJECT_IDENTIFIER(int... value) {
130                 set(value);
131         }
132
133         /**
134          * Sets the array of {@code int} to the data.
135          * 
136          * @param value
137          *            the array of {@code int} represents an object identifier.
138          */
139         public void set(int... value) {
140                 List<Integer> oids = new ArrayList<Integer>();
141                 for (int i : value) {
142                         oids.add(i);
143                 }
144                 set(oids);
145         }
146         
147         /*
148          * (non-Javadoc)
149          * 
150          * @see jp.bitmeister.asn1.type.PrimitiveType#cloneValue()
151          */
152         @Override
153         protected List<Integer> cloneValue() {
154                 List<Integer> clone = new ArrayList<Integer>();
155                 clone.addAll(value());
156                 return clone;
157         }
158
159         /*
160          * (non-Javadoc)
161          * 
162          * @see
163          * jp.bitmeister.asn1.type.ASN1Type#accept(jp.bitmeister.asn1.processor.
164          * ASN1Visitor)
165          */
166         @Override
167         public <R, E extends Throwable> R accept(ASN1Visitor<R, E> visitor) throws E {
168                 return visitor.visit(this);
169         }
170
171 }