Package httl

Examples of httl.Template


   */
  public void inited() {
    if (importMacros != null && importMacros.length > 0) {
      for (String importMacro : importMacros) {
        try {
          Template importMacroTemplate = engine.getTemplate(importMacro);
          importMacroTemplates.putAll(importMacroTemplate.getMacros());
        } catch (Exception e) {
          throw new IllegalStateException(e.getMessage(), e);
        }
      }
    }
View Full Code Here


    if (macroTypes == null || macroTypes.size() == 0) {
      return Collections.unmodifiableMap(macros);
    }
    for (Map.Entry<String, Class<?>> entry : macroTypes.entrySet()) {
      try {
        Template macro = (Template) entry.getValue()
            .getConstructor(Engine.class, Interceptor.class, Compiler.class, Switcher.class, Switcher.class, Filter.class,
                Formatter.class, Converter.class, Converter.class, Map.class, Map.class, Resource.class, Template.class, Node.class)
            .newInstance(engine, interceptor, compiler, filterSwitcher, formatterSwitcher, filter, formatter,
                mapConverter, outConverter, functions, importMacros, resource, parent, root);
        macros.put(entry.getKey(), macro);
View Full Code Here

          throw new ParseException("Failed to invoke method " + ClassUtils.getMethodFullName(filteredName, types) + " in class " + function.getCanonicalName() + ", cause: " + ClassUtils.dumpException(e), node.getOffset());
        }
      }
    }
    if (! found) {
      Template macro;
      Object value = Context.getContext().get(name);
      if (value instanceof Template) {
        macro = (Template) value;
      } else {
        macro = template.getMacros().get(name);
        if (macro == null && importMacros != null) {
          macro = importMacros.get(name);
        }
      }
      if (macro != null) {
        result = macro.evaluate(args);
      } else {
        throw new ParseException("No such macro \"" + filteredName + "\" or import method " + ClassUtils.getMethodFullName(filteredName, types) + ".", node.getOffset());
      }
    }
    parameterStack.push(result);
