/plugins/Templates/tags/rel-3_0_1/templates/TemplateFile.java

# · Java · 216 lines · 97 code · 21 blank · 98 comment · 8 complexity · bd40ff344ca9e7ded2f37272e01f33de MD5 · raw file

  1. // $Id: TemplateFile.java 7438 2002-08-09 18:53:56Z sjakob $
  2. /*
  3. * TemplateFile.java - Represents a file within the templates
  4. * directory hierarchy.
  5. * Copyright (C) 1999 Steve Jakob
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  20. */
  21. package templates;
  22. import java.awt.event.*;
  23. import java.io.*;
  24. import java.util.*;
  25. import javax.swing.tree.TreeNode;
  26. import org.gjt.sp.jedit.*;
  27. import org.gjt.sp.util.Log;
  28. import gnu.regexp.*;
  29. /**
  30. * A TemplateFile is similar to a java.io.File object as it acts as a reference
  31. * to a template file, but it also contains information describing the template
  32. * (eg. a label to be used on the Templates menu).
  33. */
  34. public class TemplateFile implements TreeNode
  35. {
  36. private static final String labelRE =
  37. "(\\s*##\\s*)(TEMPLATE)(\\s*=\\s*)(\\S+.*)";
  38. protected String label;
  39. protected File templateFile;
  40. private static RE ctpragmaLabelFilter = null;
  41. private TemplateDir parent;
  42. //Constructors
  43. public TemplateFile(TemplateDir parent, File templateFile) {
  44. super();
  45. this.parent = parent;
  46. this.templateFile = templateFile;
  47. this.label = templateFile.getName();
  48. createREs();
  49. if (!this.isDirectory()) {
  50. String s = null;
  51. try {
  52. s = readTemplateLabel(this.getBufferedReader());
  53. } catch (Exception e) {
  54. Log.log(Log.ERROR,this,jEdit.getProperty("plugin.TemplatesPlugin.error.template-label")
  55. + templateFile.getName());
  56. Log.log(Log.ERROR,this,e);
  57. }
  58. if (s != null)
  59. label = s;
  60. }
  61. }
  62. //Accessors & Mutators
  63. public String getLabel() { return label; }
  64. public void setLabel(String labelVal) { label = labelVal; }
  65. public String getPath() { return templateFile.getPath(); }
  66. /**
  67. * Determine the relative path of the file from the templates directory,
  68. * given the file's absolute path.
  69. */
  70. public String getRelativePath() {
  71. String absolutePath = templateFile.getPath();
  72. if (absolutePath.startsWith(TemplatesPlugin.getTemplateDir())) {
  73. return absolutePath.substring(TemplatesPlugin.getTemplateDir().length());
  74. }
  75. return absolutePath;
  76. }
  77. //Implementors
  78. public boolean isDirectory() { return false; }
  79. /**
  80. * Convenience method to create a BufferedReader to the template file.
  81. * @return A BufferedReader object corresponding to the underlying file.
  82. */
  83. public BufferedReader getBufferedReader() throws FileNotFoundException {
  84. return new BufferedReader(new FileReader(this.templateFile));
  85. }
  86. private static String readTemplateLabel(BufferedReader in) throws IOException{
  87. String templateLabel = null;
  88. try {
  89. String line;
  90. if ((line = in.readLine()) != null) {
  91. REMatch labelMatch = ctpragmaLabelFilter.getMatch(line);
  92. if (labelMatch != null) {
  93. templateLabel = labelMatch.toString(4);
  94. }
  95. }
  96. } catch (IOException e) {
  97. throw e;
  98. } // In case of problems, throw the exception to the caller
  99. finally { // but close the file, also.
  100. try {
  101. in.close();
  102. } catch (IOException ioe) { }
  103. }
  104. return templateLabel;
  105. }
  106. /**
  107. * Creates a RE to parse #ctpragma directives. Each directive is composed of 4 parts:<P>
  108. * <LI> #ctpragma
  109. * <LI> the directive type (eg. LABEL, NAME, etc.)
  110. * <LI> an equals ("=") sign
  111. * <LI> the value to assign for this directive type
  112. */
  113. private static void createREs() {
  114. try {
  115. ctpragmaLabelFilter = new RE(labelRE,RE.REG_ICASE);
  116. } catch (gnu.regexp.REException e) { } // this shouldn't happen
  117. }
  118. public String toString() { return label; }
  119. //
  120. // The next seven methods satisfy the TreeNode interface requirements.
  121. //
  122. public Enumeration children() {
  123. return null;
  124. }
  125. public boolean getAllowsChildren() {
  126. return false;
  127. }
  128. public TreeNode getChildAt(int index) {
  129. return null;
  130. }
  131. public int getChildCount() {
  132. return 0;
  133. }
  134. public int getIndex(TreeNode child) {
  135. return -1;
  136. }
  137. public TreeNode getParent() {
  138. return parent;
  139. }
  140. public boolean isLeaf() {
  141. return true;
  142. }
  143. }
  144. /*
  145. * Change Log:
  146. * $Log$
  147. * Revision 1.4 2002/08/09 18:53:56 sjakob
  148. * BUG FIX: removed String.replaceFirst( ) method to retain JDK1.3 compatibility.
  149. *
  150. * Revision 1.3 2002/07/29 14:14:58 sjakob
  151. * Changed template label-matching regular expression for new Velocity format:
  152. * ## template = <label>
  153. *
  154. * Revision 1.2 2002/05/07 03:28:10 sjakob
  155. * Added support for template labelling via "#template=" command.
  156. *
  157. * Revision 1.1 2002/04/30 19:26:10 sjakob
  158. * Integrated Calvin Yu's Velocity plugin into Templates to support dynamic templates.
  159. *
  160. * Revision 1.5 2002/02/22 02:34:36 sjakob
  161. * Updated Templates for jEdit 4.0 actions API changes.
  162. * Selection of template menu items can now be recorded in macros.
  163. *
  164. * Revision 1.4 2001/02/23 19:31:39 sjakob
  165. * Added "Edit Template" function to Templates menu.
  166. * Some Javadoc cleanup.
  167. *
  168. * Revision 1.3 2000/05/08 04:45:52 sjakob
  169. * Abstracted template processing to new Template class.
  170. * TemplateFile will now act merely as a proxy for a Template.
  171. *
  172. * Revision 1.2 2000/05/02 03:22:51 sjakob
  173. * Added TemplateVar.java to template variable info.
  174. * Modified TemplateFile to parse new #ctpragma directives, and to create
  175. * template variables.
  176. *
  177. * Revision 1.1.1.1 2000/04/21 05:05:45 sjakob
  178. * Initial import of rel-1.0.0
  179. *
  180. * Revision 1.3 2000/03/08 15:46:49 sjakob
  181. * Updated README, CHANGES, to-do files.
  182. * Use properties for error messages, rather than hard-coded strings.
  183. *
  184. * Revision 1.2 2000/03/08 06:55:46 sjakob
  185. * Use org.gjt.sp.util.Log instead of System.out.println.
  186. * Update documentation.
  187. * Add sample template files to project.
  188. *
  189. * Revision 1.1 2000/03/03 06:25:43 sjakob
  190. * Redesigned the plugin to fix a bug where only the most recent view had a
  191. * Templates menu. Added TemplateFile and TemplateDir classes to handle
  192. * files and directories in the Templates directory tree. Templates menus for
  193. * all views are refreshed simultaneously.
  194. *
  195. */