1 package com.tirsen.nanning.attribute; 2 3 import java.io.IOException ; 4 import java.io.InputStream ; 5 import java.io.FileInputStream ; 6 import java.util.Properties ; 7 import java.net.MalformedURLException ; 8 9 import org.apache.commons.digester.Digester; 10 import org.xml.sax.SAXException ; 11 12 public class AttributesXMLParser implements AttributesLoader { 13 private String fieldName; 14 private String methodName; 15 private String argumentList; 16 private ClassPropertiesHelper classPropertiesHelper; 17 18 public void load(ClassAttributes classAttributes) { 19 this.classPropertiesHelper = new ClassPropertiesHelper(classAttributes); 20 Class aClass = classAttributes.getAttributeClass(); 21 InputStream input = null; 22 try { 23 input = Attributes.findFile(aClass, classPropertiesHelper.getClassName() + ".xml"); 25 if (input == null) { 26 input = Attributes.findFile(aClass, aClass.getName().replace('.', '/') + ".xml"); 27 } 28 29 if (input != null) { 30 parse(input); 31 } 32 33 } catch (MalformedURLException e) { 34 throw new AttributeException("Error fetching properties for " + aClass, e); 35 } catch (IOException e) { 36 throw new AttributeException("Error fetching properties for " + aClass, e); 37 } catch (AttributeException e) { 38 throw e; 39 } catch (Exception e) { 40 throw new AttributeException("Error fetching properties for " + aClass, e); 41 } finally { 42 if (input != null) { 43 try { 44 input.close(); 45 } catch (IOException e) { 46 throw new AttributeException(e); 47 } 48 } 49 } 50 } 51 52 private void parse(InputStream input) throws IOException , SAXException { 53 Digester digester = new Digester(); 54 55 digester.addCallMethod("*/attribute", "setAttributeValue", 2); 56 digester.addCallParam("*/attribute/name", 0); 57 digester.addCallParam("*/attribute/value", 1); 58 59 digester.addSetProperties("attributes/method", "name", "methodName"); 60 digester.addCallMethod("attributes/method/parameter-type", "addArgumentType", 0); 61 62 digester.addSetProperties("attributes/field", "name", "fieldName"); 63 64 digester.push(this); 65 digester.parse(input); 66 } 67 68 public void setAttributeValue(String name, String value) { 69 if (fieldName != null) { 70 classPropertiesHelper.loadFieldAttribute(fieldName, name, value); 71 72 } else if (methodName != null) { 73 String methodSignature = methodName + "(" + ((argumentList == null) ? "" : argumentList) + ")"; 74 classPropertiesHelper.loadMethodAttribute(methodSignature, name, value); 75 76 } else { 77 classPropertiesHelper.loadClassAttribute(name, value); 78 79 } 80 fieldName = null; 81 methodName = null; 82 argumentList = null; 83 } 84 85 public void setFieldName(String name) { 86 fieldName = name; 87 } 88 89 public void setMethodName(String name) { 90 methodName = name; 91 } 92 93 public void addArgumentType(String type) { 94 if (argumentList == null) { 95 argumentList = type; 96 } else { 97 argumentList += ","; 98 argumentList += type; 99 } 100 } 101 } 102
| Popular Tags
|