1 /*
2
3 Loader - tool for transfering data from one JDBC source to another and
4 doing transformations during copy.
5
6 Copyright (C) 2002 Together
7
8 This library is free software; you can redistribute it and/or
9 modify it under the terms of the GNU Lesser General Public
10 License as published by the Free Software Foundation; either
11 version 2.1 of the License, or (at your option) any later version.
12
13 This library is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public
19 License along with this library; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21
22 LoadVariable.java
23 Date: 2.11.2001.
24 @version 1.0
25 @author:
26 Dusan Radeka
27 */
28
29
30 package org.webdocwf.util.loader;
31
32 import java.io.ByteArrayInputStream;
33 import java.io.ByteArrayOutputStream;
34 import java.io.IOException;
35 import java.io.OutputStream;
36 import java.io.OutputStreamWriter;
37 import java.io.UnsupportedEncodingException;
38 import java.io.Writer;
39 import java.util.Vector;
40
41 import org.apache.xerces.parsers.SAXParser;
42 import org.xml.sax.AttributeList;
43 import org.xml.sax.HandlerBase;
44 import org.xml.sax.InputSource;
45 import org.xml.sax.SAXException;
46 import org.xml.sax.SAXParseException;
47
48
49 /***
50 * LoadVariable.java
51 * This class parses a document (OutputStream) and writes the documents
52 * contents back to standard output. This class changes variables defined in variable
53 * tags.
54 */
55 public class LoadVariable extends HandlerBase {
56 private Writer out;
57 private String encoding;
58 private int level = 0;
59 private Vector vecVariableName = new Vector();
60 private Vector vecVariableValue = new Vector();
61 private Vector vecVariablePrefix = new Vector();
62 private Vector vecVariableSufix = new Vector();
63 private Vector vecVariableOverride = new Vector();
64 private Vector vecReplaceInConstants = new Vector();
65 private Vector vecReplaceInSQL = new Vector();
66 private Vector vecReplaceInJDBC = new Vector();
67 private ByteArrayOutputStream streamToParse = new ByteArrayOutputStream();
68 private boolean bSQL = false;
69 private boolean bJDBC = false;
70 private boolean bVarInConstant = false;
71
72 /*** Main program entry point.
73 *@param argv is input parameters
74 */
75 public static void main (String argv[]) {
76 if (argv.length == 0) {
77 System.out.println("Usage: java LoadVariable uri");
78 System.out.println(" where uri is the URI of your XML document.");
79 System.out.println(" Sample: java LoadVariable demo.xml");
80 }
81 }
82
83 /***
84 * Construct object LoadVariable with associated outputStream and
85 * object class Loader. Class uses default "UTF-8" encoding.
86 * @param out - OutputStream where will be written final XML.
87 * @param l - object of Loader class. Sets values of variable tags attributes
88 * and sets POutpuStream witch will be parsed.
89 */
90 public LoadVariable (OutputStream out, Loader l) {
91 this.vecVariableName = l.vecVariableName;
92 this.vecVariableValue = l.vecVariableValue;
93 this.vecVariablePrefix = l.vecVariablePrefix;
94 this.vecVariableSufix = l.vecVariableSufix;
95 this.vecVariableOverride = l.vecVariableOverride;
96 this.vecReplaceInConstants = l.vecReplaceInConstants;
97 this.vecReplaceInSQL = l.vecReplaceInSQL;
98 this.vecReplaceInJDBC = l.vecReplaceInJDBC;
99 this.streamToParse = l.foStreamTmp;
100 for (int i = 0; i < this.vecReplaceInConstants.size(); i++) {
101 if (this.vecReplaceInConstants.get(i).toString().equalsIgnoreCase("true"))
102 bVarInConstant = true;
103 }
104 for (int i = 0; i < this.vecReplaceInSQL.size(); i++) {
105 if (this.vecReplaceInSQL.get(i).toString().equalsIgnoreCase("true"))
106 bSQL = true;
107 }
108 for (int i = 0; i < this.vecReplaceInJDBC.size(); i++) {
109 if (this.vecReplaceInJDBC.get(i).toString().equalsIgnoreCase("true"))
110 bJDBC = true;
111 }
112 try {
113 this.out = new OutputStreamWriter(out, "UTF8");
114 this.encoding = "UTF-8";
115 } catch (UnsupportedEncodingException e) {}
116 }
117
118 /***
119 * Method parseUri parses a file "uri" and writes the file
120 * contents back to standard output including contents of 'include' files .
121 */
122 public void parseURI () {
123 try {
124 SAXParser parser = new SAXParser();
125 parser.setDocumentHandler(this);
126 parser.setErrorHandler(this);
127 parser.parse(new InputSource(new ByteArrayInputStream(this.streamToParse.toByteArray())));
128 } catch (Exception e) {
129 e.printStackTrace();
130 }
131 }
132
133 /*** Processing instruction.
134 * @param target is target
135 * @param data is target data
136 */
137 public void processingInstruction (String target, String data) {
138 try {
139 out.write("<?");
140 out.write(target);
141 if (data != null && data.length() > 0) {
142 out.write(' ');
143 out.write(data);
144 }
145 out.write("?>");
146 } catch (IOException e) {
147 System.err.println(e);
148 }
149 }
150
151 /*** Start document. */
152 public void startDocument () {
153 if (level == 0) {
154 try {
155 out.write("<?xml version=\"1.0\"?>\r\n");
156 } catch (IOException e) {
157 System.err.println(e);
158 }
159 }
160 }
161
162 /*** Start element.
163 * @param name is the name of the tag
164 * @param atts is attributes if tag
165 */
166 public void startElement (String name, AttributeList atts) {
167 try {
168 if (name.equalsIgnoreCase("constantColumn")) {
169 out.write("<" + name);
170 for (int i = 0; i < atts.getLength(); i++) {
171 out.write(" ");
172 out.write(atts.getName(i));
173 out.write("='");
174 String value = atts.getValue(i);
175 if (atts.getName(i).equalsIgnoreCase("constantValue")) {
176 // checking, and if Variable exists changing
177 if (bVarInConstant) {
178 // going throw vector
179 for (int k = 0; k < this.vecReplaceInConstants.size(); k++) {
180 // if this is to change
181 if (this.vecReplaceInConstants.get(k).toString().equalsIgnoreCase("true")) {
182 String sPreNameSu = this.vecVariablePrefix.get(k).toString()
183 + this.vecVariableName.get(k).toString()
184 + this.vecVariableSufix.get(k).toString();
185 // if costant is variable
186 if (value.equalsIgnoreCase(sPreNameSu)) {
187 value = this.vecVariableValue.get(k).toString();
188 }
189 }
190 }
191 }
192 }
193 // + 4 allows space for one entitiy reference.
194 // If there's more than that, then the StringBuffer
195 // will automatically expand
196 // Need to use character references if the encoding
197 // can't support the character
198 StringBuffer encodedValue = new StringBuffer(value.length()
199 + 4);
200 for (int j = 0; j < value.length(); j++) {
201 char c = value.charAt(j);
202 if (c == '&')
203 encodedValue.append("&");
204 else if (c == '<')
205 encodedValue.append("<");
206 else if (c == '>')
207 encodedValue.append(">");
208 else if (c == '\'')
209 encodedValue.append("'");
210 else
211 encodedValue.append(c);
212 }
213 out.write(encodedValue.toString());
214 out.write("'");
215 }
216 out.write(">");
217 }
218 else if (name.equalsIgnoreCase("jdbcSourceParameter")) {
219 out.write("<" + name);
220 for (int i = 0; i < atts.getLength(); i++) {
221 out.write(" ");
222 out.write(atts.getName(i));
223 out.write("='");
224 String value = atts.getValue(i);
225 if (atts.getName(i).equalsIgnoreCase("value")) {
226 // checking, and if Variable exists changing
227 if (bJDBC) {
228 // going throw vector
229 for (int k = 0; k < this.vecReplaceInJDBC.size(); k++) {
230 // if this is to change
231 if (this.vecReplaceInJDBC.get(k).toString().equalsIgnoreCase("true")) {
232 String sPreNameSu = this.vecVariablePrefix.get(k).toString()
233 + this.vecVariableName.get(k).toString()
234 + this.vecVariableSufix.get(k).toString();
235 // if costant is variable
236 // if (value.equalsIgnoreCase(sPreNameSu)) {
237 // value = this.vecVariableValue.get(k).toString();
238 // }
239 value = Utils.replaceAll(value, sPreNameSu, this.vecVariableValue.get(k).toString());
240 }
241 }
242 }
243 }
244 // + 4 allows space for one entitiy reference.
245 // If there's more than that, then the StringBuffer
246 // will automatically expand
247 // Need to use character references if the encoding
248 // can't support the character
249 StringBuffer encodedValue = new StringBuffer(value.length()
250 + 4);
251 for (int j = 0; j < value.length(); j++) {
252 char c = value.charAt(j);
253 if (c == '&')
254 encodedValue.append("&");
255 else if (c == '<')
256 encodedValue.append("<");
257 else if (c == '>')
258 encodedValue.append(">");
259 else if (c == '\'')
260 encodedValue.append("'");
261 else
262 encodedValue.append(c);
263 }
264 out.write(encodedValue.toString());
265 out.write("'");
266 }
267 out.write(">");
268 }
269 else if (name.equalsIgnoreCase("jdbcTargetParameter")) {
270 out.write("<" + name);
271 for (int i = 0; i < atts.getLength(); i++) {
272 out.write(" ");
273 out.write(atts.getName(i));
274 out.write("='");
275 String value = atts.getValue(i);
276 if (atts.getName(i).equalsIgnoreCase("value")) {
277 // checking, and if Variable exists changing
278 if (bJDBC) {
279 // going throw vector
280 for (int k = 0; k < this.vecReplaceInJDBC.size(); k++) {
281 // if this is to change
282 if (this.vecReplaceInJDBC.get(k).toString().equalsIgnoreCase("true")) {
283 String sPreNameSu = this.vecVariablePrefix.get(k).toString()
284 + this.vecVariableName.get(k).toString()
285 + this.vecVariableSufix.get(k).toString();
286 // if costant is variable
287 // if (value.equalsIgnoreCase(sPreNameSu)) {
288 // value = this.vecVariableValue.get(k).toString();
289 // }
290 value = Utils.replaceAll(value, sPreNameSu, this.vecVariableValue.get(k).toString());
291 }
292 }
293 }
294 }
295 // + 4 allows space for one entitiy reference.
296 // If there's more than that, then the StringBuffer
297 // will automatically expand
298 // Need to use character references if the encoding
299 // can't support the character
300 StringBuffer encodedValue = new StringBuffer(value.length()
301 + 4);
302 for (int j = 0; j < value.length(); j++) {
303 char c = value.charAt(j);
304 if (c == '&')
305 encodedValue.append("&");
306 else if (c == '<')
307 encodedValue.append("<");
308 else if (c == '>')
309 encodedValue.append(">");
310 else if (c == '\'')
311 encodedValue.append("'");
312 else
313 encodedValue.append(c);
314 }
315 out.write(encodedValue.toString());
316 out.write("'");
317 }
318 out.write(">");
319 }
320 else if (name.equalsIgnoreCase("importDefinition")) {
321 out.write("<" + name);
322 for (int i = 0; i < atts.getLength(); i++) {
323 out.write(" ");
324 out.write(atts.getName(i));
325 out.write("='");
326 String value = atts.getValue(i);
327 if (atts.getName(i).equalsIgnoreCase("selectStatement")) {
328 // checking, and if Variable exists changing
329 if (bSQL) {
330 // going throw vector
331 for (int k = 0; k < this.vecReplaceInSQL.size(); k++) {
332 // if this is to change
333 if (this.vecReplaceInSQL.get(k).toString().equalsIgnoreCase("true")) {
334 String sPreNameSu = this.vecVariablePrefix.get(k).toString()
335 + this.vecVariableName.get(k).toString()
336 + this.vecVariableSufix.get(k).toString();
337 // if costant is variable
338 // if (value.equalsIgnoreCase(sPreNameSu)) {
339 // value = this.vecVariableValue.get(k).toString();
340 // }
341 value = Utils.replaceAll(value, sPreNameSu, this.vecVariableValue.get(k).toString());
342 }
343 }
344 }
345 }
346 // + 4 allows space for one entitiy reference.
347 // If there's more than that, then the StringBuffer
348 // will automatically expand
349 // Need to use character references if the encoding
350 // can't support the character
351 StringBuffer encodedValue = new StringBuffer(value.length()
352 + 4);
353 for (int j = 0; j < value.length(); j++) {
354 char c = value.charAt(j);
355 if (c == '&')
356 encodedValue.append("&");
357 else if (c == '<')
358 encodedValue.append("<");
359 else if (c == '>')
360 encodedValue.append(">");
361 else if (c == '\'')
362 encodedValue.append("'");
363 else
364 encodedValue.append(c);
365 }
366 out.write(encodedValue.toString());
367 out.write("'");
368 }
369 out.write(">");
370 }
371
372 else {
373 out.write("<" + name);
374 for (int i = 0; i < atts.getLength(); i++) {
375 out.write(" ");
376 out.write(atts.getName(i));
377 out.write("='");
378 String value = atts.getValue(i);
379 // + 4 allows space for one entitiy reference.
380 // If there's more than that, then the StringBuffer
381 // will automatically expand
382 // Need to use character references if the encoding
383 // can't support the character
384 StringBuffer encodedValue = new StringBuffer(value.length()
385 + 4);
386 for (int j = 0; j < value.length(); j++) {
387 char c = value.charAt(j);
388 if (c == '&')
389 encodedValue.append("&");
390 else if (c == '<')
391 encodedValue.append("<");
392 else if (c == '>')
393 encodedValue.append(">");
394 else if (c == '\'')
395 encodedValue.append("'");
396 else
397 encodedValue.append(c);
398 }
399 out.write(encodedValue.toString());
400 out.write("'");
401 }
402 out.write(">");
403 }
404
405 } catch (IOException e) {
406 System.err.println(e);
407
408 }
409 }
410
411 /*** Characters.
412 * @param ch is character array
413 * @param start is int
414 * @param length is length
415 * @throws SAXException
416 */
417 public void characters (char ch[], int start, int length) throws SAXException {
418 try {
419 String s = new String(ch, start, length);
420 if (this.bSQL) {
421 // going throw vector
422 for (int k = 0; k < this.vecReplaceInJDBC.size(); k++) {
423 // if this is to change
424 if (this.vecReplaceInSQL.get(k).toString().equalsIgnoreCase("true")) {
425 String sPreNameSu = this.vecVariablePrefix.get(k).toString()
426 + this.vecVariableName.get(k).toString() +
427 this.vecVariableSufix.get(k).toString();
428 int j = s.indexOf(sPreNameSu);
429 // if costant is variable
430 while (j != -1) {
431 s = s.substring(0, j) + this.vecVariableValue.get(k).toString()
432 + s.substring(j + sPreNameSu.length(),
433 s.length());
434 j = s.indexOf(sPreNameSu);
435 }
436 }
437 }
438 }
439 // + 4 allows space for one entitiy reference.
440 // If there's more than that, then the StringBuffer
441 // will automatically expand
442 // Need to use character references if the encoding
443 // can't support the character
444 StringBuffer encodedValue = new StringBuffer(s.length()
445 + 4);
446 for (int j = 0; j < s.length(); j++) {
447 char c = s.charAt(j);
448 if (c == '&')
449 encodedValue.append("&");
450 else if (c == '<')
451 encodedValue.append("<");
452 else if (c == '>')
453 encodedValue.append(">");
454 else if (c == '\'')
455 encodedValue.append("'");
456 else
457 encodedValue.append(c);
458 }
459 out.write(encodedValue.toString());
460
461 } catch (Exception e) {
462 System.err.println(e);
463 }
464 }
465
466 /*** Ignorable whitespace.
467 * @param ch is character array
468 * @param start is int
469 * @param length is length
470 * @throws SAXException
471 */
472 public void ignorableWhitespace (char ch[], int start, int length) {
473 try {
474 this.characters(ch, start, length);
475 } catch (SAXException e) {
476 System.err.println(e);
477 }
478 }
479
480 /*** End element.
481 * @param name is name of the tag
482 */
483 public void endElement (String name) {
484 if (!name.equalsIgnoreCase("include") && !name.equalsIgnoreCase("definitionInclude")) {
485 try {
486 out.write("</");
487 out.write(name);
488 out.write(">");
489 } catch (IOException e) {
490 System.err.println(e);
491 }
492 }
493 }
494
495 /*** End document. */
496 public void endDocument () {
497 try {
498 out.flush();
499 } catch (IOException e) {
500 System.err.println(e);
501 }
502 }
503
504 //
505 // ErrorHandler methods
506 //
507 /*** Warning.
508 * @param ex is SAXParseException object
509 */
510 public void warning (SAXParseException ex) {
511 System.err.println("[Warning] " + getLocationString(ex) + ": " + ex.getMessage());
512 }
513
514 /*** Error.
515 * @param ex is SAXParseException object
516 */
517 public void error (SAXParseException ex) {
518 System.err.println("[Error] " + getLocationString(ex) + ": " + ex.getMessage());
519 }
520
521 /*** Fatal error.
522 * @param ex is SAXParseException object
523 * @throws SAXException
524 */
525 public void fatalError (SAXParseException ex) throws SAXException {
526 System.err.println("[Fatal Error] " + getLocationString(ex) + ": " +
527 ex.getMessage());
528 throw ex;
529 }
530
531 /*** Returns a string of the location.
532 * @param ex is SAXParseException object
533 * @return string
534 */
535 private String getLocationString (SAXParseException ex) {
536 StringBuffer str = new StringBuffer();
537 String systemId = ex.getSystemId();
538 if (systemId != null) {
539 int index = systemId.lastIndexOf('/');
540 if (index != -1)
541 systemId = systemId.substring(index + 1);
542 str.append(systemId);
543 }
544 str.append(':');
545 str.append(ex.getLineNumber());
546 str.append(':');
547 str.append(ex.getColumnNumber());
548 return str.toString();
549 }
550 }
551
552
This page was automatically generated by Maven