1 19 package org.jahia.utils.xml; 20 21 import java.io.FileOutputStream ; 22 import java.sql.ResultSet ; 23 import java.sql.ResultSetMetaData ; 24 import java.sql.SQLException ; 25 import java.util.Hashtable ; 26 27 import javax.xml.parsers.DocumentBuilder ; 28 import javax.xml.parsers.DocumentBuilderFactory ; 29 import javax.xml.transform.OutputKeys ; 30 import javax.xml.transform.Transformer ; 31 import javax.xml.transform.TransformerFactory ; 32 import javax.xml.transform.dom.DOMSource ; 33 import javax.xml.transform.stream.StreamResult ; 34 35 import org.jahia.exceptions.JahiaException; 36 import org.jahia.utils.DBRowDataFilter; 37 import org.jahia.utils.JahiaConsole; 38 import org.jahia.utils.JahiaTools; 39 import org.w3c.dom.Document ; 40 import org.w3c.dom.Element ; 41 import org.w3c.dom.Text ; 42 43 44 50 public class DB2DOM { 51 52 private static final String CLASS_NAME = "DB2DOM"; 53 54 private static final String DATABASE_TAG = "database"; 55 private static final String TABLE_TAG = "table"; 56 private static final String ROW_TAG = "row"; 57 private static final String COLUMN_TAG = "column"; 58 59 private static final String ID_ATTRIB = "id"; 60 private static final String NAME_ATTRIB = "name"; 61 private static final String TYPE_ATTRIB = "type"; 62 63 private Document xmlDoc = null; 64 65 66 72 public DB2DOM () 73 throws JahiaException{ 74 75 try { 76 77 DocumentBuilderFactory dfactory = 78 DocumentBuilderFactory.newInstance(); 79 DocumentBuilder docBuilder = dfactory.newDocumentBuilder(); 80 xmlDoc = docBuilder.newDocument(); 81 82 } catch ( Throwable t ){ 83 throw new JahiaException( CLASS_NAME+".getDOMFromResultSet", 84 "Exception " + t.getMessage(), 85 JahiaException.ERROR_SEVERITY, 86 JahiaException.ERROR_SEVERITY); 87 } 88 89 Element dbNode = (Element ) xmlDoc.createElement(DATABASE_TAG); 90 dbNode.normalize(); 91 92 xmlDoc.appendChild(dbNode); 94 95 } 96 97 106 public void addTable( String tableName, ResultSet rs ) 107 throws JahiaException,SQLException { 108 109 Document doc = getDOMFromResultSet(tableName,rs,null); 110 Element tableNode = doc.getDocumentElement(); 111 112 Element dbNode = xmlDoc.getDocumentElement(); 113 dbNode.appendChild(xmlDoc.importNode(tableNode,true)); 114 } 115 116 125 public void addTable( String tableName, ResultSet rs , DBRowDataFilter filter ) 126 throws JahiaException,SQLException { 127 128 Document doc = getDOMFromResultSet(tableName,rs,filter); 129 Element tableNode = doc.getDocumentElement(); 130 131 Element dbNode = xmlDoc.getDocumentElement(); 132 dbNode.appendChild(xmlDoc.importNode(tableNode,true)); 133 } 134 135 143 public void addTable( Element el ) 144 throws JahiaException,SQLException { 145 146 Element dbNode = xmlDoc.getDocumentElement(); 147 dbNode.appendChild(xmlDoc.importNode(el,true)); 148 149 } 150 151 152 158 public void save (String destFileName) throws JahiaException{ 159 saveFile(destFileName); 160 } 161 162 168 public Document getDocument () { 169 return xmlDoc; 170 } 171 172 178 public String toString () { 179 Element el = (Element )xmlDoc.getDocumentElement(); 180 if ( el != null ){ 181 return el.toString(); 182 } 183 return null; 184 } 185 186 187 216 public static Document getDOMFromResultSet( String tableName, 217 ResultSet rs, 218 DBRowDataFilter filter ) 219 throws JahiaException,SQLException { 220 221 JahiaConsole.println(CLASS_NAME+".getDOMFromResultSet","Table " + tableName); 222 223 Document xmlDoc = null; 224 225 226 try { 227 228 DocumentBuilderFactory dfactory = 229 DocumentBuilderFactory.newInstance(); 230 DocumentBuilder docBuilder = dfactory.newDocumentBuilder(); 231 xmlDoc = docBuilder.newDocument(); 232 233 } catch ( Throwable t ){ 234 throw new JahiaException( CLASS_NAME+".getDOMFromResultSet", 235 "Exception " + t.getMessage(), 236 JahiaException.ERROR_SEVERITY, 237 JahiaException.ERROR_SEVERITY); 238 } 239 240 if ( rs == null ){ 241 Element tableNode = (Element ) xmlDoc.createElement(TABLE_TAG); 242 tableNode.setAttribute(NAME_ATTRIB,tableName); 243 tableNode.normalize(); 244 xmlDoc.appendChild(tableNode); 246 return xmlDoc; 247 } 248 249 250 String output = ""; 251 StringBuffer buff = null; 252 253 ResultSetMetaData metaData = rs.getMetaData(); 254 int columnCount = metaData.getColumnCount(); 255 256 Element tableNode = (Element ) xmlDoc.createElement(TABLE_TAG); 257 tableNode.setAttribute(NAME_ATTRIB,tableName); 258 tableNode.normalize(); 259 260 String columnVal = null; 261 String columnName = null; 262 int columnType = 0; 263 264 xmlDoc.appendChild(tableNode); 266 267 Element rowNode = null; 268 Element columnNode = null; 269 270 while ( rs.next() ){ 271 rowNode = (Element ) xmlDoc.createElement(ROW_TAG); 273 rowNode.setAttribute(ID_ATTRIB,Integer.toString(rs.getRow())); 274 275 Hashtable vals = new Hashtable (); 276 for(int column=1; column<=columnCount; column++) { 277 278 columnVal = JahiaTools.text2XMLEntityRef(rs.getString(column),0); 280 columnType = metaData.getColumnType(column); 281 columnName = metaData.getColumnLabel(column); 282 283 if ( columnVal == null ){ 284 columnVal = ""; 285 } 286 287 vals.put(columnName.toLowerCase(),columnVal); 288 289 columnNode = (Element ) xmlDoc.createElement(COLUMN_TAG); 291 columnNode.setAttribute(NAME_ATTRIB, columnName); 292 columnNode.setAttribute(TYPE_ATTRIB, Integer.toString(columnType)); 293 294 Text textNode = xmlDoc.createTextNode(columnVal); 296 columnNode.appendChild(textNode); 297 298 rowNode.appendChild(columnNode); 300 columnNode = null; 301 } 302 if ( filter == null || filter.inValue(vals) ){ 303 tableNode.appendChild(rowNode); 305 } 306 rowNode = null; 307 } 308 309 314 315 return xmlDoc; 316 } 317 318 319 320 321 327 private void saveFile(String destinationFileName) 328 throws JahiaException { 329 330 try { 331 332 xmlDoc.normalize(); 334 TransformerFactory tfactory = TransformerFactory.newInstance(); 335 336 Transformer serializer = tfactory.newTransformer(); 339 340 serializer.setOutputProperty(OutputKeys.METHOD, "xml"); 341 serializer.setOutputProperty(OutputKeys.INDENT, "yes"); 342 FileOutputStream fileStream = new FileOutputStream (destinationFileName); 343 serializer.transform(new DOMSource (xmlDoc), 344 new StreamResult (fileStream)); 345 346 } catch ( Throwable t ){ 347 throw new JahiaException( "XMLPortlets", 348 "Exception " + t.getMessage(), 349 JahiaException.ERROR_SEVERITY, 350 JahiaException.SERVICE_ERROR); 351 } 352 353 } 354 355 356 357 } 358 | Popular Tags |