1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.springframework.extensions.surf.core.scripts;
20
21 import java.util.LinkedHashMap;
22 import java.util.Map;
23
24 import org.apache.commons.logging.Log;
25
26
27
28
29 public class ScriptResourceHelper
30 {
31 private static final String SCRIPT_ROOT = "_root";
32 private static final String IMPORT_PREFIX = "<import";
33 private static final String IMPORT_RESOURCE = "resource=\"";
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53 public static String resolveScriptImports(String script, ScriptResourceLoader loader, Log logger)
54 {
55
56
57 Map<String, String> scriptlets = new LinkedHashMap<String, String>(8, 1.0f);
58
59
60 recurseScriptImports(SCRIPT_ROOT, script, loader, scriptlets, logger);
61
62 if (scriptlets.size() == 1)
63 {
64
65 if (logger.isTraceEnabled())
66 logger.trace("Script content resolved to:\r\n" + script);
67
68 return script;
69 }
70 else
71 {
72
73 int length = 0;
74 for (String scriptlet : scriptlets.values())
75 {
76 length += scriptlet.length();
77 }
78
79 StringBuilder result = new StringBuilder(length);
80 for (String scriptlet : scriptlets.values())
81 {
82 result.append(scriptlet);
83 }
84
85 if (logger.isTraceEnabled())
86 logger.trace("Script content resolved to:\r\n" + result.toString());
87
88 return result.toString();
89 }
90 }
91
92
93
94
95
96
97
98
99
100 private static void recurseScriptImports(
101 String location, String script, ScriptResourceLoader loader, Map<String, String> scripts, Log logger)
102 {
103 int index = 0;
104
105 for (; index<script.length(); index++)
106 {
107 if (Character.isWhitespace(script.charAt(index)) == false)
108 {
109 break;
110 }
111 }
112
113 if (script.startsWith(IMPORT_PREFIX, index))
114 {
115
116 boolean afterWhitespace = false;
117 index += IMPORT_PREFIX.length() + 1;
118 for (; index<script.length(); index++)
119 {
120 if (Character.isWhitespace(script.charAt(index)) == false)
121 {
122 afterWhitespace = true;
123 break;
124 }
125 }
126 if (afterWhitespace == true && script.startsWith(IMPORT_RESOURCE, index))
127 {
128
129 index += IMPORT_RESOURCE.length();
130 int resourceStart = index;
131 for (; index<script.length(); index++)
132 {
133 if (script.charAt(index) == '"' && script.charAt(index + 1) == '>')
134 {
135
136 String resource = script.substring(resourceStart, index);
137
138 if (logger.isDebugEnabled())
139 logger.debug("Found script resource import: " + resource);
140
141 if (scripts.containsKey(resource) == false)
142 {
143
144 String includedScript = loader.loadScriptResource(resource);
145 if (includedScript != null)
146 {
147 if (logger.isDebugEnabled())
148 logger.debug("Succesfully located script '" + resource + "'");
149 recurseScriptImports(resource, includedScript, loader, scripts, logger);
150 }
151 }
152 else
153 {
154 if (logger.isDebugEnabled())
155 logger.debug("Note: already imported resource: " + resource);
156 }
157
158
159
160 for (index += 2; index<script.length(); index++)
161 {
162 if (Character.isWhitespace(script.charAt(index)) == false)
163 {
164 break;
165 }
166 }
167 recurseScriptImports(location, script.substring(index), loader, scripts, logger);
168 return;
169 }
170 }
171
172 throw new ScriptException(
173 "Malformed 'import' line - must be first in file, no comments and strictly of the form:" +
174 "\r\n<import resource=\"...\">");
175 }
176 else
177 {
178 throw new ScriptException(
179 "Malformed 'import' line - must be first in file, no comments and strictly of the form:" +
180 "\r\n<import resource=\"...\">");
181 }
182 }
183 else
184 {
185
186 if (logger.isDebugEnabled())
187 logger.debug("Imports resolved, adding resource '" + location);
188 if (logger.isTraceEnabled())
189 logger.trace(script);
190 scripts.put(location, script);
191 }
192 }
193 }