root / trunk / libraries / libGDBMS / src / main / java / com / hardcode / gdbms / engine / data / persistence / MementoContentHandler.java @ 38069
History | View | Annotate | Download (6.46 KB)
1 | 3199 | fjp | package com.hardcode.gdbms.engine.data.persistence; |
---|---|---|---|
2 | |||
3 | import java.util.Stack; |
||
4 | |||
5 | import org.xml.sax.Attributes; |
||
6 | import org.xml.sax.ContentHandler; |
||
7 | import org.xml.sax.Locator; |
||
8 | import org.xml.sax.SAXException; |
||
9 | |||
10 | import com.hardcode.driverManager.DriverLoadException; |
||
11 | 10627 | caballero | import com.hardcode.gdbms.driver.exceptions.ReadDriverException; |
12 | 3199 | fjp | import com.hardcode.gdbms.engine.data.DataSource; |
13 | import com.hardcode.gdbms.engine.data.DataSourceFactory; |
||
14 | import com.hardcode.gdbms.engine.data.NoSuchTableException; |
||
15 | import com.hardcode.gdbms.engine.instruction.EvaluationException; |
||
16 | import com.hardcode.gdbms.engine.instruction.SemanticException; |
||
17 | import com.hardcode.gdbms.parser.ParseException; |
||
18 | |||
19 | |||
20 | /**
|
||
21 | * ContentHandler that receives SAXEvents and generates a DataSource
|
||
22 | *
|
||
23 | * @author Fernando Gonz?lez Cort?s
|
||
24 | */
|
||
25 | public class MementoContentHandler implements ContentHandler { |
||
26 | private Stack mementos = new Stack(); |
||
27 | private Memento root;
|
||
28 | |||
29 | /**
|
||
30 | * @see org.xml.sax.ContentHandler#endDocument()
|
||
31 | */
|
||
32 | public void endDocument() throws SAXException { |
||
33 | } |
||
34 | |||
35 | /**
|
||
36 | * @see org.xml.sax.ContentHandler#startDocument()
|
||
37 | */
|
||
38 | public void startDocument() throws SAXException { |
||
39 | } |
||
40 | |||
41 | /**
|
||
42 | * @see org.xml.sax.ContentHandler#characters(char[], int, int)
|
||
43 | */
|
||
44 | public void characters(char[] ch, int start, int length) |
||
45 | throws SAXException {
|
||
46 | } |
||
47 | |||
48 | /**
|
||
49 | * @see org.xml.sax.ContentHandler#ignorableWhitespace(char[], int, int)
|
||
50 | */
|
||
51 | public void ignorableWhitespace(char[] ch, int start, int length) |
||
52 | throws SAXException {
|
||
53 | } |
||
54 | |||
55 | /**
|
||
56 | * @see org.xml.sax.ContentHandler#endPrefixMapping(java.lang.String)
|
||
57 | */
|
||
58 | public void endPrefixMapping(String prefix) throws SAXException { |
||
59 | } |
||
60 | |||
61 | /**
|
||
62 | * @see org.xml.sax.ContentHandler#skippedEntity(java.lang.String)
|
||
63 | */
|
||
64 | public void skippedEntity(String name) throws SAXException { |
||
65 | } |
||
66 | |||
67 | /**
|
||
68 | * @see org.xml.sax.ContentHandler#setDocumentLocator(org.xml.sax.Locator)
|
||
69 | */
|
||
70 | public void setDocumentLocator(Locator locator) { |
||
71 | } |
||
72 | |||
73 | /**
|
||
74 | * @see org.xml.sax.ContentHandler#processingInstruction(java.lang.String,
|
||
75 | * java.lang.String)
|
||
76 | */
|
||
77 | public void processingInstruction(String target, String data) |
||
78 | throws SAXException {
|
||
79 | } |
||
80 | |||
81 | /**
|
||
82 | * @see org.xml.sax.ContentHandler#startPrefixMapping(java.lang.String,
|
||
83 | * java.lang.String)
|
||
84 | */
|
||
85 | public void startPrefixMapping(String prefix, String uri) |
||
86 | throws SAXException {
|
||
87 | } |
||
88 | |||
89 | /**
|
||
90 | * @see org.xml.sax.ContentHandler#endElement(java.lang.String,
|
||
91 | * java.lang.String, java.lang.String)
|
||
92 | */
|
||
93 | public void endElement(String namespaceURI, String localName, String qName) |
||
94 | throws SAXException {
|
||
95 | if (("operation".equals(localName)) || ("table".equals(localName))) { |
||
96 | root = (Memento) mementos.pop(); |
||
97 | } |
||
98 | } |
||
99 | |||
100 | /**
|
||
101 | * @see org.xml.sax.ContentHandler#startElement(java.lang.String,
|
||
102 | * java.lang.String, java.lang.String, org.xml.sax.Attributes)
|
||
103 | */
|
||
104 | public void startElement(String namespaceURI, String localName, |
||
105 | String qName, Attributes atts) throws SAXException { |
||
106 | if ("operation".equals(localName)) { |
||
107 | OperationLayerMemento memento = new OperationLayerMemento(atts.getValue(
|
||
108 | "dataSourceName"), atts.getValue("sql")); |
||
109 | |||
110 | if (mementos.size() > 0) { |
||
111 | Memento m = (Memento) mementos.peek(); |
||
112 | |||
113 | if (m instanceof OperationLayerMemento) { |
||
114 | OperationLayerMemento mem = (OperationLayerMemento) m; |
||
115 | mem.addMemento(memento); |
||
116 | } |
||
117 | } |
||
118 | |||
119 | mementos.push(memento); |
||
120 | } else if ("table".equals(localName)) { |
||
121 | DataSourceLayerMemento memento = new DataSourceLayerMemento(atts.getValue(
|
||
122 | "table-name"), atts.getValue("table-alias")); |
||
123 | 30013 | jmvivo | if (mementos.size() > 0) { |
124 | Memento m = (Memento) mementos.peek(); |
||
125 | |||
126 | if (m instanceof OperationLayerMemento) { |
||
127 | OperationLayerMemento mem = (OperationLayerMemento) m; |
||
128 | mem.addMemento(memento); |
||
129 | } |
||
130 | } |
||
131 | |||
132 | |||
133 | 3199 | fjp | mementos.push(memento); |
134 | } |
||
135 | } |
||
136 | |||
137 | /**
|
||
138 | * Get's the root memento of the XML parsed. Null if no parse has been done
|
||
139 | *
|
||
140 | * @return The memento
|
||
141 | */
|
||
142 | public Memento getRoot() {
|
||
143 | return root;
|
||
144 | } |
||
145 | |||
146 | /**
|
||
147 | * DOCUMENT ME!
|
||
148 | *
|
||
149 | * @param m DOCUMENT ME!
|
||
150 | * @param dsf DOCUMENT ME!
|
||
151 | * @param mode DOCUMENT ME!
|
||
152 | *
|
||
153 | * @return DOCUMENT ME!
|
||
154 | * @throws ParseException DOCUMENT ME!
|
||
155 | * @throws SemanticException DOCUMENT ME!
|
||
156 | * @throws DriverLoadException DOCUMENT ME!
|
||
157 | * @throws NoSuchTableException DOCUMENT ME!
|
||
158 | 10627 | caballero | * @throws ReadDriverException TODO
|
159 | 3199 | fjp | * @throws RuntimeException DOCUMENT ME!
|
160 | */
|
||
161 | private DataSource createDataSource(Memento m, DataSourceFactory dsf, |
||
162 | int mode)
|
||
163 | 10627 | caballero | throws ParseException, SemanticException, DriverLoadException, NoSuchTableException, EvaluationException, ReadDriverException { |
164 | 3199 | fjp | if (m instanceof OperationLayerMemento) { |
165 | OperationLayerMemento olm = (OperationLayerMemento) m; |
||
166 | |||
167 | for (int i = 0; i < olm.getMementoCount(); i++) { |
||
168 | 30013 | jmvivo | DataSource ds = createDataSource(olm.getMemento(i), dsf, mode);
|
169 | |||
170 | 3199 | fjp | } |
171 | 30013 | jmvivo | DataSource ds = dsf.executeSQL(olm.getSql(), mode);
|
172 | 38069 | fpenarrubia | dsf.changeDataSourceName(ds.getName(), olm.getName()); |
173 | 30013 | jmvivo | return ds;
|
174 | 3199 | fjp | } else if (m instanceof DataSourceLayerMemento) { |
175 | DataSourceLayerMemento dslm = (DataSourceLayerMemento) m; |
||
176 | |||
177 | return dsf.createRandomDataSource(dslm.getTableName(),
|
||
178 | dslm.getTableAlias(), mode); |
||
179 | } |
||
180 | |||
181 | throw new RuntimeException("unrecognized data source type"); |
||
182 | } |
||
183 | |||
184 | /**
|
||
185 | * DOCUMENT ME!
|
||
186 | *
|
||
187 | * @param dsf DOCUMENT ME!
|
||
188 | * @param mode DOCUMENT ME!
|
||
189 | *
|
||
190 | * @return DOCUMENT ME!
|
||
191 | * @throws DriverLoadException DOCUMENT ME!
|
||
192 | * @throws ParseException DOCUMENT ME!
|
||
193 | * @throws SemanticException DOCUMENT ME!
|
||
194 | * @throws NoSuchTableException DOCUMENT ME!
|
||
195 | * @throws EvaluationException If there's any problem during expresion evaluation
|
||
196 | 10627 | caballero | * @throws ReadDriverException TODO
|
197 | 3199 | fjp | */
|
198 | public DataSource getDataSource(DataSourceFactory dsf, int mode) |
||
199 | 10627 | caballero | throws DriverLoadException, ParseException, SemanticException, NoSuchTableException, |
200 | EvaluationException, ReadDriverException { |
||
201 | 3199 | fjp | return createDataSource(root, dsf, mode);
|
202 | } |
||
203 | } |