package net.sourceforge.javautil.common.xml.io;
import java.io.IOException;
import net.sourceforge.javautil.common.exception.ThrowableManagerRegistry;
import net.sourceforge.javautil.common.io.IVirtualFile;
import net.sourceforge.javautil.common.io.VirtualFileWrapped;
import net.sourceforge.javautil.common.xml.XMLDocument;
import net.sourceforge.javautil.common.xml.XMLDocumentSerializer;
/**
* This allows one to treat an {@link XMLDocument} as a {@link IVirtualFile}.
*
* @author elponderador
* @author $Author: ponderator $
* @version $Id: XMLDocumentFile.java 2297 2010-06-16 00:13:14Z ponderator $
*/
public class XMLDocumentFile<T> extends VirtualFileWrapped {
protected final Class<T> type;
public XMLDocumentFile(IVirtualFile delegate, Class<T> type) {
super(delegate);
this.type = type;
}
/**
* @return A new instance of the document represented by this file.
*/
public T readDocument () {
try {
return XMLDocument.read(this.getInputStream(), type);
} catch (IOException e) {
throw ThrowableManagerRegistry.caught(e);
}
}
/**
* Write a new document to replace this file's representation of the document.
*
* @param document The document to write.
*/
public void writeDocument (T document) {
try {
new XMLDocumentSerializer().serialize(XMLDocument.getInstance(document), this.getOutputStream());
} catch (IOException e) {
throw ThrowableManagerRegistry.caught(e);
}
}
}