001: /*
002:
003: Loader - tool for transfering data from one JDBC source to another and
004: doing transformations during copy.
005:
006: Copyright (C) 2002 Together
007:
008: This library is free software; you can redistribute it and/or
009: modify it under the terms of the GNU Lesser General Public
010: License as published by the Free Software Foundation; either
011: version 2.1 of the License, or (at your option) any later version.
012:
013: This library is distributed in the hope that it will be useful,
014: but WITHOUT ANY WARRANTY; without even the implied warranty of
015: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
016: Lesser General Public License for more details.
017:
018: You should have received a copy of the GNU Lesser General Public
019: License along with this library; if not, write to the Free Software
020: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
021:
022: LoadVariable.java
023: Date: 2.11.2001.
024: @version 1.0
025: @author:
026: Dusan Radeka
027: */
028:
029: package org.webdocwf.util.loader;
030:
031: import java.io.ByteArrayInputStream;
032: import java.io.ByteArrayOutputStream;
033: import java.io.IOException;
034: import java.io.OutputStream;
035: import java.io.OutputStreamWriter;
036: import java.io.UnsupportedEncodingException;
037: import java.io.Writer;
038: import java.util.Vector;
039:
040: import org.apache.xerces.parsers.SAXParser;
041: import org.xml.sax.AttributeList;
042: import org.xml.sax.HandlerBase;
043: import org.xml.sax.InputSource;
044: import org.xml.sax.SAXException;
045: import org.xml.sax.SAXParseException;
046:
047: /**
048: * LoadVariable.java
049: * This class parses a document (OutputStream) and writes the documents
050: * contents back to standard output. This class changes variables defined in variable
051: * tags.
052: */
053: public class LoadVariable extends HandlerBase {
054: private Writer out;
055: private String encoding;
056: private int level = 0;
057: private Vector vecVariableName = new Vector();
058: private Vector vecVariableValue = new Vector();
059: private Vector vecVariablePrefix = new Vector();
060: private Vector vecVariableSufix = new Vector();
061: private Vector vecVariableOverride = new Vector();
062: private Vector vecReplaceInConstants = new Vector();
063: private Vector vecReplaceInSQL = new Vector();
064: private Vector vecReplaceInJDBC = new Vector();
065: private ByteArrayOutputStream streamToParse = new ByteArrayOutputStream();
066: private boolean bSQL = false;
067: private boolean bJDBC = false;
068: private boolean bVarInConstant = false;
069:
070: /** Main program entry point.
071: *@param argv is input parameters
072: */
073: public static void main(String argv[]) {
074: if (argv.length == 0) {
075: System.out.println("Usage: java LoadVariable uri");
076: System.out
077: .println(" where uri is the URI of your XML document.");
078: System.out.println(" Sample: java LoadVariable demo.xml");
079: }
080: }
081:
082: /**
083: * Construct object LoadVariable with associated outputStream and
084: * object class Loader. Class uses default "UTF-8" encoding.
085: * @param out - OutputStream where will be written final XML.
086: * @param l - object of Loader class. Sets values of variable tags attributes
087: * and sets POutpuStream witch will be parsed.
088: */
089: public LoadVariable(OutputStream out, Loader l) {
090: this .vecVariableName = l.vecVariableName;
091: this .vecVariableValue = l.vecVariableValue;
092: this .vecVariablePrefix = l.vecVariablePrefix;
093: this .vecVariableSufix = l.vecVariableSufix;
094: this .vecVariableOverride = l.vecVariableOverride;
095: this .vecReplaceInConstants = l.vecReplaceInConstants;
096: this .vecReplaceInSQL = l.vecReplaceInSQL;
097: this .vecReplaceInJDBC = l.vecReplaceInJDBC;
098: this .streamToParse = l.foStreamTmp;
099: for (int i = 0; i < this .vecReplaceInConstants.size(); i++) {
100: if (this .vecReplaceInConstants.get(i).toString()
101: .equalsIgnoreCase("true"))
102: bVarInConstant = true;
103: }
104: for (int i = 0; i < this .vecReplaceInSQL.size(); i++) {
105: if (this .vecReplaceInSQL.get(i).toString()
106: .equalsIgnoreCase("true"))
107: bSQL = true;
108: }
109: for (int i = 0; i < this .vecReplaceInJDBC.size(); i++) {
110: if (this .vecReplaceInJDBC.get(i).toString()
111: .equalsIgnoreCase("true"))
112: bJDBC = true;
113: }
114: try {
115: this .out = new OutputStreamWriter(out, "UTF8");
116: this .encoding = "UTF-8";
117: } catch (UnsupportedEncodingException e) {
118: }
119: }
120:
121: /**
122: * Method parseUri parses a file "uri" and writes the file
123: * contents back to standard output including contents of 'include' files .
124: */
125: public void parseURI() {
126: try {
127: SAXParser parser = new SAXParser();
128: parser.setDocumentHandler(this );
129: parser.setErrorHandler(this );
130: parser.parse(new InputSource(new ByteArrayInputStream(
131: this .streamToParse.toByteArray())));
132: } catch (Exception e) {
133: e.printStackTrace();
134: }
135: }
136:
137: /** Processing instruction.
138: * @param target is target
139: * @param data is target data
140: */
141: public void processingInstruction(String target, String data) {
142: try {
143: out.write("<?");
144: out.write(target);
145: if (data != null && data.length() > 0) {
146: out.write(' ');
147: out.write(data);
148: }
149: out.write("?>");
150: } catch (IOException e) {
151: System.err.println(e);
152: }
153: }
154:
155: /** Start document. */
156: public void startDocument() {
157: if (level == 0) {
158: try {
159: out.write("<?xml version=\"1.0\"?>\r\n");
160: } catch (IOException e) {
161: System.err.println(e);
162: }
163: }
164: }
165:
166: /** Start element.
167: * @param name is the name of the tag
168: * @param atts is attributes if tag
169: */
170: public void startElement(String name, AttributeList atts) {
171: try {
172: if (name.equalsIgnoreCase("constantColumn")) {
173: out.write("<" + name);
174: for (int i = 0; i < atts.getLength(); i++) {
175: out.write(" ");
176: out.write(atts.getName(i));
177: out.write("='");
178: String value = atts.getValue(i);
179: if (atts.getName(i).equalsIgnoreCase(
180: "constantValue")) {
181: // checking, and if Variable exists changing
182: if (bVarInConstant) {
183: // going throw vector
184: for (int k = 0; k < this .vecReplaceInConstants
185: .size(); k++) {
186: // if this is to change
187: if (this .vecReplaceInConstants.get(k)
188: .toString().equalsIgnoreCase(
189: "true")) {
190: String sPreNameSu = this .vecVariablePrefix
191: .get(k).toString()
192: + this .vecVariableName.get(
193: k).toString()
194: + this .vecVariableSufix
195: .get(k).toString();
196: // if costant is variable
197: if (value
198: .equalsIgnoreCase(sPreNameSu)) {
199: value = this .vecVariableValue
200: .get(k).toString();
201: }
202: }
203: }
204: }
205: }
206: // + 4 allows space for one entitiy reference.
207: // If there's more than that, then the StringBuffer
208: // will automatically expand
209: // Need to use character references if the encoding
210: // can't support the character
211: StringBuffer encodedValue = new StringBuffer(value
212: .length() + 4);
213: for (int j = 0; j < value.length(); j++) {
214: char c = value.charAt(j);
215: if (c == '&')
216: encodedValue.append("&");
217: else if (c == '<')
218: encodedValue.append("<");
219: else if (c == '>')
220: encodedValue.append(">");
221: else if (c == '\'')
222: encodedValue.append("'");
223: else
224: encodedValue.append(c);
225: }
226: out.write(encodedValue.toString());
227: out.write("'");
228: }
229: out.write(">");
230: } else if (name.equalsIgnoreCase("jdbcSourceParameter")) {
231: out.write("<" + name);
232: for (int i = 0; i < atts.getLength(); i++) {
233: out.write(" ");
234: out.write(atts.getName(i));
235: out.write("='");
236: String value = atts.getValue(i);
237: if (atts.getName(i).equalsIgnoreCase("value")) {
238: // checking, and if Variable exists changing
239: if (bJDBC) {
240: // going throw vector
241: for (int k = 0; k < this .vecReplaceInJDBC
242: .size(); k++) {
243: // if this is to change
244: if (this .vecReplaceInJDBC.get(k)
245: .toString().equalsIgnoreCase(
246: "true")) {
247: String sPreNameSu = this .vecVariablePrefix
248: .get(k).toString()
249: + this .vecVariableName.get(
250: k).toString()
251: + this .vecVariableSufix
252: .get(k).toString();
253: // if costant is variable
254: // if (value.equalsIgnoreCase(sPreNameSu)) {
255: // value = this.vecVariableValue.get(k).toString();
256: // }
257: value = Utils.replaceAll(value,
258: sPreNameSu,
259: this .vecVariableValue
260: .get(k).toString());
261: }
262: }
263: }
264: }
265: // + 4 allows space for one entitiy reference.
266: // If there's more than that, then the StringBuffer
267: // will automatically expand
268: // Need to use character references if the encoding
269: // can't support the character
270: StringBuffer encodedValue = new StringBuffer(value
271: .length() + 4);
272: for (int j = 0; j < value.length(); j++) {
273: char c = value.charAt(j);
274: if (c == '&')
275: encodedValue.append("&");
276: else if (c == '<')
277: encodedValue.append("<");
278: else if (c == '>')
279: encodedValue.append(">");
280: else if (c == '\'')
281: encodedValue.append("'");
282: else
283: encodedValue.append(c);
284: }
285: out.write(encodedValue.toString());
286: out.write("'");
287: }
288: out.write(">");
289: } else if (name.equalsIgnoreCase("jdbcTargetParameter")) {
290: out.write("<" + name);
291: for (int i = 0; i < atts.getLength(); i++) {
292: out.write(" ");
293: out.write(atts.getName(i));
294: out.write("='");
295: String value = atts.getValue(i);
296: if (atts.getName(i).equalsIgnoreCase("value")) {
297: // checking, and if Variable exists changing
298: if (bJDBC) {
299: // going throw vector
300: for (int k = 0; k < this .vecReplaceInJDBC
301: .size(); k++) {
302: // if this is to change
303: if (this .vecReplaceInJDBC.get(k)
304: .toString().equalsIgnoreCase(
305: "true")) {
306: String sPreNameSu = this .vecVariablePrefix
307: .get(k).toString()
308: + this .vecVariableName.get(
309: k).toString()
310: + this .vecVariableSufix
311: .get(k).toString();
312: // if costant is variable
313: // if (value.equalsIgnoreCase(sPreNameSu)) {
314: // value = this.vecVariableValue.get(k).toString();
315: // }
316: value = Utils.replaceAll(value,
317: sPreNameSu,
318: this .vecVariableValue
319: .get(k).toString());
320: }
321: }
322: }
323: }
324: // + 4 allows space for one entitiy reference.
325: // If there's more than that, then the StringBuffer
326: // will automatically expand
327: // Need to use character references if the encoding
328: // can't support the character
329: StringBuffer encodedValue = new StringBuffer(value
330: .length() + 4);
331: for (int j = 0; j < value.length(); j++) {
332: char c = value.charAt(j);
333: if (c == '&')
334: encodedValue.append("&");
335: else if (c == '<')
336: encodedValue.append("<");
337: else if (c == '>')
338: encodedValue.append(">");
339: else if (c == '\'')
340: encodedValue.append("'");
341: else
342: encodedValue.append(c);
343: }
344: out.write(encodedValue.toString());
345: out.write("'");
346: }
347: out.write(">");
348: } else if (name.equalsIgnoreCase("importDefinition")) {
349: out.write("<" + name);
350: for (int i = 0; i < atts.getLength(); i++) {
351: out.write(" ");
352: out.write(atts.getName(i));
353: out.write("='");
354: String value = atts.getValue(i);
355: if (atts.getName(i).equalsIgnoreCase(
356: "selectStatement")) {
357: // checking, and if Variable exists changing
358: if (bSQL) {
359: // going throw vector
360: for (int k = 0; k < this .vecReplaceInSQL
361: .size(); k++) {
362: // if this is to change
363: if (this .vecReplaceInSQL.get(k)
364: .toString().equalsIgnoreCase(
365: "true")) {
366: String sPreNameSu = this .vecVariablePrefix
367: .get(k).toString()
368: + this .vecVariableName.get(
369: k).toString()
370: + this .vecVariableSufix
371: .get(k).toString();
372: // if costant is variable
373: // if (value.equalsIgnoreCase(sPreNameSu)) {
374: // value = this.vecVariableValue.get(k).toString();
375: // }
376: value = Utils.replaceAll(value,
377: sPreNameSu,
378: this .vecVariableValue
379: .get(k).toString());
380: }
381: }
382: }
383: }
384: // + 4 allows space for one entitiy reference.
385: // If there's more than that, then the StringBuffer
386: // will automatically expand
387: // Need to use character references if the encoding
388: // can't support the character
389: StringBuffer encodedValue = new StringBuffer(value
390: .length() + 4);
391: for (int j = 0; j < value.length(); j++) {
392: char c = value.charAt(j);
393: if (c == '&')
394: encodedValue.append("&");
395: else if (c == '<')
396: encodedValue.append("<");
397: else if (c == '>')
398: encodedValue.append(">");
399: else if (c == '\'')
400: encodedValue.append("'");
401: else
402: encodedValue.append(c);
403: }
404: out.write(encodedValue.toString());
405: out.write("'");
406: }
407: out.write(">");
408: }
409:
410: else {
411: out.write("<" + name);
412: for (int i = 0; i < atts.getLength(); i++) {
413: out.write(" ");
414: out.write(atts.getName(i));
415: out.write("='");
416: String value = atts.getValue(i);
417: // + 4 allows space for one entitiy reference.
418: // If there's more than that, then the StringBuffer
419: // will automatically expand
420: // Need to use character references if the encoding
421: // can't support the character
422: StringBuffer encodedValue = new StringBuffer(value
423: .length() + 4);
424: for (int j = 0; j < value.length(); j++) {
425: char c = value.charAt(j);
426: if (c == '&')
427: encodedValue.append("&");
428: else if (c == '<')
429: encodedValue.append("<");
430: else if (c == '>')
431: encodedValue.append(">");
432: else if (c == '\'')
433: encodedValue.append("'");
434: else
435: encodedValue.append(c);
436: }
437: out.write(encodedValue.toString());
438: out.write("'");
439: }
440: out.write(">");
441: }
442:
443: } catch (IOException e) {
444: System.err.println(e);
445:
446: }
447: }
448:
449: /** Characters.
450: * @param ch is character array
451: * @param start is int
452: * @param length is length
453: * @throws SAXException
454: */
455: public void characters(char ch[], int start, int length)
456: throws SAXException {
457: try {
458: String s = new String(ch, start, length);
459: if (this .bSQL) {
460: // going throw vector
461: for (int k = 0; k < this .vecReplaceInJDBC.size(); k++) {
462: // if this is to change
463: if (this .vecReplaceInSQL.get(k).toString()
464: .equalsIgnoreCase("true")) {
465: String sPreNameSu = this .vecVariablePrefix.get(
466: k).toString()
467: + this .vecVariableName.get(k)
468: .toString()
469: + this .vecVariableSufix.get(k)
470: .toString();
471: int j = s.indexOf(sPreNameSu);
472: // if costant is variable
473: while (j != -1) {
474: s = s.substring(0, j)
475: + this .vecVariableValue.get(k)
476: .toString()
477: + s.substring(j
478: + sPreNameSu.length(), s
479: .length());
480: j = s.indexOf(sPreNameSu);
481: }
482: }
483: }
484: }
485: // + 4 allows space for one entitiy reference.
486: // If there's more than that, then the StringBuffer
487: // will automatically expand
488: // Need to use character references if the encoding
489: // can't support the character
490: StringBuffer encodedValue = new StringBuffer(s.length() + 4);
491: for (int j = 0; j < s.length(); j++) {
492: char c = s.charAt(j);
493: if (c == '&')
494: encodedValue.append("&");
495: else if (c == '<')
496: encodedValue.append("<");
497: else if (c == '>')
498: encodedValue.append(">");
499: else if (c == '\'')
500: encodedValue.append("'");
501: else
502: encodedValue.append(c);
503: }
504: out.write(encodedValue.toString());
505:
506: } catch (Exception e) {
507: System.err.println(e);
508: }
509: }
510:
511: /** Ignorable whitespace.
512: * @param ch is character array
513: * @param start is int
514: * @param length is length
515: * @throws SAXException
516: */
517: public void ignorableWhitespace(char ch[], int start, int length) {
518: try {
519: this .characters(ch, start, length);
520: } catch (SAXException e) {
521: System.err.println(e);
522: }
523: }
524:
525: /** End element.
526: * @param name is name of the tag
527: */
528: public void endElement(String name) {
529: if (!name.equalsIgnoreCase("include")
530: && !name.equalsIgnoreCase("definitionInclude")) {
531: try {
532: out.write("</");
533: out.write(name);
534: out.write(">");
535: } catch (IOException e) {
536: System.err.println(e);
537: }
538: }
539: }
540:
541: /** End document. */
542: public void endDocument() {
543: try {
544: out.flush();
545: } catch (IOException e) {
546: System.err.println(e);
547: }
548: }
549:
550: //
551: // ErrorHandler methods
552: //
553: /** Warning.
554: * @param ex is SAXParseException object
555: */
556: public void warning(SAXParseException ex) {
557: System.err.println("[Warning] " + getLocationString(ex) + ": "
558: + ex.getMessage());
559: }
560:
561: /** Error.
562: * @param ex is SAXParseException object
563: */
564: public void error(SAXParseException ex) {
565: System.err.println("[Error] " + getLocationString(ex) + ": "
566: + ex.getMessage());
567: }
568:
569: /** Fatal error.
570: * @param ex is SAXParseException object
571: * @throws SAXException
572: */
573: public void fatalError(SAXParseException ex) throws SAXException {
574: System.err.println("[Fatal Error] " + getLocationString(ex)
575: + ": " + ex.getMessage());
576: throw ex;
577: }
578:
579: /** Returns a string of the location.
580: * @param ex is SAXParseException object
581: * @return string
582: */
583: private String getLocationString(SAXParseException ex) {
584: StringBuffer str = new StringBuffer();
585: String systemId = ex.getSystemId();
586: if (systemId != null) {
587: int index = systemId.lastIndexOf('/');
588: if (index != -1)
589: systemId = systemId.substring(index + 1);
590: str.append(systemId);
591: }
592: str.append(':');
593: str.append(ex.getLineNumber());
594: str.append(':');
595: str.append(ex.getColumnNumber());
596: return str.toString();
597: }
598: }
|