示例#1
0
  @Test
  public void shouldConstructAppMenu() {
    Res res = Res.from("menu.yaml");

    Object data = YAML.parse(res.getContent(), Object.class);
    System.out.println(data);

    AppMenu menu = AppMenu.from(data);
    System.out.println(menu);

    eq(menu.getItems().size(), 2);

    AppMenuItem item1 = menu.getItems().get(0);
    eq(item1.getCaption(), "item1");
    eq(item1.getTarget(), "aaa");

    AppMenuItem item2 = menu.getItems().get(1);
    eq(item2.getCaption(), "item2");
    isNull(item2.getTarget());

    AppSubMenuItem subitem1 = item2.getSubmenu().getItems().get(0);
    AppSubMenuItem subitem2 = item2.getSubmenu().getItems().get(1);
    AppSubMenuItem subitem3 = item2.getSubmenu().getItems().get(2);

    eq(subitem1.getCaption(), "d");
    eq(subitem1.getTarget(), "");

    eq(subitem2.getCaption(), "e");
    eq(subitem2.getTarget(), "f");

    eq(subitem3.getCaption(), "last");
    eq(subitem3.getTarget(), "");
  }
  private Mustache customCompile(String filename, Res resource) {
    Map<String, Mustache> cache = partialCache.get();
    try {
      Mustache mustache = cache.get(filename);
      if (mustache == null) {
        String template = preprocess(resource.getContent());
        InputStreamReader reader =
            new InputStreamReader(new ByteArrayInputStream(template.getBytes()));
        mustache = mc.compile(reader, filename, SM, EM);
        cache.put(filename, mustache);
        mustache.init();
      }

      return mustache;
    } finally {
      cache.remove(filename);
    }
  }
 @Override
 public boolean canServe(Req req, Res staticResource) throws RuntimeException {
   String name = staticResource.getName();
   return !staticResource.isHidden() && !PRIVATE.matcher(name).find();
 }
 private Res getResource(String filename, boolean partial) {
   String sub = partial ? "/partials/" : "/templates/";
   return Res.from(filename, "rapidoid" + sub);
 }
示例#5
0
 @SuppressWarnings("unchecked")
 public static PageMenu parse(String filename) {
   byte[] yaml = Res.from(filename).getBytesOrNull();
   Map<String, ?> data = yaml != null ? YAML.parse(yaml, Map.class) : null;
   return from(data);
 }
  private Mustache compileIfChanged(String filename, boolean partial) {
    Mustache template = mustacheCache.getIfPresent(filename);

    if (template == null) {

      String desc = partial ? "partial" : "template";
      Log.debug("Compiling Mustache " + desc, "name", filename);

      Res res = getResource(filename, partial);
      template = customCompile(filename, res);

      res.onChange(
              "mustache",
              new Runnable() {
                @Override
                public void run() {
                  invalidateCache();
                }
              })
          .trackChanges();

      mustacheCache.put(filename, template);
    }

    return template;
  }