Package org.onemind.swingweb.templaterender

Source Code of org.onemind.swingweb.templaterender.JxpTemplateEngine

/*
* Copyright (C) 2004 TiongHiang Lee
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not,  write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*
* Email: thlee@onemindsoft.org
*/

package org.onemind.swingweb.templaterender;

import java.awt.Component;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.HashMap;
import java.util.Map;
import org.onemind.awtbridge.peer.BridgePeer;
import org.onemind.awtbridge.render.RenderingException;
import org.onemind.commons.java.util.ObjectUtils;
import org.onemind.commons.java.util.StringUtils;
import org.onemind.commons.java.xml.digest.SaxDigesterHandler;
import org.onemind.jxp.config.JxpConfigDigester;
import org.onemind.swingweb.Constants;
import org.onemind.swingweb.SwingWebContext;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
/**
* Jxp template engine
* @author TiongHiang Lee (thlee@onemindsoft.org)
*/
public class JxpTemplateEngine extends JxpConfigDigester implements TemplateEngine
{

    /** The template resolver * */
    private JxpTemplateResolver _templateResolver;

    /** debug template **/
    private boolean _debugTemplate = false;

    /** resume the rendering on exception **/
    private boolean _resumeOnException = true;

    /** whether to dump exception as comment **/
    private boolean _dumpExceptionAsComment = true;

    /** the template ext **/
    private String _templateExt;

    /** the image template **/
    private String _imageTemplateLoc;

    /**
     * Constructor
     */
    public JxpTemplateEngine()
    {
        super();
    }

    /**
     * {@inheritDoc}
     */
    public void renderTemplate(TemplateRenderContext context, BridgePeer peer, Writer writer) throws RenderingException
    {
        Object comObject = peer.getComponentObject();
        try
        {
            String templateLoc = (String) context.getContext().getComponentProperty(comObject, KEY_COMPONENT_TEMPLATE);
            if (templateLoc == null)
            {
                Object renderAsImage = peer.getComponentProperty(Constants.SW_RENDER_AS_IMAGE);
                if (renderAsImage != null && comObject instanceof Component && !StringUtils.isNullOrEmpty(_imageTemplateLoc))
                {
                    templateLoc = _imageTemplateLoc;
                } else
                {
                    templateLoc = (String) _templateResolver.resolve(comObject.getClass());
                }
            }
            if (templateLoc == null)
            {
                throw new RenderingException("Cannot resolve rendering template for " + comObject);
            }
            if (_debugTemplate)
            {
                writer.write("\n<!-- start " + templateLoc + " for " + comObject + "-->\n");
            }
            getProcessor().process(templateLoc, writer, getEnvironment(context, peer));
            if (_debugTemplate)
            {
                writer.write("\n<!-- end " + templateLoc + " for " + comObject + "-->\n");
            }
            writer.flush();
        } catch (Exception e)
        {
            if (!_resumeOnException)
            {
                if (e instanceof RenderingException)
                {
                    throw (RenderingException) e;
                } else if (e.getCause() instanceof RenderingException)
                {
                    throw (RenderingException) e.getCause();
                } else
                {
                    throw new RenderingException("Error rendering " + comObject, e);
                }
            } else
            {
                try
                {
                    writer.write("<font color=\"#ff0000\">" + e.getMessage() + "</font>");
                    if (_dumpExceptionAsComment)
                    {
                        writer.write("\n<!-- ");
                        PrintWriter pwriter = new PrintWriter(writer);
                        e.printStackTrace(pwriter);
                        pwriter.flush();
                    }
                } catch (IOException ioe)
                {
                    throw new RuntimeException(e);
                }
            }
        }
    }

    /**
     * @param context the context
     * @param peer the component peer
     * @return the map as environment the jxp
     */
    private Map getEnvironment(TemplateRenderContext context, BridgePeer peer)
    {
        Map map = new HashMap();
        map.put("context", context.getContext());
        map.put("session", context.getContext().getSession());
        map.put("rendercontext", context);
        map.put("com", peer.getComponentObject());
        map.put("_com", peer);
        map.put("peer", peer);
        map.put("comOpts", context.getContext().getSession().getValue("COMPONENT_OPTIONS"));
        map.put("renderdelegate", peer.getRenderDelegate());
        return map;
    }

    /**
     * {@inheritDoc}
     */
    public void startDigest(SaxDigesterHandler handler, Attributes attrs) throws SAXException
    {
        _debugTemplate = ObjectUtils.toBool(attrs.getValue("debugTemplate"), false);
        _resumeOnException = ObjectUtils.toBool(attrs.getValue("resumeOnException"), true);
        _dumpExceptionAsComment = ObjectUtils.toBool(attrs.getValue("dumpExceptionAsComment"), true);
        _templateExt = attrs.getValue("templateExt");
        if (_templateExt == null)
        {
            _templateExt = ".template";
        }
        _imageTemplateLoc = attrs.getValue("imageTemplate");
        super.startDigest(handler, attrs);
    }

    /**
     * {@inheritDoc}
     */
    public void endDigest(SaxDigesterHandler handler) throws SAXException
    {
        super.endDigest(handler);
        _templateResolver = new JxpTemplateResolver(getProcessor().getContext().getPageSource(), _templateExt);
    }

    /**
     * {@inheritDoc}
     */
    public void getRuntimeInfo(BridgePeer peer, Map env)
    {
        env.put("jxp template", _templateResolver.resolve(peer.getComponentObject().getClass()));
    }
}
TOP

Related Classes of org.onemind.swingweb.templaterender.JxpTemplateEngine

TOP
Copyright © 2018 www.massapi.com. 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.