001: /**
002: SqlElement - Sql element in xml file.
003: Copyright (C) 2002-2003 Together
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: This library is distributed in the hope that it will be useful,
009: but WITHOUT ANY WARRANTY; without even the implied warranty of
010: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
011: Lesser General Public License for more details.
012: You should have received a copy of the GNU Lesser General Public
013: License along with this library; if not, write to the Free Software
014: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
015: SqlElement.java
016: Date: 20.5.2003.
017: @version 1.0.0
018: @author: Zoran Milakovic zoran@prozone.co.yu
019: */package org.webdocwf.util.loader;
020:
021: import java.io.*;
022: import java.util.*;
023: import javax.xml.parsers.*;
024: import org.w3c.dom.*;
025: import org.webdocwf.util.loader.logging.*;
026:
027: /**
028: * Class is wrapper for SqlElement tag in XML file.
029: *
030: * @author Zoran Milakovic
031: * @version 1.1
032: */
033: public class SqlElement {
034:
035: private String strSqlName = "";
036: //private String strSqlLogMode = "";
037: private String strSqlOnErrorContinue = "";
038: private String strSqlCommit = "";
039:
040: // String strLogMode = "";
041: private String strDefaultLogMode = "";
042: private boolean bOnErrorContinue;
043: private boolean bDefaultOnErrorContinue = false;
044: private boolean bDefaultCommit = true;
045: // private String strTargetDbVendor = "";
046: // private String strTargetDriverName = "";
047:
048: // private String strJDBCTargetParameterDriver = "";
049: // private String strJDBCTargetParameterConnection = "";
050: // private String strJDBCTargetParameterUser = "";
051: // private String strJDBCTargetParameterPassword = "";
052: // private String strTargetDriverClassName = "";
053:
054: private JdbcParametersElement jdbcParametersElement;
055: private ConfigReader configReader;
056: private Logger logger;
057: private LoaderJobAttrReader loaderJobReader;
058:
059: /**
060: * Method parseSql is used to analyse import XML file
061: * about sql tags. Return number of sql elements in a whole xml input file.
062: * @param inStream Data from inputXML file which is converted into InputStream.
063: * @return Number of sql tags in an input XML file.
064: * @throws LoaderException
065: */
066: public int parseSql(InputStream inStream) throws LoaderException {
067: int iNumTagsImportJob = 0;
068: Document doc = null;
069: this .logger.write("full", "\tparseSql method is started.");
070: try {
071: DocumentBuilderFactory dbf = DocumentBuilderFactory
072: .newInstance();
073: DocumentBuilder db = null;
074: db = dbf.newDocumentBuilder();
075: doc = db.parse(inStream);
076: } catch (Exception e) {
077: this .logger.write("normal", "Sorry, an error occurred: "
078: + e);
079: LoaderException le = new LoaderException("Exception: ",
080: (Throwable) e);
081: throw le;
082: // System.exit(1);
083: }
084: if (doc != null) {
085: NodeList tagBasic = doc.getElementsByTagName("sql");
086: iNumTagsImportJob = tagBasic.getLength();
087: Vector vecNames = new Vector();
088: String strName = "";
089: for (int i = 0; i < iNumTagsImportJob; i++) {
090: strName = new String("");
091: strName = OctopusXMLUtil.importAttributeValue(doc,
092: "sql", "name", i);
093: for (int j = 0; j < vecNames.size(); j++) {
094: if (strName.equals("")) {
095: this .logger
096: .write("normal",
097: "Sorry, an error occurred: No Sql statement name .");
098: LoaderException le = new LoaderException(
099: "Exception: ",
100: (Throwable) (new Exception(
101: "Sorry, an error occurred: No Sql statement name .")));
102: throw le;
103: }
104: if (strName.equalsIgnoreCase(vecNames.get(j)
105: .toString())) {
106: this .logger.write("normal",
107: "Sorry, an error occurred: More Sql statements with same name :"
108: + strName);
109: LoaderException le = new LoaderException(
110: "Exception: ",
111: (Throwable) (new Exception(
112: "Sorry, an error occurred: More Sql statements with same name :")));
113: throw le;
114: }
115: }
116: vecNames.addElement(strName);
117: }
118: }
119: try {
120: inStream.reset();
121: } catch (IOException e) {
122: this .logger.write("normal", "Sorry, an error occurred: "
123: + e);
124: LoaderException le = new LoaderException("IOxception: ",
125: (Throwable) e);
126: throw le;
127: // System.exit(1);
128: }
129: this .logger.write("full", "\tparseSql method is finished.");
130: return iNumTagsImportJob;
131: }
132:
133: /**
134: * This method set value of logger parameter
135: * @param logger is value of parameter
136: */
137: public void setLogger(Logger logger) {
138: this .logger = logger;
139: }
140:
141: /**
142: * This method set value of echo parameter
143: * @param reader is value of parameter
144: */
145: public void setConfigReader(ConfigReader reader) {
146: this .configReader = reader;
147: }
148:
149: /**
150: * This method set value of jdbcParametersElement parameter
151: * @param jdbc is value of parameter
152: */
153: public void setJdbcParametersElement(JdbcParametersElement jdbc) {
154: this .jdbcParametersElement = jdbc;
155: }
156:
157: /**
158: * Method importSQLStatement imports sql attributes from xml file and puts them in the global variables.
159: * Return Vector. It's elements are strings which represents sql statements.
160: * @param inStream Data from inputXML file which is converted into InputStream.
161: * @param iSqlItem Number of sql tag in XML file which will be analised.
162: * @return vector
163: * @throws LoaderException
164: */
165: public Vector importSQLStatement(InputStream inStream, int iSqlItem)
166: throws LoaderException {
167: Vector strValue = new Vector();
168: Vector vecJDBCTargetValue = new Vector();
169: Vector vecJDBCTargetName = new Vector();
170: Document doc = null;
171: String strNodeValue = "";
172: this .logger.write("full",
173: "\timportSQLStatement method is started.");
174: try {
175: DocumentBuilderFactory dbf = DocumentBuilderFactory
176: .newInstance();
177: DocumentBuilder db = null;
178: db = dbf.newDocumentBuilder();
179: doc = db.parse(inStream);
180: if (doc != null) {
181: NodeList tagBasic = doc.getElementsByTagName("sql");
182: if (tagBasic.getLength() != 0) {
183: Element docFragment = (Element) tagBasic
184: .item(iSqlItem);
185: this .strSqlName = docFragment.getAttribute("name");
186: /*
187: this.strSqlLogMode = docFragment.getAttribute("logMode");
188: if (this.strSqlLogMode.equals("")) {
189: this.strSqlLogMode = loaderJobReader.getDefaultLogMode();
190: }
191: */
192: this .strSqlOnErrorContinue = docFragment
193: .getAttribute("onErrorContinue");
194: // if (this.strSqlOnErrorContinue.equals("")) {
195: // if (this.bOnErrorContinue == true)
196: // this.strSqlOnErrorContinue = "true";
197: // else
198: // this.strSqlOnErrorContinue = (new Boolean(this.bDefaultOnErrorContinue)).toString();
199: // }
200:
201: this .strSqlCommit = docFragment
202: .getAttribute("commit");
203: if (this .strSqlCommit.equals("")) {
204: this .strSqlCommit = (new Boolean(
205: this .bDefaultCommit)).toString();
206: }
207: String strReturnCode = docFragment
208: .getAttribute("returnCode");
209: if (!strReturnCode.equals("")) {
210: ReturnCode.setErrorReturnCode(Integer
211: .parseInt(strReturnCode));
212: } else
213: ReturnCode.setErrorReturnCode(ReturnCode
214: .getDefaultErrorReturnCode());
215:
216: NodeList targetTag = docFragment
217: .getElementsByTagName("sqlStmt");
218: for (int i = 0; i < targetTag.getLength(); i++) {
219: NodeList nodeText = targetTag.item(i)
220: .getChildNodes();
221: if (nodeText.item(0) != null) {
222: strNodeValue = nodeText.item(0)
223: .getNodeValue();
224: strValue.addElement(strNodeValue);
225: }
226: }
227: }
228: }
229: } catch (Exception e) {
230: this .logger.write("normal", "Sorry, an error occurred: "
231: + e);
232: LoaderException le = new LoaderException("Exception: ",
233: (Throwable) e);
234: throw le;
235: // System.exit(1);
236: }
237: try {
238: inStream.reset();
239: } catch (IOException e) {
240: this .logger.write("normal", "Sorry, an error occurred: "
241: + e);
242: LoaderException le = new LoaderException("IOException: ",
243: (Throwable) e);
244: throw le;
245: // System.exit(1);
246: }
247: this .logger.write("full",
248: "\timportSQLStatement method is finished.");
249: return strValue;
250: }
251:
252: // public String getTargetDriverClassName() {
253: // return this.strTargetDriverClassName;
254: // }
255: // public String getJDBCTargetParameterPassword() {
256: // return this.strJDBCTargetParameterPassword;
257: // }
258: //
259: // public String getJDBCTargetParameterUser() {
260: // return this.strJDBCTargetParameterUser;
261: // }
262: //
263: // public String getJDBCTargetParameterConnection() {
264: // return this.strJDBCTargetParameterConnection;
265: // }
266: //
267: // public String getJDBCTargetParameterDriver() {
268: // return this.strJDBCTargetParameterDriver;
269: // }
270:
271: // public String getTargetDriverName() {
272: // return this.strTargetDriverName;
273: // }
274: //
275: // public String getTargetDbVendor() {
276: // return this.strTargetDbVendor;
277: // }
278:
279: /**
280: * This method read value of bDefaultCommit parameter
281: * @return default value for attribute Commit
282: */
283: public boolean getDefaultCommit() {
284: return this .bDefaultCommit;
285: }
286:
287: /**
288: * This method read value of bDefaultOnErrorContinue parameter
289: * @return default value for attribute OnErrorContinue
290: */
291: public boolean getDefaultOnErrorContinue() {
292: return this .bDefaultOnErrorContinue;
293: }
294:
295: /**
296: * This method read value of bOnErrorContinue parameter
297: * @return value for attribute OnErrorContinue
298: */
299: public boolean getOnErrorContinue() {
300: return this .bOnErrorContinue;
301: }
302:
303: /**
304: * This method read value of strDefaultLogMode parameter
305: * @return default value for attribute LogMode
306: */
307: public String getDefaultLogMode() {
308: return this .strDefaultLogMode;
309: }
310:
311: /**
312: * This method read value of strLogMode parameter
313: * @return value for attribute LogMode
314: */
315: // public String getLogMode() {
316: // return this.strLogMode;
317: // }
318: /**
319: * This method read value of strSqlCommit parameter
320: * @return value for attribute SqlCommit
321: */
322: public String getSqlCommit() {
323: return this .strSqlCommit;
324: }
325:
326: /**
327: * This method read value of strSqlOnErrorContinue parameter
328: * @return value for attribute SqlOnErrorContinue
329: */
330: public String getSqlOnErrorContinue() {
331: return this .strSqlOnErrorContinue;
332: }
333:
334: /**
335: * This method read value of strSqlLogMode parameter
336: * @return value for attribute SqlLogMode
337: */
338: // public String getSqlLogMode() {
339: // return this.strSqlLogMode;
340: // }
341: /**
342: * This method read value of strSqlName parameter
343: * @return value for attribute SqlName
344: */
345: public String getSqlName() {
346: return this .strSqlName;
347: }
348:
349: /**
350: * Set LoaderJobAttrReader object
351: * @param loader LoaderJobAttrReader object,which stores loaderJob tag attributes
352: */
353: public void setLoaderJob(LoaderJobAttrReader loader) {
354: this.loaderJobReader = loader;
355: }
356:
357: }
|