head	1.11;
access;
symbols
	dlr:1.1.1
	TIGRIS_0_9_2_4:1.7
	TIGRIS_0_9_2_3:1.7
	HELM_PEER_PORT_BRANCH:1.1.1.1
	TURBINE_PEER:1.9
	TIGRIS_0_9_2_1:1.9
	TIGRIS_0_9_2:1.7.0.2;
locks; strict;
comment	@# @;


1.11
date	2001.02.22.00.37.53;	author dlr;	state dead;
branches;
next	1.10;

1.10
date	2001.02.21.01.08.25;	author dlr;	state Exp;
branches;
next	1.9;

1.9
date	2001.01.26.18.29.09;	author edk;	state Exp;
branches
	1.9.2.1;
next	1.8;

1.8
date	2001.01.26.05.24.08;	author edk;	state dead;
branches;
next	1.7;

1.7
date	2001.01.17.22.51.47;	author dlr;	state Exp;
branches;
next	1.6;

1.6
date	2001.01.17.01.09.32;	author dlr;	state Exp;
branches;
next	1.5;

1.5
date	2001.01.17.01.07.50;	author dlr;	state Exp;
branches;
next	1.4;

1.4
date	2001.01.16.21.29.15;	author dlr;	state Exp;
branches;
next	1.3;

1.3
date	2001.01.16.19.24.48;	author dlr;	state Exp;
branches;
next	1.2;

1.2
date	2001.01.16.19.22.59;	author dlr;	state Exp;
branches;
next	1.1;

1.1
date	2001.01.16.04.01.34;	author dlr;	state Exp;
branches
	1.1.1.1;
next	;

1.1.1.1
date	2001.02.21.00.35.21;	author dlr;	state Exp;
branches;
next	;

1.9.2.1
date	2001.01.28.01.02.19;	author dlr;	state dead;
branches;
next	1.9.2.2;

1.9.2.2
date	2001.02.09.16.03.00;	author leonardr;	state Exp;
branches;
next	1.9.2.3;

1.9.2.3
date	2001.02.15.20.50.44;	author leonardr;	state Exp;
branches;
next	1.9.2.4;

1.9.2.4
date	2001.02.16.08.04.37;	author dlr;	state Exp;
branches;
next	;


desc
@@


1.11
log
@Now using TurbineWebMacroService instead of TemplateInterpolator.
@
text
@/* ================================================================
 * Copyright (c) 2000-2001 CollabNet.  All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 * 
 * 1. Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 * 
 * 2. Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * 
 * 3. The end-user documentation included with the redistribution, if
 * any, must include the following acknowlegement: "This product includes
 * software developed by CollabNet (http://www.collab.net/)."
 * Alternately, this acknowlegement may appear in the software itself, if
 * and wherever such third-party acknowlegements normally appear.
 * 
 * 4. The hosted project names must not be used to endorse or promote
 * products derived from this software without prior written
 * permission. For written permission, please contact info@@collab.net.
 * 
 * 5. Products derived from this software may not use the "Tigris" name
 * nor may "Tigris" appear in their names without prior written
 * permission of CollabNet.
 * 
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL COLLABNET OR ITS CONTRIBUTORS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * ====================================================================
 * 
 * This software consists of voluntary contributions made by many
 * individuals on behalf of CollabNet.
 */

package org.tigris.helm.tool;

import java.io.*;

import org.apache.turbine.util.Log;

import org.webmacro.*;
import org.webmacro.engine.*;
import org.webmacro.broker.ResourceBroker;
import org.webmacro.broker.BrokerInitException;

import org.joist.util.JoistProperties;

/**
 * TemplateInterpolator is a convenience class for evaluating a template with
 * a given context.
 *
 * @@author <a href="mailto:dlr@@collab.net">Daniel Rall</a>
 * @@author <a href="mailto:leonardr@@collab.net">Leonard Richardson</a>
 * @@version $Id: TemplateInterpolator.java,v 1.10 2001/02/21 01:08:25 dlr Exp $
 */
public class TemplateInterpolator
{
    /**
     * Turning debugging on and off.
     */
    private static final boolean DEBUG = true;

