001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.modules.sql.framework.codegen;
042:
043: import java.io.StringWriter;
044: import java.util.Properties;
045: import org.apache.velocity.VelocityContext;
046: import org.apache.velocity.app.VelocityEngine;
047: import net.java.hulp.i18n.Logger;
048: import java.util.concurrent.atomic.AtomicReference;
049: import org.netbeans.modules.etl.logger.Localizer;
050: import org.netbeans.modules.etl.logger.LogUtil;
051:
052: /**
053: * @author Ritesh Adval
054: * @author Ahimanikya Satapathy
055: * @version $Revision$
056: */
057: public class TemplateBuilder {
058:
059: /** LOG_CATEGORY is the name of this class. */
060: private static final String LOG_CATEGORY = TemplateBuilder.class
061: .getName();
062: /** Default encoding for templates. */
063: private static final String TEMPLATE_ENCODING = "ISO-8859-1";
064: /** Prefix for global Velocity engine * */
065: private static final String SQLFRAMEWORK_DB_PREFIX = "org/netbeans/modules/sql/framework/codegen/";
066: private static AtomicReference<VelocityEngine> VELOCITY_ENGINE = null;
067: private static transient final Logger mLogger = LogUtil
068: .getLogger(TemplateBuilder.class.getName());
069: private static transient final Localizer mLoc = Localizer.get();
070:
071: /**
072: * Get VelocityEngine after initializing to use "autoload.Velocity" module's
073: * resource loader.
074: */
075: private static VelocityEngine getVelocityEngine() {
076: synchronized (TemplateBuilder.class) {
077: if (VELOCITY_ENGINE == null) {
078: // Velocity engine and resource loader class from "autoload.velocity" NB Module.
079: VELOCITY_ENGINE = new AtomicReference(
080: new VelocityEngine());
081: Properties velProp = new Properties();
082: velProp.put("resource.loader", "class");
083: velProp.put("class.resource.loader.description",
084: "Velocity Classpath Resource Loader");
085: velProp
086: .setProperty("class.resource.loader.class",
087: "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
088: try {
089: VELOCITY_ENGINE.get().init(velProp);
090: } catch (Exception e) {
091: throw new RuntimeException(
092: "Could not initialize velocity engine: "
093: + e);
094: }
095: }
096: }
097: return VELOCITY_ENGINE.get();
098: }
099:
100: public static String generateSql(String templateFile,
101: VelocityContext context) {
102: try {
103: // Instantiate and initialize Velocity engine.
104: VelocityEngine ve = getVelocityEngine();
105: // Lets render a template
106: StringWriter sw = new StringWriter();
107: ve.mergeTemplate(SQLFRAMEWORK_DB_PREFIX + templateFile,
108: TEMPLATE_ENCODING, context, sw);
109: return sw.toString();
110: } catch (Exception ex) {
111: mLogger
112: .errorNoloc(
113: mLoc
114: .t(
115: "PRSR090: Problem in initializing/merging Velocity template:{0}",
116: LOG_CATEGORY), ex);
117: return null;
118: }
119: }
120: }
|