/*--
Copyright (C) 2001 Brett McLaughlin.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions, and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions, and the disclaimer that follows
these conditions in the documentation and/or other materials
provided with the distribution.
3. The name "Java and XML" must not be used to endorse or promote products
derived from this software without prior written permission. For
written permission, please contact brett@newInstance.com.
In addition, we request (but do not require) that you include in the
end-user documentation provided with the redistribution and/or in the
software itself an acknowledgement equivalent to the following:
"This product includes software developed for the
'Java and XML' book, by Brett McLaughlin (O'Reilly & Associates)."
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE JDOM AUTHORS OR THE PROJECT
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
*/
package javaxml2;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
// Zeus classes
import org.enhydra.zeus.Marshaller;
import org.enhydra.zeus.Unmarshaller;
import org.enhydra.zeus.source.StreamSource;
import org.enhydra.zeus.result.StreamResult;
// Zeus generated classes
import javaxml2.zeus.Catalog;
import javaxml2.zeus.Guest;
import javaxml2.zeus.GuestImpl;
import javaxml2.zeus.Item;
import javaxml2.zeus.ItemImpl;
/**
* <p>
* <code>CatalogViewer</code> uses Zeus data binding to read in an
* XML catalog file and show its contents, as well as adding a new
* item.
* </p>
*/
public class CatalogViewer {
/**
* <p>
* This will read in the catalog from the supplied XML file.
* </p>
*
* @param catgalogFile <code>File</code> to read catalog from.
* @throws <code>IOException</code> - when file access errors occur.
*/
public void view(File catalogFile) throws IOException {
FileReader reader = new FileReader(catalogFile);
StreamSource source = new StreamSource(reader);
// Convert from XML to Java
Unmarshaller unmarshaller = new Unmarshaller();
unmarshaller.setJavaPackage("javaxml2.zeus");
Catalog catalog = (Catalog)unmarshaller.unmarshal(source);
List items = catalog.getItemList();
for (Iterator i = items.iterator(); i.hasNext(); ) {
Item item = (Item)i.next();
String id = item.getId();
System.out.println("Item ID: " + id);
String title = item.getTitle().getValue();
System.out.println("Item Title: " + title);
// Modify an item
if (id.equals("CDZ-SM01")) {
item.getTitle().setValue("Sam Bush Teaches Mandolin " +
"Repertoire and Technique, 2nd edition");
Guest guest = new GuestImpl();
guest.setValue("Bela Fleck");
item.addGuest(guest);
}
}
// Write back out
FileWriter writer = new FileWriter(new File("newCatalog.xml"));
StreamResult result = new StreamResult(writer);
Marshaller marshaller = new Marshaller();
marshaller.marshal(catalog, result);
}
/**
* <p>Provide a static entry point.</p>
*/
public static void main(String[] args) {
try {
if (args.length != 1) {
System.out.println("Usage: java javaxml2.CatalogViewer " +
"[XML Catalog Filename]");
return;
}
// Get access to XML catalog
File catalogFile = new File(args[0]);
CatalogViewer viewer = new CatalogViewer();
viewer.view(catalogFile);
} catch (Exception e) {
e.printStackTrace();
}
}
} |