    /**
     * The path to WebMacro properties file.
     */
    private static final String WM_PROPS_FILE =
        JoistProperties.getProperty("SANDBOX") + File.separator + "conf" +
        File.separator + "WebMacro.properties";

    /**
     * The path to the base template directory (i.e.
     * <code>$SANDBOX/site/templates</code>).
     */
    private static final String BASE_TEMPLATE_PATH = 
        JoistProperties.getProperty("SANDBOX") + File.separator + "site" +
        File.separator + "templates";

    /**
     * ISO 8859-1, Latin alphabet No. 1 encoding used when instantiating
     * <code>BuildContext</code> objects.  This is one of the default encodings
     * included in rt.jar.
     */
    private static final String ENCODING = "ISO8859_1";

    /**
     * The broker to use as a context factory.
     */
    private static Broker broker;

    static
    {
        try
        {
            broker = new ResourceBroker(WM_PROPS_FILE);
        }
        catch (BrokerInitException e)
        {
            Log.error("Unable to initialize ResourceBroker: ", e);
        }
    }

    /**
     * Creates a new <code>BuildContext</code> instance for use outside of a
     * <code>WMServlet</code> (where a <code>Broker</code> is not readily
     * available).
     */
    public static final BuildContext createContext ()
    {
        return new BuildContext(broker, ENCODING);
    }

    /**
     * Macro to avoid duplicate error handling.
     */
    private static final String interpolationError (String template,
                                                    Exception e)
    {
        Log.error("Unable to interpolate template: " + template+ ": ", e);
        return "";
    }

    /**
     * Derives the full path to a template from the package of the specified
     * <code>Class</code> and the template file name.  On a UNIX system, a 
     * path of <code><i>BASE_TEMPLATE_PATH</i>/com/foo/Message.wm</code> would 
     * be derived from a class name of <code>com.foo.Bar</code> and a template 
     * of <code>Message.wm</code>.
     *
     * @@param c        The <code>Class</code> to derive the path from.
     * @@param template The template name to append to the end of the path.
     * @@return         The full path to the template.
     */
    private static String derivePath (Class c, String template)
    {
        StringBuffer path = new StringBuffer(BASE_TEMPLATE_PATH);

        String className = c.getName();
        int lastDot = className.lastIndexOf('.');
        if (lastDot != -1)
        {
            StringBuffer pkg =
                new StringBuffer(className.substring(0, lastDot));
            int len = pkg.length();
            for (int i = 0; i < len; i++)
            {
                if (pkg.charAt(i) == '.')
                {
                    pkg.setCharAt(i, File.separatorChar);
                }
            }
            path.append(File.separatorChar).append(pkg);
        }
        path.append(File.separatorChar).append(template);

        return path.toString();
    }

    /**
     * Interpolate the specified template with the provided context.  On a
     * UNIX system, a path of
     * <code><i>BASE_TEMPLATE_PATH</i>/com/foo/Message.wm</code> would be 
     * derived from a class name of <code>com.foo.Bar</code> and a template of
     * <code>Message.wm</code>.
     *
     * @@param broker   The <code>Broker</code> to get a parser from.
     * @@param template The WebMacro template to interpolate.
     * @@param context  The context in which to interpolate the template.
     * @@param caller   The calling class from which the file system path to
     *                 the template is derived.
     * @@return         The interpolated template, or the empty string on error.
     */
    public static String interpolate (Broker broker, String template,
                                      BuildContext context, Class caller)
    {
        String pathToTemplate = derivePath(caller, template);
        try
        {
            if (DEBUG) Log.debug("pathToTemplate=" + pathToTemplate);
            WMParser parser = new WMParser(broker);
            BlockBuilder parsed =
                parser.parseBlock(template, new FileReader(pathToTemplate));
            Block block = (Block)parsed.build(context);
            return (String)block.evaluate(context);
        }
        catch (ParseToolException e)
        {
            return interpolationError(template, e);
        }
        catch (IOException e)
        {
            return interpolationError(template, e);
        }
        catch (BuildException e)
        {
            return interpolationError(template, e);
        }
        catch (ContextException e)
        {
            return interpolationError(template, e);
        }
    }

