Do you have a sample application that illustrates step-by-step the use of the OSS ASN.1 Compiler to compile a simple ASN.1 module, and the use of the OSS Java API to invoke the encoder/decoder?

Step 1: Invoking the OSS ASN.1 Compiler

The following describes how to compile the abstract syntax defined in bcas.asn:

INPUT

-- Baseball Card Abstract Syntax (BCAS)
BCAS 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     
  }                
  myCard BBCard ::= { 
    name "Casey",  
    team "Mudville Nine", 
    age 32,        
    position "left field",
    handedness ambidextrous,
    batting-average {mantissa 250, base 10, exponent -3}   
  }                
END

COMMAND

For the OSS ASN.1/Java compiler, you enter the following command:

asn1pjav bcas.asn

or equivalently:

asn1pjav bcas

The above command informs the OSS ASN.1/Java Compiler that we want to convert the ASN.1 file bcas.asn to Java. This is implied by default when no command line option is specified.

The OSS ASN.1/Java Compiler will emit the following directories and Java classes:

c:\baseball\      // project package 
c:\baseball\Baseball.java         // project class 
c:\baseball\bcas\ // module package 
c:\baseball\bcas\BCAS.java        // module class 
c:\baseball\bcas\BBCard.java      // PDU class 
c:\baseball\baseball.bat          // builds emitted code

Note that baseball.bat is available on Windows, and that baseball.sh is available on Unix operating systems.

OUTPUT

The following project class provides an API for initializing and running the OSS ASN.1/Java Runtime with the BCAS specification. Some internal information has been omitted.

Baseball.java:

package baseball;  
import com.oss.asn1.*;   
import com.oss.metadata.*;  
public class Baseball extends ASN1Project {   
  ... 
  /**   
  * Methods for accessing Coders.   
  */   
  public static Coder getDefaultCoder();   
  // No coder specified   
  public static BERCoder getBERCoder();   
  public static DERCoder getDERCoder();   
}

BCAS.java:

The following is the module class for the ASN.1 module BCAS:

package baseball.bcas;  
import com.oss.asn1.*;   
import com.oss.metadata.*;   
import baseball.*; 
public abstract class BCAS extends ASN1Module {   
  ...   
  // Value references   
  public static final BBCard myCard =   
    new BBCard (   
      new com.oss.asn1.IA5String (   
      "Casey"   
    ),   
    new com.oss.asn1.IA5String (   
      "Mudville Nine"   
    ),   
    new com.oss.asn1.INTEGER(32),   
    new com.oss.asn1.IA5String (   
      "left field"   
    ),   
    BBCard.Handedness.ambidextrous,   
    new com.oss.asn1.Real(250.E-3)   
  );   
}

BBCard.java:

The following class represents the BBCard PDU. Accessor and mutator methods are generated for each component of the PDU.

package baseball.bcas;  
import com.oss.asn1.*;   
import com.oss.metadata.*;   
import baseball.*;  
/**   
* Define the ASN1 Type BBCard from ASN1 Module BCAS.   
* @see Sequence   
*/  
public class BBCard extends Sequence {   
/**   
* The default constructor.   
*/   
public BBCard()   
{   
}   
/**   
* Construct with AbstractData components.   
*/   
public BBCard(IA5String name, IA5String team, INTEGER age,   
IA5String position, Handedness handedness,   
Real batting_average)   
{  
setName(name);   
setTeam(team);   
setAge(age);   
setPosition(position);   
setHandedness(handedness);   
setBatting_average(batting_average); 
 }   
/**   
* Construct with components.   
*/   
public BBCard(IA5String name, IA5String team, long age,   
IA5String position, Handedness handedness,   
double batting_average)   
{   
this(name, team, new INTEGER(age), position, handedness,   
new Real(batting_average));   
}   
...   
// Methods for field "name"   
public IA5String getName()   
{   
return (IA5String)mComponents[0];   
}   
public void setName(IA5String name)   
{   
mComponents[0] = name;   
}   

// Methods for field "team"   
public IA5String getTeam()   
{   
return (IA5String)mComponents[1];   
}   
public void setTeam(IA5String team)   
{   
mComponents[1] = team;   
}   

// Methods for field "age"   
public long getAge()   
{   
return ((INTEGER)mComponents[2]).longValue();   
}   
public void setAge(long age)   
{   
setAge(new INTEGER(age));   
}   
public void setAge(INTEGER age)   
{   
mComponents[2] = age;   
}   

// Methods for field "position"   
public IA5String getPosition()   
{   
return (IA5String)mComponents[3];   
}   
public void setPosition(IA5String position)   
{   
mComponents[3] = position;   
}   


// Methods for field "handedness"   
public Handedness getHandedness()   
{   
return (Handedness)mComponents[4];   
}   
public void setHandedness(Handedness handedness)   
{   
mComponents[4] = handedness;   
}   

/**   
* Define the ASN1 Type Handedness from ASN1 Module BCAS.   
* @see Enumerated   
*/   
public static final class Handedness extends Enumerated {  
...   
// Named list definitions.   
public static final Handedness left_handed = new Handedness(0);   
public static final Handedness right_handed = new Handedness(1);   
public static final Handedness ambidextrous = new Handedness(2);   
...   
} // End class definition for Handedness  
// Methods for field "batting_average"   
public double getBatting_average()   
{   
return ((Real)mComponents[5]).doubleValue();   
}   
public void setBatting_average(double batting_average)   
{   
setBatting_average(new Real(batting_average));   
}   
public void setBatting_average(Real batting_average)   
{   
mComponents[5] = batting_average;   
}   
... 
} // End class definition for BBCard

