view src/kryshen/tema/functions/IO.java @ 15:e9d13c7ffeb1

Update header comment.
author Mikhail Kryshen <mikhail@kryshen.net>
date Tue, 24 Mar 2009 18:51:47 +0300
parents 6c41a0b43e58
children fe2a094f4509
line source
1 /*
2 * Copyright 2006-2009 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 */
21 package kryshen.tema.functions;
23 import java.io.File;
24 import java.io.FileInputStream;
25 import java.io.FileOutputStream;
26 import java.io.IOException;
27 import java.io.InputStream;
28 import java.io.OutputStream;
29 import java.io.Reader;
30 import java.io.Writer;
31 import kryshen.tema.Function;
32 import kryshen.tema.FunctionDataParser;
33 import kryshen.tema.Tema;
34 import kryshen.tema.TemplateException;
35 import kryshen.tema.io.TemplateReader;
37 /**
38 * I/O functions.
39 */
40 public class IO {
41 /**
42 * Copy files.
43 */
44 public static final Function COPY = new Function() {
45 public int invoke(FunctionDataParser fdp, Writer out)
46 throws IOException, TemplateException {
48 String arg0 = fdp.getNextArg();
49 String arg1 = fdp.getNextArg();
51 File source = fdp.createFile(arg0);
52 File dest = fdp.createFile(arg1);
54 try {
55 copyFile(source, dest);
56 } catch (IOException e) {
57 System.err.println(e);
58 return 0;
59 }
60 out.write(arg1);
61 return 1;
62 }
63 };
65 /**
66 * Write data to file.
67 */
68 public static final Function WRITE = new Function() {
69 public int invoke(FunctionDataParser fdp, Writer out)
70 throws IOException, TemplateException {
72 String arg0 = fdp.getNextArg();
74 System.err.println("Writing " + arg0 + "...");
76 Writer fw;
78 try {
79 fw = Tema.createFileWriter(fdp.createFile(arg0));
80 } catch (IOException e) {
81 System.err.println(e);
82 //throw new TemplateException(e.getMessage(), e, in);
83 return 0;
84 }
86 try {
87 fdp.parseData(fw);
88 } finally {
89 fw.close();
90 }
92 out.write(arg0);
94 return 1;
95 }
96 };
98 /**
99 * Read from file.
100 */
101 public static final Function READ = new Function() {
102 public int invoke(FunctionDataParser fdp, Writer out)
103 throws IOException, TemplateException {
105 String filename = fdp.getData();
106 String file;
108 try {
109 readFile(fdp.createFile(filename), out);
110 } catch (IOException e) {
111 System.err.println(e);
112 //throw new TemplateException(e.getMessage(), e, in);
113 return 0;
114 }
116 return 1;
117 }
118 };
120 /**
121 * Evaluate the code read from file.
122 */
123 public static final Function INCLUDE = new Function() {
124 public int invoke(FunctionDataParser fdp, Writer out)
125 throws IOException, TemplateException {
127 String filename = fdp.getData();
129 TemplateReader fin;
131 try {
132 fin = Tema.createTemplateReader(fdp.createFile(filename));
133 } catch (IOException e) {
134 throw new TemplateException(e.getMessage(), e,
135 fdp.getTemplateReader());
136 }
138 int r = fdp.getTemplateParser().parse(fin, out);
140 fin.close();
141 return r;
142 }
143 };
145 /**
146 * Create path from base path and file name.
147 */
148 public static final Function FILE = new Function() {
149 public int invoke(FunctionDataParser fdp, Writer out)
150 throws IOException, TemplateException {
152 String base = fdp.getNextArg();
153 String name = fdp.getNextArg();
155 if (fdp.getLastReturnCode() == 0)
156 return 0;
158 out.write((new File(fdp.createFile(base), name)).getPath());
160 return fdp.getLastReturnCode();
161 }
162 };
164 /**
165 * Update file (copy if newer).
166 *
167 * @param src source file.
168 * @param dest destination file.
169 *
170 * @trows IOException on copying error.
171 */
172 private static void copyFile(File src, File dest) throws IOException {
173 System.err.print("Copying " + src + "... ");
175 if (src.lastModified() < dest.lastModified()) {
176 System.err.println(dest + " is up to date.");
177 return;
178 }
180 File parent = dest.getParentFile();
181 if (parent != null) parent.mkdirs();
183 InputStream in = new FileInputStream(src);
184 OutputStream out = new FileOutputStream(dest);
186 copy(in, out);
188 in.close();
189 out.close();
190 }
192 private static void copy(InputStream in, OutputStream out)
193 throws IOException {
195 byte[] buffer = new byte[1024];
196 int len;
198 while ((len = in.read(buffer)) > 0) {
199 out.write(buffer, 0, len);
200 }
201 }
203 private static void copy(Reader in, Writer out)
204 throws IOException {
206 char[] buffer = new char[1024];
207 int len;
209 while ((len = in.read(buffer)) > 0) {
210 out.write(buffer, 0, len);
211 }
212 }
214 private static void readFile(File src, Writer out)
215 throws IOException {
217 Reader in = Tema.createFileReader(src);
218 copy(in, out);
219 }
220 }