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 @Override
106 public void renderHtml(String view) throws IOException {
107 HttpServletRequest request = getRequest();
108 HttpServletResponse response = getResponse();
109
110 PrintWriter out = response.getWriter();
111 String contextPath = request.getContextPath();
112
113 out.println("var contextPath = '" + contextPath + "';");
114
115 prepareI18n(out);
116
117
118 String[] files = ClasspathResourcesUtil.findResources(new ClasspathResourcesUtil.Filter() {
119 @Override
120 public boolean accept(String name) {
121 return name.startsWith("/mgnl-resources/js-classes") && name.endsWith(".js");
122 }
123 });
124
125
126
127 for (int j = 0; j < files.length; j++) {
128 String name = files[j];
129 Definition def = new Definition();
130 def.name = StringUtils.replace(name, "\\", "/");
131 def.name = StringUtils.substringAfterLast(def.name, "/js-classes/");
132 def.name = StringUtils.removeEnd(def.name, ".js");
133 def.name = StringUtils.replace(def.name, "/", ".");
134 InputStream stream = ClasspathResourcesUtil.getStream(name);
135 def.content = IOUtils.toString(stream);
136 stream.close();
137 Matcher matcher = importPattern.matcher(def.content);
138 while (matcher.find()) {
139 String importName = matcher.group(1);
140 def.imports.add(importName);
141 }
142 classDefinitions.put(def.name, def);
143 }
144
145
146 Definition runtime = (Definition) classDefinitions.get("mgnl.Runtime");
147 out.println(runtime.content);
148 runtime.proceed = true;
149
150 out.println("MgnlRuntime.loadingOn=false;");
151
152 for (Iterator iter = classDefinitions.keySet().iterator(); iter.hasNext();) {
153 String className = (String) iter.next();
154 process(className, out);
155 }
156
157 for (int i = 0; i < includes.length; i++) {
158 InputStream in = ClasspathResourcesUtil.getStream("/mgnl-resources/admin-js/" + includes[i]);
159 IOUtils.copy(in, out);
160 in.close();
161 }
162
163 out.println("MgnlRuntime.loadingOn=true;");
164
165 }
166
167
168
169
170 private void prepareI18n(PrintWriter out) throws IOException {
171 InputStream in = ClasspathResourcesUtil.getStream("/mgnl-resources/admin-js/i18n.js");
172 IOUtils.copy(in, out);
173
174 final Locale defaultLocale = MessagesManager.getInstance().getDefaultLocale();
175 final Messages messages = MessagesManager.getMessages(defaultLocale);
176 MessagesUtil.generateJavaScript(out, messages);
177 in.close();
178 }
179
180 protected class Definition {
181
182 protected boolean proceed = false;
183
184 protected String content;
185
186 protected String name;
187
188 protected List imports = new ArrayList();
189 }
190
191 }