001 // Copyright 2004, 2005 The Apache Software Foundation
002 //
003 // Licensed under the Apache License, Version 2.0 (the "License");
004 // you may not use this file except in compliance with the License.
005 // You may obtain a copy of the License at
006 //
007 // http://www.apache.org/licenses/LICENSE-2.0
008 //
009 // Unless required by applicable law or agreed to in writing, software
010 // distributed under the License is distributed on an "AS IS" BASIS,
011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012 // See the License for the specific language governing permissions and
013 // limitations under the License.
014
015 package org.apache.tapestry.script;
016
017 import org.apache.hivemind.ClassResolver;
018 import org.apache.hivemind.Resource;
019 import org.apache.tapestry.IScript;
020 import org.apache.tapestry.coerce.ValueConverter;
021 import org.apache.tapestry.services.ExpressionEvaluator;
022 import org.apache.tapestry.util.xml.DocumentParseException;
023 import org.apache.tapestry.util.xml.RuleDirectedParser;
024
025 /**
026 * Parses a Tapestry Script, an XML file defined by one of the following public identifiers:
027 * <ul>
028 * <li><code>-//Primix Solutions//Tapestry Script 1.0//EN</code></li>
029 * <li><code>-//Howard Ship//Tapestry Script 1.1//EN</code></li>
030 * <li><code>-//Howard Lewis Ship//Tapestry Script 1.2//EN</code></li>.
031 * <p>
032 * The version 1.1, is largely backwards compatible to the old script, but adds a number of new
033 * features (if, if-not, foreach and the use of property paths with insert).
034 * <p>
035 * Version 1.2 removes the <insert> element, using an Ant-like syntax (
036 * <code>${<i>expression</i>}</code>). It also replaces the attribute name
037 * <code>property-path</code> with <code>expression</code> (because OGNL is used).
038 * <p>
039 * A Tapestry Script is used, in association with the {@link org.apache.tapestry.html.Body}and/or
040 * {@link org.apache.tapestry.html.Script}components, to generate JavaScript for use with a
041 * Tapestry component. Two seperate pieces of JavaScript can be generated. The body section
042 * (associated with the <code>body</code> element of the XML document) is typically used to define
043 * JavaScript functions (most often, event handlers). The initialization section (associated with
044 * the <code>initialization</code> element of the XML document) is used to add JavaScript that
045 * will be evaluated when the page finishes loading (i.e., from the HTML <body> element's
046 * onLoad event handler).
047 *
048 * @author Howard Lewis Ship
049 */
050
051 public class ScriptParser
052 {
053 private RuleDirectedParser _parser;
054
055 public static final String SCRIPT_DTD_1_0_PUBLIC_ID = "-//Primix Solutions//Tapestry Script 1.0//EN";
056
057 public static final String SCRIPT_DTD_1_1_PUBLIC_ID = "-//Howard Ship//Tapestry Script 1.1//EN";
058
059 public static final String SCRIPT_DTD_1_2_PUBLIC_ID = "-//Howard Lewis Ship//Tapestry Script 1.2//EN";
060
061 /** @since 3.0 */
062 public static final String SCRIPT_DTD_3_0_PUBLIC_ID = "-//Apache Software Foundation//Tapestry Script Specification 3.0//EN";
063
064 public ScriptParser(ClassResolver resolver, ExpressionEvaluator evaluator,
065 ValueConverter valueConverter)
066 {
067 _parser = new RuleDirectedParser();
068
069 _parser.registerEntity(
070 SCRIPT_DTD_1_0_PUBLIC_ID,
071 "/org/apache/tapestry/script/Script_1_0.dtd");
072 _parser.registerEntity(
073 SCRIPT_DTD_1_1_PUBLIC_ID,
074 "/org/apache/tapestry/script/Script_1_1.dtd");
075 _parser.registerEntity(
076 SCRIPT_DTD_1_2_PUBLIC_ID,
077 "/org/apache/tapestry/script/Script_1_2.dtd");
078 _parser.registerEntity(
079 SCRIPT_DTD_3_0_PUBLIC_ID,
080 "/org/apache/tapestry/script/Script_3_0.dtd");
081
082 _parser.addRule("script", new ScriptRule(evaluator, valueConverter));
083 _parser.addRule("let", new LetRule());
084 _parser.addRule("set", new SetRule());
085 _parser.addRule("include-script", new IncludeScriptRule());
086 _parser.addRule("input-symbol", new InputSymbolRule(resolver));
087 _parser.addRule("body", new BodyRule());
088 _parser.addRule("initialization", new InitRule());
089 _parser.addRule("if", new IfRule(true));
090 _parser.addRule("if-not", new IfRule(false));
091 _parser.addRule("foreach", new ForeachRule());
092 _parser.addRule("unique", new UniqueRule());
093
094 // This will go away when the 1.1 and earler DTDs are retired.
095 _parser.addRule("insert", new InsertRule());
096
097 }
098
099 /**
100 * Parses the given input stream to produce a parsed script, ready to execute.
101 */
102
103 public IScript parse(Resource resourceLocation) throws DocumentParseException
104 {
105 return (IScript) _parser.parse(resourceLocation);
106 }
107
108 }