View Javadoc
1 /* 2 * PROJECT : DAR Runtime and Tools 3 * COPYRIGHT : Copyright (C) 1999-2004 tim.stephenson@enableit.org 4 * LICENSE : GNU LESSER GENERAL PUBLIC LICENSE 5 * Version 2.1, February 1999 6 * 7 * This program is free software; you can redistribute it and/or modify 8 * it under the terms of the GNU General Public License as published by 9 * the Free Software Foundation; either version 2 of the License, or 10 * (at your option) any later version. 11 * 12 * This program is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * GNU General Public License for more details. 16 * 17 * You should have received a copy of the GNU General Public License 18 * along with this program; if not, write to the Free Software 19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 20 */ 21 package org.enableit.db; 22 23 import java.sql.ResultSet; 24 import java.util.Date; 25 import java.util.Iterator; 26 import java.util.List; 27 import java.util.TreeMap; 28 29 import javax.xml.parsers.DocumentBuilder; 30 import javax.xml.parsers.DocumentBuilderFactory; 31 import javax.xml.parsers.ParserConfigurationException; 32 33 import org.apache.log4j.Logger; 34 import org.w3c.dom.Attr; 35 import org.w3c.dom.Document; 36 import org.w3c.dom.Element; 37 38 39 /*** 40 * Simple utilities for constructing XML. 41 */ 42 public class XMLProxy { 43 /*** 44 * The Log4J <code>Logger</code> doing the logging. 45 */ 46 private static Logger logger = Logger.getLogger(XMLProxy.class); 47 48 // Constant members ----------------------------------------------------------- 49 50 /*** 51 * Row set element name. 52 */ 53 public static final String EL_ROWSET = "row-set"; 54 55 /*** 56 * Row element name. 57 */ 58 public static final String EL_ROW = "row"; 59 60 /*** 61 * Column element's name. 62 */ 63 public static final String EL_COL = "col"; 64 65 /*** 66 * Name of attribute holding column's name. 67 */ 68 public static final String EL_COL_NAME = "name"; 69 70 /*** 71 * Name of attribute holding column's value. 72 */ 73 public static final String EL_COL_VALUE = "value"; 74 75 /*** 76 * Name of attribute holding column's value. 77 */ 78 public static final String EL_COL_TYPE = "type"; 79 80 /*** 81 * CVS info about this class and its current version 82 */ 83 public static final String ABOUT = "$Id: XMLProxy.java,v 1.7 2004/03/19 19:18:53 tim Exp $"; 84 85 /*** 86 * Constructor to prevent instantiation. 87 */ 88 private XMLProxy() { 89 } 90 91 /*** 92 * @return a <code>Document<code> object built from the 93 * <code>List</code> data 94 */ 95 public static Document getDocument(final List list) 96 throws DBException { 97 logger.info("METHOD_ENTRY getDocument(List, String)"); 98 99 Document doc = null; 100 101 try { 102 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 103 DocumentBuilder db = dbf.newDocumentBuilder(); 104 105 // Construct the return element 106 doc = db.newDocument(); 107 108 Element root = doc.createElement(EL_ROWSET); 109 int rowIdx = 0; 110 111 for (Iterator i = list.iterator(); i.hasNext(); rowIdx++) { 112 logger.debug("Processing row: " + rowIdx); 113 114 TreeMap row = (TreeMap) i.next(); 115 Element record = doc.createElement(EL_ROW); 116 117 for (Iterator j = row.keySet().iterator(); j.hasNext();) { 118 Element field = doc.createElement(EL_COL); 119 Attr name = doc.createAttribute(EL_COL_NAME); 120 String key = (String) j.next(); 121 122 name.setValue(key); 123 field.setAttributeNode(name); 124 125 Attr value = doc.createAttribute(EL_COL_VALUE); 126 Object srcVal = row.get(key); 127 128 if (srcVal == null) { 129 value.setValue(""); 130 } else { 131 value.setValue(srcVal.toString()); 132 } 133 134 field.setAttributeNode(value); 135 136 Attr type = doc.createAttribute(EL_COL_TYPE); 137 138 if (srcVal == null) { 139 // Don't know the type! 140 type.setValue("java.lang.Object"); 141 } else if (srcVal instanceof java.sql.Time) { 142 type.setValue("java.sql.Time"); 143 } else if (srcVal instanceof java.sql.Date) { 144 type.setValue("java.sql.Date"); 145 } else if (srcVal instanceof java.sql.Timestamp) { 146 type.setValue("java.sql.Timestamp"); 147 } else if (Date.class.isAssignableFrom(srcVal.getClass())) { 148 type.setValue("java.util.Date"); 149 } else { 150 type.setValue(srcVal.getClass().getName()); 151 } 152 153 field.setAttributeNode(type); 154 record.appendChild(field); 155 } 156 157 root.appendChild(record); 158 } 159 160 doc.appendChild(root); 161 } catch (Exception e) { 162 logger.error(e.getMessage(), e); 163 throw new DBException(e.getMessage()); 164 } 165 166 logger.info("METHOD_EXIT getDocument(List, String)"); 167 168 return doc; 169 } 170 171 /*** 172 * Convert a SQL result set into an XML representation. 173 * 174 * @param rs The <code>java.sql.ResultSet</code> to convert. 175 */ 176 public static Document getDocument(ResultSet rs) 177 throws DBException { 178 logger.info("METHOD_ENTRY: getDocument(ResultSet)"); 179 180 Document doc = null; 181 182 // Construct an xml doc 183 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 184 185 try { 186 DocumentBuilder docBuilder = factory.newDocumentBuilder(); 187 188 doc = docBuilder.newDocument(); 189 190 Element rowsetTag = doc.createElement(EL_ROWSET); 191 192 while (rs.next()) { 193 Element rowTag = doc.createElement(EL_ROW); 194 195 rowsetTag.appendChild(rowTag); 196 } 197 198 doc.appendChild(rowsetTag); 199 } catch (ParserConfigurationException e) { 200 logger.error(e.getMessage()); 201 throw new DBException(e.getMessage()); 202 } catch (java.sql.SQLException e) { 203 logger.error(e.getMessage()); 204 throw new DBException(e.getMessage()); 205 } 206 207 logger.info("METHOD_EXIT: getDocument(ResultSet)"); 208 209 return doc; 210 } 211 }

This page was automatically generated by Maven