1 package org.apache.torque.engine.database.transform;
2
3 /* ====================================================================
4 * The Apache Software License, Version 1.1
5 *
6 * Copyright (c) 2001-2003 The Apache Software Foundation. All rights
7 * reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 *
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
19 * distribution.
20 *
21 * 3. The end-user documentation included with the redistribution,
22 * if any, must include the following acknowledgment:
23 * "This product includes software developed by the
24 * Apache Software Foundation (http://www.apache.org/)."
25 * Alternately, this acknowledgment may appear in the software itself,
26 * if and wherever such third-party acknowledgments normally appear.
27 *
28 * 4. The names "Apache" and "Apache Software Foundation" and
29 * "Apache Turbine" must not be used to endorse or promote products
30 * derived from this software without prior written permission. For
31 * written permission, please contact apache@apache.org.
32 *
33 * 5. Products derived from this software may not be called "Apache",
34 * "Apache Turbine", nor may "Apache" appear in their name, without
35 * prior written permission of the Apache Software Foundation.
36 *
37 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
41 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48 * SUCH DAMAGE.
49 * ====================================================================
50 *
51 * This software consists of voluntary contributions made by many
52 * individuals on behalf of the Apache Software Foundation. For more
53 * information on the Apache Software Foundation, please see
54 * <http://www.apache.org/>.
55 */
56
57 import java.io.BufferedReader;
58 import java.io.File;
59 import java.io.FileReader;
60 import java.io.IOException;
61 import java.net.MalformedURLException;
62 import java.net.URL;
63 import java.util.ArrayList;
64 import java.util.List;
65
66 import javax.xml.parsers.SAXParser;
67 import javax.xml.parsers.SAXParserFactory;
68
69 import org.apache.commons.lang.StringUtils;
70 import org.apache.commons.logging.Log;
71 import org.apache.commons.logging.LogFactory;
72 import org.apache.torque.engine.database.model.Column;
73 import org.apache.torque.engine.database.model.Database;
74 import org.apache.torque.engine.database.model.Table;
75 import org.xml.sax.Attributes;
76 import org.xml.sax.EntityResolver;
77 import org.xml.sax.InputSource;
78 import org.xml.sax.SAXException;
79 import org.xml.sax.helpers.DefaultHandler;
80
81 /***
82 * A Class that is used to parse an input xml schema file and creates and
83 * AppData java structure. <br>
84 * It uses apache Xerces to do the xml parsing.
85 *
86 * @author <a href="mailto:leon@opticode.co.za">Leon Messerschmidt</a>
87 * @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a>
88 * @author <a href="mailto:mpoeschl@marmot.at">Martin Poeschl</a>
89 * @author <a href="mailto:fedor.karpelevitch@home.com">Fedor Karpelevitch</a>
90 * @version $Id: XmlToData.java,v 1.10 2003/07/31 15:26:58 dlr Exp $
91 */
92 public class XmlToData extends DefaultHandler implements EntityResolver
93 {
94 /*** Logging class from commons.logging */
95 private static Log log = LogFactory.getLog(XmlToData.class);
96 private Database database;
97 private List data;
98 private String dtdFileName;
99 private File dtdFile;
100 private InputSource dataDTD;
101
102 private static SAXParserFactory saxFactory;
103
104 static
105 {
106 saxFactory = SAXParserFactory.newInstance();
107 saxFactory.setValidating(true);
108 }
109
110 /***
111 * Default custructor
112 */
113 public XmlToData(Database database, String dtdFilePath)
114 throws MalformedURLException, IOException
115 {
116 this.database = database;
117 dtdFile = new File(dtdFilePath);
118 this.dtdFileName = "file://" + dtdFile.getName();
119 dataDTD = new InputSource(dtdFile.toURL().openStream());
120 }
121
122 /***
123 *
124 */
125 public List parseFile(String xmlFile)
126 throws Exception
127 {
128 data = new ArrayList();
129
130 SAXParser parser = saxFactory.newSAXParser();
131
132 FileReader fr = new FileReader (xmlFile);
133 BufferedReader br = new BufferedReader (fr);
134 try
135 {
136 InputSource is = new InputSource (br);
137 parser.parse(is, this);
138 }
139 finally
140 {
141 br.close();
142 }
143 return data;
144 }
145
146 /***
147 * Handles opening elements of the xml file.
148 */
149 public void startElement(String uri, String localName, String rawName,
150 Attributes attributes)
151 throws SAXException
152 {
153 try
154 {
155 if (rawName.equals("dataset"))
156 {
157 //ignore <dataset> for now.
158 }
159 else
160 {
161 Table table = database.getTableByJavaName(rawName);
162
163 if (table == null)
164 {
165 throw new SAXException("Table '" + rawName + "' unknown");
166 }
167 List columnValues = new ArrayList();
168 for (int i = 0; i < attributes.getLength(); i++)
169 {
170 Column col = table
171 .getColumnByJavaName(attributes.getQName(i));
172
173 if (col == null)
174 {
175 throw new SAXException("Column "
176 + attributes.getQName(i) + " in table "
177 + rawName + " unknown.");
178 }
179
180 String value = attributes.getValue(i);
181 columnValues.add(new ColumnValue(col, value));
182 }
183 data.add(new DataRow(table, columnValues));
184 }
185 }
186 catch (Exception e)
187 {
188 throw new SAXException(e);
189 }
190 }
191
192 /***
193 * called by the XML parser
194 *
195 * @return an InputSource for the database.dtd file
196 */
197 public InputSource resolveEntity(String publicId, String systemId)
198 throws SAXException
199 {
200 try
201 {
202 if (dataDTD != null && dtdFileName.equals(systemId))
203 {
204 log.info("Resolver: used " + dtdFile.getPath());
205 return dataDTD;
206 }
207 else
208 {
209 log.info("Resolver: used " + systemId);
210 return getInputSource(systemId);
211 }
212 }
213 catch (IOException e)
214 {
215 throw new SAXException(e);
216 }
217 }
218
219 /***
220 * get an InputSource for an URL String
221 *
222 * @param urlString
223 * @return an InputSource for the URL String
224 */
225 public InputSource getInputSource(String urlString)
226 throws IOException
227 {
228 URL url = new URL(urlString);
229 InputSource src = new InputSource(url.openStream());
230 return src;
231 }
232
233 /***
234 *
235 */
236 public class DataRow
237 {
238 private Table table;
239 private List columnValues;
240
241 public DataRow(Table table, List columnValues)
242 {
243 this.table = table;
244 this.columnValues = columnValues;
245 }
246
247 public Table getTable()
248 {
249 return table;
250 }
251
252 public List getColumnValues()
253 {
254 return columnValues;
255 }
256 }
257
258 /***
259 *
260 */
261 public class ColumnValue
262 {
263 private Column col;
264 private String val;
265
266 public ColumnValue(Column col, String val)
267 {
268 this.col = col;
269 this.val = val;
270 }
271
272 public Column getColumn()
273 {
274 return col;
275 }
276
277 public String getValue()
278 {
279 return val;
280 }
281
282 public String getEscapedValue()
283 {
284 StringBuffer sb = new StringBuffer();
285 sb.append("'");
286 sb.append(StringUtils.replace(val, "'", "//'"));
287 sb.append("'");
288 return sb.toString();
289 }
290 }
291 }
This page was automatically generated by Maven