1 5 package xdoclet.tagshandler; 6 7 import java.io.BufferedWriter ; 8 import java.io.File ; 9 import java.io.FileWriter ; 10 import java.io.IOException ; 11 import java.net.MalformedURLException ; 12 import java.net.URL ; 13 import java.text.MessageFormat ; 14 import java.util.Properties ; 15 16 import org.apache.commons.logging.Log; 17 18 import xdoclet.TemplateSubTask; 19 import xdoclet.XDocletException; 20 import xdoclet.XDocletTagSupport; 21 import xdoclet.template.PrettyPrintWriter; 22 import xdoclet.template.TemplateParser; 23 import xdoclet.util.FileManager; 24 import xdoclet.util.LogUtil; 25 26 32 public class MergeTagsHandler extends XDocletTagSupport 33 { 34 35 47 public void ifMergeFileExists(String template, Properties attributes) throws XDocletException 48 { 49 Log log = LogUtil.getLog(MergeTagsHandler.class, "ifMergeFileExists"); 50 String merge_file_pattern = attributes.getProperty("file"); 51 52 if (log.isDebugEnabled()) { 53 log.debug("Pattern = " + merge_file_pattern); 54 } 55 if (merge_file_pattern != null) { 56 if (merge_file_pattern.endsWith(".j")) { 58 log.warn("Deprecated template file extension used for merge file, .j should now be .xdt"); 59 merge_file_pattern = merge_file_pattern.substring(0, merge_file_pattern.length() - 2) + ".xdt"; 60 } 61 62 String contents = getMergeFileContents(merge_file_pattern); 63 64 if (contents != null) { 65 if (log.isDebugEnabled()) 66 log.debug("Merge File found"); 67 68 generate(template); 69 70 } 71 } 72 else { 73 log.error("<XDtMerge:ifMergeFileExists/> file parameter missing from template file."); 74 } 75 } 76 77 92 public void merge(String template, Properties attributes) throws XDocletException 93 { 94 95 Log log = LogUtil.getLog(MergeTagsHandler.class, "merge"); 96 String merge_file_pattern = attributes.getProperty("file"); 97 98 if (log.isDebugEnabled()) { 99 log.debug("Pattern = " + merge_file_pattern); 100 } 101 if (merge_file_pattern != null) { 102 if (merge_file_pattern.endsWith(".j")) { 104 log.warn("Deprecated template file extension used for merge file, .j should now be .xdt"); 105 merge_file_pattern = merge_file_pattern.substring(0, merge_file_pattern.length() - 2) + ".xdt"; 106 } 107 108 String contents = getMergeFileContents(merge_file_pattern); 109 110 if (contents != null) { 111 if (log.isDebugEnabled()) 112 log.debug("Merge File found"); 113 114 String generate_merged_file = attributes.getProperty("generateMergedFile"); 115 116 if (generate_merged_file != null && !generate_merged_file.equalsIgnoreCase("true") && !generate_merged_file.equalsIgnoreCase("yes")) { 117 getEngine().print(contents); 118 } 119 else { 120 generateUsingMergedFile(merge_file_pattern, contents); 121 } 122 } 123 else { 124 if (log.isDebugEnabled()) { 125 log.debug("Merge File NOT found"); 126 } 127 128 generateUsingMergedFile(((TemplateSubTask) getDocletContext().getActiveSubTask()).getTemplateURL().toString(), template); 130 } 131 } 132 else { 133 log.error("<XDtMerge:merge/> file parameter missing from template file, ignoring merge command."); 134 generate(template); 135 } 136 137 } 138 139 155 protected String getMergeFileContents(String mergeFilePattern) 156 { 157 Log log = LogUtil.getLog(MergeTagsHandler.class, "getMergeFileContents"); 158 159 String file = null; 160 161 try { 162 if (mergeFilePattern.indexOf("{0}") != -1) { 163 if (getEngine() instanceof TemplateParser) { 164 ((TemplateParser) getEngine()).addMergeFile(mergeFilePattern); 165 } 166 else { 167 168 String ejb_name = MessageFormat.format(mergeFilePattern, new Object []{AbstractProgramElementTagsHandler.getClassNameFor(getCurrentClass())}); 169 String merge_file_name = PackageTagsHandler.packageNameAsPathWithoutSubstitutionFor(getCurrentClass().getContainingPackage()) + File.separator + ejb_name; 170 171 if (getDocletContext().getActiveSubTask().getMergeDir() != null) { 172 File mergeFile = new File (getDocletContext().getActiveSubTask().getMergeDir(), merge_file_name); 173 174 log.debug("Search for File " + mergeFile); 175 176 if (mergeFile.exists()) { 177 log.debug("Search for File OK"); 178 file = FileManager.getURLContent(mergeFile.toURL()); 179 } 180 else { 181 if (merge_file_name.endsWith(".xdt")) { 183 log.debug(".xdt mergefile not found, trying .j"); 184 mergeFile = new File (getDocletContext().getActiveSubTask().getMergeDir(), 185 merge_file_name.substring(0, merge_file_name.length() - 4) + ".j"); 186 log.debug(".xdt mergefile not found, search for File " + mergeFile); 187 188 if (mergeFile.exists()) { 189 log.debug("Search for File OK"); 190 file = FileManager.getURLContent(mergeFile.toURL()); 191 } 192 else { 193 log.debug("Search for File not OK"); 194 } 195 } 196 else { 197 log.debug("Search for File not OK"); 198 } 199 } 200 } 201 } 202 } 203 else { 204 if (getDocletContext().getActiveSubTask().getMergeDir() != null) { 205 File mergeFile = new File (getDocletContext().getActiveSubTask().getMergeDir(), mergeFilePattern); 206 207 if (getEngine() instanceof TemplateParser) { 208 TemplateParser parser = (TemplateParser) getEngine(); 209 210 if (parser.hasMergeFile(mergeFilePattern)) { 212 return null; 213 } 214 else { 215 parser.addMergeFile(mergeFilePattern); 216 } 217 } 218 219 if (mergeFile.exists()) { 220 log.debug("Merge file found in " + getDocletContext().getActiveSubTask().getMergeDir()); 221 file = FileManager.getURLContent(mergeFile.toURL()); 222 } 223 else { 224 if (mergeFilePattern.endsWith(".xdt")) { 226 mergeFile = new File (getDocletContext().getActiveSubTask().getMergeDir(), 227 mergeFilePattern.substring(0, mergeFilePattern.length() - 4) + ".j"); 228 log.debug(".xdt mergefile not found, trying " + mergeFile.getName()); 229 230 if (mergeFile.exists()) { 231 log.debug("Merge file found in " + getDocletContext().getActiveSubTask().getMergeDir()); 232 file = FileManager.getURLContent(mergeFile.toURL()); 233 } 234 else { 235 log.debug("Merge file NOT found in " + getDocletContext().getActiveSubTask().getMergeDir()); 236 } 237 } 238 else { 239 log.debug("Merge file NOT found in " + getDocletContext().getActiveSubTask().getMergeDir()); 240 } 241 } 242 } 243 } 244 if (file != null) 245 return file; 246 247 URL jarResource = getClass().getResource('/' + mergeFilePattern); 249 250 if (jarResource != null) { 251 log.debug("Merge file found in jar"); 252 253 if (getEngine() instanceof TemplateParser) { 254 TemplateParser parser = (TemplateParser) getEngine(); 255 256 if (parser.hasMergeFile(mergeFilePattern)) { 258 return null; 259 } 260 else { 261 parser.addMergeFile(mergeFilePattern); 262 } 263 } 264 265 file = FileManager.getURLContent(jarResource); 266 } 267 else { 268 file = null; 270 } 271 } 272 catch (MalformedURLException e) { 273 log.error(e.getMessage(), e); 274 return null; 275 } 276 return file; 277 } 278 279 288 protected void generateUsingMergedFile(String mergeFile, String contents) throws XDocletException 289 { 290 try { 291 int lineNumber = getEngine().getCurrentLineNum(); 292 URL prevTemplateUrl = ((TemplateSubTask) getDocletContext().getActiveSubTask()).getTemplateURL(); 293 294 getEngine().setTemplateURL(new File (mergeFile).toURL()); 295 getEngine().setCurrentLineNum(0); 296 297 generate(contents); 298 299 getEngine().setTemplateURL(prevTemplateUrl); 300 getEngine().setCurrentLineNum(lineNumber); 301 } 302 catch (MalformedURLException e) { 303 throw new XDocletException(e.getMessage()); 304 } 305 } 306 307 315 protected void generateFileUsingTemplate(String dest_file, String templateFileName) throws XDocletException 316 { 317 Log log = LogUtil.getLog(MergeTagsHandler.class, "generateFileUsingTemplate"); 318 319 getXJavaDoc().getSourceClasses(); 320 321 File file = new File (getDocletContext().getDestDir(), dest_file); 322 323 334 file.getParentFile().mkdirs(); 335 336 try { 337 getEngine().setTemplateURL(new File (templateFileName).toURL()); 338 339 String content = FileManager.getURLContent(((TemplateSubTask) getDocletContext().getActiveSubTask()).getTemplateURL()); 340 341 if (content != null) { 342 try { 343 PrettyPrintWriter out = new PrettyPrintWriter(new BufferedWriter (new FileWriter (file))); 344 345 getEngine().setWriter(out); 346 getEngine().setCurrentLineNum(0); 347 generate(content); 348 out.close(); 349 } 350 catch (IOException ex) { 351 log.error("An error occured while writing output to file " + file, ex); 352 } 353 } 354 } 355 catch (MalformedURLException e) { 356 throw new XDocletException(e.getMessage()); 357 } 358 } 359 } 360 | Popular Tags |