1 15 package org.apache.hivemind.impl; 16 17 import java.util.ArrayList ; 18 import java.util.HashMap ; 19 import java.util.HashSet ; 20 import java.util.Iterator ; 21 import java.util.List ; 22 import java.util.Map ; 23 import java.util.Set ; 24 25 import org.apache.hivemind.ApplicationRuntimeException; 26 import org.apache.hivemind.Attribute; 27 import org.apache.hivemind.Element; 28 import org.apache.hivemind.schema.AttributeModel; 29 import org.apache.hivemind.schema.ElementModel; 30 import org.apache.hivemind.schema.Rule; 31 import org.apache.hivemind.schema.SchemaProcessor; 32 import org.apache.hivemind.schema.Translator; 33 import org.apache.hivemind.schema.rules.NullTranslator; 34 35 41 final class SchemaElement 42 { 43 private SchemaProcessor _processor; 44 45 private ElementModel _model; 46 47 private List _requiredAttributes = new ArrayList (); 48 49 private Set _knownAttributes = new HashSet (); 50 51 private Map _nestedElements; 52 53 56 private Map _attributeTranslators = new HashMap (); 57 58 private Map _attributeDefaults = new HashMap (); 59 60 65 private Map _attributeValues = new HashMap (); 66 67 SchemaElement(SchemaProcessor processor, ElementModel model) 68 { 69 _processor = processor; 70 _model = model; 71 72 List attributeModels = model.getAttributeModels(); 73 int count = attributeModels.size(); 74 75 for (int i = 0; i < count; i++) 76 { 77 AttributeModel am = (AttributeModel) attributeModels.get(i); 78 79 String name = am.getName(); 80 81 _knownAttributes.add(name); 82 83 if (am.isRequired()) 84 _requiredAttributes.add(name); 85 86 89 if (am.isUnique()) 90 _attributeValues.put(name, new HashMap ()); 91 92 if (am.getDefault() != null) 93 _attributeDefaults.put(am.getName(), am.getDefault()); 94 95 _attributeTranslators.put(name, am.getTranslator()); 96 } 97 } 98 99 102 SchemaElement getNestedElement(String elementName) 103 { 104 if (_nestedElements == null) 105 buildNestedElements(); 106 107 return (SchemaElement) _nestedElements.get(elementName); 108 } 109 110 private void buildNestedElements() 111 { 112 _nestedElements = new HashMap (); 113 114 List l = _model.getElementModel(); 115 int count = l.size(); 116 117 for (int i = 0; i < count; i++) 118 { 119 ElementModel nested = (ElementModel) l.get(i); 120 121 SchemaElement nestedElement = new SchemaElement(_processor, nested); 122 123 125 _nestedElements.put(nested.getElementName(), nestedElement); 126 } 127 128 } 129 130 134 void validateAttributes(Element element) 135 { 136 List l = element.getAttributes(); 137 int count = l.size(); 138 Set required = new HashSet (_requiredAttributes); 139 List errors = new ArrayList (); 140 141 for (int i = 0; i < count; i++) 142 { 143 Attribute a = (Attribute) l.get(i); 144 String name = a.getName(); 145 146 if (!_knownAttributes.contains(name)) 147 errors.add(XmlImplMessages.unknownAttribute(name)); 148 149 required.remove(name); 150 } 151 152 Iterator it = required.iterator(); 153 154 while (it.hasNext()) 155 { 156 String name = (String ) it.next(); 157 errors.add(XmlImplMessages.missingAttribute(name)); 158 } 159 160 count = errors.size(); 161 162 if (count == 0) 163 return; 164 165 StringBuffer buffer = new StringBuffer (); 166 167 buffer.append(XmlImplMessages.elementErrors(_processor, element)); 168 169 for (int i = 0; i < count; i++) 170 { 171 buffer.append(' '); 172 buffer.append(errors.get(i).toString()); 173 } 174 175 178 throw new ApplicationRuntimeException(buffer.toString(), element.getLocation(), null); 179 } 180 181 182 void fireBegin(Element element) 183 { 184 List rules = _model.getRules(); 185 int count = rules.size(); 186 187 for (int i = 0; i < count; i++) 188 { 189 Rule r = (Rule) rules.get(i); 190 191 r.begin(_processor, element); 192 193 } 194 } 195 196 void fireEnd(Element element) 197 { 198 List rules = _model.getRules(); 199 int count = rules.size(); 200 201 for (int i = count - 1; i >= 0; i--) 202 { 203 Rule r = (Rule) rules.get(i); 204 205 r.end(_processor, element); 206 207 } 208 } 209 210 private Translator _nullTranslator = new NullTranslator(); 211 212 private Translator _contentTranslator; 213 214 public Translator getContentTranslator() 215 { 216 if (_contentTranslator == null) 217 _contentTranslator = getTranslator(_model.getContentTranslator()); 218 219 return _contentTranslator; 220 } 221 222 private Translator getTranslator(String translator) 223 { 224 if (translator == null) 225 return _nullTranslator; 226 227 return _processor.getTranslator(translator); 228 } 229 230 public Translator getAttributeTranslator(String attributeName) 231 { 232 String translator = (String ) _attributeTranslators.get(attributeName); 233 234 return getTranslator(translator); 235 } 236 237 public String getAttributeDefault(String attributeName) 238 { 239 return (String ) _attributeDefaults.get(attributeName); 240 } 241 242 public ElementModel getModel() 243 { 244 return _model; 245 } 246 247 } | Popular Tags |