1 15 package org.apache.hivemind.parse; 16 17 import java.util.ArrayList ; 18 import java.util.HashMap ; 19 import java.util.Iterator ; 20 import java.util.List ; 21 import java.util.ListIterator ; 22 import java.util.Map ; 23 24 import org.apache.commons.logging.Log; 25 import org.apache.commons.logging.LogFactory; 26 import org.apache.hivemind.Element; 27 import org.apache.hivemind.ErrorHandler; 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.rules.BaseRule; 33 import org.apache.hivemind.schema.rules.CreateObjectRule; 34 import org.apache.hivemind.schema.rules.InvokeParentRule; 35 import org.apache.hivemind.schema.rules.ReadAttributeRule; 36 37 45 public class ConversionDescriptor extends BaseRule 46 { 47 private static final String DEFAULT_PARENT_METHOD_NAME = "add"; 48 49 private static final Log LOG = LogFactory.getLog(ConversionDescriptor.class); 50 51 private ErrorHandler _errorHandler; 52 53 private String _className; 54 55 private String _parentMethodName = DEFAULT_PARENT_METHOD_NAME; 56 57 private Map _attributeNameMappingMap = new HashMap (); 58 59 60 private List _attributeMappings = new ArrayList (); 61 62 private List _rules; 63 64 private ElementModel _elementModel; 65 66 public ConversionDescriptor(ErrorHandler errorHandler, ElementModel elementModel) 67 { 68 _errorHandler = errorHandler; 69 _elementModel = elementModel; 70 } 71 72 75 public List getAttributeMappings() 76 { 77 return _attributeMappings; 78 } 79 80 85 public void addAttributeMapping(AttributeMappingDescriptor descriptor) 86 { 87 String attributeName = descriptor.getAttributeName(); 88 89 AttributeMappingDescriptor existing = (AttributeMappingDescriptor) _attributeNameMappingMap 90 .get(attributeName); 91 92 if (existing != null) 93 { 94 _errorHandler.error( 95 LOG, 96 ParseMessages.dupeAttributeMapping(descriptor, existing), 97 descriptor.getLocation(), 98 null); 99 100 return; 101 } 102 103 _attributeNameMappingMap.put(attributeName, descriptor); 104 105 _attributeMappings.add(descriptor); 106 } 107 108 111 public String getClassName() 112 { 113 return _className; 114 } 115 116 public void setClassName(String string) 117 { 118 _className = string; 119 } 120 121 124 public String getParentMethodName() 125 { 126 return _parentMethodName; 127 } 128 129 public void setParentMethodName(String string) 130 { 131 _parentMethodName = string; 132 } 133 134 137 public void begin(SchemaProcessor processor, Element element) 138 { 139 for (Iterator i = _rules.iterator(); i.hasNext();) 140 { 141 Rule rule = (Rule) i.next(); 142 143 rule.begin(processor, element); 144 } 145 } 146 147 150 public void end(SchemaProcessor processor, Element element) 151 { 152 for (ListIterator i = _rules.listIterator(_rules.size()); i.hasPrevious();) 153 { 154 Rule rule = (Rule) i.previous(); 155 156 rule.end(processor, element); 157 } 158 } 159 160 public void addRulesForModel() 161 { 162 _rules = new ArrayList (); 163 164 _rules.add(new CreateObjectRule(_className)); 165 166 addAttributeRules(); 167 168 _rules.add(new InvokeParentRule(_parentMethodName)); 169 } 170 171 private void addAttributeRules() 172 { 173 Iterator i = _elementModel.getAttributeModels().iterator(); 174 175 while (i.hasNext()) 176 { 177 AttributeModel am = (AttributeModel) i.next(); 178 String attributeName = am.getName(); 179 180 AttributeMappingDescriptor amd = (AttributeMappingDescriptor) _attributeNameMappingMap 181 .get(attributeName); 182 183 if (amd == null) 184 { 185 _rules.add(new ReadAttributeRule(attributeName, 186 constructPropertyName(attributeName), null, getLocation())); 187 } 188 else 189 { 190 String propertyName = amd.getPropertyName(); 191 if (propertyName == null) 192 propertyName = constructPropertyName(attributeName); 193 194 _rules.add(new ReadAttributeRule(attributeName, propertyName, null, amd 195 .getLocation())); 196 197 _attributeNameMappingMap.remove(attributeName); 198 } 199 } 200 201 if (!_attributeNameMappingMap.isEmpty()) 202 _errorHandler.error(LOG, ParseMessages.extraMappings( 203 _attributeNameMappingMap.keySet(), 204 _elementModel), _elementModel.getLocation(), null); 205 } 206 207 private String constructPropertyName(String attributeName) 208 { 209 int dashx = attributeName.indexOf('-'); 210 if (dashx < 0) 211 return attributeName; 212 213 int length = attributeName.length(); 214 StringBuffer buffer = new StringBuffer (length); 215 216 buffer.append(attributeName.substring(0, dashx)); 217 boolean toUpper = true; 218 219 for (int i = dashx + 1; i < length; i++) 220 { 221 char ch = attributeName.charAt(i); 222 223 if (ch == '-') 224 { 225 toUpper = true; 226 continue; 227 } 228 229 if (toUpper) 230 ch = Character.toUpperCase(ch); 231 232 buffer.append(ch); 233 234 toUpper = false; 235 } 236 237 return buffer.toString(); 238 } 239 } | Popular Tags |