1 56 57 package org.jdom.contrib.input; 58 59 import java.io.*; 60 import java.sql.*; 61 import java.text.*; 62 import java.util.*; 63 64 import org.jdom.*; 65 66 94 public class ResultSetBuilder { 95 96 97 private ResultSet rs; 98 99 100 private ResultSetMetaData rsmd; 101 102 103 private SQLException exception; 104 105 106 private Map names = new HashMap(); 107 108 113 private Map attribs = new HashMap(); 114 115 116 private Namespace ns = Namespace.NO_NAMESPACE; 117 118 119 int maxRows = Integer.MAX_VALUE; 121 122 private String rootName = "result"; 123 124 125 private String rowName = "entry"; 126 127 128 private String nullAttribName = null; 129 130 131 private String nullAttribValue = null; 132 133 141 public ResultSetBuilder(ResultSet rs) { 142 this.rs = rs; 143 try { 144 rsmd = rs.getMetaData(); 145 } 146 catch (SQLException e) { 147 exception = e; 149 } 150 } 151 152 165 public ResultSetBuilder(ResultSet rs, String rootName, String rowName) { 166 this(rs); 167 setRootName(rootName); 168 setRowName(rowName); 169 } 170 171 185 public ResultSetBuilder(ResultSet rs, 186 String rootName, String rowName, Namespace ns) { 187 this(rs, rootName, rowName); 188 setNamespace(ns); 189 } 190 191 202 public Document build() throws JDOMException { 203 if (exception != null) { 204 throw new JDOMException("Database problem", exception); 205 } 206 207 try { 208 int colCount = rsmd.getColumnCount(); 209 210 Element root = new Element(rootName, ns); 211 Document doc = new Document(root); 212 213 int rowCount = 0; 214 215 String [] columnName = new String [colCount]; 217 for (int index = 0; index < colCount; index++) { 218 columnName[index] = rsmd.getColumnName(index+1); 219 } 220 221 String name; 223 String value; 224 Element entry; 225 Element child; 226 227 while (rs.next() && (rowCount++ < maxRows)) { 228 entry = new Element(rowName, ns); 229 for (int col = 1; col <= colCount; col++) { 230 if (names.isEmpty()) { 231 name = columnName[col-1]; 232 } 233 else { 234 name = lookupName(columnName[col-1]); 235 } 236 237 value = getString(rs, col, rsmd.getColumnType(col)); 238 if (!attribs.isEmpty() && isAttribute(columnName[col-1])) { 239 if (!rs.wasNull()) { 240 entry.setAttribute(name, value); 241 } 242 } 243 else { 244 child = new Element(name, ns); 245 if (!rs.wasNull()) { 246 child.setText(value); 247 } else { 248 if (nullAttribName != null) { 249 child.setAttribute(nullAttribName, nullAttribValue); 250 } 251 } 252 entry.addContent(child); 253 } 254 } 255 root.addContent(entry); 256 } 257 258 return doc; 259 } 260 catch (SQLException e) { 261 throw new JDOMException("Database problem", e); 262 } 263 } 264 265 protected String getString(ResultSet rs, int column, int columnType) 266 throws SQLException { 267 if (columnType == Types.TIMESTAMP) { 268 Timestamp timeStamp = rs.getTimestamp(column); 269 if (timeStamp != null) { 270 return DateFormat.getDateTimeInstance(DateFormat.FULL, 271 DateFormat.FULL).format(timeStamp); 272 } 273 } 274 if (columnType == Types.DATE) { 275 java.sql.Date date = rs.getDate(column); 276 if (date != null) { 277 return DateFormat.getDateInstance(DateFormat.FULL).format(date); 278 } 279 } 280 if (columnType == Types.TIME) { 281 java.sql.Time time = rs.getTime(column); 282 if (time != null) { 283 return DateFormat.getTimeInstance(DateFormat.FULL).format(time); 284 } 285 } 286 return rs.getString(column); 287 } 288 289 private String lookupName(String origName) { 290 String name = (String ) names.get(origName.toLowerCase()); 291 if (name != null) { 292 return name; 293 } 294 else { 295 return origName; 296 } 297 } 298 299 private boolean isAttribute(String origName) { 300 Boolean val = (Boolean ) attribs.get(origName.toLowerCase()); 301 if (val == Boolean.TRUE) { 302 return true; 303 } 304 else { 305 return false; 306 } 307 } 308 309 316 public void setRootName(String rootName) { 317 this.rootName = rootName; 318 } 319 320 327 public void setRowName(String rowName) { 328 this.rowName = rowName; 329 } 330 331 340 public void setNamespace(Namespace ns) { 341 this.ns = ns; 342 } 343 344 353 public void setMaxRows(int maxRows) { 354 this.maxRows = maxRows; 355 } 356 357 367 public void setAsAttribute(String columnName) { 368 attribs.put(columnName.toLowerCase(), Boolean.TRUE); 369 } 370 371 381 public void setAsAttribute(String columnName, String attribName) { 382 attribs.put(columnName.toLowerCase(), Boolean.TRUE); 383 names.put(columnName.toLowerCase(), attribName); 384 } 385 386 396 public void setAsAttribute(int columnNum) { 397 try { 398 String name = rsmd.getColumnName(columnNum).toLowerCase(); 399 attribs.put(name, Boolean.TRUE); 400 } 401 catch (SQLException e) { 402 exception = e; 403 } 404 } 405 406 416 public void setAsAttribute(int columnNum, String attribName) { 417 try { 418 String name = rsmd.getColumnName(columnNum).toLowerCase(); 419 attribs.put(name, Boolean.TRUE); 420 names.put(name, attribName); 421 } 422 catch (SQLException e) { 423 exception = e; 424 } 425 } 426 427 437 public void setAsElement(String columnName, String elemName) { 438 String name = columnName.toLowerCase(); 439 attribs.put(name, Boolean.FALSE); 440 names.put(name, elemName); 441 } 442 443 453 public void setAsElement(int columnNum, String elemName) { 454 try { 455 String name = rsmd.getColumnName(columnNum).toLowerCase(); 456 attribs.put(name, Boolean.FALSE); 457 names.put(name, elemName); 458 } 459 catch (SQLException e) { 460 exception = e; 461 } 462 } 463 464 476 public void setNullAttribute(String nullAttribName, 477 String nullAttribValue) { 478 this.nullAttribName = nullAttribName; 479 this.nullAttribValue = nullAttribValue; 480 } 481 482 489 490 } 491 | Popular Tags |