public FreeMarkerTemplateEngineImpl() {
    super(DEFAULT_TEMPLATE_EXTENSION, DEFAULT_MAX_CACHE_SIZE);

    loader = new FreeMarkerTemplateLoader();
    config = new Configuration(Configuration.VERSION_2_3_22);
    config.setObjectWrapper(new VertxWebObjectWrapper(config.getIncompatibleImprovements()));
    config.setTemplateLoader(loader);
  }
Пример #2
0
  /** Creates a plain text (unparsed) template. */
  private UnboundTemplate(String content, String sourceName, Configuration cfg) {
    NullArgumentException.check(cfg);
    this.cfg = cfg;
    this.sourceName = sourceName;
    this.templateLanguageVersion =
        normalizeTemplateLanguageVersion(cfg.getIncompatibleImprovements());
    this.templateSpecifiedEncoding = null;

    rootElement = new TextBlock(content);
    actualTagSyntax = cfg.getTagSyntax();
    actualNamingConvention = cfg.getNamingConvention();
  }
Пример #3
0
  /**
   * @param reader Reads the template source code
   * @param cfg The FreeMarker configuration settings; some of them influences parsing, also the
   *     resulting {@link UnboundTemplate} will be bound to this.
   * @param assumedEncoding This is the name of the charset that we are supposed to be using. This
   *     is only needed to check if the encoding specified in the {@code #ftl} header (if any)
   *     matches this. If this is non-{@code null} and they don't match, a {@link
   *     WrongEncodingException} will be thrown by the parser.
   * @param sourceName Shown in error messages as the template "file" location.
   */
  UnboundTemplate(Reader reader, String sourceName, Configuration cfg, String assumedEncoding)
      throws IOException {
    NullArgumentException.check(cfg);
    this.cfg = cfg;
    this.sourceName = sourceName;
    this.templateLanguageVersion =
        normalizeTemplateLanguageVersion(cfg.getIncompatibleImprovements());

    LineTableBuilder ltbReader;
    try {
      if (!(reader instanceof BufferedReader)) {
        reader = new BufferedReader(reader, 0x1000);
      }
      ltbReader = new LineTableBuilder(reader);
      reader = ltbReader;

      try {
        FMParser parser =
            new FMParser(
                this,
                reader,
                assumedEncoding,
                cfg.getStrictSyntaxMode(),
                cfg.getWhitespaceStripping(),
                cfg.getTagSyntax(),
                cfg.getNamingConvention(),
                cfg.getIncompatibleImprovements().intValue());

        TemplateElement rootElement;
        try {
          rootElement = parser.Root();
        } catch (IndexOutOfBoundsException exc) {
          // There's a JavaCC bug where the Reader throws a RuntimeExcepton and then JavaCC fails
          // with
          // IndexOutOfBoundsException. If that wasn't the case, we just rethrow. Otherwise we
          // suppress the
          // IndexOutOfBoundsException and let the real cause to be thrown later.
          if (!ltbReader.hasFailure()) {
            throw exc;
          }
          rootElement = null;
        }
        this.rootElement = rootElement;

        this.actualTagSyntax = parser._getLastTagSyntax();
        this.actualNamingConvention = parser._getLastNamingConvention();
        this.templateSpecifiedEncoding = parser._getTemplateSpecifiedEncoding();
      } catch (TokenMgrError exc) {
        // TokenMgrError VS ParseException is not an interesting difference for the user, so we just
        // convert it
        // to ParseException
        throw exc.toParseException(this);
      }
    } catch (ParseException e) {
      e.setTemplateName(getSourceName());
      throw e;
    } finally {
      reader.close();
    }

    // Throws any exception that JavaCC has silently treated as EOF:
    ltbReader.throwFailure();

    if (prefixToNamespaceURIMapping != null) {
      prefixToNamespaceURIMapping = Collections.unmodifiableMap(prefixToNamespaceURIMapping);
      namespaceURIToPrefixMapping = Collections.unmodifiableMap(namespaceURIToPrefixMapping);
    }
  }