1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package jdbcadmin.core.tools;
21
22 import java.io.InputStream;
23
24 import javax.xml.parsers.SAXParser;
25 import javax.xml.parsers.SAXParserFactory;
26
27 import jdbcadmin.core.access.DeclarativeAccessImpl;
28 import jdbcadmin.core.access.IAccessAuthorization;
29 import jdbcadmin.core.access.IConfigurableAuthorizationAccess;
30 import jdbcadmin.core.access.RestrictiveAccessImpl;
31 import jdbcadmin.core.data.ConnectionInfo;
32 import jdbcadmin.core.exceptions.TechnicalException;
33
34 import org.xml.sax.Attributes;
35 import org.xml.sax.SAXException;
36 import org.xml.sax.helpers.DefaultHandler;
37
38 /***
39 * Handler SAX for configuration parsing
40 * @author Thomas Recloux (trecloux@norsys.fr)
41 */
42 public class ConfigurationHandler extends DefaultHandler {
43
44
45 /*** Configuration builded by this handler*/
46 private IConfigurableAuthorizationAccess authorizations;
47 /*** Courant schema */
48 private String courantSchema;
49 /*** Connection informations */
50 private ConnectionInfo cnxInfos;
51 /*** Working buffer */
52 private StringBuffer buff;
53 /*** Label */
54 private String label;
55
56
57 /***
58 * @return the authorizations
59 */
60 public IAccessAuthorization getAccessAuthorizations() {
61 return authorizations;
62 }
63 /*** @return connection informations */
64 public ConnectionInfo getConnexionInfo() {
65 return cnxInfos;
66 }
67
68 /*** @return the label */
69 public String getLabel(){
70 return label;
71 }
72
73 /***
74 * Loads the configuration using the specified stream
75 * @return the configuration handler
76 * @param aConfStream configuration stream
77 * @throws TechnicalException technical error.
78 */
79 public static ConfigurationHandler loadConfig(InputStream aConfStream) throws TechnicalException {
80 try {
81 SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
82 ConfigurationHandler handler = new ConfigurationHandler();
83 parser.parse(aConfStream, handler);
84 return handler;
85 } catch (Exception e) {
86 throw new TechnicalException ("Error during configuration", e);
87 }
88 }
89
90
91
92 /*** {@inheritDoc} */
93 public void startDocument() throws SAXException {
94 super.startDocument();
95 cnxInfos = new ConnectionInfo();
96 courantSchema = null;
97 }
98
99 /*** {@inheritDoc} */
100 public void startElement(String uri, String localName, String qName, Attributes attributes) {
101 if ("visibility".equals(qName)) {
102 if (Boolean.valueOf(attributes.getValue("restrictive")).booleanValue()) {
103 authorizations = new RestrictiveAccessImpl();
104 } else {
105 authorizations = new DeclarativeAccessImpl();
106 }
107 } else if ("schema".equalsIgnoreCase(qName)) {
108 courantSchema = attributes.getValue("name");
109 authorizations.addSchema(courantSchema,
110 Boolean.valueOf(attributes.getValue("totalAction")).booleanValue());
111 } else if ("table".equalsIgnoreCase(qName)) {
112 authorizations.addTable(attributes.getValue("name"), courantSchema);
113 } else if ("driver".equalsIgnoreCase(qName)) {
114 buff = new StringBuffer();
115 } else if ("url".equalsIgnoreCase(qName)) {
116 buff = new StringBuffer();
117 } else if ("userName".equalsIgnoreCase(qName)) {
118 buff = new StringBuffer();
119 } else if ("userPassword".equalsIgnoreCase(qName)) {
120 buff = new StringBuffer();
121 } else if ("dataSourceJndiName".equalsIgnoreCase(qName)) {
122 buff = new StringBuffer();
123 } else if ("jdbcadmin".equalsIgnoreCase(qName)) {
124 label = attributes.getValue("label");
125 } else if ("connection".equalsIgnoreCase(qName)) {
126 cnxInfos.setMode(attributes.getValue("mode"));
127 }
128 }
129
130 /*** {@inheritDoc} */
131 public void endElement(String uri, String localName, String qName) {
132 if ("schema".equalsIgnoreCase(qName)) {
133 courantSchema = null;
134 } else if ("driver".equalsIgnoreCase(qName)) {
135 cnxInfos.setDriverName(buff.toString());
136 buff = null;
137 } else if ("url".equalsIgnoreCase(qName)) {
138 cnxInfos.setUrl(buff.toString());
139 buff = null;
140 } else if ("userName".equalsIgnoreCase(qName)) {
141 cnxInfos.setUserName(buff.toString());
142 buff = null;
143 } else if ("userPassword".equalsIgnoreCase(qName)) {
144 cnxInfos.setUserPassword(buff.toString());
145 buff = null;
146 } else if ("dataSourceJndiName".equalsIgnoreCase(qName)) {
147 cnxInfos.setDataSourceJndiName(buff.toString());
148 buff = null;
149 }
150 }
151
152 /*** {@inheritDoc} */
153 public void characters(char[] ch, int start, int length) {
154 if (buff != null) {
155 buff.append(ch, start, length);
156 }
157 }
158 }