1 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 ; 25 import java.io.InputStream ; 26 import java.io.PrintWriter ; 27 import java.util.ArrayList ; 28 import java.util.HashMap ; 29 import java.util.Iterator ; 30 import java.util.List ; 31 import java.util.Map ; 32 import java.util.regex.Matcher ; 33 import java.util.regex.Pattern ; 34 35 import javax.servlet.http.HttpServletRequest ; 36 import javax.servlet.http.HttpServletResponse ; 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 47 public class JavascriptIncludePage extends PageMVCHandler { 48 49 private static boolean nocache = BooleanUtils.toBoolean(SystemProperty.getProperty("magnolia.develop")); 50 51 private static String [] files; 52 53 private static String [] includes = { 54 "debug.js", 55 "generic.js", 56 "general.js", 57 "controls.js", 58 "tree.js", 59 "contextmenu.js", 61 "inline.js"}; 62 63 private static Pattern importPattern = Pattern.compile("importClass\\(\"(.*)\"\\);"); 64 65 private Map classDefinitions = new HashMap (); 66 67 73 public JavascriptIncludePage(String name, HttpServletRequest request, HttpServletResponse response) { 74 super(name, request, response); 75 } 76 77 void process(String name, PrintWriter out) throws IOException { 78 Definition def = (Definition) classDefinitions.get(name); 79 if (!def.proceed) { 80 def.proceed = true; 81 for (Iterator iter = def.imports.iterator(); iter.hasNext();) { 82 String importName = (String ) iter.next(); 83 process(importName, out); 84 } 85 out.println(def.content); 86 } 87 } 88 89 92 public void renderHtml(String view) throws IOException { 93 HttpServletRequest request = getRequest(); 94 HttpServletResponse response = getResponse(); 95 96 PrintWriter out = response.getWriter(); 97 String 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 in = ClasspathResourcesUtil.getStream("/mgnl-resources/admin-js/" + includes[i]); 105 IOUtils.copy(in, out); 106 in.close(); 107 } 108 109 110 if (files == null || nocache) { 112 files = ClasspathResourcesUtil.findResources(new ClasspathResourcesUtil.Filter() { 113 114 public boolean accept(String name) { 115 return name.startsWith("/mgnl-resources/js-classes") && name.endsWith(".js"); 116 } 117 }); 118 } 119 120 122 for (int j = 0; j < files.length; j++) { 123 String 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 stream = ClasspathResourcesUtil.getStream(name); 130 def.content = IOUtils.toString(stream); 131 stream.close(); 132 Matcher matcher = importPattern.matcher(def.content); 133 while (matcher.find()) { 134 String importName = matcher.group(1); 135 def.imports.add(importName); 136 } 137 classDefinitions.put(def.name, def); 138 } 139 140 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 iter = classDefinitions.keySet().iterator(); iter.hasNext();) { 148 String className = (String ) iter.next(); 149 process(className, out); 150 } 151 152 out.println("MgnlRuntime.loadingOn=true;"); 153 154 } 155 156 private void prepareI18n(PrintWriter out) throws IOException { 157 InputStream 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 content; 168 169 protected String name; 170 171 protected List imports = new ArrayList (); 172 } 173 174 } 175 | Popular Tags |