From: jwat Date: Wed, 7 Sep 2011 08:22:13 +0000 (+0900) Subject: Bug fix on XerEncoder. X-Git-Url: http://git.osdn.net/view?p=bm-asn1%2Fbm-asn1.git;a=commitdiff_plain;h=b3d8b5c336c435b380b8a58c4453d13d5e2a01fd Bug fix on XerEncoder. Add constructor with initial values on CollectionTypes. --- diff --git a/jp/bitmeister/asn1/codec/xer/XerEncoder.java b/jp/bitmeister/asn1/codec/xer/XerEncoder.java index 53a97b7..30a0ce0 100644 --- a/jp/bitmeister/asn1/codec/xer/XerEncoder.java +++ b/jp/bitmeister/asn1/codec/xer/XerEncoder.java @@ -248,7 +248,7 @@ public class XerEncoder implements ASN1Encoder, } return builder.toString(); } else { - return new BinString(data.value()).toString(); + return new BinString(data.value()).string(); } } @@ -261,7 +261,7 @@ public class XerEncoder implements ASN1Encoder, */ @Override public String visit(OCTET_STRING data) { - return new HexString(data.value()).toString(); + return new HexString(data.value()).string(); } /* diff --git a/jp/bitmeister/asn1/sample/FrightStatusMain.java b/jp/bitmeister/asn1/sample/FrightStatusMain.java index 5544629..6ec8a5f 100644 --- a/jp/bitmeister/asn1/sample/FrightStatusMain.java +++ b/jp/bitmeister/asn1/sample/FrightStatusMain.java @@ -37,101 +37,92 @@ import jp.bitmeister.asn1.type.useful.UTCTime; 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(); } - + } - -} +} diff --git a/jp/bitmeister/asn1/sample/FrightStatusTypes.java b/jp/bitmeister/asn1/sample/FrightStatusTypes.java index b432785..246f5e5 100644 --- a/jp/bitmeister/asn1/sample/FrightStatusTypes.java +++ b/jp/bitmeister/asn1/sample/FrightStatusTypes.java @@ -131,5 +131,9 @@ public class FrightStatusTypes extends ASN1Module { super(FrightStatus.class); } + public AllFrights(FrightStatus... components) { + super(FrightStatus.class, components); + } + } } diff --git a/jp/bitmeister/asn1/type/ASN1Modules.java b/jp/bitmeister/asn1/type/ASN1Modules.java deleted file mode 100644 index 6d9073f..0000000 --- a/jp/bitmeister/asn1/type/ASN1Modules.java +++ /dev/null @@ -1,125 +0,0 @@ -/* - * 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. - * - *

- * 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. - *

- * - * @author WATANABE, Jun. - * - * @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, 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, 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, ASN1Module> definedModules = new HashMap, ASN1Module>(); - - /** - * Instantiates an {@code ASN1Modules}. - */ - private ASN1Modules() { - } - -} diff --git a/jp/bitmeister/asn1/type/CollectionType.java b/jp/bitmeister/asn1/type/CollectionType.java index 26312aa..8924498 100644 --- a/jp/bitmeister/asn1/type/CollectionType.java +++ b/jp/bitmeister/asn1/type/CollectionType.java @@ -46,6 +46,7 @@ public abstract class CollectionType extends ASN1Type * type. * * @param componentType + * The {@code class} object of the component of this collection. */ protected CollectionType(Class componentType) { this.componentType = componentType; @@ -53,6 +54,22 @@ public abstract class CollectionType extends ASN1Type } /** + * 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 componentType, T... components) { + this(componentType); + for (T e : components) { + collection.add(e); + } + } + + /** * Returns the collection that contained in the data. * * @return The collection. @@ -116,7 +133,7 @@ public abstract class CollectionType extends ASN1Type public boolean hasValue() { return true; } - + /* * (non-Javadoc) * diff --git a/jp/bitmeister/asn1/type/builtin/SEQUENCE_OF.java b/jp/bitmeister/asn1/type/builtin/SEQUENCE_OF.java index 7a778a3..4c44bef 100644 --- a/jp/bitmeister/asn1/type/builtin/SEQUENCE_OF.java +++ b/jp/bitmeister/asn1/type/builtin/SEQUENCE_OF.java @@ -34,9 +34,9 @@ import jp.bitmeister.asn1.type.CollectionType; *

* 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. *

* * @author WATANABE, Jun. @@ -57,6 +57,18 @@ public abstract class SEQUENCE_OF extends CollectionType 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 componentType, T... components) { + super(componentType, components); + } + /* * (non-Javadoc) * @@ -75,7 +87,8 @@ public abstract class SEQUENCE_OF extends CollectionType * ASN1Visitor) */ @Override - public R accept(ASN1Visitor visitor) throws E { + public R accept(ASN1Visitor visitor) + throws E { return visitor.visit(this); } diff --git a/jp/bitmeister/asn1/type/builtin/SET_OF.java b/jp/bitmeister/asn1/type/builtin/SET_OF.java index 01e5027..33b36f6 100644 --- a/jp/bitmeister/asn1/type/builtin/SET_OF.java +++ b/jp/bitmeister/asn1/type/builtin/SET_OF.java @@ -55,6 +55,18 @@ public abstract class SET_OF extends CollectionType { public SET_OF(Class 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 componentType, T... components) { + super(componentType, components); + } /* * (non-Javadoc)