TOP

ASN.1/Java Runtime Value Parsing

Applies to: ASN.1/Java v8.7

The ASN.1/Java Tools can parse ASN.1 value notation data into Java objects that represent values defined by value notation.

To parse values specified in ASN.1 value notation format:

  1. Specify the -avn compiler option to the ASN.1/Java compiler when compiling your ASN.1/Java project. The compiler will generate additional methods that are used to parse ASN.1 values. The value parsing methods reside in Java classes that represent ASN.1 types that are found in the input specification.
  2. Instantiate the value notation parser object using the getValueNotationReader() method of the project class:
    ValueNotationReader parser = ProjectName.getValueNotationReader();
  3. Invoke the readValue() or readValueAssignment() method of the ValueNotationReader class to parse the value.

Example

The Bcas.asn schema file is used in this example.

BCASModule DEFINITIONS ::= BEGIN

BBCard ::= SEQUENCE {
    name IA5String (SIZE (1..60)),
    team IA5String (SIZE (1..60)),
    age INTEGER (1..100),
    position IA5String (SIZE (1..60)),
    handedness ENUMERATED
        {left-handed(0), right-handed(1), ambidextrous(2)},
    batting-average REAL
}

END

The following source code shows the sample Java application that parses the BBCard ASN.1 type values:

package bcas;

import com.oss.asn1.AVNParseException;
import com.oss.asn1.AbstractData;
import com.oss.asn1.AVNInput;
import com.oss.asn1.ValueNotationReader;
import java.io.StringReader;


public class Test
{
      public static void main(String[] args) throws AVNParseException
      {
      // The value notation for the value of the BBCard type
      String myCard =
      "{\n"
      + "   name \"Casey\",\n"
      + "   team \"Mudville Nine\","
      + "   age 32,\n"
      + "   position \"left field\",\n"
      + "   handedness ambidextrous,\n"
      + "   batting-average {mantissa 250, base 10, exponent -3}\n"
      + "}\n";

      // Instantiate the ValueNotationReader
      ValueNotationReader parser = Bcas.getValueNotationReader();

      // Parse the value notation contained in the myCard string
      AbstractData myCardPdu1 = parser.readValue(myCard,
            new bcas.bcasmodule.BBCard());

      // Print the object parsed from the value notation in the
      // myCard string
      System.out.println("myCardPdu1 " + myCardPdu1);

      // Create the string that contains the ASN.1 value assignment for the
      // value of BBCard
      String myCardAssignment = "myCard BBCard ::= " + myCard;

      // Parse the value assignment contained in the myCardAssignment
      // string. The parser will detect the type of the value from the
      // ASN.1 type name specified in the value assignment (BBCard).
      AbstractData myCardPdu2 =
           parser.readValueAssignment(myCardAssignment);

      // Print the C# object parsed from the value assignment in the
      // myCardAssignment string
      System.out.println("myCardPdu2 " + myCardPdu2);

      // Parse the input that contains multiple values of the BBCard type
      String myCards =
      "-- first value\n"
      + "{\n"
      + "   name \"Casey\",\n"
      + "   team \"Mudville Nine\","
      + "   age 32,\n"
      + "   position \"left field\",\n"
      + "handedness ambidextrous,\n"
      + "batting-average {mantissa 250, base 10, exponent -3}\n"
      + "}\n"
      + "-- second value\n"    	 
      + "{\n"
      + "   name \"Doug\",\n"
      + "team \"Octopus Eight\","
      + "age 27,\n"
      + "position \"right field\",\n"
      + "handedness right-handed,\n"
      + "batting-average {mantissa 250, base 10, exponent -3}\n"
      + "}\n";
      {
        AbstractData myCardPdu;
        AVNInput reader = new AVNInput(new StringReader(myCards));
        // Parse all the values contained in the myCards
        while ((myCardPdu = parser.readValue(reader, new bcas.bcasmodule.BBCard())) != null)
            System.out.println("myCardPdu " + myCardPdu);
      }
   }
}

Remarks

The following methods automatically determine the value type from the ASN.1 type name specified in the ASN.1 value assignment:

  • public AbstractData readValueAssignment(String notation);
  • public AbstractData readValueAssignment(AVNInput input);

The following generic methods allow you to specify the value type explicitly. The value type is identified by the "T extends AbstractData" parameter, which is an empty instance of the Java class that represents the value type:

  • public <T extends AbstractData> T readValue(String notation, T sink);
  • public <T extends AbstractData> T readValue(AVNInput input, T sink);

Implementation Restrictions

  • The values of arcs present in OBJECT IDENTIFIER and RELATIVE-OID type values are limited to a range of 64-bit signed numbers.
  • In the notation that is used for open type values
    Type ":" Value
    only the "typereference" alternative of the "Type" production is supported. Also, the type specified by "typereference" must be a PDU.
  • ASN.1:1990 schemas that include a SEQUENCE, SET, or CHOICE type definition from which component names are omitted are not supported.

NOTE: The ASN.1 value notation parser is a chargeable feature in non-evaluation licenses. Contact Sales to obtain pricing information.


This documentation applies to the OSS® ASN.1 Tools for Java release 8.7 and later.

Copyright © 2024 OSS Nokalva, Inc. All rights reserved.
No part of this publication may be reproduced, stored in a retrieval system, or transmitted in any form or by any means electronic, mechanical, photocopying, recording or otherwise, without the prior permission of OSS Nokalva, Inc.
Every distributed copy of the OSS® ASN.1 Tools for Java is associated with a specific license and related unique license number. That license determines, among other things, what functions of the OSS ASN.1 Tools for Java are available to you.