Source code: com/sun/facelets/tag/MetaTagHandler.java
1 /**
2 * Licensed under the Common Development and Distribution License,
3 * you may not use this file except in compliance with the License.
4 * You may obtain a copy of the License at
5 *
6 * http://www.sun.com/cddl/
7 *
8 * Unless required by applicable law or agreed to in writing, software
9 * distributed under the License is distributed on an "AS IS" BASIS,
10 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
11 * implied. See the License for the specific language governing
12 * permissions and limitations under the License.
13 */
14
15 package com.sun.facelets.tag;
16
17 import com.sun.facelets.FaceletContext;
18 import com.sun.facelets.util.ParameterCheck;
19
20 /**
21 * A base tag for wiring state to an object instance based on rules populated at
22 * the time of creating a MetaRuleset.
23 *
24 * @author Jacob Hookom
25 * @version $Id: MetaTagHandler.java,v 1.2 2005/08/24 04:38:47 jhook Exp $
26 */
27 public abstract class MetaTagHandler extends TagHandler {
28
29 private Class lastType = Object.class;
30
31 private Metadata mapper;
32
33 public MetaTagHandler(TagConfig config) {
34 super(config);
35 }
36
37 /**
38 * Extend this method in order to add your own rules.
39 *
40 * @param type
41 * @return
42 */
43 protected MetaRuleset createMetaRuleset(Class type) {
44 ParameterCheck.notNull("type", type);
45 return new MetaRulesetImpl(this.tag, type);
46 }
47
48 /**
49 * Invoking/extending this method will cause the results of the created
50 * MetaRuleset to auto-wire state to the passed instance.
51 *
52 * @param ctx
53 * @param instance
54 */
55 protected void setAttributes(FaceletContext ctx, Object instance) {
56 if (instance != null) {
57 Class type = instance.getClass();
58 if (mapper == null || !this.lastType.equals(type)) {
59 this.lastType = type;
60 this.mapper = this.createMetaRuleset(type).finish();
61 }
62 this.mapper.applyMetadata(ctx, instance);
63 }
64 }
65 }