KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > finalist > jag > util > JAGTemplateEngine


1 /* Copyright (C) 2003 Finalist IT Group
2  *
3  * This file is part of JAG - the Java J2EE Application Generator
4  *
5  * JAG is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  * JAG is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  * You should have received a copy of the GNU General Public License
14  * along with JAG; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  */

17
18 package com.finalist.jag.util;
19
20 import com.finalist.jag.TagEngine;
21 import com.finalist.jag.SessionContext;
22 import com.finalist.jag.PageContext;
23 import com.finalist.jag.JApplicationGen;
24 import com.finalist.jag.skelet.*;
25 import com.finalist.jag.template.*;
26 import com.finalist.jag.template.parser.JagParser;
27 import com.finalist.jag.template.parser.CharBuffer;
28 import com.finalist.jag.template.parser.JagBlockCollection;
29 import com.finalist.util.Diff;
30 import com.finalist.jaggenerator.HtmlContentPopUp;
31 import com.finalist.jaggenerator.JagGenerator;
32
33 import javax.swing.*;
34 import java.util.Collection JavaDoc;
35 import java.util.Iterator JavaDoc;
36 import java.io.File JavaDoc;
37 import java.io.IOException JavaDoc;
38 import java.io.BufferedReader JavaDoc;
39 import java.io.FileReader JavaDoc;
40
41 /*
42 Bugs
43         <jag:equal name="blabla" property="blabla" parameter="blabla">
44         <jag:equal>
45         missing/invalid closing tag results in a infinity loop.
46 */

47
48 /**
49  * This is the original "JSP-tag like" template engine designed by Wendel D. de Witte.
50  * <p>
51  * This template engine is maintained for backwards compatability, and will probably
52  * not be developed further.
53  *
54  * @see VelocityTemplateEngine
55  *
56  * @author Michael O'Connor - Finalist IT Group
57  */

