Exemplo n.º 1
0
 public static String getFullTagName(TemplateClass tc, RythmEngine engine) {
   if (null == engine) engine = Rythm.engine();
   String key = tc.getKey().toString();
   File home = engine.conf().templateHome();
   if (key.startsWith("/") || key.startsWith("\\")) key = key.substring(1);
   if (null != home && key.startsWith(home.getPath())) {
     key = key.replace(home.getPath(), "");
   }
   if (key.startsWith("/") || key.startsWith("\\")) key = key.substring(1);
   int pos = key.lastIndexOf(".");
   if (-1 != pos) key = key.substring(0, pos);
   key = key.replace('/', '.').replace('\\', '.');
   key += tc.codeType.resourceNameSuffix();
   return key;
 }
Exemplo n.º 2
0
  private static TemplateClass tryLoadTemplate(
      String tmplName, RythmEngine engine, TemplateClass callerClass, boolean processTagName) {
    if (null == engine) engine = Rythm.engine();
    if (engine.templateRegistered(tmplName)) return null;
    String rythmSuffix = engine.conf().resourceNameSuffix();
    final List<String> suffixes =
        new ArrayList(
            Arrays.asList(
                new String[] {".html", ".json", ".js", ".css", ".csv", ".xml", ".txt", ""}));
    ICodeType codeType = TemplateResourceBase.getTypeOfPath(engine, tmplName);
    if (ICodeType.DefImpl.RAW == codeType) {
      // use caller's code type
      codeType = callerClass.codeType;
    }
    final String tagNameOrigin = tmplName;
    if (processTagName) {
      boolean tagNameProcessed = false;
      while (!tagNameProcessed) {
        // process tagName to remove suffixes
        // 1. check without rythm-suffix
        for (String s : suffixes) {
          if (tmplName.endsWith(s)) {
            tmplName = tmplName.substring(0, tmplName.lastIndexOf(s));
            break;
          }
        }
        if (S.notEmpty(rythmSuffix)) {
          // 2. check with rythm-suffix
          for (String s : suffixes) {
            s = s + rythmSuffix;
            if (tmplName.endsWith(s)) {
              tmplName = tmplName.substring(0, tmplName.lastIndexOf(s));
              break;
            }
          }
        }
        tagNameProcessed = true;
      }
    }
    tmplName = tmplName.replace('.', '/');
    String sfx = codeType.resourceNameSuffix();
    if (S.notEmpty(sfx) && !suffixes.get(0).equals(sfx)) {
      suffixes.remove(sfx);
      suffixes.add(0, sfx);
    }
    File tagFile;
    final List<String> roots = new ArrayList<String>();
    final File home = engine.conf().templateHome();
    final String root = home.getPath();

    // call tag with import path
    if (null != callerClass.importPaths) {
      for (String s : callerClass.importPaths) {
        roots.add(root + File.separator + s.replace('.', File.separatorChar));
      }
    }

    final String tagName0 = tmplName;
    // call tag using relative path
    String currentPath = callerClass.getKey().toString();
    int pos = currentPath.lastIndexOf("/");
    if (-1 == pos) {
      pos = currentPath.lastIndexOf(File.separator);
    }
    if (-1 != pos) {
      currentPath = currentPath.substring(0, pos);
      if (currentPath.startsWith("/") || currentPath.startsWith(File.separator))
        currentPath = currentPath.substring(1);
      if (!currentPath.startsWith(root)) currentPath = root + File.separator + currentPath;
      roots.add(currentPath);
    }

    // add the default root at last
    roots.add(root);

    for (String r : roots) {
      tmplName = r + File.separator + tagName0;
      for (String suffix : suffixes) {
        String name = tmplName + suffix + rythmSuffix;

        tagFile = new File(name);
        ITemplateResource tr =
            tagFile.canRead() && !tagFile.isDirectory()
                ? new FileTemplateResource(tagFile, engine)
                : new ClasspathTemplateResource(name, engine);
        if (tr.isValid()) {
          try {
            TemplateClass tc = engine.classes().getByTemplate(tr.getKey());
            if (null == tc) {
              tc = new TemplateClass(tr, engine);
            }
            try {
              tc.asTemplate(); // register the template
              return tc;
              //                            ITemplate t = tc.asTemplate();
              //                            if (null != t) {
              //                                String fullName = getFullTagName(tc, engine);
              //                                tc.setFullName(fullName);
              //                                engine.registerTemplate(fullName, t);
              //                                return tc;
              //                            }
            } catch (Exception e) {
              e.printStackTrace();
              return tc;
            }
          } catch (Exception e) {
            // e.printStackTrace();
          }
        }
      }
    }
    return processTagName ? tryLoadTemplate(tagNameOrigin, engine, callerClass, false) : null;
  }