package javajaxb;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
// Castor
import org.exolab.castor.mapping.Mapping;
import org.exolab.castor.mapping.MappingException;
import org.exolab.castor.xml.MarshalException;
import org.exolab.castor.xml.Marshaller;
import org.exolab.castor.xml.ValidationException;
// Generated hr.xml classes
import javajaxb.generated.hr.*;
public class EmployeeLister {
/** The descriptor to read in */
private File descriptor;
/** The output file to write to */
private File outputFile;
/** The converted output file */
private File convertFile;
/** The object tree read in */
private Employees employees;
/** Change this for your location! */
private static final String MAPPING_FILENAME =
"c:/dev/javajaxb/ch08/src/xml/mapping.xml";
/**
* <p>
* This takes in the descriptor to be processed.
* </p>
*
* @param descriptor the file for the hr.xml to process
* @param outputFile the file to write changes to.
* @param convertFile the file to write the conversion to.
*/
public EmployeeLister(File descriptor, File outputFile, File convertFile) {
employees = null;
this.descriptor = descriptor;
this.outputFile = outputFile;
this.convertFile = convertFile;
}
/**
* <p>
* This will list some basic information about the
* hr.xml deployment descriptor.
* </p>
*
* @param validate whether or not to validate the descriptor when processing
*/
public void list(boolean validate)
throws IOException, MarshalException, ValidationException {
// Unmarshall
employees = Employees.unmarshal(new FileReader(descriptor));
// Do some basic printing
System.out.println("--- Employee Listing ---\n");
Employee[] employeeList = employees.getEmployee();
for (int i=0; i<employeeList.length; i++) {
Employee employee = employeeList[i];
System.out.println("Employee: " + employee.getName());
System.out.println("Organization: " +
employee.getOrganization().getName());
System.out.println("Office: " +
employee.getOffice().getAddress().getCity() + ", " +
employee.getOffice().getAddress().getState() + "\n");
}
}
/**
* <p>
* This will make some simple changes to the descriptor and write it back
* out to a new file.
* </p>
*/
public void modify()
throws IOException, MarshalException, ValidationException {
// Add a new employee
Employee employee = new Employee();
employee.setName("Ben Rochester");
Address address = new Address();
address.setStreet1("708 Teakwood Drive");
address.setCity("Flower Mound");
address.setState("TX");
address.setZipCode("75028");
employee.addAddress(address);
Organization organization = new Organization();
organization.setId(43);
organization.setName("Technical Services");
employee.setOrganization(organization);
Office office = new Office();
office.setId(241);
Address officeAddress = new Address();
officeAddress.setStreet1("1202 Business Square");
officeAddress.setStreet2("Suite 302");
officeAddress.setCity("Dallas");
officeAddress.setState("TX");
officeAddress.setZipCode("75218-8921");
office.setAddress(officeAddress);
employee.setOffice(office);
// Add employee to list
employees.addEmployee(employee);
// marshal
employees.marshal(new FileWriter(outputFile));
}
/**
* <p>
* This will make some simple changes to the descriptor and write it back
* out to a new file, in a converted form.
* </p>
*/
public void convert()
throws IOException, MarshalException, ValidationException, MappingException {
// Load up the mapping information
Mapping mapping = new Mapping();
mapping.loadMapping(MAPPING_FILENAME);
// Marshall using that information
Marshaller marshaller = new Marshaller(new FileWriter(convertFile));
marshaller.setMapping(mapping);
marshaller.marshal(employees);
}
public static void main(String[] args) {
try {
if (args.length != 3) {
System.out.println("Usage: java javajaxb.EmployeeLister " +
"[web.xml filename] [output.xml filename] [convert.xml filename]");
return;
}
EmployeeLister lister =
new EmployeeLister(new File(args[0]), new File(args[1]), new File(args[2]));
lister.list(true);
lister.modify();
lister.convert();
} catch (Exception e) {
e.printStackTrace();
}
}
} |