01: package com.xoetrope.template;
02:
03: import java.io.BufferedOutputStream;
04: import java.io.File;
05: import java.io.FileInputStream;
06: import java.io.FileOutputStream;
07: import java.io.OutputStream;
08: import net.xoetrope.xui.XProject;
09:
10: /**
11: * A basic processor for a parameterized resource
12: *
13: * <p> Copyright (c) Xoetrope Ltd., 2001-2007, This software is licensed under
14: * the GNU Public License (GPL), please see license.txt for more details. If
15: * you make commercial use of this software you must purchase a commercial
16: * license from Xoetrope.</p>
17: * <p> $Revision: 1.2 $</p>
18: */
19: public class XTemplateProcessor {
20: public static final int COPY_FILE = 0;
21: public static final int RENAME_FILE = 1;
22: public static final int RENAME_ELEMENT = 2;
23: public static final int APPLY_RESOURCE = 3;
24: public static final int PROCESSING_INSTRUCTION = 4;
25:
26: protected File sourceFile, targetFile;
27: protected String sourceFileName, targetFileName;
28:
29: protected XTemplateEngine engine;
30: protected XProject currentProject;
31:
32: public XTemplateProcessor(XProject proj, XTemplateEngine te) {
33: currentProject = proj;
34: engine = te;
35: }
36:
37: public boolean process(String sourceName, String targetName,
38: int processingType) {
39: sourceFile = new File(sourceFileName = sourceName);
40: targetFile = new File(targetFileName = targetName);
41: return copyFile();
42: }
43:
44: public boolean copyFile() {
45: byte[] fileBytes = new byte[0];
46:
47: try {
48: FileInputStream fis = new FileInputStream(sourceFile);
49:
50: byte[] b = new byte[1024];
51: if (fis != null) {
52: targetFile.getParentFile().mkdirs();
53: OutputStream fos = new BufferedOutputStream(
54: new FileOutputStream(targetFile));
55: int i = fis.read(b);
56: while (i != -1) {
57: fileBytes = new byte[i];
58: System.arraycopy(b, 0, fileBytes, 0, i);
59: fos.write(fileBytes);
60: b = new byte[1024];
61: i = fis.read(b);
62: }
63: fos.flush();
64: fos.close();
65: } else
66: return false;
67: } catch (Exception ex) {
68: ex.printStackTrace();
69: return false;
70: }
71:
72: return true;
73: }
74: }
|