1 16 package scriptella.configuration; 17 18 import scriptella.core.SystemException; 19 20 import java.lang.reflect.Method ; 21 import java.util.ArrayList ; 22 import java.util.List ; 23 import java.util.regex.Pattern ; 24 25 26 29 public abstract class XmlConfigurableBase implements XmlConfigurable { 30 private Location location; 31 32 protected void setRequiredProperty(final XmlElement element, 33 final String attribute, final String property) { 34 setProperty(element, attribute, property); 35 assertRequiredFieldPresent(element, attribute, property); 36 } 37 38 41 protected void setProperty(final XmlElement element, final String attribute) { 42 setProperty(element, attribute, attribute); 43 } 44 45 52 protected void setProperty(final XmlElement element, 53 final String attribute, final String property) { 54 String attributeValue = element.getAttribute(attribute); 55 56 try { 57 final Method method = findSetter(property); 58 method.invoke(this, attributeValue); 59 } catch (Exception e) { 60 throw new ConfigurationException("Unable to set property " + 61 property + " from attribute " + attribute, e, element); 62 } 63 } 64 65 68 protected void setPatternProperty(final XmlElement element, final String property) { 69 setPatternProperty(element, property, property); 70 } 71 72 79 protected void setPatternProperty(final XmlElement element, 80 final String attribute, final String property) { 81 String ptrStr = element.getAttribute(attribute); 82 Pattern ptr = null; 83 if (ptrStr != null) { 84 try { 85 ptr = Pattern.compile(ptrStr, Pattern.CASE_INSENSITIVE + Pattern.DOTALL); 87 } catch (Exception e) { 88 throw new ConfigurationException("Attribute " + attribute + " requires valid regular expression, but was: " + 89 ptrStr, e, element); 90 } 91 } 92 try { 93 final Method method = getClass().getMethod( 94 "set" + Character.toUpperCase(property.charAt(0)) + property.substring(1), Pattern .class); 95 method.invoke(this, ptr); 96 } catch (Exception e) { 97 throw new ConfigurationException("Unable to set property " + 98 property + " from attribute " + attribute, e, element); 99 } 100 101 102 } 103 104 protected Method findSetter(final String property) 105 throws NoSuchMethodException { 106 return getClass() 107 .getMethod("set" + 108 Character.toUpperCase(property.charAt(0)) + property.substring(1), 109 String .class); 110 } 111 112 protected Method findGetter(final String property) 113 throws NoSuchMethodException { 114 return getClass() 115 .getMethod("get" + Character.toUpperCase(property.charAt(0)) + property.substring(1)); 116 } 117 118 protected void setRequiredProperty(final XmlElement element, 119 final String attribute) { 120 setRequiredProperty(element, attribute, attribute); 121 } 122 123 protected <T> T loadClass(final XmlElement element, final String attribute, 124 final Class <T> spec) { 125 final String h = element.getAttribute(attribute); 126 127 if (h == null) { 128 return null; 129 } 130 131 Class c = null; 132 133 try { 134 c = Class.forName(h); 135 } catch (ClassNotFoundException e) { 136 throw new scriptella.configuration.ConfigurationException( 137 "Invalid class " + h, e, element); 138 } 139 140 if (!spec.isAssignableFrom(c)) { 141 throw new scriptella.configuration.ConfigurationException("Class " + 142 c + " doesn't implement " + spec, element); 143 } 144 145 try { 146 return (T) c.newInstance(); 147 } catch (Exception e) { 148 throw new scriptella.configuration.ConfigurationException( 149 "Unable to instantiate class " + c, element); 150 } 151 } 152 153 protected <T extends XmlConfigurable> List <T> load( 154 final List <XmlElement> elements, final Class <T> clazz) { 155 if (elements == null) { 156 return null; 157 } 158 159 List <T> l = new ArrayList <T>(elements.size()); 160 161 for (XmlElement element : elements) { 162 try { 163 T t = clazz.newInstance(); 164 t.configure(element); 165 l.add(t); 166 } catch (InstantiationException e) { 167 throw new SystemException(e); 168 } catch (IllegalAccessException e) { 169 throw new SystemException(e); 170 } 171 } 172 173 return l; 174 } 175 176 protected void assertRequiredFieldPresent(final XmlElement element, 177 final String attribute, final String property) { 178 try { 179 final Method getter = findGetter(property); 180 181 if (getter.invoke(this, (Object []) null) == null) { 182 throw new RequiredAttributeException(attribute, element); 183 } 184 } catch (ConfigurationException e) { 185 throw e; 186 } catch (Exception e) { 187 throw new RequiredAttributeException(attribute, e, element); 188 } 189 } 190 191 protected void assertRequiredFieldPresent(final XmlElement element, 192 final String property) { 193 assertRequiredFieldPresent(element, property, property); 194 } 195 196 201 protected void setLocation(XmlElement element) { 202 location = new Location(element.getXPath()); 203 } 204 205 protected void setLocation(Location location) { 206 this.location = location; 207 } 208 209 protected Location getLocation() { 210 return location; 211 } 212 } 213 | Popular Tags |