1 19 package org.jahia.utils.xml; 20 21 import java.sql.SQLException ; 22 import java.sql.Statement ; 23 import java.sql.Types ; 24 import java.util.Enumeration ; 25 import java.util.Hashtable ; 26 import java.util.Vector ; 27 28 import org.jahia.data.xml.JahiaXmlDocument; 29 import org.jahia.exceptions.JahiaException; 30 import org.jahia.utils.DBRowDataFilter; 31 import org.jahia.utils.JahiaConsole; 32 import org.jahia.utils.JahiaTools; 33 import org.w3c.dom.Element ; 34 import org.w3c.dom.Node ; 35 36 37 44 public class DOM2DB extends JahiaXmlDocument { 45 46 private static final String CLASS_NAME = "DOM2DB"; 47 48 private static final String DATABASE_TAG = "database"; 49 private static final String TABLE_TAG = "table"; 50 private static final String ROW_TAG = "row"; 51 private static final String COLUMN_TAG = "column"; 52 53 private static final String ID_ATTRIB = "id"; 54 private static final String NAME_ATTRIB = "name"; 55 private static final String TYPE_ATTRIB = "type"; 56 57 58 Vector mTables = null; 59 60 61 67 public DOM2DB (String docPath) throws JahiaException 68 { 69 super(docPath); 70 extractDocumentData(); 71 } 72 73 74 81 public DOM2DB (String docPath, org.xml.sax.helpers.ParserAdapter parser) 82 throws JahiaException { 83 super(docPath,parser); 84 extractDocumentData(); 85 } 86 87 88 93 public void extractDocumentData() throws JahiaException { 94 95 97 if (m_XMLDocument == null) { 98 99 throw new JahiaException( CLASS_NAME, 100 "The Document is null !", 101 JahiaException.ERROR_SEVERITY, 102 JahiaException.ERROR_SEVERITY); 103 } 104 105 106 if (!m_XMLDocument.hasChildNodes()) { 107 108 throw new JahiaException( CLASS_NAME, 109 "Main document node has no children", 110 JahiaException.ERROR_SEVERITY, 111 JahiaException.ERROR_SEVERITY); 112 } 113 114 115 Element docElNode = (Element ) m_XMLDocument.getDocumentElement(); 116 117 if (!docElNode.getNodeName().equalsIgnoreCase(DATABASE_TAG)) { 118 119 throw new JahiaException( "Invalid Document format", 120 "Tag <"+DATABASE_TAG+"> is not present as starting tag in file", 121 JahiaException.ERROR_SEVERITY, 122 JahiaException.ERROR_SEVERITY); 123 } 124 125 126 mTables = XMLParser.getChildNodes(docElNode,TABLE_TAG); 127 128 } 130 131 137 public Vector getTableNames() 138 throws JahiaException { 139 Vector v = new Vector (); 140 141 int size = mTables.size(); 142 for ( int i=0 ; i<size ; i++ ){ 143 Element el = (Element )mTables.get(i); 144 v.add(XMLParser.getAttributeValue(el,NAME_ATTRIB)); 145 } 146 return v; 147 } 148 149 155 public Enumeration getTables() 156 throws JahiaException { 157 return mTables.elements(); 158 } 159 160 167 public static void sqlInsert(Element table, Statement stmt) 168 throws JahiaException,SQLException { 169 sqlInsert(table, stmt, null); 170 } 171 172 180 public static void sqlInsert(Element table, Statement stmt, DBRowDataFilter filter ) 181 throws JahiaException,SQLException { 182 183 184 String tableName = XMLParser.getAttributeValue(table,NAME_ATTRIB); 185 String insertQuery = "INSERT INTO " + tableName + " ("; 186 187 JahiaConsole.println(CLASS_NAME+".sqlInsert"," table name=" + tableName ); 188 189 String colName = null; 190 String colVal = null; 191 int colType = 0; 192 193 Vector rows = XMLParser.getChildNodes(table,ROW_TAG); 194 Vector cols = null; 195 Element r = null; 196 Element c = null; 197 int nbRows = rows.size(); 198 int nbCols = 0; 199 boolean isText = false; 200 201 StringBuffer buff = null; 202 StringBuffer valBuff = null; 203 StringBuffer colBuff = null; 204 205 for ( int i=0 ; i<nbRows ; i++ ){ 206 207 Hashtable vals = new Hashtable (); 208 209 r = (Element )rows.get(i); 210 buff = new StringBuffer (insertQuery); 211 valBuff = new StringBuffer (); 212 colBuff = new StringBuffer (); 213 cols = XMLParser.getChildNodes(r,COLUMN_TAG); 214 nbCols = cols.size(); 215 for ( int j=0 ; j<nbCols ; j++ ){ 216 isText = false; 217 c = (Element )cols.get(j); 218 colName = XMLParser.getAttributeValue(c,NAME_ATTRIB); 219 colType = Integer.parseInt(XMLParser.getAttributeValue(c,TYPE_ATTRIB)); 220 221 223 Node textNode = c.getFirstChild(); 224 225 if ( textNode == null ){ 226 colVal = ""; 227 } else if (textNode.getNodeType() == Node.TEXT_NODE) { 228 colVal = textNode.getNodeValue(); 229 } else { 230 throw new JahiaException(CLASS_NAME, 231 "Value of column is not in correct format, should only be text", 232 JahiaException.ERROR_SEVERITY, 233 JahiaException.ERROR_SEVERITY); 234 } 235 236 vals.put(colName.toLowerCase(),colVal); 237 238 if( (colType == Types.VARCHAR) 239 || (colType == Types.CHAR) 240 || (colType == Types.BLOB) 241 || (colType == Types.LONGVARCHAR) ) { 242 isText = true; 243 244 } else if ( colType == 8 ){ 245 Float f = new Float (colVal); 246 colVal = (new Long (f.longValue())).toString(); 247 } else { 248 try { 249 int testConversion = Integer.parseInt(colVal); 250 } catch (NumberFormatException nfe) { 251 isText = true; 252 } 253 } 254 255 colBuff.append(colName); 257 if(isText) { 258 valBuff.append("'"); 259 } 260 valBuff.append(JahiaTools.quote(JahiaTools.text2XMLEntityRef(colVal,1))); 261 if(isText) { 262 valBuff.append("'"); 263 } 264 265 if(j<(nbCols-1)) { 266 valBuff.append(", "); 267 colBuff.append(", "); 268 } 269 270 } 271 272 buff.append(colBuff.toString().toLowerCase()); 273 buff.append(") VALUES("); 274 buff.append(valBuff.toString()); 275 buff.append(")"); 276 277 if ( filter == null || filter.inValue(vals) ){ 278 if ( valBuff.length()>0 ){ 279 stmt.executeUpdate(buff.toString()); 280 } 281 } 282 } 284 } 285 286 } 287 | Popular Tags |