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:
042: package org.netbeans.modules.uml.core.reverseengineering.reframework.parsingframework;
043:
044: import org.netbeans.modules.uml.core.coreapplication.ICoreProduct;
045: import org.netbeans.modules.uml.core.generativeframework.IExpansionVariable;
046: import org.netbeans.modules.uml.core.generativeframework.ITemplateManager;
047: import org.netbeans.modules.uml.core.generativeframework.IVariableFactory;
048: import org.netbeans.modules.uml.core.metamodel.core.foundation.IElement;
049: import org.netbeans.modules.uml.core.support.umlsupport.ProductRetriever;
050: import org.netbeans.modules.uml.core.support.umlsupport.StringUtilities;
051: import org.netbeans.modules.uml.core.support.umlutils.DataFormatter;
052: import org.netbeans.modules.uml.core.support.umlutils.ETList;
053: import org.netbeans.modules.uml.core.support.umlutils.IDataFormatter;
054:
055: /**
056: * @author sumitabhk
057: *
058: */
059: public class CodeGenerationScript implements ICodeGenerationScript {
060: String m_Name;
061: String m_ScriptFile;
062: IDataFormatter m_pDataFormatter;
063: ILanguage m_rawLanguage;
064:
065: /* (non-Javadoc)
066: * @see org.netbeans.modules.uml.core.reverseengineering.reframework.parsingframework.ICodeGenerationScript#getName()
067: */
068: public String getName() {
069: return m_Name;
070: }
071:
072: /* (non-Javadoc)
073: * @see org.netbeans.modules.uml.core.reverseengineering.reframework.parsingframework.ICodeGenerationScript#setName(java.lang.String)
074: */
075: public void setName(String name) {
076: m_pDataFormatter = null;
077: m_Name = name;
078: }
079:
080: /* (non-Javadoc)
081: * @see org.netbeans.modules.uml.core.reverseengineering.reframework.parsingframework.ICodeGenerationScript#getFile()
082: */
083: public String getFile() {
084: return m_ScriptFile;
085: }
086:
087: /* (non-Javadoc)
088: * @see org.netbeans.modules.uml.core.reverseengineering.reframework.parsingframework.ICodeGenerationScript#setFile(java.lang.String)
089: */
090: public void setFile(String fileName) {
091: m_pDataFormatter = null;
092: m_ScriptFile = fileName;
093: }
094:
095: /* (non-Javadoc)
096: * @see org.netbeans.modules.uml.core.reverseengineering.reframework.parsingframework.ICodeGenerationScript#execute(java.lang.Object)
097: */
098: public String execute(IElement pElement) {
099: if (pElement == null || m_Name == null
100: || m_Name.trim().length() == 0 || m_ScriptFile == null
101: || m_ScriptFile.trim().length() == 0)
102: return null;
103:
104: if ("gt".equals(StringUtilities.getExtension(m_ScriptFile))) {
105: ICoreProduct pProduct = ProductRetriever.retrieveProduct();
106: if (pProduct != null) {
107: ITemplateManager pMan = pProduct.getTemplateManager();
108: if (pMan != null) {
109: ETList<IExpansionVariable> pOldOverrides = null;
110: IVariableFactory pFact = pMan.getFactory();
111: if (pFact != null) {
112: // Check the Language this script is owned by to see
113: // if there are any expansion variable overrides to worry
114: // about
115:
116: if (m_rawLanguage != null) {
117: ETList<IExpansionVariable> pVars = m_rawLanguage
118: .getExpansionVariables();
119:
120: if (pVars != null) {
121: pOldOverrides = pFact
122: .getOverrideVariables();
123: pFact.setOverrideVariables(pVars);
124: }
125: return pMan.expandTemplate(m_ScriptFile,
126: pElement);
127: }
128: }
129: }
130: } else {
131: // Looking for an XSLT script
132:
133: // See if we need to create a Data Formatter.
134: if (m_pDataFormatter == null) {
135: m_pDataFormatter = new DataFormatter();
136: // ... and initialize it
137: m_pDataFormatter.addScript(m_Name, m_ScriptFile);
138: }
139: // We've already created a data formatter and initialized it.
140: return m_pDataFormatter.formatElement(pElement, m_Name);
141: }
142: }
143: return null;
144: }
145:
146: /* (non-Javadoc)
147: * @see org.netbeans.modules.uml.core.reverseengineering.reframework.parsingframework.ICodeGenerationScript#getLanguage()
148: */
149: public ILanguage getLanguage() {
150: return m_rawLanguage;
151: }
152:
153: /* (non-Javadoc)
154: * @see org.netbeans.modules.uml.core.reverseengineering.reframework.parsingframework.ICodeGenerationScript#setLanguage(org.netbeans.modules.uml.core.reverseengineering.reframework.parsingframework.ILanguage)
155: */
156: public void setLanguage(ILanguage language) {
157: m_rawLanguage = language;
158: }
159:
160: }
|