KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > spoon > support > processing > XmlProcessorProperties


1 package spoon.support.processing;
2
3 import java.io.IOException JavaDoc;
4 import java.io.InputStream JavaDoc;
5 import java.lang.reflect.Array JavaDoc;
6 import java.util.ArrayList JavaDoc;
7 import java.util.Collection JavaDoc;
8 import java.util.Map JavaDoc;
9 import java.util.TreeMap JavaDoc;
10 import java.util.Map.Entry;
11
12 import org.xml.sax.Attributes JavaDoc;
13 import org.xml.sax.InputSource JavaDoc;
14 import org.xml.sax.SAXException JavaDoc;
15 import org.xml.sax.XMLReader JavaDoc;
16 import org.xml.sax.helpers.DefaultHandler JavaDoc;
17 import org.xml.sax.helpers.XMLReaderFactory JavaDoc;
18
19 import spoon.processing.ProcessorProperties;
20 import spoon.reflect.Factory;
21 import spoon.reflect.reference.CtExecutableReference;
22 import spoon.reflect.reference.CtFieldReference;
23 import spoon.reflect.reference.CtPackageReference;
24 import spoon.reflect.reference.CtTypeReference;
25
26 /**
27  * This class defines a processor properties accessor that parses an XML
28  * description of the properties.
29  */

