KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > scriptella > configuration > XmlConfigurableBase


1 /*
2  * Copyright 2006-2007 The Scriptella Project Team.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package scriptella.configuration;
17
18 import scriptella.core.SystemException;
19
20 import java.lang.reflect.Method JavaDoc;
21 import java.util.ArrayList JavaDoc;
22 import java.util.List JavaDoc;
23 import java.util.regex.Pattern JavaDoc;
24
25
26 /**
27  * Base class for configuration elements.
28  */

29 public abstract class XmlConfigurableBase implements XmlConfigurable {
30     private Location location;
31
32     protected void setRequiredProperty(final XmlElement element,
33                                        final String JavaDoc attribute, final String JavaDoc property) {
34         setProperty(element, attribute, property);
35         assertRequiredFieldPresent(element, attribute, property);
36     }
37
38     /**
39      * @see #setProperty(XmlElement, String, String)
40      */

41     protected void setProperty(final XmlElement element, final String JavaDoc attribute) {
42         setProperty(element, attribute, attribute);
43     }
44
45     /**
46      * Sets property on this object using attribute value.
47      *
48      * @param element element to get attribute.
49      * @param attribute attribute name
50      * @param property property name.
51      */

52     protected void setProperty(final XmlElement element,
53                                final String JavaDoc attribute, final String JavaDoc property) {
54         String JavaDoc attributeValue = element.getAttribute(attribute);
55
56         try {
57             final Method JavaDoc method = findSetter(property);
58             method.invoke(this, attributeValue);
59         } catch (Exception JavaDoc e) {
60             throw new ConfigurationException("Unable to set property " +
61                     property + " from attribute " + attribute, e, element);
62         }
63     }
64
65     /**
66      * @see #setPatternProperty(XmlElement, String, String)
67      */

68     protected void setPatternProperty(final XmlElement element, final String JavaDoc property) {
69         setPatternProperty(element, property, property);
70     }
71
72     /**
73      * Sets {@link Pattern} property on this object using attribute value.
74      *
75      * @param element element to get attribute.
76      * @param attribute attribute name
77      * @param property property name.
78      */

79     protected void setPatternProperty(final XmlElement element,
80                                       final String JavaDoc attribute, final String JavaDoc property) {
81         String JavaDoc ptrStr = element.getAttribute(attribute);
82         Pattern JavaDoc ptr = null;
83         if (ptrStr != null) {
84             try {
85                 //We use multiline+case insensitive patterns
86
ptr = Pattern.compile(ptrStr, Pattern.CASE_INSENSITIVE + Pattern.DOTALL);
87             } catch (Exception JavaDoc e) {
88                 throw new ConfigurationException("Attribute " + attribute + " requires valid regular expression, but was: " +
89                         ptrStr, e, element);
90             }
91         }
92         try {
93             final Method JavaDoc method = getClass().getMethod(
94                     "set" + Character.toUpperCase(property.charAt(0)) + property.substring(1), Pattern JavaDoc.class);
95             method.invoke(this, ptr);
96         } catch (Exception JavaDoc e) {
97             throw new ConfigurationException("Unable to set property " +
98                     property + " from attribute " + attribute, e, element);
99         }
100
101
102     }
103
104     protected Method JavaDoc findSetter(final String JavaDoc property)
105             throws NoSuchMethodException JavaDoc {
106         return getClass()
107                 .getMethod("set" +
108                         Character.toUpperCase(property.charAt(0)) + property.substring(1),
109                         String JavaDoc.class);
110     }
111
112     protected Method JavaDoc findGetter(final String JavaDoc property)
113             throws NoSuchMethodException JavaDoc {
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 JavaDoc attribute) {
120         setRequiredProperty(element, attribute, attribute);
121     }
122
123     protected <T> T loadClass(final XmlElement element, final String JavaDoc attribute,
124                               final Class JavaDoc<T> spec) {
125         final String JavaDoc h = element.getAttribute(attribute);
126
127         if (h == null) {
128             return null;
129         }
130
131         Class JavaDoc c = null;
132
133         try {
134             c = Class.forName(h);
135         } catch (ClassNotFoundException JavaDoc 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 JavaDoc e) {
148             throw new scriptella.configuration.ConfigurationException(
149                     "Unable to instantiate class " + c, element);
150         }
151     }
152
153     protected <T extends XmlConfigurable> List JavaDoc<T> load(
154             final List JavaDoc<XmlElement> elements, final Class JavaDoc<T> clazz) {
155         if (elements == null) {
156             return null;
157         }
158
159         List JavaDoc<T> l = new ArrayList JavaDoc<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 JavaDoc e) {
167                 throw new SystemException(e);
168             } catch (IllegalAccessException JavaDoc e) {
169                 throw new SystemException(e);
170             }
171         }
172
173         return l;
174     }
175
176     protected void assertRequiredFieldPresent(final XmlElement element,
177                                               final String JavaDoc attribute, final String JavaDoc property) {
178         try {
179             final Method JavaDoc getter = findGetter(property);
180
181             if (getter.invoke(this, (Object JavaDoc[]) null) == null) {
182                 throw new RequiredAttributeException(attribute, element);
183             }
184         } catch (ConfigurationException e) {
185             throw e;
186         } catch (Exception JavaDoc e) {
187             throw new RequiredAttributeException(attribute, e, element);
188         }
189     }
190
191     protected void assertRequiredFieldPresent(final XmlElement element,
192                                               final String JavaDoc property) {
193         assertRequiredFieldPresent(element, property, property);
194     }
195
196     /**
197      * Called by elements wishing to report their location in XML.
198      *
199      * @param element element to calculate location
200      */

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