/src/main/java/com/greenlaw110/rythm/resource/FileTemplateResource.java

http://github.com/greenlaw110/Rythm · Java · 198 lines · 164 code · 23 blank · 11 comment · 46 complexity · 4d944bb79fa8623c4b9833b5367130a6 MD5 · raw file

  1. package com.greenlaw110.rythm.resource;
  2. import com.greenlaw110.rythm.Rythm;
  3. import com.greenlaw110.rythm.RythmEngine;
  4. import com.greenlaw110.rythm.internal.compiler.TemplateClass;
  5. import com.greenlaw110.rythm.logger.ILogger;
  6. import com.greenlaw110.rythm.logger.Logger;
  7. import com.greenlaw110.rythm.runtime.ITag;
  8. import com.greenlaw110.rythm.utils.IO;
  9. import java.io.File;
  10. /**
  11. * Created by IntelliJ IDEA.
  12. * User: luog
  13. * Date: 20/01/12
  14. * Time: 10:59 PM
  15. * To change this template use File | Settings | File Templates.
  16. */
  17. public class FileTemplateResource extends TemplateResourceBase implements ITemplateResource {
  18. private ILogger logger = Logger.get(FileTemplateResource.class);
  19. private File file;
  20. private String key;
  21. private String tagName;
  22. @Override
  23. protected long defCheckInterval() {
  24. return 1000 * 5;
  25. }
  26. public FileTemplateResource(String path) {
  27. this(path, null);
  28. }
  29. public FileTemplateResource(String path, RythmEngine engine) {
  30. super(engine);
  31. File home = engine().templateHome;
  32. File tagHome = engine().tagHome;
  33. File f = null;
  34. if (null != home) {
  35. f = new File(home, path);
  36. }
  37. if (null == f || !f.canRead()) {
  38. // try tag home
  39. if (null != tagHome) f = new File(tagHome, path);
  40. }
  41. if (null == f || !f.canRead()) {
  42. f = new File(path);
  43. }
  44. file = f;
  45. key = path;
  46. if (null != tagHome && isValid()) {
  47. // set tag name if this file is found under tag home
  48. String tagPath = tagHome.getAbsolutePath();
  49. String filePath = f.getAbsolutePath();
  50. if (filePath.startsWith(tagPath)) {
  51. this.tagName = retrieveTagName(tagHome, f);
  52. }
  53. }
  54. }
  55. public FileTemplateResource(File templateFile) {
  56. this(templateFile, null);
  57. }
  58. public FileTemplateResource(File templateFile, RythmEngine engine) {
  59. super(engine);
  60. File home = engine().templateHome;
  61. File tagHome = engine().tagHome;
  62. file = templateFile;
  63. key = file.getPath();
  64. if (null != tagHome && isValid()) {
  65. // set tag name if this file is found under tag home
  66. String tagPath = tagHome.getAbsolutePath();
  67. String filePath = file.getAbsolutePath();
  68. if (filePath.startsWith(tagPath)) {
  69. this.tagName = retrieveTagName(tagHome, file);
  70. }
  71. }
  72. }
  73. private static String retrieveTagName(File tagHome, File tagFile) {
  74. String tagPath = tagHome.getAbsolutePath();
  75. String filePath = tagFile.getAbsolutePath();
  76. String tagName = null;
  77. if (filePath.startsWith(tagPath)) {
  78. tagName = filePath.substring(tagPath.length());
  79. while (tagName.startsWith("/") || tagName.startsWith("\\")) {
  80. tagName = tagName.substring(1);
  81. }
  82. tagName = tagName.replace('\\', '.');
  83. tagName = tagName.replace('/', '.');
  84. int dot = tagName.lastIndexOf(".");
  85. tagName = tagName.substring(0, dot);
  86. }
  87. return tagName;
  88. }
  89. public static void main(String[] args) {
  90. File tagHome = new File("W:\\_lgl\\greenscript-1.2\\java\\play\\app\\views\\tags\\");
  91. File tagFile = new File("W:\\_lgl\\greenscript-1.2\\java\\play\\app\\views\\tags\\greenscript\\css.html");
  92. System.out.println(retrieveTagName(tagHome, tagFile));
  93. }
  94. @Override
  95. public String getKey() {
  96. return key;
  97. }
  98. @Override
  99. public long lastModified() {
  100. return file.lastModified();
  101. }
  102. @Override
  103. protected Long userCheckInterval() {
  104. return engine().configuration.getAsLong("rythm.resource.file.interval", null);
  105. }
  106. @Override
  107. public boolean isValid() {
  108. return null != file && file.canRead();
  109. }
  110. @Override
  111. protected String reload() {
  112. return IO.readContentAsString(file);
  113. }
  114. @Override
  115. public String getSuggestedClassName() {
  116. return path2CN(file.getPath());
  117. }
  118. @Override
  119. public String tagName() {
  120. return tagName;
  121. }
  122. public static String getFullTagName(TemplateClass tc, RythmEngine engine) {
  123. if (null == engine) engine = Rythm.engine;
  124. String key = tc.getKey();
  125. if (key.startsWith("/") || key.startsWith("\\")) key = key.substring(1);
  126. if (null != engine.templateHome && key.startsWith(engine.templateHome.getPath())) {
  127. key = key.replace(engine.templateHome.getPath(), "");
  128. }
  129. if (key.startsWith("/") || key.startsWith("\\")) key = key.substring(1);
  130. int pos = key.lastIndexOf(".");
  131. if (-1 != pos) key = key.substring(0, pos);
  132. return key.replace('/', '.').replace('\\', '.');
  133. }
  134. public static TemplateClass tryLoadTag(String tagName, RythmEngine engine) {
  135. if (null == engine) engine = Rythm.engine;
  136. if (engine.tags.containsKey(tagName)) return null;
  137. tagName = tagName.replace('.', '/');
  138. final String[] suffixes = {
  139. ".html",
  140. ".json",
  141. ".tag"
  142. };
  143. File tagFile = null;
  144. File[] roots = {engine.tagHome, engine.templateHome};
  145. for (String suffix: suffixes) {
  146. String name = tagName + suffix;
  147. for (File root: roots) {
  148. if (null == root) continue;
  149. tagFile = new File(root, name);
  150. if (tagFile.canRead()) {
  151. try {
  152. FileTemplateResource tr = new FileTemplateResource(tagFile);
  153. TemplateClass tc = engine.classes.getByTemplate(tr.getKey());
  154. if (null == tc) {
  155. tc = new TemplateClass(tr, engine);
  156. }
  157. try {
  158. ITag tag = (ITag)tc.asTemplate();
  159. if (null != tag) {
  160. engine.registerTag(tagName, tag);
  161. return tc;
  162. }
  163. } catch (Exception e) {
  164. return tc;
  165. }
  166. } catch (Exception e) {
  167. e.printStackTrace();
  168. // ignore
  169. }
  170. }
  171. }
  172. }
  173. return null;
  174. }
  175. }