Step 2: Compiling the sample application

The following is a copy of the Java program in Tbcas.java. This program creates two values (or instances) of class BBCard, compares the values, checks their constraints, encodes the PDU (using BER) described by the bcas.asn abstract syntax, and decodes it. Tracing information is printed as well as the values and fields of the values. Note that the baseball package (generated by the compiler) has been imported into the application program.

INPUT

Tbcas.java:

/* Compiler-generated classes */  
import baseball.*;  
import baseball.bcas.*; 
/* Universal classes from the OSS runtime library */  
import com.oss.asn1.*;  
import com.oss.util.*; 
/* Java I/O classes */  
import java.io.*; 
/**  
Application program Tbcas.java  
Demonstrates OSS ASN.1/Java Tools API using BCAS (Baseball Card  
Abstract Syntax).  
*/ 
public class Tbcas { 
  /**  
  * Constructor.  
  */  
  public Tbcas () {  
  } 
  public static void main(String args[]) { 
    // Initialize the project  
    try {  
      Baseball.initialize();  
    } catch (Exception e) {  
      System.out.println("Initialization exception: " + e);  
      System.exit(1);  
    } 
    // Create the value of BBCard using the constructor with arguments  
    System.out.println("Creating 'myCard' value, using the constructor  
      with arguments...");  
    BBCard myCard = new BBCard (  
      new IA5String (  
        "Casey"  
      ),  
      new IA5String (  
       "Mudville Nine"  
      ),  
      32,  
      new IA5String (  
        "left field"  
      ), 
      BBCard.Handedness.ambidextrous,  
      250.E-3);
    // Create another value of BBCard using the default constructor  
    // and mutator methods 
    System.out.println("Creating 'anotherCard' value, using the default  
      constructor and mutator methods ...");  
    BBCard anotherCard = new BBCard();  
    // Set the value of the 'name' field  
    anotherCard.setName(new IA5String("Casey"));  
    // Set the value of the 'team' field  
    anotherCard.setTeam(new IA5String("Mudville Nine"));  
    // Set the value of the 'age' field  
    anotherCard.setAge(32);  
    // Set the value of the 'position' field  
    anotherCard.setPosition(new IA5String("left field"));  
    // Set the value of the 'handedness' field  
    anotherCard.setHandedness(BBCard.Handedness.ambidextrous);  
    // Set the value of the 'batting-average' field  
    anotherCard.setBatting_average(250.E-3); 
    // Compare myCard and anotherCard for identity  
    System.out.println("Comparing 'myCard' and 'anotherCard' values ...");  
    if (myCard.equalTo(anotherCard))  
      System.out.println("The values are identical.");  
    else  
      System.out.println("The values are not identical.");
    // Check constraints for the 'myCard'  
    try {  
       final int success = 0;  
      System.out.println("Checking constraints for the 'myCard' ...");  
      Coder coder = Baseball.getBERCoder();  
      if (coder.validate(myCard) == success)   
        System.out.println("Constraint checking suceeded.");  
    } catch (ValidateFailedException e) {  
      System.out.println("Constraint checking failed: " + e);  
    } 
    // Encode the myCard value using BER  
    try {  
      Coder coder = Baseball.getBERCoder();  
      ByteArrayOutputStream sink = new ByteArrayOutputStream();
      // Enable trace output from the encoder and decoder  
      coder.enableEncoderDebugging();  
      coder.enableDecoderDebugging(); 
      // Print the input to the encoder  
      System.out.println("\nThe input to the encoder\n");  
      System.out.println(myCard); 
      // Encode a card  
      System.out.println("\nThe encoder's trace messages ...");  
      coder.encode(myCard, sink); 
       // Extract the encoding from the sink stream  
      byte[] encoding = sink.toByteArray(); 
      // Print the encoding using the HexTool utility  
       System.out.println("Card encoded into " + encoding.length + " bytes.");  
      HexTool.printHex(encoding); 
      try {  
        ByteArrayInputStream source = new ByteArrayInputStream(encoding); 
        // Decode the card whose encoding is in the 'encoding' byte array.  
        System.out.println("\nThe decoder's trace messages ...\n");  
        BBCard decodedCard = (BBCard)coder.decode(source, new BBCard());  
        System.out.println("Card decoded.");  
        // Print out the player's batting average  
        double batting_average = decodedCard.getBatting_average();  
        String name = decodedCard.getName().stringValue();  
        String team = decodedCard.getTeam().stringValue();  
        System.out.println(  
        name + " of the " + team + " has a batting average of " +  
        batting_average);  
        System.out.println("Output from decoder ...");  
        System.out.println(decodedCard);  
      } catch (DecodeFailedException e) {  
        System.out.println("Decoder exception: " + e);  
      } catch (DecodeNotSupportedException e) { 
        System.out.println("Decoder exception: " + e);  
      }  
    } catch (EncodeFailedException e) {  
      System.out.println("Encoder exception: " + e);  
    } catch (EncodeNotSupportedException e) {  
      System.out.println("Encoder exception: " + e);  
  } 
  // Do final cleanup  
  Baseball.deinitialize();  
}  
              }

COMMAND

Next, compile the sample application program using the baseball.bat file as follows:

           javac baseball

OUTPUT

The above command Java-compiles the compiler generated .java files. The resulting class files are generated in the baseball and baseball.bcas directories.


Step 3: Invoking the encoder/decoder

COMMAND

java baseball.Tbcas

This command simply runs the Tbcas class.

OUTPUT

Creating 'myCard' value, using the constructor with arguments...  
Creating 'anotherCard' value, using the default constructor and mutator methods ...  
Comparing 'myCard' and 'anotherCard' values ...  
The values are identical.  
Checking constraints for the 'myCard' ...  
Constraint checking suceeded.
The input to the encoder 
value BBCard ::= {  
  name "Casey",  
  team "Mudville Nine",  
  age 32,  
  position "left field",  
  handedness ambidextrous,  
  batting-average { mantissa 1, base 2, exponent -2 }  
}  
The encoder's trace messages ...  
BBCard SEQUENCE: tag = [UNIVERSAL 16] constructed; length = 45  
  name IA5String: tag = [UNIVERSAL 22] primitive; length = 5  
  "Casey"  
  team IA5String: tag = [UNIVERSAL 22] primitive; length = 13  
  "Mudville Nine"  
  age INTEGER: tag = [UNIVERSAL 2] primitive; length = 1  
  32  
  position IA5String: tag = [UNIVERSAL 22] primitive; length = 10  
  "left field"  
  handedness ENUMERATED: tag = [UNIVERSAL 10] primitive; length = 1  
  2  
  batting-average REAL: tag = [UNIVERSAL 9] primitive; length = 3  
  1 * 2^-2  
Card encoded into 47 bytes.  
  302D1605 43617365 79160D4D 75647669 6C6C6520 4E696E65 02012016 0A6C6566  
  74206669 656C640A 01020903 80FE01 
The decoder's trace messages ... 
BBCard SEQUENCE: tag = [UNIVERSAL 16] constructed; length = 45  
  name IA5String: tag = [UNIVERSAL 22] primitive; length = 5  
  "Casey"  
  team IA5String: tag = [UNIVERSAL 22] primitive; length = 13  
  "Mudville Nine"  
  age INTEGER: tag = [UNIVERSAL 2] primitive; length = 1  
  32  
  position IA5String: tag = [UNIVERSAL 22] primitive; length = 10  
  "left field"  
  handedness ENUMERATED: tag = [UNIVERSAL 10] primitive; length = 1  
  2  
  batting-average REAL: tag = [UNIVERSAL 9] primitive; length = 3  
  1 * 2^-2  
  Card decoded.  
  Casey of the Mudville Nine has a batting average of 0.25  
  Output from decoder ...  
  value BBCard ::= {  
  name "Casey",  
  team "Mudville Nine",  
  age 32,  
  position "left field",  
  handedness ambidextrous,  
  batting-average { mantissa 1, base 2, exponent -2 }
  }

The samples included with some of the Knowledge Center answers are meant for your general understanding of the OSS products. Different versions of the products might produce slightly different outputs. Consult the products documentation and samples for the most up-to-date products information and code examples.



Contact Support
contact Our office hours
24 hours/day, 7 days/week

  • Phone: 1-888-OSS-2761 (USA and Canada)
  • Phone: 1-732-302-9669 (International)
  • Fax: 1-732-302-0023
  • Email: support@oss.com
Free Trial
download

Test drive the OSS Nokalva ASN.1, LTE, and XML Tools now! Your trial includes complete software, documentation, sample programs, free 24x7 technical support and more.




Learn ASN.1
Learn ASN.1

Our expert personnel can help you learn ASN.1!

We offer 4-day ASN.1 courses at our headquarters or your premises.