Add constructor with initial values on CollectionTypes.
}
return builder.toString();
} else {
- return new BinString(data.value()).toString();
+ return new BinString(data.value()).string();
}
}
*/
@Override
public String visit(OCTET_STRING data) {
- return new HexString(data.value()).toString();
+ return new HexString(data.value()).string();
}
/*
import jp.bitmeister.asn1.value.HexString;
public class FrightStatusMain {
-
+
public static void main(String[] args) {
-
- FrightStatus ontime = new FrightStatus(
- new FrightNumber("JP041"),
- new Information(
- new Airport(Airport.tokyo),
- new UTCTime("110627073000"),
- new UTCTime("110627073000")
+ AllFrights frights = new AllFrights(
+ new FrightStatus(
+ new FrightNumber("JP041"),
+ new Information(
+ new Airport(Airport.tokyo),
+ new UTCTime("110627073000"),
+ new UTCTime("110627073000")
),
- new Information(
- new Airport(Airport.osaka),
- new UTCTime("110627090000"),
- null
+ new Information(
+ new Airport(Airport.osaka),
+ new UTCTime("110627090000"),
+ null
)
- );
-
- FrightStatus delay = new FrightStatus(
- new FrightNumber("NI022"),
- new Information(
- new Airport(Airport.fukuoka),
- new UTCTime("110627080000"),
- new UTCTime("110627081000")
+ ),
+ new FrightStatus(
+ new FrightNumber("NI022"),
+ new Information(
+ new Airport(Airport.fukuoka),
+ new UTCTime("110627080000"),
+ new UTCTime("110627081000")
),
- new Information(
- new Airport(Airport.nagoya),
- new UTCTime("110627093000"),
- null
+ new Information(
+ new Airport(Airport.nagoya),
+ new UTCTime("110627093000"),
+ null
),
- new Status(ASN1TagClass.CONTEXT_SPECIFIC, 1, new INTEGER(10))
+ new Status(ASN1TagClass.CONTEXT_SPECIFIC, 1, new INTEGER(10))
+ )
);
-
- AllFrights frights = new AllFrights();
- frights.collection().add(delay);
- frights.collection().add(ontime);
-
+
System.out.println("Source data:");
System.out.println(frights);
-
- System.out.println("BER encoding:");
+
derEncAndDec(frights);
-
- System.out.println("XER encoding:");
xerEncAndDec(frights);
+
}
-
+
static void derEncAndDec(ASN1Type data) {
+ System.out.println("BER encoding:");
ByteArrayOutputStream bo = new ByteArrayOutputStream();
BerEncoder enc = new DerEncoder(bo);
try {
enc.encode(data);
- }
- catch (ASN1EncodingException e) {
+ } catch (ASN1EncodingException e) {
e.printStackTrace();
}
-
+
System.out.println(new HexString(bo.toByteArray()));
-
+
ByteArrayInputStream bi = new ByteArrayInputStream(bo.toByteArray());
BerDecoder dec = new BerDecoder(FrightStatusTypes.class, bi);
try {
ASN1Type result = dec.decode();
System.out.println("BER decoding:");
System.out.println(result);
- }
- catch (ASN1DecodingException e) {
+ } catch (ASN1DecodingException e) {
e.printStackTrace();
}
-
+
}
-
+
static void xerEncAndDec(ASN1Type data) {
-
+
+ System.out.println("XER encoding:");
ByteArrayOutputStream bo = new ByteArrayOutputStream();
XerEncoder enc = new XerEncoder(bo);
try {
enc.encode(data);
- }
- catch (ASN1EncodingException e) {
+ } catch (ASN1EncodingException e) {
e.printStackTrace();
}
System.out.println(new String(bo.toByteArray()));
-
+
ByteArrayInputStream bi = new ByteArrayInputStream(bo.toByteArray());
XerDecoder dec = new XerDecoder(FrightStatusTypes.class, bi);
try {
ASN1Type result = dec.decode();
System.out.println("XER decoding:");
System.out.println(result);
- }
- catch (ASN1DecodingException e) {
+ } catch (ASN1DecodingException e) {
e.printStackTrace();
}
-
+
}
-
-}
+}
super(FrightStatus.class);
}
+ public AllFrights(FrightStatus... components) {
+ super(FrightStatus.class, components);
+ }
+
}
}
+++ /dev/null
-/*
- * Copyright 2011 BitMeister Inc.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package jp.bitmeister.asn1.type;
-
-import java.util.HashMap;
-import java.util.Map;
-import java.util.Map.Entry;
-
-/**
- * Manager of all ASN.1 modules.
- *
- * <p>
- * This class manages all of the ASN.1 modules which are sub-class of
- * {@code ASN1Module} and provides {@code instantiate} method that instantiates
- * an ASN.1 data from an ASN.1 tag or identifier.
- * </p>
- *
- * @author WATANABE, Jun. <jwat at bitmeister.jp>
- *
- * @see ASN1Module
- */
-public class ASN1Modules {
-
- /**
- * The manager of ASN.1 modules.
- */
- private static ASN1Modules instance;
-
- static {
- instance = new ASN1Modules();
- }
-
- /**
- * Instantiates an ASN.1 data specified by the ASN.1 tag and the tag class.
- *
- * @param tagClass
- * The tag class.
- * @param tagNumber
- * The tag number.
- * @return An instance of ASN.1 data.
- */
- public static ASN1Type instantiate(ASN1TagClass tagClass, int tagNumber) {
- switch (tagClass) {
- case UNIVERSAL:
- return instance.universalModule.instantiate(tagClass, tagNumber);
- case APPLICATION:
- case PRIVATE:
- for (Entry<Class<? extends ASN1Module>, ASN1Module> e : instance.definedModules
- .entrySet()) {
- ASN1Type data = e.getValue().instantiate(tagClass, tagNumber);
- if (data != null) {
- return data;
- }
- }
- break;
- }
- return new UnknownType(tagClass, tagNumber);
- }
-
- /**
- * Instantiates a built-in ASN.1 data specified by the type identifier.
- *
- * @param typeIdentifier
- * The identifier of a type.
- * @return An instance of built-in ASN.1 data.
- */
- public static ASN1Type instanciate(String typeIdentifier) {
- return instance.universalModule.instantiate(typeIdentifier);
- }
-
- /**
- * Instantiates an ASN.1 data specified by the identifier of module and
- * type.
- *
- * @param moduleIdentifier
- * The identifier of a module.
- * @param typeIdentifier
- * The identifier of a type.
- * @return An instance of ASN.1 data.
- */
- public static ASN1Type instantiate(String moduleIdentifier,
- String typeIdentifier) {
- for (Entry<Class<? extends ASN1Module>, ASN1Module> e : instance.definedModules
- .entrySet()) {
- if (e.getValue().identifier().equals(moduleIdentifier)) {
- return e.getValue().instantiate(typeIdentifier);
- }
- }
- return null;
- }
-
- /**
- * Registers the ASN.1 module to the manager.
- *
- * @param module
- * The ASN.1 module to be registered.
- */
- public static void using(ASN1Module module) {
- instance.definedModules.put(module.getClass(), module);
- }
-
- private ASN1Module universalModule = new BuiltInModule();
-
- private Map<Class<? extends ASN1Module>, ASN1Module> definedModules = new HashMap<Class<? extends ASN1Module>, ASN1Module>();
-
- /**
- * Instantiates an {@code ASN1Modules}.
- */
- private ASN1Modules() {
- }
-
-}
* type.
*
* @param componentType
+ * The {@code class} object of the component of this collection.
*/
protected CollectionType(Class<T> componentType) {
this.componentType = componentType;
}
/**
+ * Instantiate a {@code CollectionType} instance whose component is the type
+ * and initialize it with the components.
+ *
+ * @param componentType
+ * The {@code class} object of the component of this collection.
+ * @param components
+ * Components to be set this instance.
+ */
+ protected CollectionType(Class<T> componentType, T... components) {
+ this(componentType);
+ for (T e : components) {
+ collection.add(e);
+ }
+ }
+
+ /**
* Returns the collection that contained in the data.
*
* @return The collection.
public boolean hasValue() {
return true;
}
-
+
/*
* (non-Javadoc)
*
* <p>
* This is the class used for defining 'SEQUENCE OF' types. Generic type
* parameter {@code T} indicates component type of the 'SEQUENCE OF' type.
- * Constructors of a sub-class of {@code SEQUENCE_OF} must call parent constructor
- * with {@code componentType} parameter that is the class object of {@code T} in
- * them.
+ * Constructors of a sub-class of {@code SEQUENCE_OF} must call parent
+ * constructor with {@code componentType} parameter that is the class object of
+ * {@code T} in them.
* </p>
*
* @author WATANABE, Jun. <jwat at bitmeister.jp>
super(componentType);
}
+ /**
+ * Instantiate a {@code SEQUENCE_OF} and initialize it with the components.
+ *
+ * @param componentType
+ * The class instance of component type.
+ * @param components
+ * Components to be set this instance.
+ */
+ public SEQUENCE_OF(Class<T> componentType, T... components) {
+ super(componentType, components);
+ }
+
/*
* (non-Javadoc)
*
* ASN1Visitor)
*/
@Override
- public <R, E extends Throwable> R accept(ASN1Visitor<R, E> visitor) throws E {
+ public <R, E extends Throwable> R accept(ASN1Visitor<R, E> visitor)
+ throws E {
return visitor.visit(this);
}
public SET_OF(Class<T> componentType) {
super(componentType);
}
+
+ /**
+ * Instantiate a {@code SET_OF} and initialize it with the components.
+ *
+ * @param componentType
+ * The class instance of component type.
+ * @param components
+ * Components to be set this instance.
+ */
+ public SET_OF(Class<T> componentType, T... components) {
+ super(componentType, components);
+ }
/*
* (non-Javadoc)