Exemple #1
0
 @Test
 public void testGoodTemplate() {
   String t = "Hello @who!";
   String p = "Rythm";
   String s = Rythm.substitute(t, from(p("who", p)));
   assertEquals("Hello Rythm!", s);
 }
 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;
 }
 private void setUpFor244() {
   Rythm.shutdown();
   Properties prop = System.getProperties();
   prop.put(HOME_TEMPLATE.getKey(), "root/gh244");
   prop.put(FEATURE_NATURAL_TEMPLATE_ENABLED.getKey(), "false");
   prop.put(FEATURE_TYPE_INFERENCE_ENABLED.getKey(), "false");
   prop.put(FEATURE_SMART_ESCAPE_ENABLED.getKey(), "true");
   prop.put(FEATURE_TRANSFORM_ENABLED.getKey(), "true");
   prop.put(CODEGEN_COMPACT_ENABLED.getKey(), "false");
   prop.put(ENGINE_OUTPUT_JAVA_SOURCE_ENABLED.getKey(), "false");
   // prop.put(RythmConfigurationKey.I18N_LOCALE.getKey(), new Locale("en", "AU"));
   prop.put(RythmConfigurationKey.I18N_LOCALE.getKey(), Locale.getDefault());
   prop.put("line.separator", "\n");
   prop.put(DEFAULT_CODE_TYPE_IMPL.getKey(), ICodeType.DefImpl.RAW);
   t = null;
   s = null;
 }
  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;
  }
Exemple #5
0
 @Test(expected = Exception.class)
 public void testBadTemplate() {
   String t = "@(who)'s length is @who.length()";
   String p = "Rythm";
   Rythm.substitute(t, from(p("who", p)));
 }
 /**
  * Print this to string option instance using the following logic:
  *
  * <p>
  *
  * <pre><code>
  * return Rythm.toString(
  *    "{appendStatic: @_.appendStatic; " +
  *    "appendTransient: @_.appendTransient; " +
  *    upToClass: @_.upToClass?.getName()}", this);
  * </code></pre>
  *
  * @return String representation of this option
  */
 @Override
 public String toString() {
   return Rythm.toString(
       "{appendStatic: @_.appendStatic; appendTransient: @_.appendTransient; upToClass: @_.upToClass?.getName()}",
       this);
 }