1 package org.andromda.cartridges.interfaces;
2
3 import java.io.IOException;
4 import java.io.InputStream;
5
6 import javax.xml.parsers.ParserConfigurationException;
7 import javax.xml.parsers.SAXParser;
8 import javax.xml.parsers.SAXParserFactory;
9
10 import org.xml.sax.Attributes;
11 import org.xml.sax.SAXException;
12 import org.xml.sax.helpers.DefaultHandler;
13
14 /***
15 * Reads an XML-formatted cartridge description.
16 *
17 * @author <a href="http://www.mbohlen.de">Matthias Bohlen</a>
18 *
19 */
20 public class CartridgeXmlParser extends DefaultHandler
21 {
22 private final SAXParserFactory _factory;
23 private DefaultCartridgeDescriptor desc = null;
24
25 public CartridgeXmlParser()
26 {
27 _factory = SAXParserFactory.newInstance();
28 _factory.setValidating(false);
29 }
30
31 /***
32 * Parses the XML descriptor file and returns an appropriate data structure.
33 * @param in the input stream to read from
34 * @return DefaultCartridgeDescriptor a description for this cartridge
35 */
36 public DefaultCartridgeDescriptor parse(InputStream in)
37 {
38 try
39 {
40 SAXParser parser = _factory.newSAXParser();
41 parser.parse(in, this);
42 }
43 catch (IOException e)
44 {
45 desc = null;
46 e.printStackTrace();
47 }
48 catch (IllegalArgumentException e)
49 {
50 desc = null;
51 e.printStackTrace();
52 }
53 catch (ParserConfigurationException e)
54 {
55 desc = null;
56 e.printStackTrace();
57 }
58 catch (SAXException e)
59 {
60 desc = null;
61 e.printStackTrace();
62 }
63 return desc;
64 }
65
66 public void startDocument()
67 {
68 desc = new DefaultCartridgeDescriptor();
69 }
70
71 public void startElement(
72 String namespaceURI,
73 String localName,
74 String qName,
75 Attributes attributes)
76 {
77
78 if (qName.equals("cartridge"))
79 {
80 desc.setCartridgeName(attributes.getValue("name"));
81 }
82 else if (qName.equals("property"))
83 {
84 desc.addProperty(
85 attributes.getValue("name"),
86 attributes.getValue("value"));
87 }
88 else if (qName.equals("stereotype"))
89 {
90 desc.addSupportedStereotype(attributes.getValue("name"));
91 }
92 else if (qName.equals("outlet"))
93 {
94 desc.addOutlet(attributes.getValue("name"));
95 }
96 else if (qName.equals("class"))
97 {
98 desc.setCartridgeClassName(attributes.getValue("name"));
99 }
100 else if (qName.equals("template"))
101 {
102 String val = attributes.getValue("generateEmptyFiles");
103 boolean generateEmptyFiles =
104 (val == null) || val.equalsIgnoreCase("true");
105 TemplateConfiguration tc =
106 new TemplateConfiguration(
107 desc,
108 attributes.getValue("stereotype"),
109 attributes.getValue("sheet"),
110 attributes.getValue("outputPattern"),
111 attributes.getValue("outlet"),
112 attributes.getValue("overWrite").equalsIgnoreCase(
113 "true"),
114 generateEmptyFiles);
115 String tcn = attributes.getValue("transformClass");
116 if (tcn != null)
117 {
118 try
119 {
120 tc.setTransformClassname(tcn);
121 }
122 catch (ClassNotFoundException e)
123 {
124 e.printStackTrace();
125
126 }
127 }
128 desc.addTemplateConfiguration(tc);
129 }
130 }
131 }