KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tirsen > nanning > attribute > AttributesXMLParser


1 package com.tirsen.nanning.attribute;
2
3 import java.io.IOException JavaDoc;
4 import java.io.InputStream JavaDoc;
5 import java.io.FileInputStream JavaDoc;
6 import java.util.Properties JavaDoc;
7 import java.net.MalformedURLException JavaDoc;
8
9 import org.apache.commons.digester.Digester;
10 import org.xml.sax.SAXException JavaDoc;
11
12 public class AttributesXMLParser implements AttributesLoader {
13     private String JavaDoc fieldName;
14     private String JavaDoc methodName;
15     private String JavaDoc argumentList;
16     private ClassPropertiesHelper classPropertiesHelper;
17
18     public void load(ClassAttributes classAttributes) {
19         this.classPropertiesHelper = new ClassPropertiesHelper(classAttributes);
20         Class JavaDoc aClass = classAttributes.getAttributeClass();
21         InputStream JavaDoc input = null;
22         try {
23             // load the XML-defined attributes
24
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 JavaDoc e) {
34             throw new AttributeException("Error fetching properties for " + aClass, e);
35         } catch (IOException JavaDoc e) {
36             throw new AttributeException("Error fetching properties for " + aClass, e);
37         } catch (AttributeException e) {
38             throw e;
39         } catch (Exception JavaDoc 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 JavaDoc e) {
46                     throw new AttributeException(e);
47                 }
48             }
49         }
50     }
51
52     private void parse(InputStream JavaDoc input) throws IOException JavaDoc, SAXException JavaDoc {
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 JavaDoc name, String JavaDoc value) {
69         if (fieldName != null) {
70             classPropertiesHelper.loadFieldAttribute(fieldName, name, value);
71
72         } else if (methodName != null) {
73             String JavaDoc 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 JavaDoc name) {
86         fieldName = name;
87     }
88
89     public void setMethodName(String JavaDoc name) {
90         methodName = name;
91     }
92
93     public void addArgumentType(String JavaDoc type) {
94         if (argumentList == null) {
95             argumentList = type;
96         } else {
97             argumentList += ",";
98             argumentList += type;
99         }
100     }
101 }
102
Free Books   Free Magazines  
Popular Tags