KickJava   Java API By Example, From Geeks To Geeks.

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


1 package spoon.support.processing;
2
3 import java.util.ArrayList JavaDoc;
4 import java.util.List JavaDoc;
5
6 import org.xml.sax.Attributes JavaDoc;
7 import org.xml.sax.SAXException JavaDoc;
8 import org.xml.sax.helpers.DefaultHandler JavaDoc;
9
10 import spoon.AbstractLauncher;
11 import spoon.support.builder.CtResource;
12
13 /**
14  * This class defines the SAX handler to parse a Spoonlet deployment descriptor file.
15  */

16 public class SpoonletXmlHandler extends DefaultHandler JavaDoc {
17
18     private AbstractLauncher launcher;
19
20     private List JavaDoc<CtResource> spoonletIndex;
21
22     XmlProcessorProperties prop;
23
24     String JavaDoc propName;
25
26     List JavaDoc<Object JavaDoc> values;
27
28     String JavaDoc buffer;
29
30     /**
31      * Creates a new handler.
32      * @param launcher the launcher
33      * @param spoonletIndex ?
34      */

35     public SpoonletXmlHandler(AbstractLauncher launcher,
36             List JavaDoc<CtResource> spoonletIndex) {
37         super();
38         this.launcher = launcher;
39         this.spoonletIndex = spoonletIndex;
40     }
41
42     /**
43      * Handles XML element ends.
44      */

45     @Override JavaDoc
46     public void endElement(String JavaDoc uri, String JavaDoc localName, String JavaDoc qName)
47             throws SAXException JavaDoc {
48         if (localName.equals("processor")) {
49             launcher.getFactory().getEnvironment().setProcessorProperties(
50                     prop.getProcessorName(), prop);
51             prop = null;
52         } else if (localName.equals("property")) {
53             if (values != null) {
54                 prop.addProperty(propName, values);
55             }
56             values = null;
57             propName = null;
58         } else if (localName.equals("value")) {
59             values.add(buffer);
60         }
61         buffer = null;
62         super.endElement(uri, localName, qName);
63     }
64
65     /**
66      * Handles characters.
67      */

68     @Override JavaDoc
69     public void characters(char[] ch, int start, int end) throws SAXException JavaDoc {
70         buffer = new String JavaDoc(ch, start, end);
71     }
72
73     /**
74      * Handles XML element starts.
75      */

76     @Override JavaDoc
77     public void startElement(String JavaDoc uri, String JavaDoc localName, String JavaDoc qName,
78             Attributes JavaDoc attributes) throws SAXException JavaDoc {
79         if (localName.equals("processor")) {
80             launcher.addProcessor(attributes.getValue("class"));
81             prop = new XmlProcessorProperties(launcher.getFactory(), attributes
82                     .getValue("class"));
83         } else if (localName.equals("template")) {
84             String JavaDoc foldername = attributes.getValue("folder");
85             for (CtResource r : spoonletIndex) {
86                 if (r.getName().startsWith(foldername)) {
87                     launcher.addTemplateResource(r);
88                 }
89             }
90         } else if (localName.equals("property")) {
91             propName = attributes.getValue("name");
92             if (attributes.getValue("value") != null) {
93                 prop.addProperty(propName, attributes.getValue("value"));
94             } else {
95                 values = new ArrayList JavaDoc<Object JavaDoc>();
96             }
97         }
98     }
99 }
100
Popular Tags