KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > libs > freemarker > FreemarkerFactory


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is scripting.dev.java.net. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc.
17  *
18  * Portions Copyrighted 2006 Sun Microsystems, Inc.
19  */

20
21 /*
22  * @author A. Sundararajan
23  */

24 package org.netbeans.libs.freemarker;
25
26 import javax.script.*;
27 import java.util.*;
28
29 public class FreemarkerFactory implements ScriptEngineFactory {
30     public String JavaDoc getEngineName() {
31         return "freemarker";
32     }
33
34     public String JavaDoc getEngineVersion() {
35         return "2.3.8";
36     }
37
38     public List<String JavaDoc> getExtensions() {
39         return extensions;
40     }
41
42     public String JavaDoc getLanguageName() {
43         return "freemarker";
44     }
45
46     public String JavaDoc getLanguageVersion() {
47         return "2.3.8";
48     }
49
50     public String JavaDoc getMethodCallSyntax(String JavaDoc obj, String JavaDoc m, String JavaDoc... args) {
51         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
52         buf.append("${");
53         buf.append(obj);
54         buf.append(".");
55         buf.append(m);
56         buf.append("(");
57         if (args.length != 0) {
58             int i = 0;
59             for (; i < args.length - 1; i++) {
60                 buf.append("$" + args[i]);
61                 buf.append(", ");
62             }
63             buf.append("$" + args[i]);
64         }
65         buf.append(")}");
66         return buf.toString();
67     }
68
69     public List<String JavaDoc> getMimeTypes() {
70         return mimeTypes;
71     }
72
73     public List<String JavaDoc> getNames() {
74         return names;
75     }
76
77     public String JavaDoc getOutputStatement(String JavaDoc toDisplay) {
78         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
79         int len = toDisplay.length();
80         buf.append("${context.getWriter().write(\"");
81         for (int i = 0; i < len; i++) {
82             char ch = toDisplay.charAt(i);
83             switch (ch) {
84             case '"':
85                 buf.append("\\\"");
86                 break;
87             case '\\':
88                 buf.append("\\\\");
89                 break;
90             default:
91                 buf.append(ch);
92                 break;
93             }
94         }
95         buf.append("\")}");
96         return buf.toString();
97     }
98
99     public String JavaDoc getParameter(String JavaDoc key) {
100         if (key.equals(ScriptEngine.NAME)) {
101             return getLanguageName();
102         } else if (key.equals(ScriptEngine.ENGINE)) {
103             return getEngineName();
104         } else if (key.equals(ScriptEngine.ENGINE_VERSION)) {
105             return getEngineVersion();
106         } else if (key.equals(ScriptEngine.LANGUAGE)) {
107             return getLanguageName();
108         } else if (key.equals(ScriptEngine.LANGUAGE_VERSION)) {
109             return getLanguageVersion();
110         } else if (key.equals("THREADING")) {
111             return "MULTITHREADED";
112         } else {
113             return null;
114         }
115     }
116
117     public String JavaDoc getProgram(String JavaDoc... statements) {
118         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
119         for (int i = 0; i < statements.length; i++) {
120             buf.append(statements[i]);
121             buf.append("\n");
122         }
123         return buf.toString();
124     }
125
126     public ScriptEngine getScriptEngine() {
127         return new FreemarkerEngine(this);
128     }
129
130     private static List<String JavaDoc> names;
131     private static List<String JavaDoc> extensions;
132     private static List<String JavaDoc> mimeTypes;
133     static {
134         names = new ArrayList<String JavaDoc>(2);
135         names.add("FreeMarker");
136         names.add("freemarker");
137         names = Collections.unmodifiableList(names);
138         extensions = new ArrayList<String JavaDoc>(1);
139         extensions.add("fm");
140         extensions = Collections.unmodifiableList(extensions);
141         mimeTypes = new ArrayList<String JavaDoc>(0);
142         mimeTypes = Collections.unmodifiableList(mimeTypes);
143     }
144 }
145
Popular Tags