Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libGDBMS / src / com / hardcode / gdbms / engine / data / persistence / MementoContentHandler.java @ 1956

History | View | Annotate | Download (5.36 KB)

1
package com.hardcode.gdbms.engine.data.persistence;
2

    
3
import com.hardcode.driverManager.DriverLoadException;
4

    
5
import com.hardcode.gdbms.engine.data.DataSource;
6
import com.hardcode.gdbms.engine.data.DataSourceFactory;
7
import com.hardcode.gdbms.engine.data.NoSuchTableException;
8
import com.hardcode.gdbms.engine.data.driver.DriverException;
9
import com.hardcode.gdbms.engine.instruction.SemanticException;
10
import com.hardcode.gdbms.parser.ParseException;
11

    
12
import org.xml.sax.Attributes;
13
import org.xml.sax.ContentHandler;
14
import org.xml.sax.Locator;
15
import org.xml.sax.SAXException;
16

    
17
import java.io.IOException;
18

    
19
import java.util.Stack;
20

    
21

    
22
/**
23
 * DOCUMENT ME!
24
 *
25
 * @author Fernando Gonz?lez Cort?s
26
 */
27
public class MementoContentHandler implements ContentHandler {
28
        private Stack mementos = new Stack();
29
        private Memento root;
30

    
31
        /**
32
         * @see org.xml.sax.ContentHandler#endDocument()
33
         */
34
        public void endDocument() throws SAXException {
35
        }
36

    
37
        /**
38
         * @see org.xml.sax.ContentHandler#startDocument()
39
         */
40
        public void startDocument() throws SAXException {
41
        }
42

    
43
        /**
44
         * @see org.xml.sax.ContentHandler#characters(char[], int, int)
45
         */
46
        public void characters(char[] ch, int start, int length)
47
                throws SAXException {
48
        }
49

    
50
        /**
51
         * @see org.xml.sax.ContentHandler#ignorableWhitespace(char[], int, int)
52
         */
53
        public void ignorableWhitespace(char[] ch, int start, int length)
54
                throws SAXException {
55
        }
56

    
57
        /**
58
         * @see org.xml.sax.ContentHandler#endPrefixMapping(java.lang.String)
59
         */
60
        public void endPrefixMapping(String prefix) throws SAXException {
61
        }
62

    
63
        /**
64
         * @see org.xml.sax.ContentHandler#skippedEntity(java.lang.String)
65
         */
66
        public void skippedEntity(String name) throws SAXException {
67
        }
68

    
69
        /**
70
         * @see org.xml.sax.ContentHandler#setDocumentLocator(org.xml.sax.Locator)
71
         */
72
        public void setDocumentLocator(Locator locator) {
73
        }
74

    
75
        /**
76
         * @see org.xml.sax.ContentHandler#processingInstruction(java.lang.String,
77
         *                 java.lang.String)
78
         */
79
        public void processingInstruction(String target, String data)
80
                throws SAXException {
81
        }
82

    
83
        /**
84
         * @see org.xml.sax.ContentHandler#startPrefixMapping(java.lang.String,
85
         *                 java.lang.String)
86
         */
87
        public void startPrefixMapping(String prefix, String uri)
88
                throws SAXException {
89
        }
90

    
91
        /**
92
         * @see org.xml.sax.ContentHandler#endElement(java.lang.String,
93
         *                 java.lang.String, java.lang.String)
94
         */
95
        public void endElement(String namespaceURI, String localName, String qName)
96
                throws SAXException {
97
                if ( ("operation".equals(localName)) || ("table".equals(localName))){
98
                        root = (Memento) mementos.pop();
99
                }
100
        }
101

    
102
        /**
103
         * @see org.xml.sax.ContentHandler#startElement(java.lang.String,
104
         *                 java.lang.String, java.lang.String, org.xml.sax.Attributes)
105
         */
106
        public void startElement(String namespaceURI, String localName,
107
                String qName, Attributes atts) throws SAXException {
108
                if ("operation".equals(localName)) {
109
                        OperationLayerMemento memento = new OperationLayerMemento(atts.getValue(
110
                                                "dataSourceName"), atts.getValue("sql"));
111

    
112
                        if (mementos.size() > 0) {
113
                                Memento m = (Memento) mementos.peek();
114

    
115
                                if (m instanceof OperationLayerMemento) {
116
                                        OperationLayerMemento mem = (OperationLayerMemento) m;
117
                                        mem.addMemento(memento);
118
                                } else {
119
                                        throw new RuntimeException(
120
                                                "No table inside table is allowed");
121
                                }
122
                        }
123

    
124
                        mementos.push(memento);
125
                } else if ("table".equals(localName)) {
126
                        DataSourceLayerMemento memento = new DataSourceLayerMemento(atts.getValue(
127
                                                "table-name"), atts.getValue("table-alias"));
128
                        mementos.push(memento);
129
                }
130
        }
131

    
132
        /**
133
         * Get's the root memento of the XML parsed. Null  if no parse has been
134
         * done
135
         *
136
         * @return The memento
137
         */
138
        public Memento getRoot() {
139
                return root;
140
        }
141

    
142
        /**
143
         * DOCUMENT ME!
144
         *
145
         * @param m DOCUMENT ME!
146
         * @param dsf DOCUMENT ME!
147
         * @param mode DOCUMENT ME!
148
         *
149
         * @return DOCUMENT ME!
150
         *
151
         * @throws ParseException DOCUMENT ME!
152
         * @throws DriverException DOCUMENT ME!
153
         * @throws SemanticException DOCUMENT ME!
154
         * @throws IOException DOCUMENT ME!
155
         * @throws DriverLoadException DOCUMENT ME!
156
         * @throws NoSuchTableException DOCUMENT ME!
157
         * @throws RuntimeException DOCUMENT ME!
158
         */
159
        private DataSource createDataSource(Memento m, DataSourceFactory dsf,
160
                int mode)
161
                throws ParseException, DriverException, SemanticException, IOException, 
162
                        DriverLoadException, NoSuchTableException {
163
                if (m instanceof OperationLayerMemento) {
164
                        OperationLayerMemento olm = (OperationLayerMemento) m;
165

    
166
                        for (int i = 0; i < olm.getMementoCount(); i++) {
167
                                createDataSource(olm.getMemento(i), dsf, mode);
168
                        }
169

    
170
                        return dsf.executeSQL(olm.getSql(), mode);
171
                } else if (m instanceof DataSourceLayerMemento) {
172
                        DataSourceLayerMemento dslm = (DataSourceLayerMemento) m;
173

    
174
                        return dsf.createRandomDataSource(dslm.getTableName(),
175
                                dslm.getTableAlias(), mode);
176
                }
177

    
178
                throw new RuntimeException("unrecognized data source type");
179
        }
180

    
181
        /**
182
         * DOCUMENT ME!
183
         *
184
         * @param dsf DOCUMENT ME!
185
         * @param mode DOCUMENT ME!
186
         *
187
         * @return DOCUMENT ME!
188
         *
189
         * @throws DriverLoadException DOCUMENT ME!
190
         * @throws ParseException DOCUMENT ME!
191
         * @throws DriverException DOCUMENT ME!
192
         * @throws SemanticException DOCUMENT ME!
193
         * @throws IOException DOCUMENT ME!
194
         * @throws NoSuchTableException DOCUMENT ME!
195
         */
196
        public DataSource getDataSource(DataSourceFactory dsf, int mode)
197
                throws DriverLoadException, ParseException, DriverException, 
198
                        SemanticException, IOException, NoSuchTableException {
199
                return createDataSource(root, dsf, mode);
200
        }
201
}