1 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 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 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 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 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 System.err.println(e); 130 } 131 } 132 133 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 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 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 if (bVarInConstant) { 178 for (int k = 0; k < this.vecReplaceInConstants.size(); k++) { 180 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 (value.equalsIgnoreCase(sPreNameSu)) { 187 value = this.vecVariableValue.get(k).toString(); 188 } 189 } 190 } 191 } 192 } 193 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 if (bJDBC) { 228 for (int k = 0; k < this.vecReplaceInJDBC.size(); k++) { 230 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 value = Utils.replaceAll(value, sPreNameSu, this.vecVariableValue.get(k).toString()); 240 } 241 } 242 } 243 } 244 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 if (bJDBC) { 279 for (int k = 0; k < this.vecReplaceInJDBC.size(); k++) { 281 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 value = Utils.replaceAll(value, sPreNameSu, this.vecVariableValue.get(k).toString()); 291 } 292 } 293 } 294 } 295 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 if (bSQL) { 330 for (int k = 0; k < this.vecReplaceInSQL.size(); k++) { 332 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 value = Utils.replaceAll(value, sPreNameSu, this.vecVariableValue.get(k).toString()); 342 } 343 } 344 } 345 } 346 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 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 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 for (int k = 0; k < this.vecReplaceInJDBC.size(); k++) { 423 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 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 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 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 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 496 public void endDocument () { 497 try { 498 out.flush(); 499 } catch (IOException e) { 500 System.err.println(e); 501 } 502 } 503 504 510 public void warning (SAXParseException ex) { 511 System.err.println("[Warning] " + getLocationString(ex) + ": " + ex.getMessage()); 512 } 513 514 517 public void error (SAXParseException ex) { 518 System.err.println("[Error] " + getLocationString(ex) + ": " + ex.getMessage()); 519 } 520 521 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 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 553 | Popular Tags |