    /**
     * Interpolate the specified template with the provided context.  Wraps
     * three parameter version, supplying <code>Broker</code>.
     *
     * @@see #interpolate(Broker, String, BuildContext, Class)
     */
    public static String interpolate (String template, BuildContext context,
                                      Class caller)
    {
        return interpolate(broker, template, context, caller);
    }
}
@


1.10
log
@This is the highest revision from the HELM_PEER_PORT_BRANCH.  The previous import caused too many conflicts to resolve manually
@
text
@d66 1
a66 1
 * @@version $Id: TemplateInterpolator.java,v 1.9.2.4 2001/02/16 08:04:37 dlr Exp $
@


1.9
log
@readding all the files as part of the process of returning to the way
things were as of tuesday
@
text
@d2 1
a2 1
 * Copyright (c) 2000 CollabNet.  All rights reserved.
d51 2
a56 1
import org.webmacro.util.Log;
d66 1
a66 1
 * @@version $Id: TemplateInterpolator.java,v 1.7 2001/01/17 22:51:47 dlr Exp $
d73 1
a73 1
    private static final boolean debug = true;
a101 5
    /**
     * For logging.
     */
    private static Log log = new Log("tmplInterp", "TemplateInterpolator");    

d110 1
a110 2
            log.error("Unable to initialize ResourceBroker");
            log.exception(e);
d130 1
a130 2
        log.error("Unable to interpolate template: " + template);
        log.exception(e);
d190 1
a190 1
            if (debug) log.info("pathToTemplate=" + pathToTemplate);
@


1.9.2.1
log
@Porting to the Turbine Peer system.  -dlr and leonardr
@
text
@d65 1
a65 1
 * @@version $Id: TemplateInterpolator.java,v 1.9 2001/01/26 18:29:09 edk Exp $
@


1.9.2.2
log
@Ported TemplateInterpolator.
@
text
@a50 2
import org.apache.turbine.util.Log;

d55 1
d101 5
d114 2
a115 1
            Log.error("Unable to initialize ResourceBroker: ", e);
d135 2
a136 1
        Log.error("Unable to interpolate template: " + template+ ": ", e);
d196 1
a196 1
            if (debug) Log.debug("pathToTemplate=" + pathToTemplate);
@


1.9.2.3
log
@Normalized copyright dates.
@
text
@d2 1
a2 1
 * Copyright (c) 2000-2001 CollabNet.  All rights reserved.
d66 1
a66 1
 * @@version $Id: TemplateInterpolator.java,v 1.9.2.2 2001/02/09 16:03:00 leonardr Exp $
@


1.9.2.4
log
@Correct case for constants.
@
text
@d66 1
a66 1
 * @@version $Id: TemplateInterpolator.java,v 1.9.2.3 2001/02/15 20:50:44 leonardr Exp $
d73 1
a73 1
    private static final boolean DEBUG = true;
d190 1
a190 1
            if (DEBUG) Log.debug("pathToTemplate=" + pathToTemplate);
@


1.8
log
@revert this stuff to its state as of 2001/01/24 12:42:00 GMT -- with
the following exceptions, which have some different modifications so
they'll work with changes which have happened in joist:

ProjectNewsEdit.java
SkinManager.java
SkinStyleElementValueManager.java
StyleElementManager.java
StyleManager.java

it compiles.  i dunno if it works, though.
@
text
@@


1.7
log
@Duplicated some JavaDoc to provide it for the public interface.
@
text
@d65 1
a65 1
 * @@version $Id: TemplateInterpolator.java,v 1.6 2001/01/17 01:09:32 dlr Exp $
@


1.6
log
@Removed low value debug trace.
@
text
@d65 1
a65 1
 * @@version $Id: TemplateInterpolator.java,v 1.5 2001/01/17 01:07:50 dlr Exp $
d104 1
a104 1
    private static Log log = new Log("TempInt", "TemplateInterpolator");    
d177 5
a181 1
     * Interpolate the specified template with the provided context.
@


1.5
log
@* Added oh so clever derivePath(Class c, String template) method to
  derive the full path to the template from an instance of the caller
  and the template file name.
* Added BASE_TEMPLATE_PATH field used by derivePath().
* Noted that the encoding used is a default one included with rt.jar.
* Added Class parameter to interpolate() methods from which to derive
  the part of path to the template.
