Example #1
0
 @Test
 public void test143() {
   s = S.join("::", new Integer[] {1, 2, 3});
   eq("1::2::3");
   s = S.join(":", new Double[] {1.0, 2.0, 3.0});
   eq("1.0:2.0:3.0");
 }
Example #2
0
  @Test
  public void test145() {
    t = "@args Date today = new Date(),Boolean b;@today.format(\"yyyy\"):@b";
    s = r(t);
    eq(S.format(new Date(), "yyyy") + ":false");

    t = "@args String x = \"x\";@x";
    s = r(t);
    eq("x");
  }
Example #3
0
 @Test
 public void test155() {
   String x = "\uD83D\uDE30";
   assertEquals(x, S.escapeCSV(x).toString());
   assertEquals(x, Escape.CSV.apply(x).toString());
   System.getProperties()
       .setProperty(
           RythmConfigurationKey.DEFAULT_CODE_TYPE_IMPL.getKey(),
           "org.rythmengine.extension.ICodeType.DefImpl.CSV");
   t = "@s";
   s = r(t, x);
   eq(x);
 }
 /**
  * Construct a <code>ToStringOption</code> instance out from a string. The format of the String
  * should be the same as the format output of {@link #toString()} method
  *
  * @param s
  * @return an option instance corresponding to the string specified
  */
 public static ToStringOption valueOf(String s) {
   Pattern p =
       Pattern.compile(
           "\\{appendStatic *\\: *(true|false) *; *appendTransient *\\: *(true|false) *; *upToClass *: *(.*)\\}");
   Matcher m = p.matcher(s);
   if (!m.matches()) throw new IllegalArgumentException("Unknown ToStringOption: " + s);
   boolean appendStatic = Boolean.valueOf(m.group(1));
   boolean appendTransient = Boolean.valueOf(m.group(2));
   String upToClassStr = m.group(3);
   Class<?> upToClass;
   if (S.isEmpty(upToClassStr)) upToClass = null;
   else
     try {
       upToClass = Class.forName(upToClassStr);
     } catch (ClassNotFoundException e) {
       throw new IllegalArgumentException("Cannot find upToClass: " + upToClassStr);
     }
   return new ToStringOption(appendStatic, appendTransient, upToClass);
 }
Example #5
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;
  }
 @Override
 public String getMessage(ITemplate template, String key, Object... args) {
   return S.i18n(template, key, args);
 }
 @Test
 public void test226() {
   String s = "aaa\u0000bbb";
   String s0 = S.escapeJSON(s).toString();
   assertTrue(s0.contains("u0000"));
 }