01: /*
02: * XPropertiesTemplateProcessor.java
03: *
04: * Created on 11 June 2007, 12:14
05: *
06: * To change this template, choose Tools | Template Manager
07: * and open the template in the editor.
08: */
09:
10: package com.xoetrope.template;
11:
12: import java.io.File;
13: import java.io.FileInputStream;
14: import java.io.FileNotFoundException;
15: import java.io.FileOutputStream;
16: import java.util.Enumeration;
17: import java.util.Properties;
18: import net.xoetrope.xui.XProject;
19:
20: /**
21: *
22: * @author kingsley.elmes
23: */
24: public class XPropertiesTemplateProcessor extends XTemplateProcessor {
25: protected Properties properties;
26:
27: /** Creates a new instance of XPropertiesTemplateProcessor */
28: public XPropertiesTemplateProcessor(XProject proj,
29: XTemplateEngine te) {
30: super (proj, te);
31: properties = new Properties();
32: }
33:
34: public boolean process(String sourceName, String targetName,
35: int processingType) {
36: sourceFile = new File(sourceFileName = sourceName);
37: targetFile = new File(targetName);
38:
39: boolean rc = loadFile();
40: if (rc) {
41: rc &= includeProperties();
42:
43: if (rc)
44: rc = saveFile();
45: }
46:
47: return rc;
48: }
49:
50: public boolean loadFile() {
51: try {
52: properties.load(new FileInputStream(sourceFileName));
53: } catch (Exception ex) {
54: ex.printStackTrace();
55: }
56:
57: return true;
58: }
59:
60: public boolean saveFile() {
61: Properties writer = new Properties();
62: FileOutputStream stream = null;
63:
64: try {
65: stream = new FileOutputStream(targetFile);
66: properties.store(stream, "properties");
67: stream.flush();
68: stream.close();
69: } catch (Exception ex) {
70: ex.printStackTrace();
71: }
72:
73: return true;
74: }
75:
76: public boolean includeProperties() {
77: Enumeration e = properties.propertyNames();
78:
79: while (e.hasMoreElements()) {
80: String s = e.nextElement().toString();
81:
82: if (!engine.includes(s)) {
83: properties.remove(s);
84: }
85: }
86:
87: return true;
88: }
89: }
|