View Javadoc

1   /**
2    * This file Copyright (c) 2003-2011 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
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   * @author Fabrizio Giustina
61   * @version $Revision: 41137 $ ($Author: gjoseph $)
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          //"i18n.js", moved alone since it must be initialized first
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       * Required constructor.
82       * @param name page name
83       * @param request HttpServletRequest
84       * @param response HttpServletResponse
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      * @see info.magnolia.cms.servlets.MVCServletHandler#renderHtml(java.lang.String)
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         // finding files in classpath is too expensive, just cache the list of paths!
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         // request.getRequestDispatcher("/.resources/js-libs/*.js").include(request, response);
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         // write first the runtime
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      * Render the default messages using the fallback local. This is useful because the messages_[lang].js include is new
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         // put the default messages into the main file if one misses the messages.js file
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 }