All Downloads are FREE. Search and download functionalities are using the official Maven repository.

juzu.template.TemplateRenderContext Maven / Gradle / Ivy

There is a newer version: 1.2.0
Show newest version
/*
 * Copyright 2013 eXo Platform SAS
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package juzu.template;

import juzu.PropertyMap;
import juzu.PropertyType;
import juzu.impl.template.spi.TemplateStub;
import juzu.impl.template.spi.juzu.dialect.gtmpl.MessageKey;
import juzu.io.AppendableStream;
import juzu.io.Stream;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Collections;
import java.util.Locale;
import java.util.Map;

/** @author Julien Viet */
public class TemplateRenderContext {

  /** . */
  private final Map attributes;

  /** . */
  private final Locale locale;

  /** . */
  private PropertyMap properties;

  /** . */
  protected Stream.Char printer;

  /** . */
  private final TemplateStub stub;

  public TemplateRenderContext(TemplateStub stub) {
    this(stub, Collections.emptyMap());
  }

  public TemplateRenderContext(TemplateStub stub, Map attributes) {
    this(stub, null, attributes, null);
  }

  public TemplateRenderContext(TemplateStub stub, Locale locale) {
    this(stub, null, Collections.emptyMap(), locale);
  }

  public TemplateRenderContext(TemplateStub stub, PropertyMap properties, Map attributes, Locale locale) {
    this.locale = locale;
    this.attributes = attributes;
    this.stub = stub;
    this.properties = properties;
  }

  public Map getAttributes() {
    return attributes;
  }

  public Locale getLocale() {
    return locale;
  }

  public Stream.Char getPrinter() {
    return printer;
  }

  public void setTitle(String title) {
    if (properties != null) {
      properties.setValue(PropertyType.TITLE, title);
    }
  }

  public TemplateStub resolveTemplate(String path) {
    return null;
  }

  public Object resolveBean(String expression) throws InvocationTargetException {
    return null;
  }

  public String resolveMessage(MessageKey key) {
    return key.toString();
  }

  public StringBuilder render() throws IOException {
    StringBuilder buffer = new StringBuilder();
    render(new AppendableStream(buffer));
    return buffer;
  }

  public void render(Stream.Char printer) throws IOException {
    if (this.printer != null) {
      throw new IllegalStateException("Already rendering");
    }

    //
    this.printer = printer;

    //
    try {
      stub.render(this);
    }
    finally {
      this.printer = null;
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy