View Javadoc
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.OutputStreamWriter; 33 import java.io.OutputStream; 34 import java.io.Writer; 35 import java.io.UnsupportedEncodingException; 36 import java.io.IOException; 37 import java.io.File; 38 import org.xml.sax.XMLReader; 39 import org.xml.sax.helpers.XMLReaderFactory; 40 import org.xml.sax.AttributeList; 41 import org.xml.sax.HandlerBase; 42 import org.xml.sax.InputSource; 43 import org.xml.sax.SAXException; 44 import org.xml.sax.SAXParseException; 45 import org.apache.xerces.parsers.SAXParser; 46 import java.io.ByteArrayOutputStream; 47 import java.io.ByteArrayInputStream; 48 import java.util.Vector; 49 50 51 /*** 52 * LoadVariable.java 53 * This class parses a document (OutputStream) and writes the documents 54 * contents back to standard output. This class changes variables defined in variable 55 * tags. 56 */ 57 public class LoadVariable extends HandlerBase { 58 private Writer out; 59 private String encoding; 60 private int level = 0; 61 private Vector vecVariableName = new Vector(); 62 private Vector vecVariableValue = new Vector(); 63 private Vector vecVariablePrefix = new Vector(); 64 private Vector vecVariableSufix = new Vector(); 65 private Vector vecVariableOverride = new Vector(); 66 private Vector vecReplaceInConstants = new Vector(); 67 private Vector vecReplaceInSQL = new Vector(); 68 private Vector vecReplaceInJDBC = new Vector(); 69 private ByteArrayOutputStream streamToParse = new ByteArrayOutputStream(); 70 private boolean bSQL = false; 71 private boolean bJDBC = false; 72 private boolean bVarInConstant = false; 73 74 /*** Main program entry point. */ 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 * @param uri - Name of XML file. 122 */ 123 public void parseURI () { 124 try { 125 SAXParser parser = new SAXParser(); 126 parser.setDocumentHandler(this); 127 parser.setErrorHandler(this); 128 parser.parse(new InputSource(new ByteArrayInputStream(this.streamToParse.toByteArray()))); 129 } catch (Exception e) { 130 System.err.println(e); 131 } 132 } 133 134 /*** Processing instruction. */ 135 public void processingInstruction (String target, String data) { 136 try { 137 out.write("<?"); 138 out.write(target); 139 if (data != null && data.length() > 0) { 140 out.write(' '); 141 out.write(data); 142 } 143 out.write("?>"); 144 } catch (IOException e) { 145 System.err.println(e); 146 } 147 } 148 149 /*** Start document. */ 150 public void startDocument () { 151 if (level == 0) { 152 try { 153 out.write("<?xml version=\"1.0\"?>\r\n"); 154 } catch (IOException e) { 155 System.err.println(e); 156 } 157 } 158 } 159 160 /*** Start element. */ 161 public void startElement (String name, AttributeList atts) { 162 try { 163 if (name.equalsIgnoreCase("constantColumn")) { 164 out.write("<" + name); 165 for (int i = 0; i < atts.getLength(); i++) { 166 out.write(" "); 167 out.write(atts.getName(i)); 168 out.write("='"); 169 String value = atts.getValue(i); 170 if (atts.getName(i).equalsIgnoreCase("constantValue")) { 171 // checking, and if Variable exists changing 172 if (bVarInConstant) { 173 // going throw vector 174 for (int k = 0; k < this.vecReplaceInConstants.size(); k++) { 175 // if this is to change 176 if (this.vecReplaceInConstants.get(k).toString().equalsIgnoreCase("true")) { 177 String sPreNameSu = this.vecVariablePrefix.get(k).toString() 178 + this.vecVariableName.get(k).toString() 179 + this.vecVariableSufix.get(k).toString(); 180 // if costant is variable 181 if (value.equalsIgnoreCase(sPreNameSu)) { 182 value = this.vecVariableValue.get(k).toString(); 183 } 184 } 185 } 186 } 187 } 188 // + 4 allows space for one entitiy reference. 189 // If there's more than that, then the StringBuffer 190 // will automatically expand 191 // Need to use character references if the encoding 192 // can't support the character 193 StringBuffer encodedValue = new StringBuffer(value.length() 194 + 4); 195 for (int j = 0; j < value.length(); j++) { 196 char c = value.charAt(j); 197 if (c == '&') 198 encodedValue.append("&"); 199 else if (c == '<') 200 encodedValue.append("<"); 201 else if (c == '>') 202 encodedValue.append(">"); 203 else if (c == '\'') 204 encodedValue.append("'"); 205 else 206 encodedValue.append(c); 207 } 208 out.write(encodedValue.toString()); 209 out.write("'"); 210 } 211 out.write(">"); 212 } 213 else if (name.equalsIgnoreCase("jdbcSourceParameter")) { 214 out.write("<" + name); 215 for (int i = 0; i < atts.getLength(); i++) { 216 out.write(" "); 217 out.write(atts.getName(i)); 218 out.write("='"); 219 String value = atts.getValue(i); 220 if (atts.getName(i).equalsIgnoreCase("value")) { 221 // checking, and if Variable exists changing 222 if (bVarInConstant) { 223 // going throw vector 224 for (int k = 0; k < this.vecReplaceInJDBC.size(); k++) { 225 // if this is to change 226 if (this.vecReplaceInJDBC.get(k).toString().equalsIgnoreCase("true")) { 227 String sPreNameSu = this.vecVariablePrefix.get(k).toString() 228 + this.vecVariableName.get(k).toString() 229 + this.vecVariableSufix.get(k).toString(); 230 // if costant is variable 231 if (value.equalsIgnoreCase(sPreNameSu)) { 232 value = this.vecVariableValue.get(k).toString(); 233 } 234 } 235 } 236 } 237 } 238 // + 4 allows space for one entitiy reference. 239 // If there's more than that, then the StringBuffer 240 // will automatically expand 241 // Need to use character references if the encoding 242 // can't support the character 243 StringBuffer encodedValue = new StringBuffer(value.length() 244 + 4); 245 for (int j = 0; j < value.length(); j++) { 246 char c = value.charAt(j); 247 if (c == '&') 248 encodedValue.append("&"); 249 else if (c == '<') 250 encodedValue.append("<"); 251 else if (c == '>') 252 encodedValue.append(">"); 253 else if (c == '\'') 254 encodedValue.append("'"); 255 else 256 encodedValue.append(c); 257 } 258 out.write(encodedValue.toString()); 259 out.write("'"); 260 } 261 out.write(">"); 262 } 263 else if (name.equalsIgnoreCase("jdbcTargetParameter")) { 264 out.write("<" + name); 265 for (int i = 0; i < atts.getLength(); i++) { 266 out.write(" "); 267 out.write(atts.getName(i)); 268 out.write("='"); 269 String value = atts.getValue(i); 270 if (atts.getName(i).equalsIgnoreCase("value")) { 271 // checking, and if Variable exists changing 272 if (bJDBC) { 273 // going throw vector 274 for (int k = 0; k < this.vecReplaceInJDBC.size(); k++) { 275 // if this is to change 276 if (this.vecReplaceInJDBC.get(k).toString().equalsIgnoreCase("true")) { 277 String sPreNameSu = this.vecVariablePrefix.get(k).toString() 278 + this.vecVariableName.get(k).toString() 279 + this.vecVariableSufix.get(k).toString(); 280 // if costant is variable 281 if (value.equalsIgnoreCase(sPreNameSu)) { 282 value = this.vecVariableValue.get(k).toString(); 283 } 284 } 285 } 286 } 287 } 288 // + 4 allows space for one entitiy reference. 289 // If there's more than that, then the StringBuffer 290 // will automatically expand 291 // Need to use character references if the encoding 292 // can't support the character 293 StringBuffer encodedValue = new StringBuffer(value.length() 294 + 4); 295 for (int j = 0; j < value.length(); j++) { 296 char c = value.charAt(j); 297 if (c == '&') 298 encodedValue.append("&"); 299 else if (c == '<') 300 encodedValue.append("<"); 301 else if (c == '>') 302 encodedValue.append(">"); 303 else if (c == '\'') 304 encodedValue.append("'"); 305 else 306 encodedValue.append(c); 307 } 308 out.write(encodedValue.toString()); 309 out.write("'"); 310 } 311 out.write(">"); 312 } 313 else if (name.equalsIgnoreCase("importDefinition")) { 314 out.write("<" + name); 315 for (int i = 0; i < atts.getLength(); i++) { 316 out.write(" "); 317 out.write(atts.getName(i)); 318 out.write("='"); 319 String value = atts.getValue(i); 320 if (atts.getName(i).equalsIgnoreCase("selectStatement")) { 321 // checking, and if Variable exists changing 322 if (bSQL) { 323 // going throw vector 324 for (int k = 0; k < this.vecReplaceInSQL.size(); k++) { 325 // if this is to change 326 if (this.vecReplaceInSQL.get(k).toString().equalsIgnoreCase("true")) { 327 String sPreNameSu = this.vecVariablePrefix.get(k).toString() 328 + this.vecVariableName.get(k).toString() 329 + this.vecVariableSufix.get(k).toString(); 330 // if costant is variable 331 if (value.equalsIgnoreCase(sPreNameSu)) { 332 value = this.vecVariableValue.get(k).toString(); 333 } 334 } 335 } 336 } 337 } 338 // + 4 allows space for one entitiy reference. 339 // If there's more than that, then the StringBuffer 340 // will automatically expand 341 // Need to use character references if the encoding 342 // can't support the character 343 StringBuffer encodedValue = new StringBuffer(value.length() 344 + 4); 345 for (int j = 0; j < value.length(); j++) { 346 char c = value.charAt(j); 347 if (c == '&') 348 encodedValue.append("&"); 349 else if (c == '<') 350 encodedValue.append("<"); 351 else if (c == '>') 352 encodedValue.append(">"); 353 else if (c == '\'') 354 encodedValue.append("'"); 355 else 356 encodedValue.append(c); 357 } 358 out.write(encodedValue.toString()); 359 out.write("'"); 360 } 361 out.write(">"); 362 } 363 364 else { 365 out.write("<" + name); 366 for (int i = 0; i < atts.getLength(); i++) { 367 out.write(" "); 368 out.write(atts.getName(i)); 369 out.write("='"); 370 String value = atts.getValue(i); 371 // + 4 allows space for one entitiy reference. 372 // If there's more than that, then the StringBuffer 373 // will automatically expand 374 // Need to use character references if the encoding 375 // can't support the character 376 StringBuffer encodedValue = new StringBuffer(value.length() 377 + 4); 378 for (int j = 0; j < value.length(); j++) { 379 char c = value.charAt(j); 380 if (c == '&') 381 encodedValue.append("&"); 382 else if (c == '<') 383 encodedValue.append("<"); 384 else if (c == '>') 385 encodedValue.append(">"); 386 else if (c == '\'') 387 encodedValue.append("'"); 388 else 389 encodedValue.append(c); 390 } 391 out.write(encodedValue.toString()); 392 out.write("'"); 393 } 394 out.write(">"); 395 } 396 397 } catch (IOException e) { 398 System.err.println(e); 399 400 } 401 } 402 403 /*** Characters. */ 404 public void characters (char ch[], int start, int length) throws SAXException { 405 try { 406 String s = new String(ch, start, length); 407 if (bVarInConstant) { 408 // going throw vector 409 for (int k = 0; k < this.vecReplaceInJDBC.size(); k++) { 410 // if this is to change 411 if (this.vecReplaceInSQL.get(k).toString().equalsIgnoreCase("true")) { 412 String sPreNameSu = this.vecVariablePrefix.get(k).toString() 413 + this.vecVariableName.get(k).toString() + 414 this.vecVariableSufix.get(k).toString(); 415 int j = s.indexOf(sPreNameSu); 416 // if costant is variable 417 while (j != -1) { 418 s = s.substring(0, j) + this.vecVariableValue.get(k).toString() 419 + s.substring(j + sPreNameSu.length(), 420 s.length()); 421 j = s.indexOf(sPreNameSu); 422 } 423 } 424 } 425 } 426 out.write(s); 427 } catch (Exception e) { 428 System.err.println(e); 429 } 430 } 431 432 /*** Ignorable whitespace. */ 433 public void ignorableWhitespace (char ch[], int start, int length) { 434 try { 435 this.characters(ch, start, length); 436 } catch (SAXException e) { 437 System.err.println(e); 438 } 439 } 440 441 /*** End element. */ 442 public void endElement (String name) { 443 if (!name.equalsIgnoreCase("include") && !name.equalsIgnoreCase("definitionInclude")) { 444 try { 445 out.write("</"); 446 out.write(name); 447 out.write(">"); 448 } catch (IOException e) { 449 System.err.println(e); 450 } 451 } 452 } 453 454 /*** End document. */ 455 public void endDocument () { 456 try { 457 out.flush(); 458 } catch (IOException e) { 459 System.err.println(e); 460 } 461 } 462 463 // 464 // ErrorHandler methods 465 // 466 /*** Warning. */ 467 public void warning (SAXParseException ex) { 468 System.err.println("[Warning] " + getLocationString(ex) + ": " + ex.getMessage()); 469 } 470 471 /*** Error. */ 472 public void error (SAXParseException ex) { 473 System.err.println("[Error] " + getLocationString(ex) + ": " + ex.getMessage()); 474 } 475 476 /*** Fatal error. */ 477 public void fatalError (SAXParseException ex) throws SAXException { 478 System.err.println("[Fatal Error] " + getLocationString(ex) + ": " + 479 ex.getMessage()); 480 throw ex; 481 } 482 483 /*** Returns a string of the location. */ 484 private String getLocationString (SAXParseException ex) { 485 StringBuffer str = new StringBuffer(); 486 String systemId = ex.getSystemId(); 487 if (systemId != null) { 488 int index = systemId.lastIndexOf('/'); 489 if (index != -1) 490 systemId = systemId.substring(index + 1); 491 str.append(systemId); 492 } 493 str.append(':'); 494 str.append(ex.getLineNumber()); 495 str.append(':'); 496 str.append(ex.getColumnNumber()); 497 return str.toString(); 498 } 499 } 500 501

This page automatically generated by Maven