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