| 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 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
public class XmlToData extends DefaultHandler implements EntityResolver { |
| 59 | |
|
| 60 | 0 | 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 | |
|
| 67 | |
static { |
| 68 | 0 | saxFactory = SAXParserFactory.newInstance(); |
| 69 | 0 | saxFactory.setValidating(true); |
| 70 | 0 | } |
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | 0 | public XmlToData(Database database, String dtdFilePath) throws MalformedURLException, IOException { |
| 76 | 0 | this.database = database; |
| 77 | 0 | this.dtdFile = new File(dtdFilePath); |
| 78 | 0 | } |
| 79 | |
|
| 80 | |
|
| 81 | |
|
| 82 | |
|
| 83 | |
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 | |
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 | 0 | } |
| 96 | 0 | return data; |
| 97 | |
} |
| 98 | |
|
| 99 | |
|
| 100 | |
|
| 101 | |
|
| 102 | |
public void startElement(String uri, String localName, String rawName, Attributes attributes) throws SAXException { |
| 103 | |
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 | 0 | } catch (Exception e) { |
| 126 | 0 | throw new SAXException(e); |
| 127 | 0 | } |
| 128 | 0 | } |
| 129 | |
|
| 130 | |
|
| 131 | |
|
| 132 | |
|
| 133 | |
|
| 134 | |
|
| 135 | |
public InputSource resolveEntity(String publicId, String systemId) throws SAXException { |
| 136 | |
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 | 0 | } catch (IOException e) { |
| 146 | 0 | throw new SAXException(e); |
| 147 | |
} |
| 148 | |
} |
| 149 | |
|
| 150 | |
|
| 151 | |
|
| 152 | |
|
| 153 | |
|
| 154 | |
|
| 155 | |
|
| 156 | |
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 | |
|
| 165 | |
public class DataRow { |
| 166 | |
private Table table; |
| 167 | |
private List<ColumnValue> columnValues; |
| 168 | |
|
| 169 | 0 | public DataRow(Table table, List<ColumnValue> columnValues) { |
| 170 | 0 | this.table = table; |
| 171 | 0 | this.columnValues = columnValues; |
| 172 | 0 | } |
| 173 | |
|
| 174 | |
public Table getTable() { |
| 175 | 0 | return table; |
| 176 | |
} |
| 177 | |
|
| 178 | |
public List<?> getColumnValues() { |
| 179 | 0 | return columnValues; |
| 180 | |
} |
| 181 | |
} |
| 182 | |
|
| 183 | |
|
| 184 | |
|
| 185 | |
|
| 186 | |
public class ColumnValue { |
| 187 | |
private Column col; |
| 188 | |
private String val; |
| 189 | |
|
| 190 | 0 | public ColumnValue(Column col, String val) { |
| 191 | 0 | this.col = col; |
| 192 | 0 | this.val = val; |
| 193 | 0 | } |
| 194 | |
|
| 195 | |
public Column getColumn() { |
| 196 | 0 | return col; |
| 197 | |
} |
| 198 | |
|
| 199 | |
public String getValue() { |
| 200 | 0 | return val; |
| 201 | |
} |
| 202 | |
|
| 203 | |
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 | |
} |