001: package com.xoetrope.template;
002:
003: import com.xoetrope.template.XTemplateProcessor;
004: import java.io.BufferedReader;
005: import java.io.BufferedWriter;
006: import java.io.File;
007: import java.io.FileNotFoundException;
008: import java.io.FileReader;
009: import java.io.FileWriter;
010: import java.io.IOException;
011: import java.util.HashMap;
012: import java.util.Iterator;
013: import java.util.Set;
014: import net.xoetrope.xui.XProject;
015:
016: /**
017: *
018: * <p> Copyright (c) Xoetrope Ltd., 2001-2006, This software is licensed under
019: * the GNU Public License (GPL), please see license.txt for more details. If
020: * you make commercial use of this software you must purchase a commercial
021: * license from Xoetrope.</p>
022: */
023: public class XJavaTemplateProcessor extends XTemplateProcessor {
024: protected String code;
025: protected boolean debugCode;
026:
027: /** Creates a new instance of XJavaTemplateProcessor */
028: public XJavaTemplateProcessor(XProject proj, XTemplateEngine te) {
029: super (proj, te);
030: }
031:
032: public boolean process(String sourceName, String targetName,
033: int processingType) {
034: sourceFile = new File(sourceFileName = sourceName);
035: targetFile = new File(targetName);
036:
037: boolean rc = loadFile();
038: if (rc) {
039: rc &= replacePackageName();
040: rc &= includeElements();
041: rc &= replaceNames();
042: rc &= includeDebug();
043:
044: if (rc)
045: rc = saveFile();
046: }
047:
048: return rc;
049: }
050:
051: protected boolean loadFile() {
052: FileReader fileReader;
053: try {
054: fileReader = new FileReader(sourceFileName);
055: } catch (FileNotFoundException ex) {
056: ex.printStackTrace();
057: return false;
058: }
059:
060: BufferedReader in = new BufferedReader(fileReader);
061:
062: String temp = null;
063: try {
064: while ((temp = in.readLine()) != null) {
065: if (code == null)
066: code = temp + "\n";
067: else
068: code = code + temp + "\n";
069: }
070: in.close();
071: } catch (IOException ex) {
072: ex.printStackTrace();
073: return false;
074: }
075:
076: return true;
077: }
078:
079: protected boolean saveFile() {
080: FileWriter fw = null;
081:
082: try {
083: targetFile.getParentFile().mkdirs();
084: fw = new FileWriter(targetFile);
085:
086: BufferedWriter out = new BufferedWriter(fw);
087: out.write(code);
088: out.flush();
089: out.close();
090: } catch (IOException ex) {
091: ex.printStackTrace();
092: return false;
093: } finally {
094: if (fw != null) {
095: try {
096: fw.close();
097: } catch (IOException ex) {
098: ex.printStackTrace();
099: }
100: }
101: }
102:
103: return true;
104: }
105:
106: protected boolean replacePackageName() {
107: if (code.contains("package")) {
108: int startIndex = code.indexOf("package", 0);
109: int endIndex = code.indexOf("\n", startIndex);
110: String p = code.substring(startIndex, endIndex);
111:
112: String s = engine.getContent("PACKAGE");
113: if (s != null)
114: code = code.replaceAll(p, "package " + s + ";");
115: }
116: return true;
117: }
118:
119: protected boolean includeDebug() {
120: if (code.contains("//BEGIN_BLOCK_DEBUG")) {
121: int startIndex = code.indexOf("//BEGIN_BLOCK_DEBUG", 0);
122: int endIndex = code.indexOf("\n", startIndex);
123:
124: if (debugCode) {
125: code = code.replaceAll("//BEGIN_BLOCK_DEBUG", "");
126: code = code.replaceAll("//END_BLOCK_DEBUG", "");
127: } else {
128: String s1 = code.substring(0, startIndex);
129: int endBlock = code.indexOf("//END_BLOCK_DEBUG")
130: + ("//END_BLOCK_DEBUG").length();
131: String s2 = code.substring(endBlock);
132: code = s1 + s2;
133: }
134: }
135: return true;
136: }
137:
138: public void debugCode(boolean b) {
139: debugCode = b;
140: }
141:
142: protected boolean includeElements() {
143: while (code.contains("//BEGIN_BLOCK")) {
144: int startIndex = code.indexOf("//BEGIN_BLOCK_", 0);
145: int endIndex = code.indexOf("\n", startIndex);
146: String name = code.substring(startIndex
147: + "//BEGIN_BLOCK_".length(), endIndex);
148:
149: if (engine.includes(name)) {
150: code = code.replaceAll("//BEGIN_BLOCK_" + name, "");
151: code = code.replaceAll("//END_BLOCK_" + name, "");
152: } else {
153: String s1 = code.substring(0, startIndex);
154: int endBlock = code.indexOf("//END_BLOCK_" + name)
155: + ("//END_BLOCK_" + name).length();
156: String s2 = code.substring(endBlock);
157: code = s1 + s2;
158: }
159: }
160:
161: return true;
162: }
163:
164: protected boolean replaceNames() {
165: if (code.contains("//METHOD_NAME")) {
166: int i = 1;
167: if (code.contains("//METHOD_NAME_" + i)) {
168: String methodName = engine.getContent("//METHOD_NAME_"
169: + i);
170: if (methodName != null)
171: code = code.replaceAll("//METHOD_NAME_" + i,
172: methodName);
173: i++;
174: }
175: }
176:
177: if (code.contains("//IMPORT_NAME")) {
178: int i = 1;
179: if (code.contains("//IMPORT_NAME_" + i)) {
180: String importName = engine.getContent("IMPORT_NAME_"
181: + i);
182: if (importName != null)
183: code = code.replaceAll("//IMPORT_NAME_" + i,
184: "import " + importName + ";");
185: i++;
186: }
187: }
188:
189: Iterator iter = engine.getContents(this ).keySet().iterator();
190: while (iter.hasNext()) {
191: String key = (String) iter.next();
192: if (code.contains(key)) {
193: String value = engine.getContent(key);
194: if (value != null)
195: code = code.replaceAll(key, value);
196: }
197: }
198:
199: return true;
200: }
201: }
|