From a3926ba723236feb6c5af2f31f1785e8b0a94b0d Mon Sep 17 00:00:00 2001 From: jwat Date: Wed, 22 Aug 2012 18:52:50 +0900 Subject: [PATCH] * If automatic tagging is not used, value parameter of @ASN1Alternative for CHICE type and @ASN1Element for SET type can be omitted. * Fix typo in EXTERNAL type definition ('arbitary' -> 'arbitrary'). * Modify comments in some file. --- jp/bitmeister/asn1/annotation/ASN1Alternative.java | 15 ++++++++------- jp/bitmeister/asn1/annotation/ASN1BuiltIn.java | 1 - jp/bitmeister/asn1/annotation/ASN1Element.java | 15 +++++++++------ jp/bitmeister/asn1/annotation/ASN1Identifier.java | 2 +- jp/bitmeister/asn1/annotation/ASN1ModuleTags.java | 4 +++- jp/bitmeister/asn1/annotation/ASN1Tag.java | 6 ++++-- jp/bitmeister/asn1/codec/ber/BerDecoder.java | 4 ++-- jp/bitmeister/asn1/codec/ber/BerEncoder.java | 2 +- jp/bitmeister/asn1/codec/ber/DerEncoder.java | 7 ++++--- jp/bitmeister/asn1/codec/xer/XerDecoder.java | 2 +- jp/bitmeister/asn1/codec/xer/XerEncoder.java | 2 +- jp/bitmeister/asn1/sample/FrightStatus.java | 5 ++--- jp/bitmeister/asn1/sample/FrightStatusMain.java | 3 +-- jp/bitmeister/asn1/sample/FrightStatusTypes.java | 2 +- jp/bitmeister/asn1/type/ASN1TagValue.java | 2 +- jp/bitmeister/asn1/type/ASN1Type.java | 4 ++-- jp/bitmeister/asn1/type/BuiltInModule.java | 2 +- jp/bitmeister/asn1/type/ConstructiveType.java | 15 +++++++++++---- jp/bitmeister/asn1/type/NamedTypeSpecification.java | 8 ++++++++ jp/bitmeister/asn1/type/PrimitiveType.java | 2 +- jp/bitmeister/asn1/type/builtin/BIT_STRING.java | 16 +++++++++++----- jp/bitmeister/asn1/type/useful/EXTERNAL.java | 8 ++++---- jp/bitmeister/asn1/value/BinString.java | 2 +- 23 files changed, 78 insertions(+), 51 deletions(-) diff --git a/jp/bitmeister/asn1/annotation/ASN1Alternative.java b/jp/bitmeister/asn1/annotation/ASN1Alternative.java index 0438960..6878bce 100644 --- a/jp/bitmeister/asn1/annotation/ASN1Alternative.java +++ b/jp/bitmeister/asn1/annotation/ASN1Alternative.java @@ -28,12 +28,13 @@ import jp.bitmeister.asn1.type.builtin.CHOICE; * *

* If an {@code @ASN1Alternative} annotation is present on a field, the field is - * treated as an alternative of a sub-class of {@code SelectiveType}. A field - * annotated as {@code @ASN1Alternative} must be declared as {@code public} - * non-{@code final} non-{@code static}, and the type shall be a sub-class of - * {@code ASN1Type}. The {@code value} parameter represents an order of - * appearance of a field in an ASN.1 type definition and is used for automatic - * tagging. + * treated as an alternative element of a sub-class of {@code SelectiveType}. A + * field annotated as {@code @ASN1Alternative} must be declared as + * {@code public} non- {@code final} non-{@code static}, and the type shall be a + * sub-class of {@code ASN1Type}. The {@code value} parameter represents an + * order of appearance of a field in an ASN.1 type definition and is used for + * automatic tagging. If automatic tagging is not used, {@code value} parameter + * can be omitted. *

*