30 public class XmlProcessorProperties implements ProcessorProperties {
31
32     /**
33      * Defines the tag handler of an XML Spoon property file.
34      */

35     public class Loader extends DefaultHandler JavaDoc {
36         boolean isValue = false;
37
38         String JavaDoc name;
39
40         Object JavaDoc value;
41
42         /**
43          * Handdles a tag content.
44          */

45         @SuppressWarnings JavaDoc("unchecked")
46         @Override JavaDoc
47         public void characters(char[] ch, int start, int length)
48                 throws SAXException JavaDoc {
49             if (isValue) {
50                 if (value == null || !(value instanceof Collection JavaDoc))
51                     value = new ArrayList JavaDoc<Object JavaDoc>();
52                 ((Collection JavaDoc<Object JavaDoc>) value).add(new String JavaDoc(ch, start, length));
53             }
54         }
55
56         /**
57          * Handdles a tag end.
58          */

59         @Override JavaDoc
60         public void endElement(String JavaDoc uri, String JavaDoc localName, String JavaDoc qName)
61                 throws SAXException JavaDoc {
62             if (localName.equals("property")) {
63                 props.put(name, value);
64                 value = null;
65             } else if (localName.equals("value")) {
66                 isValue = false;
67             }
68         }
69
70         /**
71          * Handdles a tag start.
72          */

73         @Override JavaDoc
74         public void startElement(String JavaDoc uri, String JavaDoc localName, String JavaDoc qName,
75                 Attributes JavaDoc attributes) throws SAXException JavaDoc {
76             if (localName.equals("property")) {
77                 name = attributes.getValue("name");
78                 if (attributes.getValue("value") != null)
79                     value = attributes.getValue("value");
80             } else if (localName.equals("value")) {
81                 isValue = true;
82             }
83         }
84     }
85
86     @SuppressWarnings JavaDoc("unchecked")
87     public static <T> T convert(Factory JavaDoc factory, Class JavaDoc<T> type, Object JavaDoc o) {
88         if (o == null)
89             return null;
90         if (type == boolean.class)
91             return (T) new Boolean JavaDoc(o.toString());
92         if (type == byte.class)
93             return (T) new Byte JavaDoc(o.toString());
94         if (type == char.class)
95             return (T) new Character JavaDoc(o.toString().charAt(0));
96         if (type == double.class)
97             return (T) new Double JavaDoc(o.toString());
98         if (type == float.class)
99             return (T) new Float JavaDoc(o.toString());
100         if (type == int.class)
101             return (T) new Integer JavaDoc(o.toString());
102         if (type == long.class)
103             return (T) new Long JavaDoc(o.toString());
104         if (CtTypeReference.class.isAssignableFrom(type)) {
105             return (T) factory.Type().createReference(o.toString());
106         }
107         if (CtExecutableReference.class.isAssignableFrom(type)) {
108             return (T) factory.Executable().createReference(o.toString());
109         }
110         if (CtFieldReference.class.isAssignableFrom(type)) {
111             return (T) factory.Field().createReference(o.toString());
112         }
113         if (CtPackageReference.class.isAssignableFrom(type)) {
114             return (T) factory.Package().createReference(o.toString());
115         }
116         if (type.isEnum()) {
117             return (T) Enum.valueOf((Class JavaDoc<Enum JavaDoc>) type, o.toString());
118         }
119         return (T) o.toString();
120     }
121
122     @SuppressWarnings JavaDoc("unchecked")
123     public static <T> T convertArray(Factory JavaDoc factory, Class JavaDoc<T> type,
124             Collection JavaDoc<Object JavaDoc> val) {
125         if (type.equals(boolean.class)) {
126             boolean[] ret = new boolean[val.size()];
127             int i = 0;
128             for (Object JavaDoc o : val) {
129                 ret[i++] = convert(factory, boolean.class, o);
130             }
131             return (T) ret;
132         } else if (type.equals(byte.class)) {
133             byte[] ret = new byte[val.size()];
134             int i = 0;
135             for (Object JavaDoc o : val) {
136                 ret[i++] = convert(factory, byte.class, o);
137             }
138             return (T) ret;
139         } else if (type.equals(char.class)) {
140             char[] ret = new char[val.size()];
141             int i = 0;
142             for (Object JavaDoc o : val) {
143                 ret[i++] = convert(factory, char.class, o);
144             }
145             return (T) ret;
146         } else if (type.equals(double.class)) {
147             double[] ret = new double[val.size()];
148             int i = 0;
149             for (Object JavaDoc o : val) {
150                 ret[i++] = convert(factory, double.class, o);
151             }
152             return (T) ret;
153         } else if (type.equals(float.class)) {
154             float[] ret = new float[val.size()];
155             int i = 0;
156             for (Object JavaDoc o : val) {
157                 ret[i++] = convert(factory, float.class, o);
158             }
159             return (T) ret;
160         } else if (type.equals(int.class)) {
161             int[] ret = new int[val.size()];
162             int i = 0;
163             for (Object JavaDoc o : val) {
164                 ret[i++] = convert(factory, int.class, o);
165             }
166             return (T) ret;
167         } else if (type.equals(long.class)) {
168             long[] ret = new long[val.size()];
169             int i = 0;
170             for (Object JavaDoc o : val) {
171                 ret[i++] = convert(factory, long.class, o);
172             }
173             return (T) ret;
174         } else if (type.equals(String JavaDoc.class)) {
175             String JavaDoc[] ret = new String JavaDoc[val.size()];
176             int i = 0;
177             for (Object JavaDoc o : val) {
178                 ret[i++] = convert(factory, String JavaDoc.class, o);
179             }
180             return (T) ret;
181         } else if (CtPackageReference.class.isAssignableFrom(type)) {
182             CtPackageReference[] ret = new CtPackageReference[val.size()];
183             int i = 0;
184             for (Object JavaDoc o : val) {
185                 ret[i++] = convert(factory, CtPackageReference.class, o);
186             }
187             return (T) ret;
188         } else if (CtTypeReference.class.isAssignableFrom(type)) {
189             CtTypeReference[] ret = new CtTypeReference[val.size()];
190             int i = 0;
191             for (Object JavaDoc o : val) {
192                 ret[i++] = convert(factory, CtTypeReference.class, o);
193             }
194             return (T) ret;
195         } else if (CtFieldReference.class.isAssignableFrom(type)) {
196             CtFieldReference[] ret = new CtFieldReference[val.size()];
197             int i = 0;
198             for (Object JavaDoc o : val) {
199                 ret[i++] = convert(factory, CtFieldReference.class, o);
200             }
201             return (T) ret;
202         } else if (CtExecutableReference.class.isAssignableFrom(type)) {
203             CtExecutableReference[] ret = new CtExecutableReference[val.size()];
204             int i = 0;
205             for (Object JavaDoc o : val) {
206                 ret[i++] = convert(factory, CtExecutableReference.class, o);
207             }
208             return (T) ret;
209         } else if (type.isEnum()) {
210             Collection JavaDoc<Enum JavaDoc<?>> ret = new ArrayList JavaDoc<Enum JavaDoc<?>>();
211             for (Object JavaDoc o : val) {
212                 ret.add((Enum JavaDoc) convert(factory, type, o));
213             }
214             return (T) ret.toArray((Enum JavaDoc[]) Array.newInstance(type, 0));
215         }
216         return null;
217     }
218
219     Factory JavaDoc factory;
220
221     String JavaDoc processorName;
222
223     private Map JavaDoc<String JavaDoc, Object JavaDoc> props = new TreeMap JavaDoc<String JavaDoc, Object JavaDoc>();
224
225     public XmlProcessorProperties(Factory JavaDoc factory, String JavaDoc processorName) {
226         this.processorName = processorName;
227         this.factory = factory;
228     }
229
230     public XmlProcessorProperties(Factory JavaDoc factory, String JavaDoc processorName,
231             InputStream JavaDoc stream) throws IOException JavaDoc {
232         this.processorName = processorName;
233         this.factory = factory;
234         load(stream);
235     }
236
237     public void addProperty(String JavaDoc name, Object JavaDoc value) {
238         props.put(name, value);
239     }
240
241     @SuppressWarnings JavaDoc("unchecked")
242     public <T> T get(Class JavaDoc<T> type, String JavaDoc name) {
243         if (!props.containsKey(name)) {
244             return null;
245         }
246         if (type.isArray()) {
247             return (T) convertArray(factory, type.getComponentType(),
248                     (Collection JavaDoc<Object JavaDoc>) props.get(name));
249         }
250         return (T) convert(factory, type, props.get(name));
251     }
252
253     public String JavaDoc getProcessorName() {
254         return processorName;
255     }
256
257     private void load(InputStream JavaDoc stream) throws IOException JavaDoc {
258         if (stream == null)
259             return;
260         XMLReader JavaDoc xr;
261         try {
262             xr = XMLReaderFactory.createXMLReader();
263             Loader handler = new Loader();
264             xr.setContentHandler(handler);
265             xr.parse(new InputSource JavaDoc(stream));
266         } catch (SAXException JavaDoc e) {
267             throw new IOException JavaDoc(e.getMessage());
268         }
269     }
270
271     @Override JavaDoc
272     public String JavaDoc toString() {
273         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
274         buf.append("Properties : \n");
275
276         for (Entry<String JavaDoc, Object JavaDoc> ent : props.entrySet()) {
277             buf.append(ent.getKey());
278             for (int i = ent.getKey().length(); i < 15; i++)
279                 buf.append(" ");
280             buf.append(": " + ent.getValue() + "\n");
281         }
282         return buf.toString();
283     }
284 }
285
Popular Tags