This example illustrates the differences between using RFML and using the IBM® Toolbox for Java™ Record classes.
Using the traditional Record classes, you interweave the data format specifications with the business logic of your application. Adding, changing, or deleting a field means that you must edit and recompile your Java code. However, using RFML isolates the data format specifications into RFML source files that are entirely separate from the business logic. Accommodating field changes means modifying the RFML file, often without having to change or recompile your Java application.
The example assumes that your application deals with customer records, which you have defined in an RFML source file and named qcustcdt.rfml. The source file represents the fields that compose each customer record.
The listing below shows how a Java application might interpret a customer record using the IBM Toolbox for Java Record, RecordFormat, and FieldDescription classes:
// Buffer containing the binary representation of one record of information. byte[] bytes; // ... Read the record data into the buffer ... // Set up a RecordFormat object to represent one customer record. RecordFormat recFmt1 = new RecordFormat("cusrec"); recFmt1.addFieldDescription(new ZonedDecimalFieldDescription(new AS400ZonedDecimal(6, 0), "cusnum")); recFmt1.addFieldDescription(new CharacterFieldDescription(new AS400Text(8, 37), "lstnam")); recFmt1.addFieldDescription(new CharacterFieldDescription(new AS400Text(3, 37), "init")); recFmt1.addFieldDescription(new CharacterFieldDescription(new AS400Text(13, 37), "street")); recFmt1.addFieldDescription(new CharacterFieldDescription(new AS400Text(6, 37), "city")); recFmt1.addFieldDescription(new CharacterFieldDescription(new AS400Text(2, 37), "state")); recFmt1.addFieldDescription(new ZonedDecimalFieldDescription(new AS400ZonedDecimal(5, 0), "zipcod")); recFmt1.addFieldDescription(new ZonedDecimalFieldDescription(new AS400ZonedDecimal(4, 0), "cdtlmt")); recFmt1.addFieldDescription(new ZonedDecimalFieldDescription(new AS400ZonedDecimal(1, 0), "chgcod")); recFmt1.addFieldDescription(new ZonedDecimalFieldDescription(new AS400ZonedDecimal(6, 2), "baldue")); recFmt1.addFieldDescription(new ZonedDecimalFieldDescription(new AS400ZonedDecimal(6, 2), "cdtdue")); // Read the byte buffer into the RecordFormatDocument object. Record rec1 = new Record(recFmt1, bytes); // Get the field values. System.out.println("cusnum: " + rec1.getField("cusnum")); System.out.println("lstnam: " + rec1.getField("lstnam")); System.out.println("init: " + rec1.getField("init")); System.out.println("street: " + rec1.getField("street")); System.out.println("city: " + rec1.getField("city")); System.out.println("state: " + rec1.getField("state")); System.out.println("zipcod: " + rec1.getField("zipcod")); System.out.println("cdtlmt: " + rec1.getField("cdtlmt")); System.out.println("chgcod: " + rec1.getField("chgcod")); System.out.println("baldue: " + rec1.getField("baldue")); System.out.println("cdtdue: " + rec1.getField("cdtdue"));
By comparison, here is how the same record might be interpreted using RFML.
The Java code to interpret the contents of the customer data record using RFML might look like this:
// Buffer containing the binary representation of one record of information.
byte[] bytes;
// ... Read the record data into the buffer ...
// Parse the RFML file into a RecordFormatDocument object.
// The RFML source file is called qcustcdt.rfml.
RecordFormatDocument rfml1 = new RecordFormatDocument("qcustcdt");
// Read the byte buffer into the RecordFormatDocument object.
rfml1.setValues("cusrec", bytes);
// Get the field values.
System.out.println("cusnum: " + rfml1.getValue("cusrec.cusnum"));
System.out.println("lstnam: " + rfml1.getValue("cusrec.lstnam"));
System.out.println("init: " + rfml1.getValue("cusrec.init"));
System.out.println("street: " + rfml1.getValue("cusrec.street"));
System.out.println("city: " + rfml1.getValue("cusrec.city"));
System.out.println("state: " + rfml1.getValue("cusrec.state"));
System.out.println("zipcod: " + rfml1.getValue("cusrec.zipcod"));
System.out.println("cdtlmt: " + rfml1.getValue("cusrec.cdtlmt"));
System.out.println("chgcod: " + rfml1.getValue("cusrec.chgcod"));
System.out.println("baldue: " + rfml1.getValue("cusrec.baldue"));
System.out.println("cdtdue: " + rfml1.getValue("cusrec.cdtdue"));