* For example, following ASN.1 definition : @@ -72,6 +73,6 @@ public @interface ASN1Alternative { * * @return Order of this element. */ - public int value() default 0; + public int value() default -1; } diff --git a/jp/bitmeister/asn1/annotation/ASN1BuiltIn.java b/jp/bitmeister/asn1/annotation/ASN1BuiltIn.java index 825aa14..d66495c 100644 --- a/jp/bitmeister/asn1/annotation/ASN1BuiltIn.java +++ b/jp/bitmeister/asn1/annotation/ASN1BuiltIn.java @@ -34,5 +34,4 @@ import java.lang.annotation.Target; @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) public @interface ASN1BuiltIn { - } diff --git a/jp/bitmeister/asn1/annotation/ASN1Element.java b/jp/bitmeister/asn1/annotation/ASN1Element.java index c7d6de4..1ed49c8 100644 --- a/jp/bitmeister/asn1/annotation/ASN1Element.java +++ b/jp/bitmeister/asn1/annotation/ASN1Element.java @@ -31,9 +31,10 @@ import jp.bitmeister.asn1.type.ConstructiveType; * {@code @ASN1Element} must be declared as {@code public} non-{@code final} * non-{@code static}, and the type shall be a sub-class of {@code ASN1Type}. * The {@code value} parameter represents an order of appearance of a field in - * an ASN.1 type definition. The {@code value} parameter must be set even if a - * container type is a {@code SET} type. It will be used by the automatic - * tagging process. + * an ASN.1 type definition. If automatic tagging is used, the {@code value} + * parameter must be set even if a container type is a {@code SET} type. + * Otherwise, the {@code value} parameter for element of {@code SET} type can be + * omitted. *

*

