1 /*
2 Copyright (C) 2003 Together
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with this library; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18
19 package org.webdocwf.util.xml;
20
21 //xml imports
22 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 /***
35 * Class load existing XML file, creating DOM from file or creating
36 * new DOM.Class has methods for reading data from XML file.
37 *
38 * @author Zoran Milakovic
39 */
40 public class XmlReader
41 {
42 private String[] columnNames;
43 private String[] columnValues;
44 private String tableName;
45
46 /***
47 * Document made from XML file, and in which will
48 * be made changes.Document will be saved in XML file.
49 */
50 private SearchElement searchDocument;
51 private Document document;
52 /***
53 * Full path of the XML file.
54 */
55 private String fileName;
56 /***
57 * Constructor will build Document from the specified file
58 * if file exist, or will create new Document if file not exist.
59 *
60 * @param fileName full pathname of the XML file
61 * @throws SQLException
62 */
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 /***
84 * Gets data from database.Method will fill array list which will be result set.
85 * ArrayList will contain arrays of strings.Every array of string will present
86 * one row in database.
87 *
88 * @param tableName Name of table.
89 * @param columnNames Names of columns from which will be select data.
90 * @param whereColumnNames Names of columns in where conditions.
91 * @param whereColumnValues Values of conditions.
92 * @throws SQLException
93 */
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 //columnNames has names of ALL columns, even if some of them are not have tag in xml file.
110 //This is posible because all column names are stored in CREATE TABLE statement
111 for(int k = 0; k < columnNames.length; k++) {
112 NodeList columns = ( (SearchElement)tableRows.item(i) ).getSubElementsByTagName(columnNames[k]);
113 //it is posible that variable columns has zero length,if some column tags are missing
114 //in that case, column has null value
115 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 /***
136 * Gets table names from database.
137 *
138 * @throws SQLException
139 */
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 /***
185 *
186 * @return list with results
187 */
188 public ArrayList getResultSet() {
189 return this.rset;
190 }
191
192 }
193
This page was automatically generated by Maven