001: /*
002: Copyright (C) 2003 Together
003:
004: This library is free software; you can redistribute it and/or
005: modify it under the terms of the GNU Lesser General Public
006: License as published by the Free Software Foundation; either
007: version 2.1 of the License, or (at your option) any later version.
008:
009: This library is distributed in the hope that it will be useful,
010: but WITHOUT ANY WARRANTY; without even the implied warranty of
011: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: Lesser General Public License for more details.
013:
014: You should have received a copy of the GNU Lesser General Public
015: License along with this library; if not, write to the Free Software
016: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */
018:
019: package org.webdocwf.util.xml;
020:
021: //xml imports
022: import org.w3c.dom.Document;
023: import org.w3c.dom.NodeList;
024: import org.w3c.dom.Node;
025: import org.w3c.dom.Element;
026: import org.enhydra.xml.*;
027: import javax.xml.parsers.DocumentBuilder;
028: import javax.xml.parsers.DocumentBuilderFactory;
029:
030: import java.sql.*;
031: import java.io.File;
032: import java.util.ArrayList;
033:
034: /**
035: * Class load existing XML file, creating DOM from file or creating
036: * new DOM.Class has methods for reading data from XML file.
037: *
038: * @author Zoran Milakovic
039: */
040: public class XmlReader {
041: private String[] columnNames;
042: private String[] columnValues;
043: private String tableName;
044:
045: /**
046: * Document made from XML file, and in which will
047: * be made changes.Document will be saved in XML file.
048: */
049: private SearchElement searchDocument;
050: private Document document;
051: /**
052: * Full path of the XML file.
053: */
054: private String fileName;
055:
056: /**
057: * Constructor will build Document from the specified file
058: * if file exist, or will create new Document if file not exist.
059: *
060: * @param fileName full pathname of the XML file
061: * @throws SQLException
062: */
063: public XmlReader(String fileName) throws SQLException {
064: DocumentBuilderFactory factory = DocumentBuilderFactory
065: .newInstance();
066: try {
067: this .fileName = fileName;
068: File file = new File(fileName);
069: DocumentBuilder builder = factory.newDocumentBuilder();
070: try {
071: this .document = builder.parse(file);
072: } catch (Exception e) {
073: throw new SQLException(
074: "Error while parsing XML file ! : "
075: + e.getMessage());
076: }
077: this .searchDocument = (SearchElement) SearchElement
078: .newInstance(document);
079: } catch (Exception e) {
080: throw new SQLException("Error in creating DOM : "
081: + e.getMessage());
082: }
083: }
084:
085: private ArrayList rset = new ArrayList();
086:
087: /**
088: * Gets data from database.Method will fill array list which will be result set.
089: * ArrayList will contain arrays of strings.Every array of string will present
090: * one row in database.
091: *
092: * @param tableName Name of table.
093: * @param columnNames Names of columns from which will be select data.
094: * @param whereColumnNames Names of columns in where conditions.
095: * @param whereColumnValues Values of conditions.
096: * @throws SQLException
097: */
098: public void select(String tableName, String[] columnNames,
099: String[] whereColumnNames, String[] whereColumnValues)
100: throws SQLException {
101: try {
102: NodeList tableRows = searchDocument
103: .getSubElementsByTagName("dml/" + tableName);
104: for (int i = 0; i < tableRows.getLength(); i++) {
105: boolean isMatch = true;
106: if (whereColumnNames != null
107: && whereColumnValues != null) {
108: for (int k = 0; k < whereColumnNames.length; k++) {
109: NodeList columns = ((SearchElement) tableRows
110: .item(i))
111: .getSubElementsByCondition(whereColumnNames[k]
112: + "=" + whereColumnValues[k]);
113: if (columns.getLength() == 0)
114: isMatch = false;
115: }
116: }
117: if (isMatch) {
118: ArrayList colValuesList = new ArrayList();
119: colValuesList.clear();
120: //columnNames has names of ALL columns, even if some of them are not have tag in xml file.
121: //This is posible because all column names are stored in CREATE TABLE statement
122: for (int k = 0; k < columnNames.length; k++) {
123: NodeList columns = ((SearchElement) tableRows
124: .item(i))
125: .getSubElementsByTagName(columnNames[k]);
126: //it is posible that variable columns has zero length,if some column tags are missing
127: //in that case, column has null value
128: if (columns.getLength() != 0) {
129: Node column = columns.item(0);
130: Node textNode = column.getFirstChild();
131: if (textNode == null)
132: colValuesList.add("null");
133: else
134: colValuesList.add(formatString(textNode
135: .getNodeValue()));
136: } else {
137: colValuesList.add("null");
138: }
139: }
140: rset.add(colValuesList.toArray(new String[0]));
141: int y = 0;
142: }
143: }
144: } catch (Exception e) {
145: throw new SQLException("Error in select : "
146: + e.getMessage());
147: }
148: }
149:
150: /**
151: * Gets table names from database.
152: *
153: * @throws SQLException
154: */
155: public void selectTableNames() throws SQLException {
156: try {
157: ArrayList tableNames = new ArrayList();
158: ArrayList tableNamesAll = new ArrayList();
159:
160: NodeList sqlStatements = searchDocument
161: .getSubElementsByTagName("ddl");
162: XmlSqlParser parser = new XmlSqlParser();
163: for (int i = 0; i < sqlStatements.getLength(); i++) {
164: Node node = sqlStatements.item(i);
165: parser.parse(node.getFirstChild().toString());
166: String tableName = parser.getTableName();
167: if (!tableNamesAll.contains(tableName)) {
168: tableNamesAll.add(tableName);
169: tableNames.clear();
170: tableNames.add(tableName);
171: rset.add(tableNames.toArray(new String[0]));
172: }
173: }
174:
175: NodeList allRowTableNames = ((Element) (searchDocument
176: .getSubElementsByTagName("dml").item(0)))
177: .getChildNodes();
178: for (int i = 0; i < allRowTableNames.getLength(); i++) {
179: if (allRowTableNames.item(i).getNodeType() != 3) {
180: String tableName = allRowTableNames.item(i)
181: .getNodeName();
182: if (!tableNamesAll.contains(tableName)) {
183: tableNamesAll.add(tableName);
184: tableNames.clear();
185: tableNames.add(tableName);
186: rset.add(tableNames.toArray(new String[0]));
187: }
188: }
189: }
190: } catch (Exception e) {
191: throw new SQLException("Error in selectTableNames : "
192: + e.getMessage());
193: }
194: }
195:
196: private String formatString(String str) {
197: String retVal = str;
198: retVal = Utils
199: .replaceAll(retVal, XmlSqlParser.equalEscape, "=");
200: retVal = Utils.replaceAll(retVal, XmlSqlParser.atEscape, "@");
201: retVal = Utils
202: .replaceAll(retVal, XmlSqlParser.slashEscape, "/");
203: return retVal;
204: }
205:
206: /**
207: *
208: * @return list with results
209: */
210: public ArrayList getResultSet() {
211: return this.rset;
212: }
213:
214: }
|