Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
ScriptParser |
|
| 1.0;1 |
1 | // Copyright 2004, 2005 The Apache Software Foundation | |
2 | // | |
3 | // Licensed under the Apache License, Version 2.0 (the "License"); | |
4 | // you may not use this file except in compliance with the License. | |
5 | // You may obtain a copy of the License at | |
6 | // | |
7 | // http://www.apache.org/licenses/LICENSE-2.0 | |
8 | // | |
9 | // Unless required by applicable law or agreed to in writing, software | |
10 | // distributed under the License is distributed on an "AS IS" BASIS, | |
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
12 | // See the License for the specific language governing permissions and | |
13 | // limitations under the License. | |
14 | ||
15 | package org.apache.tapestry.script; | |
16 | ||
17 | import org.apache.hivemind.ClassResolver; | |
18 | import org.apache.hivemind.Resource; | |
19 | import org.apache.tapestry.IScript; | |
20 | import org.apache.tapestry.coerce.ValueConverter; | |
21 | import org.apache.tapestry.services.ExpressionEvaluator; | |
22 | import org.apache.tapestry.util.xml.RuleDirectedParser; | |
23 | ||
24 | /** | |
25 | * Parses a Tapestry Script, an XML file defined by one of the following public | |
26 | * identifiers: | |
27 | * <ul> | |
28 | * <li><code>-//Primix Solutions//Tapestry Script 1.0//EN</code></li> | |
29 | * <li><code>-//Howard Ship//Tapestry Script 1.1//EN</code></li> | |
30 | * <li><code>-//Howard Lewis Ship//Tapestry Script 1.2//EN</code></li> | |
31 | * </ul> | |
32 | * <p> | |
33 | * The version 1.1, is largely backwards compatible to the old script, but adds | |
34 | * a number of new features (if, if-not, foreach and the use of property paths | |
35 | * with insert). | |
36 | * <p> | |
37 | * Version 1.2 removes the <insert> element, using an Ant-like syntax ( | |
38 | * <code>${<i>expression</i>}</code>). It also replaces the attribute name | |
39 | * <code>property-path</code> with <code>expression</code> (because OGNL is | |
40 | * used). | |
41 | * <p> | |
42 | * A Tapestry Script is used, in association with the | |
43 | * {@link org.apache.tapestry.html.Body}and/or | |
44 | * {@link org.apache.tapestry.html.Script}components, to generate JavaScript | |
45 | * for use with a Tapestry component. Two seperate pieces of JavaScript can be | |
46 | * generated. The body section (associated with the <code>body</code> element | |
47 | * of the XML document) is typically used to define JavaScript functions (most | |
48 | * often, event handlers). The initialization section (associated with the | |
49 | * <code>initialization</code> element of the XML document) is used to add | |
50 | * JavaScript that will be evaluated when the page finishes loading (i.e., from | |
51 | * the HTML <body> element's onLoad event handler). | |
52 | * | |
53 | * @author Howard Lewis Ship | |
54 | */ | |
55 | ||
56 | public class ScriptParser | |
57 | { | |
58 | public static final String SCRIPT_DTD_1_0_PUBLIC_ID = "-//Primix Solutions//Tapestry Script 1.0//EN"; | |
59 | ||
60 | public static final String SCRIPT_DTD_1_1_PUBLIC_ID = "-//Howard Ship//Tapestry Script 1.1//EN"; | |
61 | ||
62 | public static final String SCRIPT_DTD_1_2_PUBLIC_ID = "-//Howard Lewis Ship//Tapestry Script 1.2//EN"; | |
63 | ||
64 | /** @since 3.0 */ | |
65 | public static final String SCRIPT_DTD_3_0_PUBLIC_ID = "-//Apache Software Foundation//Tapestry Script Specification 3.0//EN"; | |
66 | ||
67 | /** @since 4.1 */ | |
68 | public static final String SCRIPT_DTD_4_0_PUBLIC_ID = "-//Apache Software Foundation//Tapestry Script Specification 4.0//EN"; | |
69 | ||
70 | private RuleDirectedParser _parser; | |
71 | ||
72 | public ScriptParser(ClassResolver resolver, ExpressionEvaluator evaluator, ValueConverter valueConverter) | |
73 | 0 | { |
74 | 0 | _parser = new RuleDirectedParser(); |
75 | ||
76 | 0 | _parser.registerEntity(SCRIPT_DTD_1_0_PUBLIC_ID, |
77 | "/org/apache/tapestry/script/Script_1_0.dtd"); | |
78 | 0 | _parser.registerEntity(SCRIPT_DTD_1_1_PUBLIC_ID, |
79 | "/org/apache/tapestry/script/Script_1_1.dtd"); | |
80 | 0 | _parser.registerEntity(SCRIPT_DTD_1_2_PUBLIC_ID, |
81 | "/org/apache/tapestry/script/Script_1_2.dtd"); | |
82 | 0 | _parser.registerEntity(SCRIPT_DTD_3_0_PUBLIC_ID, |
83 | "/org/apache/tapestry/script/Script_3_0.dtd"); | |
84 | 0 | _parser.registerEntity(SCRIPT_DTD_4_0_PUBLIC_ID, |
85 | "/org/apache/tapestry/script/Script_4_0.dtd"); | |
86 | ||
87 | 0 | _parser.addRule("script", new ScriptRule(evaluator, valueConverter)); |
88 | 0 | _parser.addRule("let", new LetRule()); |
89 | 0 | _parser.addRule("set", new SetRule()); |
90 | 0 | _parser.addRule("include-script", new IncludeScriptRule()); |
91 | 0 | _parser.addRule("input-symbol", new InputSymbolRule(resolver)); |
92 | 0 | _parser.addRule("body", new BodyRule()); |
93 | 0 | _parser.addRule("initialization", new InitRule()); |
94 | 0 | _parser.addRule("if", new IfRule(true)); |
95 | 0 | _parser.addRule("if-not", new IfRule(false)); |
96 | 0 | _parser.addRule("foreach", new ForeachRule()); |
97 | 0 | _parser.addRule("unique", new UniqueRule()); |
98 | ||
99 | // This will go away when the 1.1 and earler DTDs are retired. | |
100 | 0 | _parser.addRule("insert", new InsertRule()); |
101 | ||
102 | 0 | } |
103 | ||
104 | /** | |
105 | * Parses the given input stream to produce a parsed script, ready to | |
106 | * execute. | |
107 | */ | |
108 | ||
109 | public IScript parse(Resource resourceLocation) | |
110 | { | |
111 | 0 | return (IScript) _parser.parse(resourceLocation); |
112 | } | |
113 | ||
114 | } |