1 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 ; 35 import java.util.Iterator ; 36 import java.io.File ; 37 import java.io.IOException ; 38 import java.io.BufferedReader ; 39 import java.io.FileReader ; 40 41 47 48 58 public class JAGTemplateEngine implements TemplateEngine { 59 60 private Boolean overwrite; 61 62 public void setOverwrite(Boolean overwrite) { 63 this.overwrite = overwrite; 64 } 65 66 public int process(Collection documents, String outputDir) throws InterruptedException { 67 overwrite = null; 68 int totalNumberOfNewFiles = 0; 69 String templateDir = JagGenerator.getTemplate().getTemplateDir().getAbsolutePath(); 70 71 SkeletDataObj skeletObj = null; 72 try { 73 SkeletLoader skeletLoader = 74 new XMLSkeletLoader(new File (JApplicationGen.getApplicationFile())); 75 skeletObj = skeletLoader.getSkeletData(); 76 if (JApplicationGen.isDisplaySkeletView()) { 77 JagSkeletViewer.show(skeletObj); 78 } 79 } catch (Exception 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 iterator = documents.iterator(); 94 while (iterator.hasNext()) { 95 File document = (File ) iterator.next(); 96 97 try { 98 BufferedReader in = new BufferedReader (new FileReader (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 exc) { 106 } 107 108 JagBlockCollection lnkJagBlockCollection = parser.getJagBlockCollection(); 109 111 TemplateStructureFactory textBlockTagFactory = new TemplateStructureFactory(); 112 textBlockTagFactory.create(lnkJagBlockCollection); 113 TemplateStructure templateData = textBlockTagFactory.getTemplateStructure(); 114 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 outputDir, TemplateTextBlockList textBlocks) 145 throws InterruptedException { 146 String 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 iterator = textBlocks.iterator(); 152 StringBuffer fileBuffer = new StringBuffer (); 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 tempFile = null; 160 File file = new File (new File (textBlock.getFile()).getCanonicalPath()); 161 String path = file.getCanonicalPath(); 162 String 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 (); 170 continue; 171 } 172 tempFile = new File (path + "_generated_" + name); 173 FileUtils.createFile(tempFile, fileBuffer.toString()); 174 String 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 (); 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 (); 220 newFileCounter++; 221 } catch (IOException 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 |