/**
* Project Wonderland
*
* Copyright (c) 2004-2009, Sun Microsystems, Inc., All Rights Reserved
*
* Redistributions in source code form must reproduce the above
* copyright and this condition.
*
* The contents of this file are subject to the GNU General Public
* License, Version 2 (the "License"); you may not use this file
* except in compliance with the License. A copy of the License is
* available at http://www.opensource.org/licenses/gpl-license.php.
*
* Sun designates this particular file as subject to the "Classpath"
* exception as provided by Sun in the License file that accompanied
* this code.
*/
package org.jdesktop.wonderland.common.wfs;
import java.io.Reader;
import java.io.Writer;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;
/**
* Represents a wfs world 'root', the base directory of a wfs, whether it is
* a world definition or a snapshot (backup) of the world. Using this, the
* web services knows which wfs to write to or read from.
*
* @author Jordan Slott <jslott@dev.java.net>
*/
@XmlRootElement(name="wfs-world-root")
public class WorldRoot {
/*
* The name of the root, with respect to some directory root defined by
* the web service. This can be something like "worlds/default-wfs" or
* "snapshots/<date>/world-wfs".
*/
@XmlElement(name="root-path")
private String rootPath = null;
private static JAXBContext jaxbContext = null;
static {
try {
jaxbContext = JAXBContext.newInstance(WorldRoot.class);
} catch (javax.xml.bind.JAXBException excp) {
Logger.getLogger(WorldRoot.class.getName()).log(Level.WARNING,
"Unable to create JAXBContext", excp);
}
}
/** Default constructor */
public WorldRoot() {
}
/** Constructor, takes the root path */
public WorldRoot(String rootPath) {
this.rootPath = rootPath;
}
@XmlTransient public String getRootPath() { return this.rootPath; }
/**
* Takes a reader for the XML stream and returns an instance of this class
* <p>
* @param r The reader of the XML stream
* @throw ClassCastException If the input file does not map to this class
* @throw JAXBException Upon error reading the XML stream
*/
public static WorldRoot decode(Reader r) throws JAXBException {
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
return (WorldRoot)unmarshaller.unmarshal(r);
}
/**
* Writes the XML representation of this class to a writer.
* <p>
* @param w The output writer to write to
* @throw JAXBException Upon error writing the XML file
*/
public void encode(Writer w) throws JAXBException {
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.setProperty("jaxb.formatted.output", true);
marshaller.marshal(this, w);
}
@Override
public String toString() {
return this.rootPath;
}
}