1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package info.magnolia.module.admininterface.pages;
35
36 import info.magnolia.cms.i18n.MessagesManager;
37 import info.magnolia.cms.i18n.MessagesUtil;
38 import info.magnolia.cms.i18n.Messages;
39 import info.magnolia.cms.util.ClasspathResourcesUtil;
40 import info.magnolia.module.admininterface.PageMVCHandler;
41 import org.apache.commons.io.IOUtils;
42 import org.apache.commons.lang.StringUtils;
43
44 import javax.servlet.http.HttpServletRequest;
45 import javax.servlet.http.HttpServletResponse;
46 import java.io.IOException;
47 import java.io.InputStream;
48 import java.io.PrintWriter;
49 import java.util.ArrayList;
50 import java.util.HashMap;
51 import java.util.Iterator;
52 import java.util.List;
53 import java.util.Map;
54 import java.util.Locale;
55 import java.util.regex.Matcher;
56 import java.util.regex.Pattern;
57
58
59
60
61
62
63 public class JavascriptIncludePage extends PageMVCHandler {
64
65
66 private static String[] includes = {
67 "debug.js",
68 "generic.js",
69 "general.js",
70 "controls.js",
71 "tree.js",
72
73 "contextmenu.js",
74 "inline.js"};
75
76 private static Pattern importPattern = Pattern.compile("importClass\\(\"(.*)\"\\);");
77
78 private Map classDefinitions = new HashMap();
79
80
81
82
83
84
85
86 public JavascriptIncludePage(String name, HttpServletRequest request, HttpServletResponse response) {
87 super(name, request, response);
88 }
89
90 void process(String name, PrintWriter out) throws IOException {
91 Definition def = (Definition) classDefinitions.get(name);
92 if (!def.proceed) {
93 def.proceed = true;
94 for (Iterator iter = def.imports.iterator(); iter.hasNext();) {
95 String importName = (String) iter.next();
96 process(importName, out);
97 }
98 out.println(def.content);
99 }
100 }
101
102
103
104
105 public void renderHtml(String view) throws IOException {
106 HttpServletRequest request = getRequest();
107 HttpServletResponse response = getResponse();
108
109 PrintWriter out = response.getWriter();
110 String contextPath = request.getContextPath();
111
112 out.println("var contextPath = '" + contextPath + "';");
113
114 prepareI18n(out);
115
116
117 String[] files = ClasspathResourcesUtil.findResources(new ClasspathResourcesUtil.Filter() {
118 public boolean accept(String name) {
119 return name.startsWith("/mgnl-resources/js-classes") && name.endsWith(".js");
120 }
121 });
122
123
124
125 for (int j = 0; j < files.length; j++) {
126 String name = files[j];
127 Definition def = new Definition();
128 def.name = StringUtils.replace(name, "\\", "/");
129 def.name = StringUtils.substringAfterLast(def.name, "/js-classes/");
130 def.name = StringUtils.removeEnd(def.name, ".js");
131 def.name = StringUtils.replace(def.name, "/", ".");
132 InputStream stream = ClasspathResourcesUtil.getStream(name);
133 def.content = IOUtils.toString(stream);
134 stream.close();
135 Matcher matcher = importPattern.matcher(def.content);
136 while (matcher.find()) {
137 String importName = matcher.group(1);
138 def.imports.add(importName);
139 }
140 classDefinitions.put(def.name, def);
141 }
142
143
144 Definition runtime = (Definition) classDefinitions.get("mgnl.Runtime");
145 out.println(runtime.content);
146 runtime.proceed = true;
147
148 out.println("MgnlRuntime.loadingOn=false;");
149
150 for (Iterator iter = classDefinitions.keySet().iterator(); iter.hasNext();) {
151 String className = (String) iter.next();
152 process(className, out);
153 }
154
155 for (int i = 0; i < includes.length; i++) {
156 InputStream in = ClasspathResourcesUtil.getStream("/mgnl-resources/admin-js/" + includes[i]);
157 IOUtils.copy(in, out);
158 in.close();
159 }
160
161 out.println("MgnlRuntime.loadingOn=true;");
162
163 }
164
165
166
167
168 private void prepareI18n(PrintWriter out) throws IOException {
169 InputStream in = ClasspathResourcesUtil.getStream("/mgnl-resources/admin-js/i18n.js");
170 IOUtils.copy(in, out);
171
172 final Locale defaultLocale = MessagesManager.getInstance().getDefaultLocale();
173 final Messages messages = MessagesManager.getMessages(defaultLocale);
174 MessagesUtil.generateJavaScript(out, messages);
175 in.close();
176 }
177
178 protected class Definition {
179
180 protected boolean proceed = false;
181
182 protected String content;
183
184 protected String name;
185
186 protected List imports = new ArrayList();
187 }
188
189 }