Allow using custom input and output streams in subclasses of YAMLProcessor.

This commit is contained in:
zml2008 2011-12-26 16:43:33 -08:00
parent 70205bfd11
commit 2d8a4a9f8a

View File

@ -25,11 +25,7 @@ import org.yaml.snakeyaml.constructor.SafeConstructor;
import org.yaml.snakeyaml.reader.UnicodeReader; import org.yaml.snakeyaml.reader.UnicodeReader;
import org.yaml.snakeyaml.representer.Representer; import org.yaml.snakeyaml.representer.Representer;
import java.io.File; import java.io.*;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@ -95,10 +91,11 @@ public class YAMLProcessor extends YAMLNode {
* @throws java.io.IOException * @throws java.io.IOException
*/ */
public void load() throws IOException { public void load() throws IOException {
FileInputStream stream = null; InputStream stream = null;
try { try {
stream = new FileInputStream(file); stream = getInputStream();
if (stream == null) throw new IOException("Stream is null!");
read(yaml.load(new UnicodeReader(stream))); read(yaml.load(new UnicodeReader(stream)));
} catch (YAMLProcessorException e) { } catch (YAMLProcessorException e) {
root = new HashMap<String, Object>(); root = new HashMap<String, Object>();
@ -158,7 +155,7 @@ public class YAMLProcessor extends YAMLNode {
* @return true if it was successful * @return true if it was successful
*/ */
public boolean save() { public boolean save() {
FileOutputStream stream = null; OutputStream stream = null;
File parent = file.getParentFile(); File parent = file.getParentFile();
@ -167,7 +164,8 @@ public class YAMLProcessor extends YAMLNode {
} }
try { try {
stream = new FileOutputStream(file); stream = getOutputStream();
if (stream == null) return false;
OutputStreamWriter writer = new OutputStreamWriter(stream, "UTF-8"); OutputStreamWriter writer = new OutputStreamWriter(stream, "UTF-8");
if (header != null) { if (header != null) {
writer.append(header); writer.append(header);
@ -200,6 +198,14 @@ public class YAMLProcessor extends YAMLNode {
} }
} }
public InputStream getInputStream() throws IOException {
return new FileInputStream(file);
}
public OutputStream getOutputStream() throws IOException {
return new FileOutputStream(file);
}
/** /**
* This method returns an empty ConfigurationNode for using as a * This method returns an empty ConfigurationNode for using as a
* default in methods that select a node from a node list. * default in methods that select a node from a node list.