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

com.pojosontheweb.ttt.Template Maven / Gradle / Ivy

The newest version!
package com.pojosontheweb.ttt;

import java.io.IOException;
import java.io.Writer;

import static com.pojosontheweb.ttt.Util.toRtExNoResult;

/**
 * Base template class. Defines the policy for writing
 * to the output from expressions :
 * 
    *
  • null writes nothing
  • *
  • objects implementing ITemplate are rendered
  • *
  • objects implementing IBodyTemplate are opened
  • *
  • Strings are rendered as is
  • *
  • other objects are rendered using their toString() method
  • *
*/ public abstract class Template implements ITemplate { protected void write(Writer out, Object o) { toRtExNoResult(() -> { if (o != null) { if (o instanceof ITemplate) { ((ITemplate) o).render(out); } else if (o instanceof String) { out.write((String) o); } else if (o instanceof IBodyTemplate) { IBodyTemplate st = (IBodyTemplate)o; // call the open method on the sub template // and let it manage stuff. SubTemplate is an // auto-closable and should be used with // a try-resource block, therefore we do not // need to call close ourselves... st.open((TttWriter)out); } else { out.write(o.toString()); } } }); } protected void write(Writer out, Object first, Object... rest) { write(out, first); if (rest != null) { for (Object o : rest) { write(out, o); } } } @Override public final void render(Writer out) { // wrap the supplied writer into a TttWriter // if not already done... final TttWriter tw; if (out instanceof TttWriter) { tw = (TttWriter)out; } else { tw = new TttWriter(out); } toRtExNoResult("error while rendering the template", () -> doRender(tw)); } protected abstract void doRender(TttWriter tw) throws Exception; }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy