OSDN Git Service

version 0.2
[bm-asn1/bm-asn1.git] / jp / bitmeister / asn1 / type / builtin / OBJECT_IDENTIFIER.java
index 9d710e2..0c584aa 100644 (file)
 package jp.bitmeister.asn1.type.builtin;
 
 import java.util.ArrayList;
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
 
 import jp.bitmeister.asn1.annotation.ASN1BuiltIn;
 import jp.bitmeister.asn1.annotation.ASN1Identifier;
 import jp.bitmeister.asn1.annotation.ASN1Tag;
+import jp.bitmeister.asn1.annotation.ASN1XmlTypeName;
+import jp.bitmeister.asn1.exception.ASN1IllegalArgument;
 import jp.bitmeister.asn1.processor.ASN1Visitor;
 import jp.bitmeister.asn1.type.ASN1TagClass;
 import jp.bitmeister.asn1.type.ASN1TagMode;
@@ -39,9 +43,64 @@ import jp.bitmeister.asn1.type.PrimitiveType;
  */
 @ASN1BuiltIn
 @ASN1Identifier("OBJECT IDENTIFIER")
+@ASN1XmlTypeName("OBJECT_IDENTIFIER")
 @ASN1Tag(tagClass = ASN1TagClass.UNIVERSAL, value = 6, tagMode = ASN1TagMode.IMPLICIT)
 public class OBJECT_IDENTIFIER extends PrimitiveType<List<Integer>> {
-
+       
+       private static Map<String, Integer> NAMES_ROOT = new HashMap<String, Integer>();
+       private static Map<String, Integer> NAMES_ITU_T = new HashMap<String, Integer>();
+       private static Map<String, Integer> NAMES_ISO = new HashMap<String, Integer>();
+       private static Map<String, Integer> NAMES_ITU_T_REC = new HashMap<String, Integer>();
+       
+       static {
+               // top level
+               NAMES_ROOT.put("itu-t", 0);
+               NAMES_ROOT.put("ccitt", 0);
+               NAMES_ROOT.put("iso", 1);
+               NAMES_ROOT.put("joint-iso-itu-t", 2);
+               NAMES_ROOT.put("joint-iso-ccitt", 2);
+               // itu-t/ccitt
+               NAMES_ITU_T.put("recommendation", 0);
+               NAMES_ITU_T.put("question", 1);
+               NAMES_ITU_T.put("administration", 2);
+               NAMES_ITU_T.put("network-operator", 3);
+               NAMES_ITU_T.put("identified-organization", 4);
+               NAMES_ITU_T.put("r-recommendation", 5);
+               // iso
+               NAMES_ISO.put("standard", 0);
+               NAMES_ISO.put("registration-authority", 1);
+               NAMES_ISO.put("member-body", 2);
+               NAMES_ISO.put("identified-organization", 3);
+               // itu-t recommendation
+               for (char c = 'a'; c <= 'z'; c++) {
+                       NAMES_ITU_T_REC.put(String.valueOf(c), c - 'a');
+               } 
+       }
+       
+       public static int nameFormToInt(List<Integer> list, String nameForm) {
+               Integer number = null;
+               if (list.size() == 0) {
+                       number = NAMES_ROOT.get(nameForm);
+               }
+               else if (list.size() == 1) {
+                       if (list.get(0) == 0) {
+                               number = NAMES_ITU_T.get(nameForm);
+                       }
+                       else if (list.get(0) == 1) {
+                               number = NAMES_ISO.get(nameForm);
+                       }
+               }
+               else if (list.size() == 2 && list.get(0) == 0 && list.get(1) == 0) {
+                       number = NAMES_ITU_T_REC.get(nameForm);
+               }
+               if (number == null) {
+                       ASN1IllegalArgument e = new ASN1IllegalArgument();
+                       e.setMessage("Invalid OID name form '" + nameForm + "'.", null, OBJECT_IDENTIFIER.class, null, null);
+                       throw e;
+               }
+               return number;
+       }
+       
        /**
         * Instantiates an empty {@code OBJECT_IDENTIFIER}.
         */
@@ -84,7 +143,7 @@ public class OBJECT_IDENTIFIER extends PrimitiveType<List<Integer>> {
                }
                set(oids);
        }
-
+       
        /*
         * (non-Javadoc)
         *