initial commit for a new layout.

git-svn-id: https://hudson.dev.java.net/svn/hudson/trunk/hudson/main@969 71c3de6d-444a-0410-be80-ed276b4c234a
This commit is contained in:
kohsuke committed 2006-11-05 21:16:01 +00:00
commit 8a0dc230f4
594 files changed
+48337

No files matched your search

+97
View File
@@ -0,0 +1,97 @@
package hudson;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.converters.ConversionException;
import com.thoughtworks.xstream.io.StreamException;
import com.thoughtworks.xstream.io.xml.XppReader;
import hudson.util.AtomicFileWriter;
import hudson.util.IOException2;
import hudson.util.XStream2;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
/**
* Represents an XML data file that Hudson uses as a data file.
*
* @author Kohsuke Kawaguchi
*/
public final class XmlFile {
private final XStream xs;
private final File file;
public XmlFile(File file) {
this(DEFAULT_XSTREAM,file);
}
public XmlFile(XStream xs, File file) {
this.xs = xs;
this.file = file;
}
/**
* Loads the contents of this file into a new object.
*/
public Object read() throws IOException {
Reader r = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
try {
return xs.fromXML(r);
} catch(StreamException e) {
throw new IOException2("Unable to read "+file,e);
} catch(ConversionException e) {
throw new IOException2("Unable to read "+file,e);
} finally {
r.close();
}
}
/**
* Loads the contents of this file into an existing object.
*/
public void unmarshal( Object o ) throws IOException {
Reader r = new BufferedReader(new InputStreamReader(new FileInputStream(file),"UTF-8"));
try {
xs.unmarshal(new XppReader(r),o);
} catch (StreamException e) {
throw new IOException2(e);
} catch(ConversionException e) {
throw new IOException2("Unable to read "+file,e);
} finally {
r.close();
}
}
public void write( Object o ) throws IOException {
AtomicFileWriter w = new AtomicFileWriter(file);
try {
w.write("<?xml version='1.0' encoding='UTF-8'?>\n");
xs.toXML(o,w);
w.commit();
} catch(StreamException e) {
throw new IOException2(e);
} finally {
w.close();
}
}
public boolean exists() {
return file.exists();
}
public void mkdirs() {
file.getParentFile().mkdirs();
}
public String toString() {
return file.toString();
}
/**
* {@link XStream} instance is supposed to be thread-safe.
*/
private static final XStream DEFAULT_XSTREAM = new XStream2();
}