01: /*
02: * @(#)saveProperties.java 1.1 05/01/15
03: *
04: * Copyright (c) 2001-2005 Sun Microsystems, Inc. All Rights Reserved.
05: *
06: * See the file "LICENSE.txt" for information on usage and redistribution
07: * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
08: */
09: package org.pnuts.util;
10:
11: import pnuts.lang.Pnuts;
12: import pnuts.lang.PnutsException;
13: import pnuts.lang.PnutsFunction;
14: import pnuts.lang.Context;
15: import java.net.URL;
16: import java.util.Properties;
17: import java.io.OutputStream;
18: import java.io.File;
19: import java.io.FileOutputStream;
20: import java.io.IOException;
21: import java.io.FileNotFoundException;
22: import org.pnuts.lib.PathHelper;
23:
24: /*
25: * function loadProperties(input)
26: */
27: public class saveProperties extends PnutsFunction {
28:
29: public saveProperties() {
30: super ("saveProperties");
31: }
32:
33: public boolean defined(int nargs) {
34: return nargs == 2;
35: }
36:
37: protected Object exec(Object[] args, Context context) {
38: int nargs = args.length;
39: Properties prop;
40: Object dest;
41: if (nargs == 2) {
42: prop = (Properties) args[0];
43: dest = args[1];
44: } else {
45: undefined(args, context);
46: return null;
47: }
48: OutputStream out = null;
49: try {
50: File destFile = null;
51: if (dest instanceof String) {
52: destFile = PathHelper.getFile((String) dest, context);
53: FileOutputStream fout = null;
54: try {
55: fout = new FileOutputStream(destFile);
56: prop.store(fout, "");
57: } finally {
58: if (fout != null) {
59: fout.close();
60: }
61: }
62: } else if (dest instanceof File) {
63: destFile = (File) dest;
64: FileOutputStream fout = null;
65: try {
66: fout = new FileOutputStream(destFile);
67: prop.store(fout, "");
68: } finally {
69: if (fout != null) {
70: fout.close();
71: }
72: }
73: } else if (dest instanceof OutputStream) {
74: prop.store((OutputStream) dest, "");
75: }
76: return null;
77: } catch (IOException e) {
78: throw new PnutsException(e, context);
79: }
80: }
81:
82: public String toString() {
83: return "function saveProperties(properties, String|File|OutputStream)";
84: }
85: }
|