1 22 23 24 package org.webdocwf.util.loader; 25 26 import java.io.BufferedInputStream ; 27 import java.io.BufferedReader ; 28 import java.io.File ; 29 import java.io.IOException ; 30 import java.io.InputStream ; 31 import java.io.InputStreamReader ; 32 import java.io.OutputStream ; 33 import java.io.OutputStreamWriter ; 34 import java.io.UnsupportedEncodingException ; 35 import java.io.Writer ; 36 import java.net.MalformedURLException ; 37 import java.net.URL ; 38 import java.net.URLConnection ; 39 40 import org.apache.xerces.parsers.SAXParser; 41 import org.webdocwf.util.loader.logging.Logger; 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 53 public class LoaderXIncluder 54 extends HandlerBase { 55 private Writer out; 56 private String encoding; 57 private int level = 0; 58 private int counter1 = 1; 60 61 private int counter2 = 0; 63 64 private int counter3 = 0; 66 67 private int counter4 = 0; 69 70 private int counter5 = 0; 72 73 private int counter6 = 0; 75 76 private boolean firstPartXml = true; 77 78 private String loaderJobPath=""; 79 80 83 public static void main(String argv[]) { 84 if (argv.length == 0) { 85 System.out.println("Usage: java LoaderXIncluder uri"); 86 System.out.println(" where uri is the URI of your XML document."); 87 System.out.println(" Sample: java LoaderXIncluder demo.xml"); 88 System.exit(1); 89 } 90 LoaderXIncluder s1 = new LoaderXIncluder(); 91 s1.parseURI(argv[0]); 92 } 93 94 97 public LoaderXIncluder() { 98 } 99 100 106 public LoaderXIncluder(OutputStream out, String encoding) throws UnsupportedEncodingException { 107 this.out = new OutputStreamWriter (out, encoding); 108 this.encoding = encoding; 109 } 110 111 116 public LoaderXIncluder(OutputStream out) { 117 try { 118 this.out = new OutputStreamWriter (out, "UTF8"); 119 this.encoding = "UTF-8"; 120 } 121 catch (UnsupportedEncodingException e) {} 122 } 123 124 129 public void parseURI(String uri) { 130 try { 131 SAXParser parser = new SAXParser(); 132 parser.setDocumentHandler(this); 133 parser.setEntityResolver(this); 134 parser.setFeature("http://xml.org/sax/features/validation", false); 135 parser.setErrorHandler(this); 136 Logger.getCentralLogger().write("normal", "Loader loads XML file : " + uri); 138 139 File f = new File (uri); 140 this.loaderJobPath=f.getCanonicalFile().getParent(); 141 URL xml = f.toURL(); 142 143 InputStreamReader xmlStream = new InputStreamReader (xml.openStream()); 144 BufferedReader reader = new BufferedReader (xmlStream); 145 String intro = ""; 146 while ( (intro = reader.readLine()) != null) { 147 if (intro.indexOf("include") == -1) 148 counter6++; 149 else 150 break; 151 } 152 counter6 = counter6 - 4; 153 LocationOfException.setJdbcNumber(counter6); 154 reader.close(); 155 xmlStream.close(); 156 parser.parse(uri); 157 } 158 catch (Exception e) { 159 e.printStackTrace(); 161 Logger.getCentralLogger().write("normal", e.getMessage()); 162 } 163 } 164 165 boolean hasDTD = false; 166 171 public InputSource resolveEntity(String publicId, String systemId) { 172 hasDTD = true; 173 try { 174 if (systemId != null) { 175 out.write("<!DOCTYPE loaderJob SYSTEM \"" + systemId + "\">" + "\n"); 176 counter4++; 177 } else if (publicId != null) { 178 out.write("<!DOCTYPE loaderJob PUBLIC \"" + publicId + "\">" + "\n"); 179 counter4++; 180 } 181 } 182 catch (Exception e) { 183 System.err.println(e); 184 } 185 return null; 186 } 187 188 192 public void processingInstruction(String target, String data) { 193 try { 194 out.write("<?"); 195 out.write(target); 196 if (data != null && data.length() > 0) { 197 out.write(' '); 198 out.write(data); 199 } 200 out.write("?>"); 201 counter4++; 202 } 203 catch (IOException e) { 204 System.err.println(e); 205 } 206 } 207 208 209 public void startDocument() { 210 if (level == 0) { 211 try { 212 out.write("<?xml version=\"1.0\"?>\r\n"); 213 counter4++; 214 } 215 catch (IOException e) { 216 System.err.println(e); 217 } 218 LocationOfException.setIntroNumber(counter4); 219 } 220 } 221 222 226 public void startElement(String name, AttributeList atts) { 227 try { 228 if (name.equalsIgnoreCase("loaderJob")) { 230 258 } 259 if (name.equalsIgnoreCase("include")) { 260 String href = atts.getValue("href"); 261 if (href == null) { 262 System.out.println("Missing href attribute in include Element"); 263 System.exit(1); 264 } 265 266 File file=new File (href); 267 if(!file.isAbsolute()){ 268 String tmp=this.loaderJobPath + System.getProperty("file.separator") + href; 269 File fileHref=new File (tmp); 270 href = fileHref.getCanonicalPath(); 271 } 272 273 String parse = atts.getValue("parse"); 274 if (parse == null) 275 parse = "xml"; 276 if (parse.equals("text")) { 277 String encoding = atts.getValue("encoding"); 278 includeTextDocument(href, encoding); 279 } else if (parse.equals("xml")) { 280 level++; 281 includeXMLDocument(href); 282 level--; 283 } else { 284 System.out.println("Illegal value for parse attribute: " + parse); 285 Logger.getCentralLogger().write("normal", "Illegal value for parse attribute: " + parse); 286 System.exit(1); 287 } 288 } else if (name.equalsIgnoreCase("definitionInclude")) { 289 } else { 290 out.write("<" + name); 291 if (name.equals("loaderJob")) { 292 out.write(" xmlns='http://www.objectweb.org'"); 293 out.write(" xmlns:xsi=\'http://www.w3.org/2001/XMLSchema-instance\'"); 294 out.write(" xsi:schemaLocation='http://www.objectweb.org http://octopus.objectweb.org/doc/XmlTransform/xml/xmlschema/loaderJob.xsd'"); 295 out.write(" "); 296 } 297 for (int i = 0; i < atts.getLength(); i++) { 306 out.write(" "); 307 out.write(atts.getName(i)); 308 out.write("='"); 309 String value = atts.getValue(i); 310 StringBuffer encodedValue = new StringBuffer (value.length() + 4); 316 for (int j = 0; j < value.length(); j++) { 317 char c = value.charAt(j); 318 if (c == '&') 319 encodedValue.append("&"); 320 else if (c == '<') 321 encodedValue.append("<"); 322 else if (c == '>') 323 encodedValue.append(">"); 324 else if (c == '\'') 325 encodedValue.append("'"); 326 else 327 encodedValue.append(c); 328 } 329 out.write(encodedValue.toString()); 330 out.write("'"); 331 } 332 out.write(">"); 333 } 334 } 335 catch (IOException e) { 336 System.err.println(e); 337 Logger.getCentralLogger().write("normal", e.getMessage()); 338 } 339 catch (SAXException ex) { 340 System.err.println(ex); 341 Logger.getCentralLogger().write("normal", ex.getMessage()); 342 } 343 } 344 345 351 public void characters(char ch[], int start, int length) throws SAXException { 352 try { 353 for (int i = 0; i < length; i++) { 354 char c = ch[start + i]; 355 if (c == '&') 356 out.write("&"); 357 else if (c == '<') 358 out.write("<"); 359 else if (c == '\n') { 360 counter2++; 361 out.write(c); 362 } else 363 out.write(c); 364 } 365 } 366 catch (IOException e) { 367 System.err.println(e); 368 Logger.getCentralLogger().write("normal", e.getMessage()); 369 } 370 } 371 372 377 public void ignorableWhitespace(char ch[], int start, int length) { 378 try { 379 this.characters(ch, start, length); 380 } 381 catch (SAXException e) { 382 System.err.println(e); 383 Logger.getCentralLogger().write("normal", e.getMessage()); 384 } 385 } 386 387 390 public void endElement(String name) { 391 if (!name.equalsIgnoreCase("include") && !name.equalsIgnoreCase("definitionInclude")) { 392 try { 395 out.write("</"); 396 out.write(name); 397 out.write(">"); 398 } 399 catch (IOException e) { 400 System.err.println(e); 401 Logger.getCentralLogger().write("normal", e.getMessage()); 402 } 403 } 404 } 405 406 407 public void endDocument() { 408 try { 409 out.flush(); 410 } 411 catch (IOException e) { 412 System.err.println(e); 413 Logger.getCentralLogger().write("normal", e.getMessage()); 414 } 415 } 416 417 423 public void warning(SAXParseException ex) { 424 System.err.println("[Warning] " + getLocationString(ex) + ": " + ex.getMessage()); 425 Logger.getCentralLogger().write("normal", 426 "[Warning] " + getLocationString(ex) + ": " + ex.getMessage()); 427 } 428 429 432 public void error(SAXParseException ex) { 433 System.err.println("[Error] " + getLocationString(ex) + ": " + ex.getMessage()); 434 Logger.getCentralLogger().write("normal", 435 "[Error] " + getLocationString(ex) + ": " + ex.getMessage()); 436 } 437 438 442 public void fatalError(SAXParseException ex) throws SAXException { 443 System.err.println("[Fatal Error] " + getLocationString(ex) + ": " + ex.getMessage()); 444 Logger.getCentralLogger().write("normal", 445 "[Fatal Error] " + getLocationString(ex) + ": " + ex.getMessage()); 446 throw ex; 447 } 448 449 453 private String getLocationString(SAXParseException ex) { 454 StringBuffer str = new StringBuffer (); 455 String systemId = ex.getSystemId(); 456 if (systemId != null) { 457 int index = systemId.lastIndexOf('/'); 458 if (index != -1) 459 systemId = systemId.substring(index + 1); 460 str.append(systemId); 461 } 462 str.append(':'); 463 str.append(ex.getLineNumber()); 464 str.append(':'); 465 str.append(ex.getColumnNumber()); 466 return str.toString(); 467 } 468 469 483 private void includeTextDocument(String url, String encoding) throws SAXException { 484 if (encoding == null || encoding.trim().equals("")) 485 encoding = "UTF-8"; 486 File sourceXml = new File (url); 487 URL source; 488 counter2 = 1; 489 try { 490 source = sourceXml.toURL(); 491 } 492 catch (MalformedURLException e) { 493 Logger.getCentralLogger().write("normal", "Unresolvable URL :" + e.getMessage()); 494 throw new SAXException ("Unresolvable URL :", e); 495 } 496 try { 497 URLConnection uc = source.openConnection(); 498 String encodingFromHeader = uc.getContentEncoding(); 499 if (encodingFromHeader != null) 500 encoding = encodingFromHeader; 501 InputStream in = new BufferedInputStream (uc.getInputStream()); 502 InputStreamReader reader = new InputStreamReader (in, encoding); 503 char[] c = new char[1024]; 504 while (true) { 505 int charsRead = reader.read(c, 0, 1024); 506 if (charsRead == -1) 507 break; 508 if (charsRead > 0) 509 this.characters(c, 0, charsRead); 510 } 511 counter2 = counter2 + 4; 512 LocationOfException.setFileLineNumber(counter2, url); 513 counter2 = 0; 515 } 516 catch (UnsupportedEncodingException e) { 517 Logger.getCentralLogger().write("normal", "Unsupported encoding: " + encoding); 518 throw new SAXException ("Unsupported encoding: " + encoding, e); 519 } 520 catch (IOException e) { 521 Logger.getCentralLogger().write("normal", "Document not found: " + source.toExternalForm()); 522 throw new SAXException ("Document not found: " + source.toExternalForm(), e); 523 } 524 } 525 526 537 private void includeXMLDocument(String url) throws SAXException { 538 File sourceXml = new File (url); 539 URL source; 540 InputStream includeXML = null; 541 try { 543 source = sourceXml.toURL(); 544 includeXML = source.openStream(); 545 InputStreamReader isr = new InputStreamReader (includeXML); 546 BufferedReader br = new BufferedReader (isr); 547 while (br.readLine() != null) 548 counter3++; 549 counter3 = counter3 + 2; 550 LocationOfException.setFileLineNumber(counter3, url); 551 counter3 = 0; 553 555 } 556 catch (MalformedURLException e) { 557 Logger.getCentralLogger().write("normal", "Unresolvable URL :" + e.getMessage()); 558 throw new SAXException ("Unresolvable URL :", e); 559 } 560 catch (IOException e) { 561 Logger.getCentralLogger().write("normal", "Error in opening input stream :" + e.getMessage()); 562 throw new SAXException ("Error in opening input stream :", e); 563 } 564 565 SAXParser includeParser = new SAXParser(); 566 includeParser.setDocumentHandler(this); 567 includeParser.setEntityResolver(this); 568 includeParser.setErrorHandler(this); 569 try { 570 includeParser.parse(url); 571 } 572 catch (Exception e) { 573 System.err.println(e); 574 Logger.getCentralLogger().write("normal", e.getMessage()); 575 } 576 } 577 578 579 } 580 | Popular Tags |