/**
   * Sets the settings of the {@link Template} which are not yet set in the {@link Template} and are
   * set in this {@link TemplateConfigurer}, leaves the other settings as is. A setting is said to
   * be set in a {@link TemplateConfigurer} or {@link Template} if it was explicitly set via a
   * setter method on that object, as opposed to be inherited from the {@link Configuration}.
   *
   * <p>Note that the {@code encoding} setting of the {@link Template} counts as unset if it's
   * {@code null}, even if {@code null} was set via {@link Template#setEncoding(String)}.
   *
   * @throws IllegalStateException If the parent configuration wasn't yet set.
   */
  public void configure(Template template) {
    checkParentConfigurationSet();
    Configuration cfg = getParentConfiguration();
    if (template.getConfiguration() != cfg) {
      // This is actually not a problem right now, but for future BC we enforce this.
      throw new IllegalArgumentException(
          "The argument Template doesn't belong to the same Configuration as the TemplateConfigurer");
    }

    if (isAPIBuiltinEnabledSet() && !template.isAPIBuiltinEnabledSet()) {
      template.setAPIBuiltinEnabled(isAPIBuiltinEnabled());
    }
    if (isArithmeticEngineSet() && !template.isArithmeticEngineSet()) {
      template.setArithmeticEngine(getArithmeticEngine());
    }
    if (isAutoFlushSet() && !template.isAutoFlushSet()) {
      template.setAutoFlush(getAutoFlush());
    }
    if (isBooleanFormatSet() && !template.isBooleanFormatSet()) {
      template.setBooleanFormat(getBooleanFormat());
    }
    if (isClassicCompatibleSet() && !template.isClassicCompatibleSet()) {
      template.setClassicCompatibleAsInt(getClassicCompatibleAsInt());
    }
    if (isDateFormatSet() && !template.isDateFormatSet()) {
      template.setDateFormat(getDateFormat());
    }
    if (isDateTimeFormatSet() && !template.isDateTimeFormatSet()) {
      template.setDateTimeFormat(getDateTimeFormat());
    }
    if (isEncodingSet() && template.getEncoding() == null) {
      template.setEncoding(getEncoding());
    }
    if (isLocaleSet() && !template.isLocaleSet()) {
      template.setLocale(getLocale());
    }
    if (isLogTemplateExceptionsSet() && !template.isLogTemplateExceptionsSet()) {
      template.setLogTemplateExceptions(getLogTemplateExceptions());
    }
    if (isNewBuiltinClassResolverSet() && !template.isNewBuiltinClassResolverSet()) {
      template.setNewBuiltinClassResolver(getNewBuiltinClassResolver());
    }
    if (isNumberFormatSet() && !template.isNumberFormatSet()) {
      template.setNumberFormat(getNumberFormat());
    }
    if (isObjectWrapperSet() && !template.isObjectWrapperSet()) {
      template.setObjectWrapper(getObjectWrapper());
    }
    if (isOutputEncodingSet() && !template.isOutputEncodingSet()) {
      template.setOutputEncoding(getOutputEncoding());
    }
    if (isShowErrorTipsSet() && !template.isShowErrorTipsSet()) {
      template.setShowErrorTips(getShowErrorTips());
    }
    if (isSQLDateAndTimeTimeZoneSet() && !template.isSQLDateAndTimeTimeZoneSet()) {
      template.setSQLDateAndTimeTimeZone(getSQLDateAndTimeTimeZone());
    }
    if (isTemplateExceptionHandlerSet() && !template.isTemplateExceptionHandlerSet()) {
      template.setTemplateExceptionHandler(getTemplateExceptionHandler());
    }
    if (isTimeFormatSet() && !template.isTimeFormatSet()) {
      template.setTimeFormat(getTimeFormat());
    }
    if (isTimeZoneSet() && !template.isTimeZoneSet()) {
      template.setTimeZone(getTimeZone());
    }
    if (isURLEscapingCharsetSet() && !template.isURLEscapingCharsetSet()) {
      template.setURLEscapingCharset(getURLEscapingCharset());
    }

    copyDirectCustomAttributes(template, false);
  }
Example #2
0
  /**
   * Constructs a template on-the-fly and returns it embedded in a {@link TemplateTransformModel}.
   *
   * <p>The built-in has two arguments: the arguments passed to the method. It can receive at least
   * one and at most two arguments, both must evaluate to a scalar. The first scalar is interpreted
   * as a template source code and a template is built from it. The second (optional) is used to
   * give the generated template a name.
   *
   * @return a {@link TemplateTransformModel} that when executed inside a <tt>&lt;transform></tt>
   *     block will process the generated template just as if it had been <tt>&lt;transform></tt>-ed
   *     at that point.
   */
  @Override
  TemplateModel _eval(Environment env) throws TemplateException {
    TemplateModel model = target.eval(env);
    Expression sourceExpr = null;
    String id = "anonymous_interpreted";
    if (model instanceof TemplateSequenceModel) {
      sourceExpr =
          ((Expression)
              new DynamicKeyName(target, new NumberLiteral(Integer.valueOf(0)))
                  .copyLocationFrom(target));
      if (((TemplateSequenceModel) model).size() > 1) {
        id =
            ((Expression)
                    new DynamicKeyName(target, new NumberLiteral(Integer.valueOf(1)))
                        .copyLocationFrom(target))
                .evalAndCoerceToString(env);
      }
    } else if (model instanceof TemplateScalarModel) {
      sourceExpr = target;
    } else {
      throw new UnexpectedTypeException(
          target,
          model,
          "sequence or string",
          new Class[] {TemplateSequenceModel.class, TemplateScalarModel.class},
          env);
    }
    String templateSource = sourceExpr.evalAndCoerceToString(env);
    Template parentTemplate = env.getTemplate();

    final Template interpretedTemplate;
    try {
      interpretedTemplate =
          new Template(
              (parentTemplate.getName() != null ? parentTemplate.getName() : "nameless_template")
                  + "->"
                  + id,
              null,
              new StringReader(templateSource),
              parentTemplate.getConfiguration(),
              parentTemplate.getParserConfiguration(),
              null);
    } catch (IOException e) {
      throw new _MiscTemplateException(
          this,
          e,
          env,
          new Object[] {
            "Template parsing with \"?",
            key,
            "\" has failed with this error:\n\n",
            MessageUtil.EMBEDDED_MESSAGE_BEGIN,
            new _DelayedGetMessage(e),
            MessageUtil.EMBEDDED_MESSAGE_END,
            "\n\nThe failed expression:"
          });
    }

    interpretedTemplate.setLocale(env.getLocale());
    return new TemplateProcessorModel(interpretedTemplate);
  }