001: /**
002: * Licensed under the Common Development and Distribution License,
003: * you may not use this file except in compliance with the License.
004: * You may obtain a copy of the License at
005: *
006: * http://www.sun.com/cddl/
007: *
008: * Unless required by applicable law or agreed to in writing, software
009: * distributed under the License is distributed on an "AS IS" BASIS,
010: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
011: * implied. See the License for the specific language governing
012: * permissions and limitations under the License.
013: */package com.sun.facelets.tag;
014:
015: import java.beans.IntrospectionException;
016: import java.util.ArrayList;
017: import java.util.HashMap;
018: import java.util.Iterator;
019: import java.util.List;
020: import java.util.Map;
021: import java.util.WeakHashMap;
022: import java.util.logging.Level;
023: import java.util.logging.Logger;
024:
025: import com.sun.facelets.FaceletContext;
026: import com.sun.facelets.util.ParameterCheck;
027:
028: /**
029: *
030: * @author Jacob Hookom
031: * @version $Id: MetaRulesetImpl.java,v 1.2 2005/08/24 04:38:47 jhook Exp $
032: */
033: final class MetaRulesetImpl extends MetaRuleset {
034:
035: private final static WeakHashMap metadata = new WeakHashMap();
036:
037: private final static Logger log = Logger
038: .getLogger("facelets.tag.meta");
039:
040: private final Tag tag;
041:
042: private final Class type;
043:
044: private final Map attributes;
045:
046: private final List mappers;
047:
048: private final List rules;
049:
050: public MetaRulesetImpl(Tag tag, Class type) {
051: this .tag = tag;
052: this .type = type;
053: this .attributes = new HashMap();
054: this .mappers = new ArrayList();
055: this .rules = new ArrayList();
056:
057: // setup attributes
058: TagAttribute[] attrs = this .tag.getAttributes().getAll();
059: for (int i = 0; i < attrs.length; i++) {
060: attributes.put(attrs[i].getLocalName(), attrs[i]);
061: }
062:
063: // add default rules
064: this .rules.add(BeanPropertyTagRule.Instance);
065: }
066:
067: public MetaRuleset ignore(String attribute) {
068: ParameterCheck.notNull("attribute", attribute);
069: this .attributes.remove(attribute);
070: return this ;
071: }
072:
073: public MetaRuleset alias(String attribute, String property) {
074: ParameterCheck.notNull("attribute", attribute);
075: ParameterCheck.notNull("property", property);
076: TagAttribute attr = (TagAttribute) this .attributes
077: .remove(attribute);
078: if (attr != null) {
079: this .attributes.put(property, attr);
080: }
081: return this ;
082: }
083:
084: public MetaRuleset add(Metadata mapper) {
085: ParameterCheck.notNull("mapper", mapper);
086: if (!this .mappers.contains(mapper)) {
087: this .mappers.add(mapper);
088: }
089: return this ;
090: }
091:
092: public MetaRuleset addRule(MetaRule rule) {
093: ParameterCheck.notNull("rule", rule);
094: this .rules.add(rule);
095: return this ;
096: }
097:
098: private final MetadataTarget getMetadataTarget() {
099: String key = this .type.getName();
100: MetadataTarget meta = (MetadataTarget) metadata.get(key);
101: if (meta == null) {
102: try {
103: meta = new MetadataTargetImpl(type);
104: } catch (IntrospectionException e) {
105: throw new TagException(this .tag,
106: "Error Creating TargetMetadata", e);
107: }
108: metadata.put(key, meta);
109: }
110: return meta;
111: }
112:
113: public Metadata finish() {
114: if (!this .attributes.isEmpty()) {
115: if (this .rules.isEmpty()) {
116: if (log.isLoggable(Level.SEVERE)) {
117: for (Iterator itr = this .attributes.values()
118: .iterator(); itr.hasNext();) {
119: log
120: .severe(itr.next()
121: + " Unhandled by MetaTagHandler for type "
122: + this .type.getName());
123: }
124: }
125: } else {
126: MetadataTarget target = this .getMetadataTarget();
127: // now iterate over attributes
128: Map.Entry entry;
129: MetaRule rule;
130: Metadata data;
131: int ruleEnd = this .rules.size() - 1;
132: for (Iterator itr = this .attributes.entrySet()
133: .iterator(); itr.hasNext();) {
134: entry = (Map.Entry) itr.next();
135: data = null;
136: int i = ruleEnd;
137: while (data == null && i >= 0) {
138: rule = (MetaRule) this .rules.get(i);
139: data = rule
140: .applyRule(
141: (String) entry.getKey(),
142: (TagAttribute) entry.getValue(),
143: target);
144: i--;
145: }
146: if (data == null) {
147: if (log.isLoggable(Level.SEVERE)) {
148: log
149: .severe(entry.getValue()
150: + " Unhandled by MetaTagHandler for type "
151: + this .type.getName());
152: }
153: } else {
154: this .mappers.add(data);
155: }
156: }
157: }
158: }
159:
160: if (this .mappers.isEmpty()) {
161: return NONE;
162: } else {
163: return new MetadataImpl((Metadata[]) this .mappers
164: .toArray(new Metadata[this .mappers.size()]));
165: }
166: }
167:
168: public MetaRuleset ignoreAll() {
169: this .attributes.clear();
170: return this ;
171: }
172:
173: private final static Metadata NONE = new Metadata() {
174: public void applyMetadata(FaceletContext ctx, Object instance) {
175: // do nothing
176: }
177: };
178: }
|