view src/kryshen/tema/functions/IO.java @ 2:6c41a0b43e58

Tema 0.3 (imported from CVS).
author Mikhail Kryshen <mikhail@kryshen.net>
date Tue, 19 Feb 2008 20:32:17 +0300
parents 548a93c24e55
children e9d13c7ffeb1
line source
1 /*
2 * Copyright 2006-2008 Mikhail Kryshen
3 *
4 * This file is part of Tema.
5 *
6 * Tema is free software: you can redistribute it and/or modify it
7 * under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation, either version 3 of the
9 * License, or (at your option) any later version.
10 *
11 * Tema is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the
17 * GNU Lesser General Public License along with Tema.
18 * If not, see <http://www.gnu.org/licenses/>.
19 *
20 * $Id: IO.java,v 1.19 2008/02/19 16:21:00 mikhail Exp $
21 */
23 package kryshen.tema.functions;
25 import java.io.File;
26 import java.io.FileInputStream;
27 import java.io.FileOutputStream;
28 import java.io.IOException;
29 import java.io.InputStream;
30 import java.io.OutputStream;
31 import java.io.Reader;
32 import java.io.Writer;
33 import kryshen.tema.Function;
34 import kryshen.tema.FunctionDataParser;
35 import kryshen.tema.Tema;
36 import kryshen.tema.TemplateException;
37 import kryshen.tema.io.TemplateReader;
39 /**
40 * I/O functions.
41 */
42 public class IO {
43 /**
44 * Copy files.
45 */
46 public static final Function COPY = new Function() {
47 public int invoke(FunctionDataParser fdp, Writer out)
48 throws IOException, TemplateException {
50 String arg0 = fdp.getNextArg();
51 String arg1 = fdp.getNextArg();
53 File source = fdp.createFile(arg0);
54 File dest = fdp.createFile(arg1);
56 try {
57 copyFile(source, dest);
58 } catch (IOException e) {
59 System.err.println(e);
60 return 0;
61 }
62 out.write(arg1);
63 return 1;
64 }
65 };
67 /**
68 * Write data to file.
69 */
70 public static final Function WRITE = new Function() {
71 public int invoke(FunctionDataParser fdp, Writer out)
72 throws IOException, TemplateException {
74 String arg0 = fdp.getNextArg();
76 System.err.println("Writing " + arg0 + "...");
78 Writer fw;
80 try {
81 fw = Tema.createFileWriter(fdp.createFile(arg0));
82 } catch (IOException e) {
83 System.err.println(e);
84 //throw new TemplateException(e.getMessage(), e, in);
85 return 0;
86 }
88 try {
89 fdp.parseData(fw);
90 } finally {
91 fw.close();
92 }
94 out.write(arg0);
96 return 1;
97 }
98 };
100 /**
101 * Read from file.
102 */
103 public static final Function READ = new Function() {
104 public int invoke(FunctionDataParser fdp, Writer out)
105 throws IOException, TemplateException {
107 String filename = fdp.getData();
108 String file;
110 try {
111 readFile(fdp.createFile(filename), out);
112 } catch (IOException e) {
113 System.err.println(e);
114 //throw new TemplateException(e.getMessage(), e, in);
115 return 0;
116 }
118 return 1;
119 }
120 };
122 /**
123 * Evaluate the code read from file.
124 */
125 public static final Function INCLUDE = new Function() {
126 public int invoke(FunctionDataParser fdp, Writer out)
127 throws IOException, TemplateException {
129 String filename = fdp.getData();
131 TemplateReader fin;
133 try {
134 fin = Tema.createTemplateReader(fdp.createFile(filename));
135 } catch (IOException e) {
136 throw new TemplateException(e.getMessage(), e,
137 fdp.getTemplateReader());
138 }
140 int r = fdp.getTemplateParser().parse(fin, out);
142 fin.close();
143 return r;
144 }
145 };
147 /**
148 * Create path from base path and file name.
149 */
150 public static final Function FILE = new Function() {
151 public int invoke(FunctionDataParser fdp, Writer out)
152 throws IOException, TemplateException {
154 String base = fdp.getNextArg();
155 String name = fdp.getNextArg();
157 if (fdp.getLastReturnCode() == 0)
158 return 0;
160 out.write((new File(fdp.createFile(base), name)).getPath());
162 return fdp.getLastReturnCode();
163 }
164 };
166 /**
167 * Update file (copy if newer).
168 *
169 * @param src source file.
170 * @param dest destination file.
171 *
172 * @trows IOException on copying error.
173 */
174 private static void copyFile(File src, File dest) throws IOException {
175 System.err.print("Copying " + src + "... ");
177 if (src.lastModified() < dest.lastModified()) {
178 System.err.println(dest + " is up to date.");
179 return;
180 }
182 File parent = dest.getParentFile();
183 if (parent != null) parent.mkdirs();
185 InputStream in = new FileInputStream(src);
186 OutputStream out = new FileOutputStream(dest);
188 copy(in, out);
190 in.close();
191 out.close();
192 }
194 private static void copy(InputStream in, OutputStream out)
195 throws IOException {
197 byte[] buffer = new byte[1024];
198 int len;
200 while ((len = in.read(buffer)) > 0) {
201 out.write(buffer, 0, len);
202 }
203 }
205 private static void copy(Reader in, Writer out)
206 throws IOException {
208 char[] buffer = new char[1024];
209 int len;
211 while ((len = in.read(buffer)) > 0) {
212 out.write(buffer, 0, len);
213 }
214 }
216 private static void readFile(File src, Writer out)
217 throws IOException {
219 Reader in = Tema.createFileReader(src);
220 copy(in, out);
221 }
222 }