001: // THIS SOFTWARE IS PROVIDED BY SOFTARIS PTY.LTD. AND OTHER METABOSS
002: // CONTRIBUTORS ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING,
003: // BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
004: // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SOFTARIS PTY.LTD.
005: // OR OTHER METABOSS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
006: // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
007: // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
008: // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
009: // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
010: // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
011: // EVEN IF SOFTARIS PTY.LTD. OR OTHER METABOSS CONTRIBUTORS ARE ADVISED OF THE
012: // POSSIBILITY OF SUCH DAMAGE.
013: //
014: // Copyright 2000-2005 © Softaris Pty.Ltd. All Rights Reserved.
015: package com.metaboss.sdlctools.services.jdktools.impl;
016:
017: import java.io.PrintWriter;
018: import java.io.StringWriter;
019:
020: import org.apache.commons.logging.Log;
021: import org.apache.commons.logging.LogFactory;
022: import org.apache.velocity.VelocityContext;
023: import org.apache.velocity.app.VelocityEngine;
024: import org.apache.velocity.runtime.RuntimeServices;
025: import org.apache.velocity.runtime.log.LogSystem;
026:
027: import com.metaboss.enterprise.bs.BSException;
028: import com.metaboss.enterprise.bs.BSServiceProviderException;
029: import com.metaboss.sdlctools.services.jdktools.BSVelocityTemplateProcessor;
030: import com.metaboss.sdlctools.services.jdktools.MergeResult;
031: import com.metaboss.util.CollectionUtils;
032:
033: /** This class implements template merging functionality based on Apache Velocity */
034: public class BSVelocityTemplateProcessorImpl implements
035: BSVelocityTemplateProcessor {
036: // Commons Logging instance.
037: private static final Log sLogger = LogFactory
038: .getLog(BSVelocityTemplateProcessorImpl.class);
039:
040: private class SpecialVelocityLogger implements LogSystem {
041: private StringWriter mStringWriter = new StringWriter();
042: private PrintWriter mPrintWriter = new PrintWriter(
043: mStringWriter);
044:
045: public void init(RuntimeServices rs) {
046: }
047:
048: public void logVelocityMessage(int level,
049: java.lang.String message) {
050: if (level == LogSystem.DEBUG_ID)
051: mPrintWriter.print("Velocity Debug Message : ");
052: else if (level == LogSystem.ERROR_ID)
053: mPrintWriter.print("Velocity Error Message : ");
054: else if (level == LogSystem.INFO_ID)
055: mPrintWriter.print("Velocity Info Message : ");
056: else if (level == LogSystem.WARN_ID)
057: mPrintWriter.print("Velocity Warning Message : ");
058: else
059: mPrintWriter.print("Velocity Message : ");
060: mPrintWriter.println(message);
061: }
062:
063: /** Flushes the internal buffers and returns log output */
064: public String getLogOutput() {
065: mPrintWriter.flush();
066: return mStringWriter.toString();
067: }
068: }
069:
070: /* Merges given template with given set of properties. */
071: public MergeResult mergeTemplate(String pSourceTemplate,
072: String pSourceTemplateName, java.util.Map pContextMap)
073: throws BSException {
074: try {
075: if (sLogger.isDebugEnabled()) {
076: sLogger.debug("Merging template : "
077: + pSourceTemplate);
078: sLogger.debug("Source context keys : "
079: + CollectionUtils.toDebugString(pContextMap
080: .keySet()));
081: }
082: SpecialVelocityLogger lMergeLogger = new SpecialVelocityLogger();
083: StringWriter lMergeResultWriter = new StringWriter();
084: VelocityContext lMergeContext = new VelocityContext(
085: pContextMap);
086:
087: VelocityEngine lEngine = new VelocityEngine();
088: lEngine.setProperty(VelocityEngine.RUNTIME_LOG_LOGSYSTEM,
089: lMergeLogger);
090: lEngine.init();
091: if (!lEngine.evaluate(lMergeContext, lMergeResultWriter,
092: pSourceTemplateName, pSourceTemplate))
093: return MergeResult.createMergeFailure(lMergeLogger
094: .getLogOutput());
095: if (sLogger.isDebugEnabled()) {
096: sLogger.debug("Result of merging is : "
097: + lMergeResultWriter.toString());
098: sLogger.debug("Result context keys : "
099: + CollectionUtils.toDebugString(pContextMap
100: .keySet()));
101: }
102: return MergeResult.createMergeSuccess(lMergeResultWriter
103: .toString(), lMergeLogger.getLogOutput());
104: } catch (Exception e) {
105: sLogger.error("Error has occurred while merging "
106: + pSourceTemplate
107: + " template. Context keys are :"
108: + CollectionUtils.toDebugString(pContextMap
109: .keySet()), e);
110: throw new BSServiceProviderException(
111: "Exception caught while invoking Apache Velocity templating engine",
112: e);
113: }
114: }
115: }
|