view src/kryshen/tema/functions/IO.java @ 20:fe2a094f4509

Fix warning messages.
author Mikhail Kryshen <mikhail@kryshen.net>
date Tue, 21 Apr 2009 20:49:53 +0400
parents e9d13c7ffeb1
children 55fe63bb7858
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 fdp.warning(e.getMessage());
112 //System.err.println(e);
113 //throw new TemplateException(e.getMessage(), e, in);
114 return 0;
115 }
117 return 1;
118 }
119 };
121 /**
122 * Evaluate the code read from file.
123 */
124 public static final Function INCLUDE = new Function() {
125 public int invoke(FunctionDataParser fdp, Writer out)
126 throws IOException, TemplateException {
128 String filename = fdp.getData();
130 TemplateReader fin;
132 try {
133 fin = Tema.createTemplateReader(fdp.createFile(filename));
134 } catch (IOException e) {
135 throw new TemplateException(e.getMessage(), e,
136 fdp.getTemplateReader());
137 }
139 int r = fdp.getTemplateParser().parse(fin, out);
141 fin.close();
142 return r;
143 }
144 };
146 /**
147 * Create path from base path and file name.
148 */
149 public static final Function FILE = new Function() {
150 public int invoke(FunctionDataParser fdp, Writer out)
151 throws IOException, TemplateException {
153 String base = fdp.getNextArg();
154 String name = fdp.getNextArg();
156 if (fdp.getLastReturnCode() == 0)
157 return 0;
159 out.write((new File(fdp.createFile(base), name)).getPath());
161 return fdp.getLastReturnCode();
162 }
163 };
165 /**
166 * Update file (copy if newer).
167 *
168 * @param src source file.
169 * @param dest destination file.
170 *
171 * @trows IOException on copying error.
172 */
173 private static void copyFile(File src, File dest) throws IOException {
174 System.err.print("Copying " + src + "... ");
176 if (src.lastModified() < dest.lastModified()) {
177 System.err.println(dest + " is up to date.");
178 return;
179 }
181 File parent = dest.getParentFile();
182 if (parent != null) parent.mkdirs();
184 InputStream in = new FileInputStream(src);
185 OutputStream out = new FileOutputStream(dest);
187 copy(in, out);
189 in.close();
190 out.close();
191 }
193 private static void copy(InputStream in, OutputStream out)
194 throws IOException {
196 byte[] buffer = new byte[1024];
197 int len;
199 while ((len = in.read(buffer)) > 0) {
200 out.write(buffer, 0, len);
201 }
202 }
204 private static void copy(Reader in, Writer out)
205 throws IOException {
207 char[] buffer = new char[1024];
208 int len;
210 while ((len = in.read(buffer)) > 0) {
211 out.write(buffer, 0, len);
212 }
213 }
215 private static void readFile(File src, Writer out)
216 throws IOException {
218 Reader in = Tema.createFileReader(src);
219 copy(in, out);
220 }
221 }