KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > facelets > tag > MetaRulesetImpl


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 java.beans.IntrospectionException JavaDoc;
18 import java.util.ArrayList JavaDoc;
19 import java.util.HashMap JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.List JavaDoc;
22 import java.util.Map JavaDoc;
23 import java.util.WeakHashMap JavaDoc;
24 import java.util.logging.Level JavaDoc;
25 import java.util.logging.Logger JavaDoc;
26
27 import com.sun.facelets.FaceletContext;
28 import com.sun.facelets.util.ParameterCheck;
29
30 /**
31  *
32  * @author Jacob Hookom
33  * @version $Id: MetaRulesetImpl.java,v 1.2 2005/08/24 04:38:47 jhook Exp $
34  */

35 final class MetaRulesetImpl extends MetaRuleset {
36
37     private final static WeakHashMap JavaDoc metadata = new WeakHashMap JavaDoc();
38     
39     private final static Logger JavaDoc log = Logger
40     .getLogger("facelets.tag.meta");
41
42     private final Tag tag;
43
44     private final Class JavaDoc type;
45
46     private final Map JavaDoc attributes;
47
48     private final List JavaDoc mappers;
49
50     private final List JavaDoc rules;
51
52     public MetaRulesetImpl(Tag tag, Class JavaDoc type) {
53         this.tag = tag;
54         this.type = type;
55         this.attributes = new HashMap JavaDoc();
56         this.mappers = new ArrayList JavaDoc();
57         this.rules = new ArrayList JavaDoc();
58
59         // setup attributes
60
TagAttribute[] attrs = this.tag.getAttributes().getAll();
61         for (int i = 0; i < attrs.length; i++) {
62             attributes.put(attrs[i].getLocalName(), attrs[i]);
63         }
64
65         // add default rules
66
this.rules.add(BeanPropertyTagRule.Instance);
67     }
68
69     public MetaRuleset ignore(String JavaDoc attribute) {
70         ParameterCheck.notNull("attribute", attribute);
71         this.attributes.remove(attribute);
72         return this;
73     }
74
75     public MetaRuleset alias(String JavaDoc attribute, String JavaDoc property) {
76         ParameterCheck.notNull("attribute", attribute);
77         ParameterCheck.notNull("property", property);
78         TagAttribute attr = (TagAttribute) this.attributes.remove(attribute);
79         if (attr != null) {
80             this.attributes.put(property, attr);
81         }
82         return this;
83     }
84
85     public MetaRuleset add(Metadata mapper) {
86         ParameterCheck.notNull("mapper", mapper);
87         if (!this.mappers.contains(mapper)) {
88             this.mappers.add(mapper);
89         }
90         return this;
91     }
92
93     public MetaRuleset addRule(MetaRule rule) {
94         ParameterCheck.notNull("rule", rule);
95         this.rules.add(rule);
96         return this;
97     }
98
99     private final MetadataTarget getMetadataTarget() {
100         String JavaDoc key = this.type.getName();
101         MetadataTarget meta = (MetadataTarget) metadata.get(key);
102         if (meta == null) {
103             try {
104                 meta = new MetadataTargetImpl(type);
105             } catch (IntrospectionException JavaDoc e) {
106                 throw new TagException(this.tag,
107                         "Error Creating TargetMetadata", e);
108             }
109             metadata.put(key, meta);
110         }
111         return meta;
112     }
113
114     public Metadata finish() {
115         if (!this.attributes.isEmpty()) {
116             if (this.rules.isEmpty()) {
117                 if (log.isLoggable(Level.SEVERE)) {
118                     for (Iterator JavaDoc itr = this.attributes.values().iterator(); itr.hasNext(); ) {
119                         log.severe(itr.next() + " Unhandled by MetaTagHandler for type "+this.type.getName());
120                     }
121                 }
122             } else {
123                 MetadataTarget target = this.getMetadataTarget();
124                 // now iterate over attributes
125
Map.Entry JavaDoc entry;
126                 MetaRule rule;
127                 Metadata data;
128                 int ruleEnd = this.rules.size() - 1;
129                 for (Iterator JavaDoc itr = this.attributes.entrySet().iterator(); itr.hasNext(); ) {
130                     entry = (Map.Entry JavaDoc) itr.next();
131                     data = null;
132                     int i = ruleEnd;
133                     while (data == null && i >= 0) {
134                         rule = (MetaRule) this.rules.get(i);
135                         data = rule.applyRule((String JavaDoc) entry.getKey(), (TagAttribute) entry.getValue(), target);
136                         i--;
137                     }
138                     if (data == null) {
139                         if (log.isLoggable(Level.SEVERE)) {
140                             log.severe(entry.getValue() + " Unhandled by MetaTagHandler for type "+this.type.getName());
141                         }
142                     } else {
143                         this.mappers.add(data);
144                     }
145                 }
146             }
147         }
148         
149         if (this.mappers.isEmpty()) {
150             return NONE;
151         } else {
152             return new MetadataImpl((Metadata[]) this.mappers
153                 .toArray(new Metadata[this.mappers.size()]));
154         }
155     }
156
157     public MetaRuleset ignoreAll() {
158         this.attributes.clear();
159         return this;
160     }
161     
162     private final static Metadata NONE = new Metadata() {
163         public void applyMetadata(FaceletContext ctx, Object JavaDoc instance) {
164             // do nothing
165
}
166     };
167 }
168
Popular Tags