* Now correctly extracting a Block from the BlockBuilder in
  interpolate().  Evaluation of the Block supplies the String used as
  the method return value.
@
text
@d65 1
a65 1
 * @@version $Id: TemplateInterpolator.java,v 1.4 2001/01/16 21:29:15 dlr Exp $
a225 1
        if (debug) log.info("WM_PROPS_FILE=" + WM_PROPS_FILE);
@


1.4
log
@* Context factory method now returns a concrete BuildContext to use
  with the interpolate() methods.
* Exceptions now logged internally by interpolate() methods via new
  interpolationError() macro.
* Added JavaDoc.
@
text
@d65 1
a65 1
 * @@version $Id: TemplateInterpolator.java,v 1.3 2001/01/16 19:24:48 dlr Exp $
d82 8
d91 1
a91 1
     * <code>BuildContext</code> objects.  One of the default encodings
d124 1
a124 1
    public static BuildContext createContext ()
d141 36
d180 1
a180 1
     * @@param template The path to the WebMacro template to interpolate.
d182 2
d187 1
a187 1
                                      BuildContext context)
d189 1
d192 1
a192 1
            if (debug) log.info("template=" + template);
d194 4
a197 3
            BlockBuilder parsed = parser.parseBlock(template,
                                                    new FileReader(template));
            return parsed.build(context).toString();
d211 4
d221 1
a221 1
     * @@see #interpolate(Broker, String, BuildContext)
d223 2
a224 1
    public static String interpolate (String template, BuildContext context)
d227 1
a227 1
        return interpolate(broker, template, context);
@


1.3
log
@Renamed newInstance() to createContext(), since it isn't a
TemplateInterpolator instance that is being created.
@
text
@d65 1
a65 1
 * @@version $Id: TemplateInterpolator.java,v 1.2 2001/01/16 19:22:59 dlr Exp $
d112 3
a114 2
     * Creates a new <code>Context</code> instance for use outside of a
     * <code>WMServlet</code>.
d116 1
a116 1
    public static Context createContext ()
d122 11
d134 5
a141 1
        throws ParseToolException, IOException, BuildException
d143 20
a162 5
        if (debug) log.info("template=" + template);
        WMParser parser = new WMParser(broker);
        BlockBuilder parsed = parser.parseBlock(template,
                                                new FileReader(template));
        return parsed.build(context).toString();
d166 4
a169 1
     * Interpolate the specified template with the provided context.
a171 1
        throws ParseToolException, IOException, BuildException
@


1.2
log
@Added a Context factory method for instantiating new Context instances
outside of a WMServlet subclass.
@
text
@d65 1
a65 1
 * @@version $Id: TemplateInterpolator.java,v 1.1 2001/01/16 04:01:34 dlr Exp $
d115 1
a115 1
    public static Context newInstance ()
@


1.1
log
@At Leonard's request, we now have a class to evaluate templates in a given context.
@
text
@d65 1
a65 1
 * @@version $Id: $
d74 3
d82 12
d98 22
d123 2
a124 2
    public String interpolate(Broker broker, String template,
                              BuildContext context)
d137 2
a138 3
    public String interpolate(String template, BuildContext context)
        throws BrokerInitException, ParseToolException, IOException,
               BuildException
d141 1
a141 2
        return interpolate(new ResourceBroker(WM_PROPS_FILE), template,
                           context);
@


1.1.1.1
log
@Replacing the HEAD with the HELM_PEER_PORT_BRANCH.
@
text
@d2 1
a2 1
 * Copyright (c) 2000-2001 CollabNet.  All rights reserved.
a50 2
import org.apache.turbine.util.Log;

d55 1
d65 1
a65 1
 * @@version $Id: TemplateInterpolator.java,v 1.9.2.4 2001/02/16 08:04:37 dlr Exp $
d72 1
a72 1
    private static final boolean DEBUG = true;
a73 3
    /**
     * The path to WebMacro properties file.
     */
