001: /* Copyright (C) 2003 Finalist IT Group
002: *
003: * This file is part of JAG - the Java J2EE Application Generator
004: *
005: * JAG is free software; you can redistribute it and/or modify
006: * it under the terms of the GNU General Public License as published by
007: * the Free Software Foundation; either version 2 of the License, or
008: * (at your option) any later version.
009: * JAG is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU General Public License for more details.
013: * You should have received a copy of the GNU General Public License
014: * along with JAG; if not, write to the Free Software
015: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
016: */
017:
018: package com.finalist.jag.util;
019:
020: import com.finalist.jag.TagEngine;
021: import com.finalist.jag.SessionContext;
022: import com.finalist.jag.PageContext;
023: import com.finalist.jag.JApplicationGen;
024: import com.finalist.jag.skelet.*;
025: import com.finalist.jag.template.*;
026: import com.finalist.jag.template.parser.JagParser;
027: import com.finalist.jag.template.parser.CharBuffer;
028: import com.finalist.jag.template.parser.JagBlockCollection;
029: import com.finalist.util.Diff;
030: import com.finalist.jaggenerator.HtmlContentPopUp;
031: import com.finalist.jaggenerator.JagGenerator;
032:
033: import javax.swing.*;
034: import java.util.Collection;
035: import java.util.Iterator;
036: import java.io.File;
037: import java.io.IOException;
038: import java.io.BufferedReader;
039: import java.io.FileReader;
040:
041: /*
042: Bugs
043: <jag:equal name="blabla" property="blabla" parameter="blabla">
044: <jag:equal>
045: missing/invalid closing tag results in a infinity loop.
046: */
047:
048: /**
049: * This is the original "JSP-tag like" template engine designed by Wendel D. de Witte.
050: * <p>
051: * This template engine is maintained for backwards compatability, and will probably
052: * not be developed further.
053: *
054: * @see VelocityTemplateEngine
055: *
056: * @author Michael O'Connor - Finalist IT Group
057: */
058: public class JAGTemplateEngine implements TemplateEngine {
059:
060: private Boolean overwrite;
061:
062: public void setOverwrite(Boolean overwrite) {
063: this .overwrite = overwrite;
064: }
065:
066: public int process(Collection documents, String outputDir)
067: throws InterruptedException {
068: overwrite = null;
069: int totalNumberOfNewFiles = 0;
070: String templateDir = JagGenerator.getTemplate()
071: .getTemplateDir().getAbsolutePath();
072:
073: SkeletDataObj skeletObj = null;
074: try {
075: SkeletLoader skeletLoader = new XMLSkeletLoader(new File(
076: JApplicationGen.getApplicationFile()));
077: skeletObj = skeletLoader.getSkeletData();
078: if (JApplicationGen.isDisplaySkeletView()) {
079: JagSkeletViewer.show(skeletObj);
080: }
081: } catch (Exception exc) {
082: JApplicationGen.log("[Skelet Error] :" + exc.getMessage());
083: return 0;
084: }
085:
086: SessionContext sessionContext = new SessionContext();
087: sessionContext.setSkelet(skeletObj);
088:
089: TagEngine tagEngine = new TagEngine();
090:
091: JApplicationGen.createOutputStructure(templateDir);
092:
093: JApplicationGen.log("Number of template files found : "
094: + documents.size() + "\n");
095: JagParser parser = new JagParser();
096: Iterator iterator = documents.iterator();
097: while (iterator.hasNext()) {
098: File document = (File) iterator.next();
099:
100: try {
101: BufferedReader in = new BufferedReader(new FileReader(
102: document));
103: CharBuffer input = new CharBuffer(in);
104:
105: JApplicationGen.log("[Processing " + document.getPath()
106: + "]");
107: parser.process(input);
108: in.close();
109: } catch (Exception exc) {
110: }
111:
112: JagBlockCollection lnkJagBlockCollection = parser
113: .getJagBlockCollection();
114: //new JagBlockViewer(lnkJagBlockCollection);
115:
116: TemplateStructureFactory textBlockTagFactory = new TemplateStructureFactory();
117: textBlockTagFactory.create(lnkJagBlockCollection);
118: TemplateStructure templateData = textBlockTagFactory
119: .getTemplateStructure();
120: //TemplateTreeItemViewer.show(templateData.getRoot());
121:
122: TemplateHeaderFactory headerFactory = new TemplateHeaderFactory();
123: headerFactory.create(lnkJagBlockCollection);
124: TemplateHeaderCollection headers = headerFactory
125: .getHeaderCollection();
126:
127: PageContext pageContext = new PageContext(sessionContext);
128: pageContext.setHeaderCollection(headers);
129: pageContext.setTemplateData(templateData);
130: tagEngine.processTags(pageContext);
131:
132: FileCreationResult result = createOutputFiles(outputDir,
133: pageContext.getTextCollection());
134: if (result.created == 0 && result.skipped == 0) {
135: JApplicationGen.log("No files generated by : "
136: + document);
137: }
138: if (result.skipped > 0) {
139: JApplicationGen.log("Skipped generation of "
140: + result.skipped
141: + (result.skipped == 1 ? " file." : " files."));
142: }
143: if (result.created > 0) {
144: JApplicationGen.log("Created " + result.created
145: + " new "
146: + (result.created == 1 ? "file" : "files")
147: + " from template " + document.getName());
148: }
149:
150: totalNumberOfNewFiles += result.created;
151: }
152:
153: return totalNumberOfNewFiles;
154: }
155:
156: private FileCreationResult createOutputFiles(String outputDir,
157: TemplateTextBlockList textBlocks)
158: throws InterruptedException {
159: String userdir = System.getProperty("user.dir");
160: System.setProperty("user.dir", outputDir);
161: boolean skipFile = false;
162: int newFileCounter = 0;
163: int skippedFilesCounter = 0;
164: Iterator iterator = textBlocks.iterator();
165: StringBuffer fileBuffer = new StringBuffer();
166: while (iterator.hasNext()) {
167: skipFile = false;
168: TemplateTextBlock textBlock = (TemplateTextBlock) iterator
169: .next();
170: fileBuffer.append(textBlock.getText());
171: if (textBlock.newFile()) {
172: try {
173: File tempFile = null;
174: File file = new File(new File(textBlock.getFile())
175: .getCanonicalPath());
176: String path = file.getCanonicalPath();
177: String name = file.getName();
178: path = path.substring(0, (path.length() - name
179: .length()));
180:
181: if (file.exists() && overwrite != Boolean.TRUE) {
182: if (overwrite == Boolean.FALSE) {
183: skippedFilesCounter++;
184: fileBuffer = new StringBuffer();
185: continue;
186: }
187: tempFile = new File(path + "_generated_" + name);
188: FileUtils.createFile(tempFile, fileBuffer
189: .toString());
190: String diff = new Diff(file, tempFile)
191: .performDiff();
192: int choice = 999;
193: if (diff != null) {
194: while (choice == 999
195: || JApplicationGen.DIALOGUE_OPTIONS[choice] == JApplicationGen.OPTION_VIEW_DIFF) {
196: choice = JOptionPane
197: .showOptionDialog(
198: null,
199: "The file "
200: + file
201: + " differs from the existing copy.\n"
202: + "Do you want to overwrite this file?",
203: "File already exists!",
204: JOptionPane.YES_NO_CANCEL_OPTION,
205: JOptionPane.QUESTION_MESSAGE,
206: null,
207: JApplicationGen.DIALOGUE_OPTIONS,
208: JApplicationGen.OPTION_VIEW_DIFF);
209: if (JApplicationGen.DIALOGUE_OPTIONS[choice] == JApplicationGen.OPTION_VIEW_DIFF) {
210: new HtmlContentPopUp(null,
211: "Diff report:", true, diff,
212: false).show();
213: }
214: }
215: if (choice == JOptionPane.CLOSED_OPTION
216: || JApplicationGen.DIALOGUE_OPTIONS[choice] == JApplicationGen.OPTION_NO) {
217: skipFile = true;
218: } else if (JApplicationGen.DIALOGUE_OPTIONS[choice] == JApplicationGen.OPTION_NO_ALL) {
219: overwrite = Boolean.FALSE;
220: skipFile = true;
221: } else if (JApplicationGen.DIALOGUE_OPTIONS[choice] == JApplicationGen.OPTION_YES_ALL) {
222: overwrite = Boolean.TRUE;
223: }
224: } else {
225: skipFile = true;
226: }
227: }
228:
229: if (skipFile) {
230: FileUtils.deleteFile(tempFile);
231: skippedFilesCounter++;
232: fileBuffer = new StringBuffer();
233: continue;
234: }
235:
236: if (tempFile != null) {
237: FileUtils.deleteFile(file);
238: tempFile.renameTo(file);
239: } else {
240: FileUtils.createFile(file, fileBuffer
241: .toString());
242: }
243: fileBuffer = new StringBuffer();
244: newFileCounter++;
245: } catch (IOException exc) {
246: JApplicationGen
247: .log("[Error] Create output file failed : "
248: + textBlock.getFile());
249: JApplicationGen.log(exc.getMessage());
250: }
251: }
252: }
253: System.setProperty("user.dir", userdir);
254: return new FileCreationResult(newFileCounter,
255: skippedFilesCounter);
256: }
257:
258: }
|