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.RuleDirectedParser; 023 024 /** 025 * Parses a Tapestry Script, an XML file defined by one of the following public 026 * 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 * </ul> 032 * <p> 033 * The version 1.1, is largely backwards compatible to the old script, but adds 034 * a number of new features (if, if-not, foreach and the use of property paths 035 * with insert). 036 * <p> 037 * Version 1.2 removes the <insert> element, using an Ant-like syntax ( 038 * <code>${<i>expression</i>}</code>). It also replaces the attribute name 039 * <code>property-path</code> with <code>expression</code> (because OGNL is 040 * used). 041 * <p> 042 * A Tapestry Script is used, in association with the 043 * {@link org.apache.tapestry.html.Body}and/or 044 * {@link org.apache.tapestry.html.Script}components, to generate JavaScript 045 * for use with a Tapestry component. Two seperate pieces of JavaScript can be 046 * generated. The body section (associated with the <code>body</code> element 047 * of the XML document) is typically used to define JavaScript functions (most 048 * often, event handlers). The initialization section (associated with the 049 * <code>initialization</code> element of the XML document) is used to add 050 * JavaScript that will be evaluated when the page finishes loading (i.e., from 051 * the HTML <body> element's onLoad event handler). 052 * 053 * @author Howard Lewis Ship 054 */ 055 056 public class ScriptParser 057 { 058 public static final String SCRIPT_DTD_1_0_PUBLIC_ID = "-//Primix Solutions//Tapestry Script 1.0//EN"; 059 060 public static final String SCRIPT_DTD_1_1_PUBLIC_ID = "-//Howard Ship//Tapestry Script 1.1//EN"; 061 062 public static final String SCRIPT_DTD_1_2_PUBLIC_ID = "-//Howard Lewis Ship//Tapestry Script 1.2//EN"; 063 064 /** @since 3.0 */ 065 public static final String SCRIPT_DTD_3_0_PUBLIC_ID = "-//Apache Software Foundation//Tapestry Script Specification 3.0//EN"; 066 067 /** @since 4.1 */ 068 public static final String SCRIPT_DTD_4_0_PUBLIC_ID = "-//Apache Software Foundation//Tapestry Script Specification 4.0//EN"; 069 070 private RuleDirectedParser _parser; 071 072 public ScriptParser(ClassResolver resolver, ExpressionEvaluator evaluator, ValueConverter valueConverter) 073 { 074 _parser = new RuleDirectedParser(); 075 076 _parser.registerEntity(SCRIPT_DTD_1_0_PUBLIC_ID, 077 "/org/apache/tapestry/script/Script_1_0.dtd"); 078 _parser.registerEntity(SCRIPT_DTD_1_1_PUBLIC_ID, 079 "/org/apache/tapestry/script/Script_1_1.dtd"); 080 _parser.registerEntity(SCRIPT_DTD_1_2_PUBLIC_ID, 081 "/org/apache/tapestry/script/Script_1_2.dtd"); 082 _parser.registerEntity(SCRIPT_DTD_3_0_PUBLIC_ID, 083 "/org/apache/tapestry/script/Script_3_0.dtd"); 084 _parser.registerEntity(SCRIPT_DTD_4_0_PUBLIC_ID, 085 "/org/apache/tapestry/script/Script_4_0.dtd"); 086 087 _parser.addRule("script", new ScriptRule(evaluator, valueConverter)); 088 _parser.addRule("let", new LetRule()); 089 _parser.addRule("set", new SetRule()); 090 _parser.addRule("include-script", new IncludeScriptRule()); 091 _parser.addRule("input-symbol", new InputSymbolRule(resolver)); 092 _parser.addRule("body", new BodyRule()); 093 _parser.addRule("initialization", new InitRule()); 094 _parser.addRule("if", new IfRule(true)); 095 _parser.addRule("if-not", new IfRule(false)); 096 _parser.addRule("foreach", new ForeachRule()); 097 _parser.addRule("unique", new UniqueRule()); 098 099 // This will go away when the 1.1 and earler DTDs are retired. 100 _parser.addRule("insert", new InsertRule()); 101 102 } 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 return (IScript) _parser.parse(resourceLocation); 112 } 113 114 }