View Full Code Here

                } catch (NoSuchFieldException e2) {
                }
              }
              if (! found) {
                if (leftParameter instanceof Template) {
                  Template macro = ((Template) leftParameter).getMacros().get(name);
                  if (macro != null) {
                    result = macro.evaluate(args);
                  } else {
                    throw new ParseException("No such macro or method " + name + " in " + leftParameter.getClass().getCanonicalName(), node.getOffset());
                  }
                } else if (leftParameter instanceof Class) {
                  Class<?> function = (Class<?>) leftParameter;
View Full Code Here

   */
  public void inited() {
    if (importMacros != null && importMacros.length > 0) {
      for (String importMacro : importMacros) {
        try {
          Template importMacroTemplate = engine.getTemplate(importMacro);
          importMacroTemplates.putAll(importMacroTemplate.getMacros());
        } catch (Exception e) {
          throw new IllegalStateException(e.getMessage(), e);
        }
      }
    }
View Full Code Here

  public Template translate(Resource resource, Node root, Map<String, Class<?>> defVariableTypes) throws IOException, ParseException {
    if (logger != null && logger.isDebugEnabled()) {
      logger.debug("Compile template " + resource.getName());
    }
    try {
      Template writerTemplate = null;
      Template streamTemplate = null;
      if (isOutputWriter || ! isOutputStream) {
        Class<?> clazz = parseClass(resource, root, defVariableTypes, false, 0);
        writerTemplate = (Template) clazz.getConstructor(Engine.class, Interceptor.class, Compiler.class, Switcher.class, Switcher.class, Filter.class, Formatter.class, Converter.class, Converter.class, Map.class, Map.class, Resource.class, Template.class, Node.class)
            .newInstance(engine, interceptor, compiler, valueFilterSwitcher, formatterSwitcher, valueFilter, formatter, mapConverter, outConverter, functions, importMacroTemplates, resource, null, root);
      }
View Full Code Here

    } else if ("out".equals(key)) {
      return Context.getContext().getOut();
    } else if ("level".equals(key)) {
      return Context.getContext().getLevel();
    } else {
      Template template = Context.getContext().getTemplate();
      if (template != null) {
        return template.getMacros().get(key);
      }
      return null;
    }
  }
View Full Code Here

      extendsName = (String) context.get(extendsVariable);
    }
    // extends.default=default.httl
    // 如果默认模板存在,则继承默认模板。
    // 注意:默认模板是从继承模板目录中查找的,即实际为:template.directory + extends.directory + extends.default
    Template template = context.getTemplate();
    if (StringUtils.isEmpty(extendsName) && StringUtils.isNotEmpty(extendsDefault)) {
      String templateName = template.getName();
      String name = UrlUtils.relativeUrl(extendsDefault, templateName);
      if (StringUtils.isNotEmpty(extendsDirectory)) {
        name = extendsDirectory + name;
      }
      if (! name.equals(templateName) && engine.hasResource(name)) {
        extendsName = extendsDefault;
      }
    }
    if (StringUtils.isNotEmpty(extendsName)) {
      // extends.nested=nested
      Object oldNested = null;
      if (StringUtils.isNotEmpty(extendsNested)) {
        oldNested = context.put(extendsNested, new ListenerTemplate(template, listener));
      }
      try {
        Template extend = fileMethod.$extends(extendsName, template.getLocale(), template.getEncoding());
        extend.render(context.getOut());
      } finally {
        if (StringUtils.isNotEmpty(extendsNested)) {
          if (oldNested != null) {
            context.put(extendsNested, oldNested);
          } else {
View Full Code Here

    }
    Engine _engine = engine;
    if ("extends_default.httl".equals(templateName)) {
      _engine = Engine.getEngine("httl-comment-extends.properties");
    }
    Template template = _engine.getTemplate("/templates/" + templateName, Locale.CHINA, encoding, data);
    UnsafeByteArrayOutputStream actualStream = new UnsafeByteArrayOutputStream();
    StringWriter actualWriter = new StringWriter();
    if ("extends_var.httl".equals(templateName)) {
      if (data instanceof Map) {
        ((Map<String, Object>) data).put("extends", "default.httl");
      } else if (data instanceof Model) {
        ((Model) data).setExtends("default.httl");
      }
    }
    try {
      template.render(data, actualWriter);
      template.render(data, actualStream);
    } catch (Throwable e) {
      System.out.println("\n================================\n" +  config + ": " + template.getName() + "\n================================\n");
      e.printStackTrace();
      throw new IllegalStateException(e.getMessage() + "\n================================\n" +  config + ": " + template.getName() + "\n================================\n", e);
    }
    if ("extends_var.httl".equals(templateName)) {
      if (data instanceof Map) {
        ((Map<String, Object>) data).remove("extends");
      } else if (data instanceof Model) {
        ((Model) data).setExtends(null);
      }
    }
    if (data != null && ! (data instanceof String)) { // FIXME JSON数据的Map没有排序,导致断言失败,暂先跳过
      URL url = this.getClass().getClassLoader().getResource(dir + "results/" + templateName + ".txt");
      if (url == null) {
        throw new FileNotFoundException("Not found file: " + dir + "results/" + templateName + ".txt");
      }
      File result = new File(url.getFile());
      if (! result.exists()) {
        throw new FileNotFoundException("Not found file: " + result.getAbsolutePath());
      }
      String expected = IOUtils.readToString(new InputStreamReader(new FileInputStream(result), encoding));
      expected = expected.replace("\r", "");
      if ("httl-comment-text.properties".equals(config)
          && ! template.getSource().contains("read(")) {
        expected = expected.replace("<!--", "").replace("-->", "");
      }
      assertEquals(templateName, expected, actualWriter.getBuffer().toString().replace("\r", ""));
      assertEquals(templateName, expected, new String(actualStream.toByteArray()).replace("\r", ""));
      if ("set_parameters.httl".equals(templateName)) {
View Full Code Here

    return SCOPES;
  }

  public Reader getReader() {
    if (reader == null) {
      Template template = Context.getContext().getTemplate();
      if (template != null) {
        try {
          return template.openReader();
        } catch (IOException e) {
          return null;
        }
      }
    }
View Full Code Here

TOP

Related Classes of httl.Template

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.