package javajaxb;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
// Quick classes
import com.jxml.quick.QDoc;
import com.jxml.quick.Quick;
// SAX classes
import org.xml.sax.SAXException;
// The PO user-defined classes
import javajaxb.po.*;
public class PurchaseOrderViewer {
/** The descriptor to read in */
private File inputFile;
/** The output file to write to */
private File outputFile;
/** The QJML schema in Java format */
private QDoc purchaseOrderSchema;
/** The QDoc for the purchase order */
private QDoc purchaseOrderDoc;
/** The object tree */
private PurchaseOrder purchaseOrder;
/**
* <p>
* This takes in the descriptor to be processed.
* </p>
*
* @param inputFile the file for the po.xml to process
* @param outputFile the file to write changes to.
*/
public PurchaseOrderViewer(File inputFile, File outputFile) {
this.inputFile = inputFile;
this.outputFile = outputFile;
}
/**
* <p>
* This will display some basic information about the
* supplied purchase order.
* </p>
*
* @param validate whether or not to validate the descriptor when processing
*/
public void view(boolean validate) throws Exception {
// 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());
}
/**
* <p>
* This will make some simple changes to the purchase order and write it back
* out to a new file.
* </p>
*/
public void modify() throws Exception {
// 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 static void main(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();
}
}
} |