1 14 15 package com.sun.facelets.tag; 16 17 import java.beans.IntrospectionException ; 18 import java.util.ArrayList ; 19 import java.util.HashMap ; 20 import java.util.Iterator ; 21 import java.util.List ; 22 import java.util.Map ; 23 import java.util.WeakHashMap ; 24 import java.util.logging.Level ; 25 import java.util.logging.Logger ; 26 27 import com.sun.facelets.FaceletContext; 28 import com.sun.facelets.util.ParameterCheck; 29 30 35 final class MetaRulesetImpl extends MetaRuleset { 36 37 private final static WeakHashMap metadata = new WeakHashMap (); 38 39 private final static Logger log = Logger 40 .getLogger("facelets.tag.meta"); 41 42 private final Tag tag; 43 44 private final Class type; 45 46 private final Map attributes; 47 48 private final List mappers; 49 50 private final List rules; 51 52 public MetaRulesetImpl(Tag tag, Class type) { 53 this.tag = tag; 54 this.type = type; 55 this.attributes = new HashMap (); 56 this.mappers = new ArrayList (); 57 this.rules = new ArrayList (); 58 59 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 this.rules.add(BeanPropertyTagRule.Instance); 67 } 68 69 public MetaRuleset ignore(String attribute) { 70 ParameterCheck.notNull("attribute", attribute); 71 this.attributes.remove(attribute); 72 return this; 73 } 74 75 public MetaRuleset alias(String attribute, String 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 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 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 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 Map.Entry entry; 126 MetaRule rule; 127 Metadata data; 128 int ruleEnd = this.rules.size() - 1; 129 for (Iterator itr = this.attributes.entrySet().iterator(); itr.hasNext(); ) { 130 entry = (Map.Entry ) 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 ) 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 instance) { 164 } 166 }; 167 } 168 | Popular Tags |