KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > magnolia > module > admininterface > pages > JavascriptIncludePage


1 /**
2  *
3  * Magnolia and its source-code is licensed under the LGPL.
4  * You may copy, adapt, and redistribute this file for commercial or non-commercial use.
5  * When copying, adapting, or redistributing this document in keeping with the guidelines above,
6  * you are required to provide proper attribution to obinary.
7  * If you reproduce or distribute the document without making any substantive modifications to its content,
8  * please use the following attribution line:
9  *
10  * Copyright 1993-2006 obinary Ltd. (http://www.obinary.com) All rights reserved.
11  *
12  */

13 package info.magnolia.module.admininterface.pages;
14
15 import info.magnolia.cms.core.SystemProperty;
16 import info.magnolia.cms.i18n.MessagesManager;
17 import info.magnolia.cms.i18n.MessagesUtil;
18 import info.magnolia.cms.util.ClasspathResourcesUtil;
19 import info.magnolia.cms.util.FactoryUtil;
20 import info.magnolia.context.MgnlContext;
21 import info.magnolia.context.WebContext;
22 import info.magnolia.module.admininterface.PageMVCHandler;
23
24 import java.io.IOException JavaDoc;
25 import java.io.InputStream JavaDoc;
26 import java.io.PrintWriter JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.HashMap JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.List JavaDoc;
31 import java.util.Map JavaDoc;
32 import java.util.regex.Matcher JavaDoc;
33 import java.util.regex.Pattern JavaDoc;
34
35 import javax.servlet.http.HttpServletRequest JavaDoc;
36 import javax.servlet.http.HttpServletResponse JavaDoc;
37
38 import org.apache.commons.io.IOUtils;
39 import org.apache.commons.lang.BooleanUtils;
40 import org.apache.commons.lang.StringUtils;
41
42
43 /**
44  * @author Fabrizio Giustina
45  * @version $Revision: 7294 $ ($Author: philipp $)
46  */

47 public class JavascriptIncludePage extends PageMVCHandler {
48
49     private static boolean nocache = BooleanUtils.toBoolean(SystemProperty.getProperty("magnolia.develop"));
50
51     private static String JavaDoc[] files;
52
53     private static String JavaDoc[] includes = {
54         "debug.js",
55         "generic.js",
56         "general.js",
57         "controls.js",
58         "tree.js",
59         //"i18n.js", moved alone since it must be initialized first
60
"contextmenu.js",
61         "inline.js"};
62
63     private static Pattern JavaDoc importPattern = Pattern.compile("importClass\\(\"(.*)\"\\);");
64
65     private Map JavaDoc classDefinitions = new HashMap JavaDoc();
66
67     /**
68      * Required constructor.
69      * @param name page name
70      * @param request HttpServletRequest
71      * @param response HttpServletResponse
72      */

73     public JavascriptIncludePage(String JavaDoc name, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) {
74         super(name, request, response);
75     }
76
77     void process(String JavaDoc name, PrintWriter JavaDoc out) throws IOException JavaDoc {
78         Definition def = (Definition) classDefinitions.get(name);
79         if (!def.proceed) {
80             def.proceed = true;
81             for (Iterator JavaDoc iter = def.imports.iterator(); iter.hasNext();) {
82                 String JavaDoc importName = (String JavaDoc) iter.next();
83                 process(importName, out);
84             }
85             out.println(def.content);
86         }
87     }
88
89     /**
90      * @see info.magnolia.cms.servlets.MVCServletHandler#renderHtml(java.lang.String)
91      */

92     public void renderHtml(String JavaDoc view) throws IOException JavaDoc {
93         HttpServletRequest JavaDoc request = getRequest();
94         HttpServletResponse JavaDoc response = getResponse();
95
96         PrintWriter JavaDoc out = response.getWriter();
97         String JavaDoc contextPath = request.getContextPath();
98
99         out.println("var contextPath = '" + contextPath + "';");
100
101         prepareI18n(out);
102
103         for (int i = 0; i < includes.length; i++) {
104             InputStream JavaDoc in = ClasspathResourcesUtil.getStream("/mgnl-resources/admin-js/" + includes[i]);
105             IOUtils.copy(in, out);
106             in.close();
107         }
108
109
110         // finding files in classpath is too expensive, just cache the list of paths!
111
if (files == null || nocache) {
112             files = ClasspathResourcesUtil.findResources(new ClasspathResourcesUtil.Filter() {
113
114                 public boolean accept(String JavaDoc name) {
115                     return name.startsWith("/mgnl-resources/js-classes") && name.endsWith(".js");
116                 }
117             });
118         }
119
120         // request.getRequestDispatcher("/.resources/js-libs/*.js").include(request, response);
121

122         for (int j = 0; j < files.length; j++) {
123             String JavaDoc name = files[j];
124             Definition def = new Definition();
125             def.name = StringUtils.replace(name, "\\", "/");
126             def.name = StringUtils.substringAfterLast(def.name, "/js-classes/");
127             def.name = StringUtils.removeEnd(def.name, ".js");
128             def.name = StringUtils.replace(def.name, "/", ".");
129             InputStream JavaDoc stream = ClasspathResourcesUtil.getStream(name);
130             def.content = IOUtils.toString(stream);
131             stream.close();
132             Matcher JavaDoc matcher = importPattern.matcher(def.content);
133             while (matcher.find()) {
134                 String JavaDoc importName = matcher.group(1);
135                 def.imports.add(importName);
136             }
137             classDefinitions.put(def.name, def);
138         }
139
140         // write first the runtime
141
Definition runtime = (Definition) classDefinitions.get("mgnl.Runtime");
142         out.println(runtime.content);
143         runtime.proceed = true;
144
145         out.println("MgnlRuntime.loadingOn=false;");
146
147         for (Iterator JavaDoc iter = classDefinitions.keySet().iterator(); iter.hasNext();) {
148             String JavaDoc className = (String JavaDoc) iter.next();
149             process(className, out);
150         }
151
152         out.println("MgnlRuntime.loadingOn=true;");
153
154     }
155
156     private void prepareI18n(PrintWriter JavaDoc out) throws IOException JavaDoc {
157         InputStream JavaDoc in = ClasspathResourcesUtil.getStream("/mgnl-resources/admin-js/i18n.js");
158         IOUtils.copy(in, out);
159         out.println(MessagesUtil.generateJavaScript(MessagesManager.getMessages()));
160         in.close();
161     }
162
163     protected class Definition {
164
165         protected boolean proceed = false;
166
167         protected String JavaDoc content;
168
169         protected String JavaDoc name;
170
171         protected List JavaDoc imports = new ArrayList JavaDoc();
172     }
173
174 }
175
Popular Tags