KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > riotfamily > forms > resource > LoadingCodeGenerator


1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1
3  * The contents of this file are subject to the Mozilla Public License Version
4  * 1.1 (the "License"); you may not use this file except in compliance with
5  * the License. You may obtain a copy of the License at
6  * http://www.mozilla.org/MPL/
7  *
8  * Software distributed under the License is distributed on an "AS IS" basis,
9  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
10  * for the specific language governing rights and limitations under the
11  * License.
12  *
13  * The Original Code is Riot.
14  *
15  * The Initial Developer of the Original Code is
16  * Neteye GmbH.
17  * Portions created by the Initial Developer are Copyright (C) 2007
18  * the Initial Developer. All Rights Reserved.
19  *
20  * Contributor(s):
21  * Felix Gnass [fgnass at neteye dot de]
22  *
23  * ***** END LICENSE BLOCK ***** */

24 package org.riotfamily.forms.resource;
25
26 import java.io.PrintWriter JavaDoc;
27 import java.util.Collection JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.LinkedHashSet JavaDoc;
30
31 /**
32  * @author Felix Gnass [fgnass at neteye dot de]
33  * @since 6.4
34  */

35 public class LoadingCodeGenerator implements ResourceVisitor {
36
37     private LinkedHashSet JavaDoc scripts = new LinkedHashSet JavaDoc();
38     
39     private LinkedHashSet JavaDoc stylesheets = new LinkedHashSet JavaDoc();
40
41     private LoadingCodeGenerator() {
42     }
43     
44     public static void renderLoadingCode(Collection JavaDoc resources,
45             PrintWriter JavaDoc writer) {
46         
47         new LoadingCodeGenerator().render(resources, writer);
48     }
49     
50     private void loadResources(Collection JavaDoc resources) {
51         if (resources == null) {
52             return;
53         }
54         Iterator JavaDoc it = resources.iterator();
55         while (it.hasNext()) {
56             FormResource resource = (FormResource) it.next();
57             if (resource != null) {
58                 resource.accept(this);
59             }
60         }
61     }
62     
63     public void visitScript(ScriptResource script) {
64         if (!scripts.contains(script)) {
65             loadResources(script.getDependencies());
66             scripts.add(script);
67         }
68     }
69
70     public void visitStyleSheet(StylesheetResource stylesheet) {
71         if (!stylesheets.contains(stylesheet)) {
72             stylesheets.add(stylesheet);
73         }
74     }
75
76     private void render(Collection JavaDoc resources, PrintWriter JavaDoc writer) {
77         loadResources(resources);
78         Iterator JavaDoc it = stylesheets.iterator();
79         while (it.hasNext()) {
80             StylesheetResource stylesheet = (StylesheetResource) it.next();
81             writer.print("Resources.loadStyleSheet('");
82             writer.print(stylesheet.getUrl());
83             writer.print("');");
84         }
85         
86         if (!scripts.isEmpty()) {
87             writer.print("Resources.loadScriptSequence([");
88             it = scripts.iterator();
89             while (it.hasNext()) {
90                 ScriptResource script = (ScriptResource) it.next();
91                 writer.print("{src:'");
92                 writer.print(script.getUrl());
93                 writer.print('\'');
94                 if (script.getTest() != null) {
95                     writer.print(", test:'");
96                     writer.print(script.getTest());
97                     writer.print('\'');
98                 }
99                 writer.print("}");
100                 if (it.hasNext()) {
101                     writer.print(',');
102                 }
103             }
104             writer.print("]);");
105         }
106     }
107     
108 }
109
Popular Tags