FileDocCategorySizeDatePackage
Categorizer.javaAPI DocExample5657Wed Sep 19 09:52:16 BST 2001javaxml2

Categorizer

public class Categorizer extends Object

Categorizer uses JAXB to construct a catalog of tapes and videos, and organizes them by their categories.

Fields Summary
Constructors Summary
Methods Summary
public voidcategorize(java.io.File catalogFile)

This will load the catalog from the supplied XML file, break the catalog up by category, and write out new, categorized XML sub-catalogs.

param
catalogFile File to read catalog from.
throws
IOException - when file access errors occur.


        // Convert from XML to Java
        FileInputStream fis = new FileInputStream(catalogFile);
        Catalog catalog = new Catalog();
        try {
            catalog = Catalog.unmarshal(fis);
        } finally {
            fis.close();
        }

        // Create new catalogs for the different categories
        Catalog fingerpickingCatalog = new Catalog();
        Catalog flatpickingCatalog = new Catalog();
        Catalog mandolinCatalog = new Catalog();

        List items = catalog.getItem();
        for (Iterator i = items.iterator(); i.hasNext(); ) {
            Item item = (Item)i.next();
            String teacher = item.getTeacher();
            if ((teacher.equals("Doc Watson")) ||
                (teacher.equals("Steve Kaufman"))) {
                flatpickingCatalog.getItem().add(item);
            } else if (teacher.equals("David Wilcox")) {
                fingerpickingCatalog.getItem().add(item);
            } else if ((teacher.equals("Sam Bush")) ||
                       (teacher.equals("Chris Thile"))) {
                mandolinCatalog.getItem().add(item);
            }
        }

        // Write back out to XML
        FileOutputStream fingerOutput = 
            new FileOutputStream(new File("fingerpickingCatalog.xml"));
        FileOutputStream flatpickOutput = 
            new FileOutputStream(new File("flatpickingCatalog.xml"));
        FileOutputStream mandolinOutput = 
            new FileOutputStream(new File("mandolinCatalog.xml"));
        try {
            // Validate the catalogs
            fingerpickingCatalog.validate();
            flatpickingCatalog.validate();
            mandolinCatalog.validate();

            // Output the catalogs
            fingerpickingCatalog.marshal(fingerOutput);
            flatpickingCatalog.marshal(flatpickOutput);
            mandolinCatalog.marshal(mandolinOutput);
        } finally {
            fingerOutput.close();
            flatpickOutput.close();
            mandolinOutput.close();
        }
    
public static voidmain(java.lang.String[] args)

Provide a static entry point.

        try {
            if (args.length != 1) {
                System.out.println("Usage: java javaxml2.Categorizer " +
                    "[XML Catalog Filename]");
                return;
            }

            // Get access to XML catalog
            File catalogFile = new File(args[0]);
            Categorizer categorizer = new Categorizer();
            categorizer.categorize(catalogFile);
            
        } catch (Exception e) {
            e.printStackTrace();
        }