1 18 19 package org.webdocwf.util.xml; 20 21 import org.w3c.dom.Document ; 23 import org.w3c.dom.NodeList ; 24 import org.w3c.dom.Node ; 25 import org.w3c.dom.Element ; 26 import org.enhydra.xml.*; 27 import javax.xml.parsers.DocumentBuilder ; 28 import javax.xml.parsers.DocumentBuilderFactory ; 29 30 import java.sql.*; 31 import java.io.File ; 32 import java.util.ArrayList ; 33 34 40 public class XmlReader 41 { 42 private String [] columnNames; 43 private String [] columnValues; 44 private String tableName; 45 46 50 private SearchElement searchDocument; 51 private Document document; 52 55 private String fileName; 56 63 public XmlReader(String fileName) throws SQLException { 64 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 65 try { 66 this.fileName = fileName; 67 File file = new File ( fileName ); 68 DocumentBuilder builder = factory.newDocumentBuilder(); 69 try { 70 this.document = builder.parse( file ); 71 } catch( Exception e ) { 72 throw new SQLException("Error while parsing XML file ! : "+e.getMessage()); 73 } 74 this.searchDocument = (SearchElement)SearchElement.newInstance( document ); 75 } catch( Exception e ) { throw new SQLException("Error in creating DOM : "+e.getMessage()); } 76 } 77 78 79 80 81 private ArrayList rset = new ArrayList (); 82 83 94 public void select(String tableName , String [] columnNames , String [] whereColumnNames , String [] whereColumnValues) throws SQLException { 95 try { 96 NodeList tableRows = searchDocument.getSubElementsByTagName("dml/"+tableName); 97 for(int i = 0; i < tableRows.getLength(); i++) { 98 boolean isMatch = true; 99 if( whereColumnNames != null && whereColumnValues != null ) { 100 for(int k = 0; k < whereColumnNames.length; k++) { 101 NodeList columns = ( (SearchElement)tableRows.item(i) ).getSubElementsByCondition(whereColumnNames[k]+"="+whereColumnValues[k]); 102 if( columns.getLength() == 0 ) 103 isMatch = false; 104 } 105 } 106 if( isMatch ) { 107 ArrayList colValuesList = new ArrayList (); 108 colValuesList.clear(); 109 for(int k = 0; k < columnNames.length; k++) { 112 NodeList columns = ( (SearchElement)tableRows.item(i) ).getSubElementsByTagName(columnNames[k]); 113 if( columns.getLength() != 0 ) { 116 Node column = columns.item(0); 117 Node textNode = column.getFirstChild(); 118 if( textNode == null ) 119 colValuesList.add( "null" ); 120 else 121 colValuesList.add( formatString(textNode.getNodeValue()) ); 122 } else { 123 colValuesList.add( "null" ); 124 } 125 } 126 rset.add( colValuesList.toArray(new String [0]) ); 127 int y = 0; 128 } 129 } 130 }catch(Exception e) { 131 throw new SQLException("Error in select : "+e.getMessage()); 132 } 133 } 134 135 140 public void selectTableNames() throws SQLException { 141 try { 142 ArrayList tableNames = new ArrayList (); 143 ArrayList tableNamesAll = new ArrayList (); 144 145 NodeList sqlStatements = searchDocument.getSubElementsByTagName("ddl"); 146 XmlSqlParser parser = new XmlSqlParser(); 147 for( int i = 0; i < sqlStatements.getLength(); i++ ) { 148 Node node = sqlStatements.item(i); 149 parser.parse( node.getFirstChild().toString() ); 150 String tableName = parser.getTableName(); 151 if ( !tableNamesAll.contains( tableName ) ) { 152 tableNamesAll.add( tableName ); 153 tableNames.clear(); 154 tableNames.add( tableName ); 155 rset.add( tableNames.toArray(new String [0])); 156 } 157 } 158 159 NodeList allRowTableNames = ((Element )( searchDocument.getSubElementsByTagName("dml").item(0) )).getChildNodes(); 160 for(int i = 0;i < allRowTableNames.getLength();i++) { 161 if( allRowTableNames.item(i).getNodeType() != 3 ) { 162 String tableName = allRowTableNames.item(i).getNodeName(); 163 if( !tableNamesAll.contains( tableName ) ) { 164 tableNamesAll.add( tableName ); 165 tableNames.clear(); 166 tableNames.add( tableName ); 167 rset.add( tableNames.toArray(new String [0])); 168 } 169 } 170 } 171 }catch(Exception e) { 172 throw new SQLException("Error in selectTableNames : "+e.getMessage()); 173 } 174 } 175 176 private String formatString(String str) { 177 String retVal = str; 178 retVal = Utils.replaceAll( retVal, XmlSqlParser.equalEscape, "=" ); 179 retVal = Utils.replaceAll( retVal, XmlSqlParser.atEscape, "@" ); 180 retVal = Utils.replaceAll( retVal, XmlSqlParser.slashEscape, "/" ); 181 return retVal; 182 } 183 184 188 public ArrayList getResultSet() { 189 return this.rset; 190 } 191 192 } 193 194 | Popular Tags |