KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > script > InputSymbolRule


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.HiveMind;
19 import org.apache.tapestry.Tapestry;
20 import org.apache.tapestry.util.xml.BaseRule;
21 import org.apache.tapestry.util.xml.DocumentParseException;
22 import org.apache.tapestry.util.xml.RuleDirectedParser;
23 import org.xml.sax.Attributes JavaDoc;
24
25 /**
26  * Constructs an {@link org.apache.tapestry.script.InputSymbolToken}from an <input-symbol>
27  * element.
28  *
29  * @author Howard Lewis Ship
30  * @since 3.0
31  */

32 class InputSymbolRule extends BaseRule
33 {
34     private ClassResolver _resolver;
35
36     public InputSymbolRule(ClassResolver resolver)
37     {
38         _resolver = resolver;
39     }
40
41     public void startElement(RuleDirectedParser parser, Attributes JavaDoc attributes)
42     {
43         String JavaDoc key = getAttribute(attributes, "key");
44
45         parser.validate(key, Tapestry.SIMPLE_PROPERTY_NAME_PATTERN, "ScriptParser.invalid-key");
46
47         String JavaDoc className = getAttribute(attributes, "class");
48         Class JavaDoc expectedClass = lookupClass(parser, className);
49
50         String JavaDoc required = getAttribute(attributes, "required");
51
52         InputSymbolToken token = new InputSymbolToken(key, expectedClass, required.equals("yes"),
53                 parser.getLocation());
54
55         IScriptToken parent = (IScriptToken) parser.peek();
56         parent.addToken(token);
57     }
58
59     private Class JavaDoc lookupClass(RuleDirectedParser parser, String JavaDoc className)
60     {
61         if (HiveMind.isBlank(className))
62             return null;
63
64         try
65         {
66             return _resolver.findClass(className);
67         }
68         catch (Exception JavaDoc ex)
69         {
70             throw new DocumentParseException(Tapestry.format(
71                     "ScriptParser.unable-to-resolve-class",
72                     className), parser.getLocation(), ex);
73         }
74     }
75 }
Popular Tags