d79 1
a79 35
     * The path to the base template directory (i.e.
     * <code>$SANDBOX/site/templates</code>).
     */
    private static final String BASE_TEMPLATE_PATH = 
        JoistProperties.getProperty("SANDBOX") + File.separator + "site" +
        File.separator + "templates";

    /**
     * ISO 8859-1, Latin alphabet No. 1 encoding used when instantiating
     * <code>BuildContext</code> objects.  This is one of the default encodings
     * included in rt.jar.
     */
    private static final String ENCODING = "ISO8859_1";

    /**
     * The broker to use as a context factory.
     */
    private static Broker broker;

    static
    {
        try
        {
            broker = new ResourceBroker(WM_PROPS_FILE);
        }
        catch (BrokerInitException e)
        {
            Log.error("Unable to initialize ResourceBroker: ", e);
        }
    }

    /**
     * Creates a new <code>BuildContext</code> instance for use outside of a
     * <code>WMServlet</code> (where a <code>Broker</code> is not readily
     * available).
d81 1
a81 4
    public static final BuildContext createContext ()
    {
        return new BuildContext(broker, ENCODING);
    }
d84 1
a84 1
     * Macro to avoid duplicate error handling.
d86 3
a88 2
    private static final String interpolationError (String template,
                                                    Exception e)
d90 5
a94 2
        Log.error("Unable to interpolate template: " + template+ ": ", e);
        return "";
d98 1
a98 9
     * Derives the full path to a template from the package of the specified
     * <code>Class</code> and the template file name.  On a UNIX system, a 
     * path of <code><i>BASE_TEMPLATE_PATH</i>/com/foo/Message.wm</code> would 
     * be derived from a class name of <code>com.foo.Bar</code> and a template 
     * of <code>Message.wm</code>.
     *
     * @@param c        The <code>Class</code> to derive the path from.
     * @@param template The template name to append to the end of the path.
     * @@return         The full path to the template.
d100 7
a106 80
    private static String derivePath (Class c, String template)
    {
        StringBuffer path = new StringBuffer(BASE_TEMPLATE_PATH);

        String className = c.getName();
        int lastDot = className.lastIndexOf('.');
        if (lastDot != -1)
        {
            StringBuffer pkg =
                new StringBuffer(className.substring(0, lastDot));
            int len = pkg.length();
            for (int i = 0; i < len; i++)
            {
                if (pkg.charAt(i) == '.')
                {
                    pkg.setCharAt(i, File.separatorChar);
                }
            }
            path.append(File.separatorChar).append(pkg);
        }
        path.append(File.separatorChar).append(template);

        return path.toString();
    }

    /**
     * Interpolate the specified template with the provided context.  On a
     * UNIX system, a path of
     * <code><i>BASE_TEMPLATE_PATH</i>/com/foo/Message.wm</code> would be 
     * derived from a class name of <code>com.foo.Bar</code> and a template of
     * <code>Message.wm</code>.
     *
     * @@param broker   The <code>Broker</code> to get a parser from.
     * @@param template The WebMacro template to interpolate.
     * @@param context  The context in which to interpolate the template.
     * @@param caller   The calling class from which the file system path to
     *                 the template is derived.
     * @@return         The interpolated template, or the empty string on error.
     */
    public static String interpolate (Broker broker, String template,
                                      BuildContext context, Class caller)
    {
        String pathToTemplate = derivePath(caller, template);
        try
        {
            if (DEBUG) Log.debug("pathToTemplate=" + pathToTemplate);
            WMParser parser = new WMParser(broker);
            BlockBuilder parsed =
                parser.parseBlock(template, new FileReader(pathToTemplate));
            Block block = (Block)parsed.build(context);
            return (String)block.evaluate(context);
        }
        catch (ParseToolException e)
        {
            return interpolationError(template, e);
        }
        catch (IOException e)
        {
            return interpolationError(template, e);
        }
        catch (BuildException e)
        {
            return interpolationError(template, e);
        }
        catch (ContextException e)
        {
            return interpolationError(template, e);
        }
    }

    /**
     * Interpolate the specified template with the provided context.  Wraps
     * three parameter version, supplying <code>Broker</code>.
     *
     * @@see #interpolate(Broker, String, BuildContext, Class)
     */
    public static String interpolate (String template, BuildContext context,
                                      Class caller)
    {
        return interpolate(broker, template, context, caller);
@


