/*
* $Id: ConfigWriter.java,v 1.3 2002/09/16 08:05:06 jkl Exp $
*
* Copyright (c) 2002 Njet Communications Ltd. All Rights Reserved.
*
* Use is subject to license terms, as defined in
* Anvil Sofware License, Version 1.1. See LICENSE
* file, or http://njet.org/license-1.1.txt
*/
package anvil.server;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.FileOutputStream;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.io.IOException;
/**
* class ConfigWriter
*
* @author: Jani Lehtim�ki
*/
public class ConfigWriter
{
public ConfigWriter()
{
}
public void write(File config, Configurable conf) throws IOException
{
FileOutputStream out = null;
try {
out = new FileOutputStream(config);
doWrite(new PrintStream(out), conf, new StringBuffer());
out.close();
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
}
}
}
}
public void write(PrintStream out, Configurable conf) throws IOException
{
doWrite(out, conf, new StringBuffer());
}
private void doWrite(PrintStream out, Configurable conf, StringBuffer fill)
throws IOException
{
out.print(fill);
out.print(Configurable.CLASSES[conf.getType()]);
out.println(":");
fill.append(" ");
String[] names = conf.getPreferences();
if (names != null) {
int n = names.length / 2;
for(int i=0; i<n; i++) {
String name = names[i*2];
Object obj = conf.getPreference(name);
if (obj != null) {
out.print(fill);
out.print(name);
out.print(" = ");
out.println(obj);
}
}
}
names = conf.getAdditionalPreferenceNames();
if (names != null) {
int n = names.length;
for(int i=0; i<n; i++) {
String name = names[i];
Object obj = conf.getPreference(name);
if (obj != null) {
out.print(fill);
out.print(name);
out.print(" = ");
out.println(obj);
}
}
}
Configurable[] children = conf.getConfigurations();
if (children != null) {
int n = children.length;
if (n>0) {
out.println();
for(int i=0; i<n; i++) {
doWrite(out, children[i], fill);
out.println();
}
}
}
fill.setLength(fill.length()-2);
out.print(fill);
out.println("end");
}
}