Sample Application

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/Pure Java compiler, you enter the following command: 

        asn1pjav bcas.asn
or equivalently: 
        asn1pjav bcas
The above command informs the OSS ASN.1/Pure 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 emitted on Windows 98/NT, and that baseball.sh is emitted 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)  
    {  

    }  
  
    /**  
     * 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 {  
} // End class definition for BBCard  
 

Step 2: Compiling the sample application  

The following is a copy of the Java program in Tbcas.java. It works with both the Native and the Pure Java versions. 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  

Java File, 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: 

baseball javac
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 }
}


 

Copyright © 2010 OSS Nokalva, Inc.   All Rights Reserved.