FileDocCategorySizeDatePackage
PurchaseOrderViewer.javaAPI DocExample3992Fri Mar 01 11:57:40 GMT 2002javajaxb

PurchaseOrderViewer

public class PurchaseOrderViewer extends Object

Fields Summary
private File
inputFile
The descriptor to read in
private File
outputFile
The output file to write to
private com.jxml.quick.QDoc
purchaseOrderSchema
The QJML schema in Java format
private com.jxml.quick.QDoc
purchaseOrderDoc
The QDoc for the purchase order
private PurchaseOrder
purchaseOrder
The object tree
Constructors Summary
public PurchaseOrderViewer(File inputFile, File outputFile)

This takes in the descriptor to be processed.

param
inputFile the file for the po.xml to process
param
outputFile the file to write changes to.

        this.inputFile = inputFile;
        this.outputFile = outputFile;
    
Methods Summary
public static voidmain(java.lang.String[] args)

        try {
            if (args.length != 2) {
                System.out.println("Usage: java javajaxb.PurchaseOrderViewer " +
                    "[po.xml filename] [output.xml filename]");
                return;
            }
            
            PurchaseOrderViewer viewer = 
                new PurchaseOrderViewer(new File(args[0]), new File(args[1]));
            viewer.view(true);
            viewer.modify();
        } catch (Exception e) {
            e.printStackTrace();
        }
    
public voidmodify()

This will make some simple changes to the purchase order and write it back out to a new file.

        // Add a new order
        Order order = new Order(77, "990-7716-23");
        order.setManufacturerName("Toshiba");
        order.setProductName("PCX1100U Cable Modem");
        order.setPurchasePrice(new Float(87.98).floatValue());
        order.setStock(new Stock(30));
        
        // Add to overall order
        purchaseOrder.addOrder(order);

        // Remove the second item in the order
        purchaseOrder.getOrderList().remove(1);
        
        // Marshal to XML
        Quick.express(purchaseOrderDoc, outputFile.getAbsolutePath());
    
public voidview(boolean validate)

This will display some basic information about the supplied purchase order.

param
validate whether or not to validate the descriptor when processing

        // Instantiate the QIML class object
        purchaseOrderSchema = PurchaseOrderSchema.createSchema();
        
        // Unmarshal
        purchaseOrderDoc = Quick.parse(purchaseOrderSchema, inputFile.getAbsolutePath());
        
        // Get the object tree
        purchaseOrder = (PurchaseOrder)Quick.getRoot(purchaseOrderDoc);
        
        // Do some printing
        System.out.println("Purchase Order:");
        for (Iterator i = purchaseOrder.getOrderList().iterator(); i.hasNext(); ) {
            Order order = (Order)i.next();
            System.out.println(" * Order (ID=" + order.getId() + ", SKU=" +
                order.getSku() + "):");
            System.out.println("   + Product Name: " + order.getProductName());
            System.out.println("   + Manufacturer Name: " + 
                order.getManufacturerName());
            System.out.println("   + Purchase Price: $" + 
                order.getPurchasePrice());
            
            Stock stock = order.getStock();
            System.out.println("   + Quantity Ordered: " + stock.getQuantity());
            System.out.println("   + Order Total: $" + 
                (order.getPurchasePrice() * stock.getQuantity()));
        }
        System.out.println("Total Order Cost: $" + purchaseOrder.getTotalPrice());