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 scripting.dev.java.net. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc.
028: *
029: * Portions Copyrighted 2006 Sun Microsystems, Inc.
030: */
031:
032: /*
033: * @author A. Sundararajan
034: */
035: package org.netbeans.libs.freemarker;
036:
037: import javax.script.*;
038: import java.util.*;
039:
040: public class FreemarkerFactory implements ScriptEngineFactory {
041: public String getEngineName() {
042: return "freemarker";
043: }
044:
045: public String getEngineVersion() {
046: return "2.3.8";
047: }
048:
049: public List<String> getExtensions() {
050: return extensions;
051: }
052:
053: public String getLanguageName() {
054: return "freemarker";
055: }
056:
057: public String getLanguageVersion() {
058: return "2.3.8";
059: }
060:
061: public String getMethodCallSyntax(String obj, String m,
062: String... args) {
063: StringBuffer buf = new StringBuffer();
064: buf.append("${");
065: buf.append(obj);
066: buf.append(".");
067: buf.append(m);
068: buf.append("(");
069: if (args.length != 0) {
070: int i = 0;
071: for (; i < args.length - 1; i++) {
072: buf.append("$" + args[i]);
073: buf.append(", ");
074: }
075: buf.append("$" + args[i]);
076: }
077: buf.append(")}");
078: return buf.toString();
079: }
080:
081: public List<String> getMimeTypes() {
082: return mimeTypes;
083: }
084:
085: public List<String> getNames() {
086: return names;
087: }
088:
089: public String getOutputStatement(String toDisplay) {
090: StringBuffer buf = new StringBuffer();
091: int len = toDisplay.length();
092: buf.append("${context.getWriter().write(\"");
093: for (int i = 0; i < len; i++) {
094: char ch = toDisplay.charAt(i);
095: switch (ch) {
096: case '"':
097: buf.append("\\\"");
098: break;
099: case '\\':
100: buf.append("\\\\");
101: break;
102: default:
103: buf.append(ch);
104: break;
105: }
106: }
107: buf.append("\")}");
108: return buf.toString();
109: }
110:
111: public String getParameter(String key) {
112: if (key.equals(ScriptEngine.NAME)) {
113: return getLanguageName();
114: } else if (key.equals(ScriptEngine.ENGINE)) {
115: return getEngineName();
116: } else if (key.equals(ScriptEngine.ENGINE_VERSION)) {
117: return getEngineVersion();
118: } else if (key.equals(ScriptEngine.LANGUAGE)) {
119: return getLanguageName();
120: } else if (key.equals(ScriptEngine.LANGUAGE_VERSION)) {
121: return getLanguageVersion();
122: } else if (key.equals("THREADING")) {
123: return "MULTITHREADED";
124: } else {
125: return null;
126: }
127: }
128:
129: public String getProgram(String... statements) {
130: StringBuffer buf = new StringBuffer();
131: for (int i = 0; i < statements.length; i++) {
132: buf.append(statements[i]);
133: buf.append("\n");
134: }
135: return buf.toString();
136: }
137:
138: public ScriptEngine getScriptEngine() {
139: return new FreemarkerEngine(this );
140: }
141:
142: private static List<String> names;
143: private static List<String> extensions;
144: private static List<String> mimeTypes;
145: static {
146: names = new ArrayList<String>(2);
147: names.add("FreeMarker");
148: names.add("freemarker");
149: names = Collections.unmodifiableList(names);
150: extensions = new ArrayList<String>(1);
151: extensions.add("fm");
152: extensions = Collections.unmodifiableList(extensions);
153: mimeTypes = new ArrayList<String>(0);
154: mimeTypes = Collections.unmodifiableList(mimeTypes);
155: }
156: }
|