view src/kryshen/tema/functions/IO.java @ 1:548a93c24e55

Tema 0.1jk - Javakonkurs edition (imported from CVS).
author Mikhail Kryshen <mikhail@kryshen.net>
date Thu, 14 Dec 2006 23:22:05 +0300
parents
children 6c41a0b43e58
line source
1 /*
2 * Copyright (C) 2005, 2006 Mikhail A. Kryshen
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 * $Id: IO.java,v 1.4 2006/12/14 15:59:49 mikhail Exp $
19 */
21 package kryshen.tema.functions;
23 import java.io.*;
24 import java.util.*;
26 import kryshen.tema.*;
28 /**
29 * I/O functions.
30 */
31 public class IO {
32 public static final Function COPY =
33 new Function() {
34 public int invoke(FunctionDataParser fdp, Writer out)
35 throws IOException, TemplateException {
37 String arg0 = fdp.getNextArg();
38 String arg1 = fdp.getNextArg();
40 File source = new File
41 (Tema.getProperty("resource_base"), arg0);
42 File dest = new File(arg1);
44 try {
45 copyFile(source, dest);
46 } catch (IOException e) {
47 System.err.println(e);
48 return 0;
49 }
50 out.write(arg1);
51 return 1;
52 }
53 };
55 public static final Function WRITE =
56 new Function() {
57 public int invoke(FunctionDataParser fdp, Writer out)
58 throws IOException, TemplateException {
60 String arg0 = fdp.getNextArg();
62 System.err.println("Writing " + arg0 + "...");
64 Writer fw;
66 try {
67 fw = Tema.createFileWriter(arg0);
68 } catch (IOException e) {
69 System.err.println(e);
70 //throw new TemplateException(e.getMessage(), e, in);
71 return 0;
72 }
74 try {
75 fdp.parseData(fw);
76 } finally {
77 fw.close();
78 }
80 out.write(arg0);
82 System.err.println("Saved " + arg0 + ".");
83 return 1;
84 }
85 };
87 public static final Function READ =
88 new Function() {
89 public int invoke(FunctionDataParser fdp, Writer out)
90 throws IOException, TemplateException {
92 String filename = fdp.getData();
93 String file;
95 try {
96 file = Tema.readFile(new File(filename));
97 } catch (IOException e) {
98 System.err.println(e);
99 //throw new TemplateException(e.getMessage(), e, in);
100 return 0;
101 }
103 out.write(file);
104 return 1;
105 }
106 };
108 public static final Function INCLUDE =
109 new Function() {
110 public int invoke(FunctionDataParser fdp, Writer out)
111 throws IOException, TemplateException {
113 String filename = fdp.getData();
115 TemplateReader fin;
117 try {
118 fin = Tema.createTemplateReader(new File(filename));
119 } catch (IOException e) {
120 throw new TemplateException(e.getMessage(), e,
121 fdp.getTemplateReader());
122 }
124 int r = fdp.getTemplateParser().parse(fin, out);
126 fin.close();
127 return r;
128 }
129 };
131 /**
132 * Update file (copy if newer).
133 *
134 * @param src source file.
135 * @param dest destination file.
136 *
137 * @trows IOException on copying error.
138 */
139 private static void copyFile(File src, File dest) throws IOException {
140 System.err.print("Copying " + src + "... ");
142 if (src.lastModified() < dest.lastModified()) {
143 System.err.println(dest + " is up to date.");
144 return;
145 }
147 File parent = dest.getParentFile();
148 if (parent != null) parent.mkdirs();
150 InputStream in = new FileInputStream(src);
151 OutputStream out = new FileOutputStream(dest);
153 byte[] buffer = new byte[1024];
154 int len;
155 while ((len = in.read(buffer)) > 0) {
156 out.write(buffer, 0, len);
157 }
158 in.close();
159 out.close();
161 System.err.println("saved " + dest + ".");
162 }
163 }