58 public class JAGTemplateEngine implements TemplateEngine {
59
60    private Boolean JavaDoc overwrite;
61
62    public void setOverwrite(Boolean JavaDoc overwrite) {
63       this.overwrite = overwrite;
64    }
65    
66    public int process(Collection documents, String JavaDoc outputDir) throws InterruptedException JavaDoc {
67       overwrite = null;
68       int totalNumberOfNewFiles = 0;
69       String JavaDoc templateDir = JagGenerator.getTemplate().getTemplateDir().getAbsolutePath();
70
71       SkeletDataObj skeletObj = null;
72       try {
73          SkeletLoader skeletLoader =
74                new XMLSkeletLoader(new File JavaDoc(JApplicationGen.getApplicationFile()));
75          skeletObj = skeletLoader.getSkeletData();
76          if (JApplicationGen.isDisplaySkeletView()) {
77             JagSkeletViewer.show(skeletObj);
78          }
79       } catch (Exception JavaDoc exc) {
80          JApplicationGen.log("[Skelet Error] :" + exc.getMessage());
81          return 0;
82       }
83
84       SessionContext sessionContext = new SessionContext();
85       sessionContext.setSkelet(skeletObj);
86
87       TagEngine tagEngine = new TagEngine();
88
89       JApplicationGen.createOutputStructure(templateDir);
90
91       JApplicationGen.log("Number of template files found : " + documents.size() + "\n");
92       JagParser parser = new JagParser();
93       Iterator JavaDoc iterator = documents.iterator();
94       while (iterator.hasNext()) {
95          File JavaDoc document = (File JavaDoc) iterator.next();
96
97          try {
98             BufferedReader JavaDoc in = new BufferedReader JavaDoc(new FileReader JavaDoc(document));
99             CharBuffer input = new CharBuffer(in);
100
101             JApplicationGen.log("[Processing "
102                   + document.getPath() + "]");
103             parser.process(input);
104             in.close();
105          } catch (Exception JavaDoc exc) {
106          }
107
108          JagBlockCollection lnkJagBlockCollection = parser.getJagBlockCollection();
109          //new JagBlockViewer(lnkJagBlockCollection);
110

111          TemplateStructureFactory textBlockTagFactory = new TemplateStructureFactory();
112          textBlockTagFactory.create(lnkJagBlockCollection);
113          TemplateStructure templateData = textBlockTagFactory.getTemplateStructure();
114          //TemplateTreeItemViewer.show(templateData.getRoot());
115

116          TemplateHeaderFactory headerFactory = new TemplateHeaderFactory();
117          headerFactory.create(lnkJagBlockCollection);
118          TemplateHeaderCollection headers = headerFactory.getHeaderCollection();
119
120          PageContext pageContext = new PageContext(sessionContext);
121          pageContext.setHeaderCollection(headers);
122          pageContext.setTemplateData(templateData);
123          tagEngine.processTags(pageContext);
124
125          FileCreationResult result = createOutputFiles(outputDir, pageContext.getTextCollection());
126          if (result.created == 0 && result.skipped == 0) {
127             JApplicationGen.log("No files generated by : "
128                   + document);
129          }
130          if (result.skipped > 0) {
131             JApplicationGen.log("Skipped generation of " + result.skipped + (result.skipped == 1 ? " file." : " files."));
132          }
133          if (result.created > 0) {
134             JApplicationGen.log("Created " + result.created + " new " + (result.created == 1 ? "file" : "files") +
135                   " from template " + document.getName());
136          }
137
138          totalNumberOfNewFiles += result.created;
139       }
140
141       return totalNumberOfNewFiles;
142    }
143
144    private FileCreationResult createOutputFiles(String JavaDoc outputDir, TemplateTextBlockList textBlocks)
145    throws InterruptedException JavaDoc {
146       String JavaDoc userdir = System.getProperty("user.dir");
147       System.setProperty("user.dir", outputDir);
148       boolean skipFile = false;
149       int newFileCounter = 0;
150       int skippedFilesCounter = 0;
151       Iterator JavaDoc iterator = textBlocks.iterator();
152       StringBuffer JavaDoc fileBuffer = new StringBuffer JavaDoc();
153       while (iterator.hasNext()) {
154          skipFile = false;
155          TemplateTextBlock textBlock = (TemplateTextBlock) iterator.next();
156          fileBuffer.append(textBlock.getText());
157          if (textBlock.newFile()) {
158             try {
159                File JavaDoc tempFile = null;
160                File JavaDoc file = new File JavaDoc(new File JavaDoc(textBlock.getFile()).getCanonicalPath());
161                String JavaDoc path = file.getCanonicalPath();
162                String JavaDoc name = file.getName();
163                path = path.substring(0, (path.length() - name.length()));
164
165
166                if (file.exists() && overwrite != Boolean.TRUE) {
167                   if (overwrite == Boolean.FALSE) {
168                      skippedFilesCounter++;
169                      fileBuffer = new StringBuffer JavaDoc();
170                      continue;
171                   }
172                   tempFile = new File JavaDoc(path + "_generated_" + name);
173                   FileUtils.createFile(tempFile, fileBuffer.toString());
174                   String JavaDoc diff = new Diff(file, tempFile).performDiff();
175                   int choice = 999;
176                   if (diff != null) {
177                      while (choice == 999 ||
178                            JApplicationGen.DIALOGUE_OPTIONS[choice] == JApplicationGen.OPTION_VIEW_DIFF) {
179                         choice = JOptionPane.showOptionDialog(null,
180                               "The file " + file + " differs from the existing copy.\n" +
181                               "Do you want to overwrite this file?",
182                               "File already exists!",
183                               JOptionPane.YES_NO_CANCEL_OPTION,
184                               JOptionPane.QUESTION_MESSAGE,
185                               null,
186                               JApplicationGen.DIALOGUE_OPTIONS,
187                               JApplicationGen.OPTION_VIEW_DIFF);
188                         if (JApplicationGen.DIALOGUE_OPTIONS[choice] == JApplicationGen.OPTION_VIEW_DIFF) {
189                            new HtmlContentPopUp(null, "Diff report:", true, diff, false).show();
190                         }
191                      }
192                      if (choice == JOptionPane.CLOSED_OPTION ||
193                            JApplicationGen.DIALOGUE_OPTIONS[choice] == JApplicationGen.OPTION_NO) {
194                         skipFile = true;
195                      } else if (JApplicationGen.DIALOGUE_OPTIONS[choice] == JApplicationGen.OPTION_NO_ALL) {
196                         overwrite = Boolean.FALSE;
197                         skipFile = true;
198                      } else if (JApplicationGen.DIALOGUE_OPTIONS[choice] == JApplicationGen.OPTION_YES_ALL) {
199                         overwrite = Boolean.TRUE;
200                      }
201                   } else {
202                      skipFile = true;
203                   }
204                }
205
206                if (skipFile) {
207                   FileUtils.deleteFile(tempFile);
208                   skippedFilesCounter++;
209                   fileBuffer = new StringBuffer JavaDoc();
210                   continue;
211                }
212
213                if (tempFile != null) {
214                   FileUtils.deleteFile(file);
215                   tempFile.renameTo(file);
216                } else {
217                   FileUtils.createFile(file, fileBuffer.toString());
218                }
219                fileBuffer = new StringBuffer JavaDoc();
220                newFileCounter++;
221             } catch (IOException JavaDoc exc) {
222                JApplicationGen.log("[Error] Create output file failed : " + textBlock.getFile());
223                JApplicationGen.log(exc.getMessage());
224             }
225          }
226       }
227       System.setProperty("user.dir", userdir);
228       return new FileCreationResult(newFileCounter, skippedFilesCounter);
229    }
230
231 }
232
Popular Tags