* For example, following ASN.1 definition : @@ -73,17 +74,19 @@ public @interface ASN1Element { * * @return Order of this element. */ - public int value(); + public int value() default -1; /** - * Indicates that the element is optional or not. + * Indicates that the element is optional or not. The default value of + * {@code optional} is {@code false}. * * @return {@code true} if the element is optional. */ public boolean optional() default false; /** - * Indicates that the element has default value or not. + * Indicates that the element has default value or not. The default value of + * {@code hasDefault} is {@code false}. * * @return {@code true} if the element has default value. */ diff --git a/jp/bitmeister/asn1/annotation/ASN1Identifier.java b/jp/bitmeister/asn1/annotation/ASN1Identifier.java index a2f9ce4..4f91ccd 100644 --- a/jp/bitmeister/asn1/annotation/ASN1Identifier.java +++ b/jp/bitmeister/asn1/annotation/ASN1Identifier.java @@ -34,7 +34,7 @@ import java.lang.annotation.Target; * For example, following ASN.1 definition : * *

- * Signed-Number ::= SEQUENCE {	-- '-' can't be used for identifier.
+ * Signed-Number ::= SEQUENCE {	-- '-' can't be used for Java identifier.
  * 	int INTEGER		-- 'int' is a reserved word.
  * }
  * 
diff --git a/jp/bitmeister/asn1/annotation/ASN1ModuleTags.java b/jp/bitmeister/asn1/annotation/ASN1ModuleTags.java index fb07d52..a32f926 100644 --- a/jp/bitmeister/asn1/annotation/ASN1ModuleTags.java +++ b/jp/bitmeister/asn1/annotation/ASN1ModuleTags.java @@ -33,13 +33,15 @@ import jp.bitmeister.asn1.type.ASN1TagDefault; * @author WATANABE, Jun. * * @see ASN1Module + * @see ASN1TagDefault */ @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) public @interface ASN1ModuleTags { /** - * Indicates 'TagDefault' specification. + * Indicates 'TagDefault' specification. The default value of {@code value} + * is {@code ASN1TagDefault.EXPLICIT_TAGS}. * * @return The default tagging mode. */ diff --git a/jp/bitmeister/asn1/annotation/ASN1Tag.java b/jp/bitmeister/asn1/annotation/ASN1Tag.java index 20ee32a..f36fed9 100644 --- a/jp/bitmeister/asn1/annotation/ASN1Tag.java +++ b/jp/bitmeister/asn1/annotation/ASN1Tag.java @@ -73,14 +73,16 @@ public @interface ASN1Tag { public int value(); /** - * Indicates tag class of this tag. + * Indicates tag class of this tag. The default value of {@code tagClass} is + * {@code ASN1TagClass.CONTEXT_SPECIFIC}. * * @return Tag class. */ public ASN1TagClass tagClass() default ASN1TagClass.CONTEXT_SPECIFIC; /** - * Indicates the tagging mode of this tag. + * Indicates the tagging mode of this tag. The default value of + * {@code tagMode} is {@code ASN1TagMode.DEFAULT}. * * @return The tagging mode. */ diff --git a/jp/bitmeister/asn1/codec/ber/BerDecoder.java b/jp/bitmeister/asn1/codec/ber/BerDecoder.java index 25c5ae8..567e8a9 100644 --- a/jp/bitmeister/asn1/codec/ber/BerDecoder.java +++ b/jp/bitmeister/asn1/codec/ber/BerDecoder.java @@ -61,7 +61,7 @@ import jp.bitmeister.asn1.type.builtin.SET_OF; *

* {@code BerDecoder} is an implementation of {@code ASN1Decoder}. It reads a * number of bytes from an {@code InputStream} that is specified when a decoder - * is instantiated, and decodes them to an ASN.1 data with Basic Encoding Rules + * is instantiated, and decodes them to an ASN.1 data using Basic Encoding Rules * (BER). *

* @@ -591,7 +591,7 @@ public class BerDecoder implements ASN1Decoder, public void process() throws ASN1DecodingException { @SuppressWarnings("unchecked") - T component = (T) ASN1Type.instantiate(data.getClass()); + T component = (T) ASN1Type.instantiate(data.getClass()); data.concatenate(decodeImpl(component)); } diff --git a/jp/bitmeister/asn1/codec/ber/BerEncoder.java b/jp/bitmeister/asn1/codec/ber/BerEncoder.java index 9bb84c4..e3e3186 100644 --- a/jp/bitmeister/asn1/codec/ber/BerEncoder.java +++ b/jp/bitmeister/asn1/codec/ber/BerEncoder.java @@ -56,7 +56,7 @@ import jp.bitmeister.asn1.type.builtin.SET_OF; * *

* {@code BerEncoder} is an implementation of {@code ASN1Encoder}. It encodes an - * ASN.1 data to an array of {@code byte} with Basic Encoding Rules(BER) and + * ASN.1 data to an array of {@code byte} using Basic Encoding Rules(BER) and * writes the result to {@code OutputStream} that is specified when the encoder * was instantiated. BER encoding process is light-weight compared with * Distinguished Encoding Rules (DER) encoding because some restrictions on DER diff --git a/jp/bitmeister/asn1/codec/ber/DerEncoder.java b/jp/bitmeister/asn1/codec/ber/DerEncoder.java index 2f2b5e1..49818aa 100644 --- a/jp/bitmeister/asn1/codec/ber/DerEncoder.java +++ b/jp/bitmeister/asn1/codec/ber/DerEncoder.java @@ -44,15 +44,16 @@ import jp.bitmeister.asn1.type.useful.UTCTime; * *

* {@code DerEncoder} is an implementation of {@code ASN1Encoder}. It encodes an - * ASN.1 data to an array of {@code byte} with Distinguished Encoding Rules(DER) and - * writes the result to {@code OutputStream} that is specified when the encoder - * was instantiated. + * ASN.1 data to an array of {@code byte} using Distinguished Encoding + * Rules(DER) and writes the result to {@code OutputStream} that is specified + * when the encoder was instantiated. *

*

* DER applies some restrictions on Basic Encoding Rules(BER). The result octets * can be decoded by any BER decoder because DER is a sub-set of BER and * compatible with it. *

+ * * @author WATANABE, Jun. * * @see ASN1Encoder diff --git a/jp/bitmeister/asn1/codec/xer/XerDecoder.java b/jp/bitmeister/asn1/codec/xer/XerDecoder.java index cc0c88e..4991e50 100644 --- a/jp/bitmeister/asn1/codec/xer/XerDecoder.java +++ b/jp/bitmeister/asn1/codec/xer/XerDecoder.java @@ -69,7 +69,7 @@ import org.xml.sax.helpers.XMLReaderFactory; *

* {@code XerDecoder} is an implementation of {@code ASN1Decoder}. It reads a * number of bytes from an {@code InputStream} that is specified when a decoder - * is instantiated, and decodes them to an ASN.1 data with XML Encoding Rules + * is instantiated, and decodes them to an ASN.1 data using XML Encoding Rules * (XER). *

* diff --git a/jp/bitmeister/asn1/codec/xer/XerEncoder.java b/jp/bitmeister/asn1/codec/xer/XerEncoder.java index 0a4a41f..19ca50e 100644 --- a/jp/bitmeister/asn1/codec/xer/XerEncoder.java +++ b/jp/bitmeister/asn1/codec/xer/XerEncoder.java @@ -65,7 +65,7 @@ import jp.bitmeister.asn1.value.HexString; * *

* {@code XerEncoder} is an implementation of {@code ASN1Encoder}. It encodes an - * ASN.1 data to an XML document with XML Encoding Rules(XER) and writes the + * ASN.1 data to an XML document using XML Encoding Rules(XER) and writes the * result to {@code OutputStream} that is specified when the encoder was * instantiated. XER encoding process is light-weight compared with Canonical * XML Encoding Rules (CXER) encoding because some restrictions on CXER are not diff --git a/jp/bitmeister/asn1/sample/FrightStatus.java b/jp/bitmeister/asn1/sample/FrightStatus.java index 0ccd75e..27ddae7 100644 --- a/jp/bitmeister/asn1/sample/FrightStatus.java +++ b/jp/bitmeister/asn1/sample/FrightStatus.java @@ -41,9 +41,8 @@ public class FrightStatus extends SEQUENCE { @ASN1Tag(value = 1, tagMode = ASN1TagMode.IMPLICIT) public Information arrival; - @ASN1Element(value = 3, hasDefault = true) - @ASN1Tag(2) - public Status status = new Status(ASN1TagClass.CONTEXT_SPECIFIC, 0, new NULL()); + @ASN1Element(value = 3,hasDefault = true) + public Status status = new Status(new NULL()); public FrightStatus() {} diff --git a/jp/bitmeister/asn1/sample/FrightStatusMain.java b/jp/bitmeister/asn1/sample/FrightStatusMain.java index 1ce192c..9c3d557 100644 --- a/jp/bitmeister/asn1/sample/FrightStatusMain.java +++ b/jp/bitmeister/asn1/sample/FrightStatusMain.java @@ -30,7 +30,6 @@ import jp.bitmeister.asn1.sample.FrightStatusTypes.AllFrights; import jp.bitmeister.asn1.sample.FrightStatusTypes.FrightNumber; import jp.bitmeister.asn1.sample.FrightStatusTypes.Information; import jp.bitmeister.asn1.sample.FrightStatusTypes.Status; -import jp.bitmeister.asn1.type.ASN1TagClass; import jp.bitmeister.asn1.type.ASN1Type; import jp.bitmeister.asn1.type.builtin.INTEGER; import jp.bitmeister.asn1.type.useful.UTCTime; @@ -66,7 +65,7 @@ public class FrightStatusMain { new UTCTime("110627093000"), null ), - new Status(ASN1TagClass.CONTEXT_SPECIFIC, 1, new INTEGER(10)) + new Status(new INTEGER(10)) ) ); diff --git a/jp/bitmeister/asn1/sample/FrightStatusTypes.java b/jp/bitmeister/asn1/sample/FrightStatusTypes.java index 5237550..4faf044 100644 --- a/jp/bitmeister/asn1/sample/FrightStatusTypes.java +++ b/jp/bitmeister/asn1/sample/FrightStatusTypes.java @@ -89,7 +89,7 @@ public class FrightStatusTypes extends ASN1Module { public static class Information extends AbstractSequence { - @ASN1Element(value = 0) + @ASN1Element(0) public Airport airport; @ASN1Element(1) diff --git a/jp/bitmeister/asn1/type/ASN1TagValue.java b/jp/bitmeister/asn1/type/ASN1TagValue.java index 65c66d9..c97deec 100644 --- a/jp/bitmeister/asn1/type/ASN1TagValue.java +++ b/jp/bitmeister/asn1/type/ASN1TagValue.java @@ -72,7 +72,7 @@ public class ASN1TagValue { && tag.tagClass() != ASN1TagClass.PRIVATE) { ASN1IllegalDefinition ex = new ASN1IllegalDefinition(); ex.setMessage( - "Only context-specific or private tagging is allowed to an elements of structured type.", + "Only context-specific or private tagging is allowed to elements of structured type.", null, namedType.enclosingType(), namedType.identifier(), null); throw ex; diff --git a/jp/bitmeister/asn1/type/ASN1Type.java b/jp/bitmeister/asn1/type/ASN1Type.java index ffb3dbe..3d45ecc 100644 --- a/jp/bitmeister/asn1/type/ASN1Type.java +++ b/jp/bitmeister/asn1/type/ASN1Type.java @@ -72,7 +72,7 @@ public abstract class ASN1Type implements Cloneable { throw ex; } catch (IllegalAccessException e) { ASN1RuntimeException ex = new ASN1IllegalDefinition(); - ex.setMessage("Default constructor might be not accessable.", e, + ex.setMessage("Default constructor might be not accessible.", e, type, null, null); throw ex; } @@ -143,7 +143,7 @@ public abstract class ASN1Type implements Cloneable { * The visitor. * @return Result. * @throws E - * When an error occured in the {@code visit} method of the + * When an error occurred in the {@code visit} method of the * visitor. */ public abstract R accept(ASN1Visitor visitor) diff --git a/jp/bitmeister/asn1/type/BuiltInModule.java b/jp/bitmeister/asn1/type/BuiltInModule.java index 74e6418..dfdebfd 100644 --- a/jp/bitmeister/asn1/type/BuiltInModule.java +++ b/jp/bitmeister/asn1/type/BuiltInModule.java @@ -87,7 +87,7 @@ import jp.bitmeister.asn1.type.useful.VisibleString; GeneralizedTime.class, // UNIVERSAL 24 GraphicString.class, // UNIVERSAL 25 VisibleString.class, // UNIVERSAL 26 - GeneralString.class, // UNIVERSAL27 + GeneralString.class, // UNIVERSAL 27 UniversalString.class, // UNIVERSAL 28 CHARACTER_STRING.class, // UNIVERSAL 29 BMPString.class // UNIVERSAL 30 diff --git a/jp/bitmeister/asn1/type/ConstructiveType.java b/jp/bitmeister/asn1/type/ConstructiveType.java index 3320ae4..0194751 100644 --- a/jp/bitmeister/asn1/type/ConstructiveType.java +++ b/jp/bitmeister/asn1/type/ConstructiveType.java @@ -61,14 +61,21 @@ public abstract class ConstructiveType extends StructuredType { if (ELEMENTS_MAP.containsKey(type)) { return ELEMENTS_MAP.get(type); } + Class parent = type.getSuperclass().asSubclass(ConstructiveType.class); List elements = new ArrayList(); for (Field f : type.getDeclaredFields()) { if (f.isAnnotationPresent(ASN1Element.class)) { - elements.add(new ElementSpecification(f - .getAnnotation(ASN1Element.class), f)); + ASN1Element element = f.getAnnotation(ASN1Element.class); + if (element.value() < 0 && SEQUENCE.class.isAssignableFrom(type)) { + ASN1IllegalDefinition ex = new ASN1IllegalDefinition(); + ex.setMessage( + "Order of appearance of fields of a SEQUENCE type must be defined by @ASN1Element annotation.", + null, type, f.getName(), null); + throw ex; + } + elements.add(new ElementSpecification(element, f)); } } - Class parent = type.getSuperclass().asSubclass(ConstructiveType.class); ElementSpecification[] array; if (parent == SET.class || parent == SEQUENCE.class || parent.isAnnotationPresent(ASN1Extendable.class)) { @@ -77,7 +84,7 @@ public abstract class ConstructiveType extends StructuredType { if (TypeSpecification.getSpecification(type).tagDefault() == ASN1TagDefault.AUTOMATIC_TAGS) { generateAutomaticTags(array); } - if (parent == SET.class) { + if (SET.class.isAssignableFrom(type)) { new UnorderedElementsChecker(type).check(array); } else { new OrderedElementsChecker(type).check(array); diff --git a/jp/bitmeister/asn1/type/NamedTypeSpecification.java b/jp/bitmeister/asn1/type/NamedTypeSpecification.java index 77b3754..0f81215 100644 --- a/jp/bitmeister/asn1/type/NamedTypeSpecification.java +++ b/jp/bitmeister/asn1/type/NamedTypeSpecification.java @@ -198,6 +198,14 @@ public class NamedTypeSpecification implements * The appearing order of this element in the type definition. */ void generateAutomaticTag(int order) { + if (this.order < 0) { + ASN1IllegalDefinition ex = new ASN1IllegalDefinition(); + ex.setMessage( + "Order of appearance of the field must be specified by @ASN1Element" + + " or @ASN1Alternative annotation for automatic tagging process.", + null, enclosingType(), identifier, null); + throw ex; + } tag = new ASN1TagValue(order, type()); } diff --git a/jp/bitmeister/asn1/type/PrimitiveType.java b/jp/bitmeister/asn1/type/PrimitiveType.java index 87274fd..fde1637 100644 --- a/jp/bitmeister/asn1/type/PrimitiveType.java +++ b/jp/bitmeister/asn1/type/PrimitiveType.java @@ -20,7 +20,7 @@ package jp.bitmeister.asn1.type; * *

* This class provides common methods and a value field for classes which - * represents simple, unconstructive ASN.1 types. + * represent simple, unconstructive ASN.1 types. *

* * @author WATANABE, Jun. diff --git a/jp/bitmeister/asn1/type/builtin/BIT_STRING.java b/jp/bitmeister/asn1/type/builtin/BIT_STRING.java index 348f026..b6fde13 100644 --- a/jp/bitmeister/asn1/type/builtin/BIT_STRING.java +++ b/jp/bitmeister/asn1/type/builtin/BIT_STRING.java @@ -208,8 +208,11 @@ public class BIT_STRING extends PrimitiveType implements * Indexes of bits to be set true. */ public void set(int ...indexes) { - Arrays.sort(indexes); - expand(indexes[indexes.length - 1]); + int max = -1; + for (int e: indexes) { + max = Math.max(max, e); + } + expand(max); for (int e: indexes) { value()[e] = true; } @@ -243,8 +246,11 @@ public class BIT_STRING extends PrimitiveType implements * Indexes of bits to be set false. */ public void unset(int ...indexes) { - Arrays.sort(indexes); - expand(indexes[indexes.length - 1]); + int max = -1; + for (int e: indexes) { + max = Math.max(max, e); + } + expand(max); for (int e: indexes) { value()[e] = false; } @@ -296,7 +302,7 @@ public class BIT_STRING extends PrimitiveType implements } /** - * Returns the indentifier of the bit specified by the index number. + * Returns the identifier of the bit specified by the index number. * * @param index * The index number of a bit. diff --git a/jp/bitmeister/asn1/type/useful/EXTERNAL.java b/jp/bitmeister/asn1/type/useful/EXTERNAL.java index 4ac80e1..9bc9b66 100644 --- a/jp/bitmeister/asn1/type/useful/EXTERNAL.java +++ b/jp/bitmeister/asn1/type/useful/EXTERNAL.java @@ -48,7 +48,7 @@ import jp.bitmeister.asn1.type.builtin.SEQUENCE; * encoding CHOICE { * single-ASN1-type [0] ANY, * octet-aligned [1] IMPLICIT OCTET STRING, - * arbitary [2] IMPLICIT BIT STRING } } + * arbitrary [2] IMPLICIT BIT STRING } } * * *

@@ -111,11 +111,11 @@ public class EXTERNAL extends SEQUENCE { public OCTET_STRING octet_aligned; /** - * 'arbitary' field. + * 'arbitrary' field. */ @ASN1Alternative @ASN1Tag(2) - public BIT_STRING arbitary; + public BIT_STRING arbitrary; public Encoding() { super(); @@ -128,7 +128,7 @@ public class EXTERNAL extends SEQUENCE { } /** - * Instanciate an empty {@code ExternalType}. + * Instantiate an empty {@code ExternalType}. */ public EXTERNAL() { } diff --git a/jp/bitmeister/asn1/value/BinString.java b/jp/bitmeister/asn1/value/BinString.java index 7ff0ddf..1dc7264 100644 --- a/jp/bitmeister/asn1/value/BinString.java +++ b/jp/bitmeister/asn1/value/BinString.java @@ -65,7 +65,7 @@ public class BinString implements StringItem { /** * Returns the string value of this bString. * - * @return The hexadecimal string. + * @return The binary string that consists of '0's and '1's. */ public String string() { return string; -- 2.11.0