Coverage Report - org.apache.torque.engine.database.transform.XmlToData
 
Classes in this File Line Coverage Branch Coverage Complexity
XmlToData
0%
0/44
0%
0/10
2.083
XmlToData$ColumnValue
0%
0/11
N/A
2.083
XmlToData$DataRow
0%
0/6
N/A
2.083
 
 1  
 package org.apache.torque.engine.database.transform;
 2  
 
 3  
 /*
 4  
  * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE
 5  
  * file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file
 6  
  * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the
 7  
  * License. You may obtain a copy of the License at
 8  
  * 
 9  
  * http://www.apache.org/licenses/LICENSE-2.0
 10  
  * 
 11  
  * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
 12  
  * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
 13  
  * specific language governing permissions and limitations under the License.
 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  
  * A Class that is used to parse an input xml schema file and creates and AppData java structure.
 45  
  * 
 46  
  * @author <a href="mailto:leon@opticode.co.za">Leon Messerschmidt</a>
 47  
  * @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a>
 48  
  * @author <a href="mailto:mpoeschl@marmot.at">Martin Poeschl</a>
 49  
  * @author <a href="mailto:fedor.karpelevitch@home.com">Fedor Karpelevitch</a>
 50  
  * @version $Id: XmlToData.java,v 1.1 2007-10-21 07:57:26 abyrne Exp $
 51  
  */
 52  
 public class XmlToData extends DefaultHandler implements EntityResolver {
 53  
     /** Logging class from commons.logging */
 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  
      * Default custructor
 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  
      * Handles opening elements of the xml file.
 95  
      */
 96  
     public void startElement(String uri, String localName, String rawName, Attributes attributes) throws SAXException {
 97  
         try {
 98  0
             if (rawName.equals("dataset")) {
 99  
                 // ignore <dataset> for now.
 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  
      * called by the XML parser
 127  
      * 
 128  
      * @return an InputSource for the database.dtd file
 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  
      * get an InputSource for an URL String
 147  
      * 
 148  
      * @param urlString
 149  
      * @return an InputSource for the URL String
 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  
 }