1 | |
package org.apache.torque.engine.database.transform; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
import java.io.BufferedInputStream; |
17 | |
import java.io.BufferedReader; |
18 | |
import java.io.File; |
19 | |
import java.io.FileInputStream; |
20 | |
import java.io.FileReader; |
21 | |
import java.io.IOException; |
22 | |
import java.io.Reader; |
23 | |
import java.net.MalformedURLException; |
24 | |
import java.net.URL; |
25 | |
import java.util.ArrayList; |
26 | |
import java.util.List; |
27 | |
|
28 | |
import javax.xml.parsers.SAXParser; |
29 | |
import javax.xml.parsers.SAXParserFactory; |
30 | |
|
31 | |
import org.apache.commons.lang.StringUtils; |
32 | |
import org.apache.commons.logging.Log; |
33 | |
import org.apache.commons.logging.LogFactory; |
34 | |
import org.apache.torque.engine.database.model.Column; |
35 | |
import org.apache.torque.engine.database.model.Database; |
36 | |
import org.apache.torque.engine.database.model.Table; |
37 | |
import org.xml.sax.Attributes; |
38 | |
import org.xml.sax.EntityResolver; |
39 | |
import org.xml.sax.InputSource; |
40 | |
import org.xml.sax.SAXException; |
41 | |
import org.xml.sax.helpers.DefaultHandler; |
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
public class XmlToData extends DefaultHandler implements EntityResolver { |
53 | |
|
54 | 0 | private static Log log = LogFactory.getLog(XmlToData.class); |
55 | |
private Database database; |
56 | |
private List<Object> data; |
57 | |
private File dtdFile; |
58 | |
|
59 | |
private static SAXParserFactory saxFactory; |
60 | |
|
61 | |
static { |
62 | 0 | saxFactory = SAXParserFactory.newInstance(); |
63 | 0 | saxFactory.setValidating(true); |
64 | 0 | } |
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | 0 | public XmlToData(Database database, String dtdFilePath) throws MalformedURLException, IOException { |
70 | 0 | this.database = database; |
71 | 0 | this.dtdFile = new File(dtdFilePath); |
72 | 0 | } |
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
public List<?> parseFile(String xmlFile) throws Exception { |
78 | 0 | data = new ArrayList<Object>(); |
79 | |
|
80 | 0 | SAXParser parser = saxFactory.newSAXParser(); |
81 | |
|
82 | 0 | Reader r = new BufferedReader(new FileReader(xmlFile)); |
83 | |
try { |
84 | 0 | InputSource is = new InputSource(r); |
85 | 0 | is.setSystemId(dtdFile.getAbsolutePath()); |
86 | 0 | parser.parse(is, this); |
87 | |
} finally { |
88 | 0 | r.close(); |
89 | 0 | } |
90 | 0 | return data; |
91 | |
} |
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
public void startElement(String uri, String localName, String rawName, Attributes attributes) throws SAXException { |
97 | |
try { |
98 | 0 | if (rawName.equals("dataset")) { |
99 | |
|
100 | |
} else { |
101 | 0 | Table table = database.getTableByJavaName(rawName); |
102 | |
|
103 | 0 | if (table == null) { |
104 | 0 | throw new SAXException("Table '" + rawName + "' unknown"); |
105 | |
} |
106 | 0 | List<ColumnValue> columnValues = new ArrayList<ColumnValue>(); |
107 | 0 | for (int i = 0; i < attributes.getLength(); i++) { |
108 | 0 | Column col = table.getColumnByJavaName(attributes.getQName(i)); |
109 | |
|
110 | 0 | if (col == null) { |
111 | 0 | throw new SAXException("Column " + attributes.getQName(i) + " in table " + rawName |
112 | |
+ " unknown."); |
113 | |
} |
114 | |
|
115 | 0 | String value = attributes.getValue(i); |
116 | 0 | columnValues.add(new ColumnValue(col, value)); |
117 | |
} |
118 | 0 | data.add(new DataRow(table, columnValues)); |
119 | |
} |
120 | 0 | } catch (Exception e) { |
121 | 0 | throw new SAXException(e); |
122 | 0 | } |
123 | 0 | } |
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | |
|
130 | |
public InputSource resolveEntity(String publicId, String systemId) throws SAXException { |
131 | |
try { |
132 | 0 | File systemIdFile = new File(systemId); |
133 | 0 | if (dtdFile.equals(systemIdFile)) { |
134 | 0 | log.debug("Resolver: -> used " + dtdFile.getPath()); |
135 | 0 | return new InputSource(new BufferedInputStream(new FileInputStream(dtdFile))); |
136 | |
} else { |
137 | 0 | log.debug("Resolver: -> used " + systemId); |
138 | 0 | return getInputSource(systemId); |
139 | |
} |
140 | 0 | } catch (IOException e) { |
141 | 0 | throw new SAXException(e); |
142 | |
} |
143 | |
} |
144 | |
|
145 | |
|
146 | |
|
147 | |
|
148 | |
|
149 | |
|
150 | |
|
151 | |
public InputSource getInputSource(String urlString) throws IOException { |
152 | 0 | URL url = new URL(urlString); |
153 | 0 | InputSource src = new InputSource(url.openStream()); |
154 | 0 | return src; |
155 | |
} |
156 | |
|
157 | |
|
158 | |
|
159 | |
|
160 | |
public class DataRow { |
161 | |
private Table table; |
162 | |
private List<ColumnValue> columnValues; |
163 | |
|
164 | 0 | public DataRow(Table table, List<ColumnValue> columnValues) { |
165 | 0 | this.table = table; |
166 | 0 | this.columnValues = columnValues; |
167 | 0 | } |
168 | |
|
169 | |
public Table getTable() { |
170 | 0 | return table; |
171 | |
} |
172 | |
|
173 | |
public List<?> getColumnValues() { |
174 | 0 | return columnValues; |
175 | |
} |
176 | |
} |
177 | |
|
178 | |
|
179 | |
|
180 | |
|
181 | |
public class ColumnValue { |
182 | |
private Column col; |
183 | |
private String val; |
184 | |
|
185 | 0 | public ColumnValue(Column col, String val) { |
186 | 0 | this.col = col; |
187 | 0 | this.val = val; |
188 | 0 | } |
189 | |
|
190 | |
public Column getColumn() { |
191 | 0 | return col; |
192 | |
} |
193 | |
|
194 | |
public String getValue() { |
195 | 0 | return val; |
196 | |
} |
197 | |
|
198 | |
public String getEscapedValue() { |
199 | 0 | StringBuffer sb = new StringBuffer(); |
200 | 0 | sb.append("'"); |
201 | 0 | sb.append(StringUtils.replace(val, "'", "''")); |
202 | 0 | sb.append("'"); |
203 | 0 | return sb.toString(); |
204 | |
} |
205 | |
} |
206 | |
} |