1 package org.objectweb.celtix.tools.processors.wsdl2.validators; 2 3 import java.io.ByteArrayInputStream ; 4 import java.io.ByteArrayOutputStream ; 5 import java.io.File ; 6 import java.io.FilenameFilter ; 7 import java.io.IOException ; 8 import java.io.InputStream ; 9 import java.net.MalformedURLException ; 10 import java.net.URL ; 11 import java.util.ArrayList ; 12 import java.util.HashMap ; 13 import java.util.Iterator ; 14 import java.util.List ; 15 import java.util.Map ; 16 import java.util.Vector ; 17 18 import javax.wsdl.WSDLException; 19 import javax.wsdl.factory.WSDLFactory; 20 import javax.wsdl.xml.WSDLReader; 21 import javax.xml.namespace.QName ; 22 23 import org.w3c.dom.Document ; 24 import org.w3c.dom.Element ; 25 import org.w3c.dom.NamedNodeMap ; 26 import org.w3c.dom.Node ; 27 import org.w3c.dom.NodeList ; 28 29 import org.xml.sax.EntityResolver ; 30 import org.xml.sax.ErrorHandler ; 31 import org.xml.sax.InputSource ; 32 import org.xml.sax.SAXException ; 33 import org.xml.sax.SAXParseException ; 34 35 import com.sun.org.apache.xerces.internal.parsers.DOMParser; 36 import com.sun.org.apache.xerces.internal.parsers.XMLGrammarPreparser; 37 import com.sun.org.apache.xerces.internal.xni.XMLResourceIdentifier; 38 import com.sun.org.apache.xerces.internal.xni.XNIException; 39 import com.sun.org.apache.xerces.internal.xni.grammars.XMLGrammarDescription; 40 import com.sun.org.apache.xerces.internal.xni.grammars.XSGrammar; 41 import com.sun.org.apache.xerces.internal.xni.parser.XMLEntityResolver; 42 import com.sun.org.apache.xerces.internal.xni.parser.XMLErrorHandler; 43 import com.sun.org.apache.xerces.internal.xni.parser.XMLInputSource; 44 import com.sun.org.apache.xerces.internal.xni.parser.XMLParseException; 45 import com.sun.org.apache.xerces.internal.xs.XSModel; 46 import com.sun.org.apache.xml.internal.serialize.OutputFormat; 47 import com.sun.org.apache.xml.internal.serialize.XMLSerializer; 48 49 import org.objectweb.celtix.tools.common.ToolException; 50 import org.objectweb.celtix.tools.common.WSDLConstants; 51 import org.objectweb.celtix.tools.utils.LineNumDOMParser; 52 import org.objectweb.celtix.tools.utils.URLFactory; 53 import org.objectweb.celtix.tools.utils.WSDLExtensionRegister; 54 55 public class SchemaWSDLValidator extends AbstractValidator { 56 57 protected String [] defaultSchemas; 58 protected String schemaLocation = "./"; 59 60 private DOMParser parser; 61 private XMLGrammarPreparser preparser; 62 63 private List <XSModel> xsmodelList = new Vector <XSModel>(); 64 65 private String wsdlsrc; 66 private String [] xsds; 67 private boolean isdeep; 68 69 private Document schemaValidatedDoc; 70 private Map <QName , List > msgPartsMap = new HashMap <QName , List >(); 71 private Map <QName , Map > portTypes = new HashMap <QName , Map >(); 72 private Map <QName , QName > bindingMap = new HashMap <QName , QName >(); 73 74 public SchemaWSDLValidator(String schemaDir) throws ToolException { 75 super(schemaDir); 76 schemaLocation = schemaDir; 77 defaultSchemas = getDefaultSchemas(); 78 init(); 79 } 80 81 public SchemaWSDLValidator(String schemaDir, String wsdl, String [] schemas, boolean deep) { 82 super(schemaDir); 83 schemaLocation = schemaDir; 84 defaultSchemas = getDefaultSchemas(); 85 init(); 86 wsdlsrc = wsdl; 87 xsds = schemas; 88 isdeep = deep; 89 } 90 91 public boolean isValid() { 92 return validate(wsdlsrc, xsds, isdeep); 93 } 94 95 public boolean validate(String wsdlsource, String [] schemas, boolean deep) throws ToolException { 96 String systemId = null; 97 try { 98 systemId = getWsdlUrl(wsdlsource); 99 } catch (IOException ioe) { 100 throw new ToolException(ioe); 101 } 102 103 return validate(new InputSource (systemId), schemas, deep); 104 105 } 106 107 private boolean validate(InputSource wsdlsource, String [] schemas, boolean deep) throws ToolException { 108 boolean isValid = false; 109 try { 110 schemas = addSchemas(defaultSchemas, schemas); 111 setExternalSchemaLocations(schemas); 112 StackTraceErrorHandler handler = setErrorHandler(); 113 schemaValidatedDoc = doValidation(wsdlsource, deep); 114 if (!handler.isValid()) { 116 ToolException ex = new ToolException(handler.getErrorMessages()); 117 throw ex; 118 } 119 120 WSDLFactory wsdlFactory; 121 try { 122 wsdlFactory = WSDLFactory.newInstance(); 123 WSDLReader reader = wsdlFactory.newWSDLReader(); 124 reader.setFeature("javax.wsdl.verbose", false); 125 WSDLExtensionRegister register = new WSDLExtensionRegister(wsdlFactory , reader); 126 register.registerExtenstions(); 127 def = reader.readWSDL(wsdlsource.getSystemId()); 128 } catch (WSDLException e) { 129 throw new ToolException("Can not create wsdl definition for " + wsdlsource.getSystemId()); 130 } 131 132 WSDLElementReferenceValidator wsdlRefValiadtor = new WSDLElementReferenceValidator(def, this); 133 isValid = wsdlRefValiadtor.isValid(); 134 135 if (!isValid) { 136 throw new ToolException(this.getErrorMessage()); 137 } 138 139 isValid = true; 140 141 } catch (IOException ioe) { 142 throw new ToolException("Can not get the wsdl " + wsdlsource.getSystemId() , ioe); 143 } catch (SAXException saxEx) { 144 throw new ToolException(saxEx); 145 146 } 147 return isValid; 148 } 149 150 public Document validate(InputStream wsdlsource, String [] schemas) throws Exception { 151 schemas = addSchemas(defaultSchemas, schemas); 152 setExternalSchemaLocations(schemas); 153 StackTraceErrorHandler handler = setErrorHandler(); 154 Document doc = doValidation(new InputSource (wsdlsource), false); 155 156 if (!handler.isValid()) { 157 throw new SAXException (handler.getErrorMessages()); 158 } 159 return doc; 160 } 161 162 public Document validate(String wsdlsource, String [] schemas) throws ToolException { 163 schemas = addSchemas(defaultSchemas, schemas); 164 Document doc = null; 165 try { 166 setExternalSchemaLocations(schemas); 167 setErrorHandler(); 169 doc = doValidation(new InputSource (getWsdlUrl(wsdlsource)), false); 170 174 } catch (Exception e) { 175 ToolException ex = new ToolException(e); 176 throw ex; 177 } 178 return doc; 179 } 180 181 void setErrorHandler(StackTraceErrorHandler handler) { 182 parser.setErrorHandler(handler); 183 184 preparser.setErrorHandler(handler); 185 } 186 187 private void init() throws ToolException { 188 try { 189 190 parser = new LineNumDOMParser(); 191 preparser = new XMLGrammarPreparser(); 192 parser.setFeature("http://xml.org/sax/features/validation", true); 193 parser.setFeature("http://apache.org/xml/features/validation/schema", true); 194 parser.setFeature("http://apache.org/xml/features/validation/schema-full-checking", true); 195 parser.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); 196 197 preparser.registerPreparser(XMLGrammarDescription.XML_SCHEMA, null); 198 preparser.setFeature("http://xml.org/sax/features/namespaces", true); 199 preparser.setFeature("http://xml.org/sax/features/validation", true); 200 preparser.setFeature("http://apache.org/xml/features/validation/schema", true); 201 preparser.setFeature("http://apache.org/xml/features/validation/schema-full-checking", true); 202 203 EntityResolver eresolver = new EntityResolver () { 204 public InputSource resolveEntity(String publicID, String systemID) throws SAXException , 205 IOException { 206 207 String url = resolveUrl(systemID); 208 if (url == null) { 209 String msg = "Can't resolve entity from publicID=" + publicID + ", systemID=" 210 + systemID; 211 212 throw new IOException (msg); 213 } 214 return new InputSource (url); 215 } 216 }; 217 218 parser.setEntityResolver(eresolver); 219 220 SchemaEntityResolver xmlResolver = new SchemaEntityResolver(); 221 preparser.setEntityResolver(xmlResolver); 222 } catch (SAXException sax) { 223 Throwable embedded = sax.getException(); 224 if (embedded == null) { 225 embedded = sax; 226 } 227 throw new ToolException(sax.getMessage()); 228 } 229 } 230 231 private String [] addSchemas(String [] defaults, String [] schemas) { 232 if (schemas == null || schemas.length == 0) { 233 return defaultSchemas; 234 } 235 String [] ss = new String [schemas.length + defaults.length]; 236 System.arraycopy(defaults, 0, ss, 0, defaults.length); 237 System.arraycopy(schemas, 0, ss, defaults.length, schemas.length); 238 return ss; 239 } 240 241 private Document doValidation(InputSource wsdlsource, boolean deep) throws IOException , SAXException , 242 ToolException { 243 244 byte[] bytes = getBytes(wsdlsource); 245 parser.parse(copyInputSource(wsdlsource, bytes)); 246 Document doc = parser.getDocument(); 247 248 String base = wsdlsource.getSystemId(); 249 if (doc == null) { 250 return null; 251 } 252 253 if (deep) { 254 NodeList nodes = doc.getDocumentElement().getElementsByTagNameNS(WSDLConstants.NS_WSDL, "import"); 255 for (int i = 0; i < nodes.getLength(); i++) { 256 Element el = (Element )nodes.item(i); 257 String ns = el.getAttribute("namespace"); 258 String loc = el.getAttribute("location"); 259 260 if (!"".equals(ns) && !"".equals(loc)) { 261 262 SchemaWSDLValidator validator = new SchemaWSDLValidator(schemaLocation); 263 validator.setErrorHandler(getErrorHandler()); 264 265 validator.validate(new InputSource (getWsdlUrl(base, loc)), null, true); 266 267 if (!getErrorHandler().isValid()) { 268 throw new SAXException (getErrorHandler().getErrorMessages()); 269 } 270 return doc; 271 272 } 273 } 274 } 275 StackTraceErrorHandler handler = getErrorHandler(); 276 if (handler.isValid()) { 277 doSchemaValidation(wsdlsource, bytes, doc, handler); 278 } 279 return doc; 280 } 281 282 private byte[] getBytes(InputSource source) throws IOException { 283 if (source.getByteStream() != null) { 284 int length = source.getByteStream().available(); 285 byte[] bytes = new byte[length]; 286 source.getByteStream().read(bytes); 287 return bytes; 288 } 289 return null; 290 } 291 292 private void doSchemaValidation(InputSource wsdlsource, byte[] bytes, Document doc, 293 StackTraceErrorHandler handler) throws IOException , SAXException { 294 if (isSchemaDocument(doc)) { 295 XSGrammar xsGrammer = (XSGrammar)preparser.preparseGrammar(XMLGrammarDescription.XML_SCHEMA, 296 copyInputSourceXML(wsdlsource, bytes)); 297 xsmodelList.add(xsGrammer.toXSModel()); 298 299 } else { 300 Map schemas = serializeSchemaElements(doc); 301 SchemaEntityResolver schemaResolver = (SchemaEntityResolver)preparser.getEntityResolver(); 302 schemaResolver.setSchemas(schemas); 303 Iterator it = schemas.keySet().iterator(); 304 while (it.hasNext()) { 305 String tns = (String )it.next(); 306 byte[] schemaBytes = (byte[])schemas.get(tns); 307 WSDLSchemaErrorHandler schemaHandler = new WSDLSchemaErrorHandler(handler, schemaBytes, doc 308 .getXmlEncoding()); 309 310 try { 311 preparser.setErrorHandler(schemaHandler); 312 313 XSGrammar xsGrammer = (XSGrammar)preparser 314 .preparseGrammar(XMLGrammarDescription.XML_SCHEMA, 315 copyInputSourceXML(wsdlsource, tns, schemaBytes, doc 316 .getXmlEncoding())); 317 xsmodelList.add(xsGrammer.toXSModel()); 318 319 } finally { 320 preparser.setErrorHandler(handler); 321 } 322 } 323 } 324 } 325 326 private boolean isSchemaDocument(Document doc) { 327 String tagName = doc.getDocumentElement().getTagName(); 328 int idx = tagName.lastIndexOf(':'); 329 if (idx != -1) { 330 tagName = tagName.substring(idx + 1, tagName.length()); 331 } 332 return "schema".equals(tagName); 333 } 334 335 private Map <String , byte[]> serializeSchemaElements(Document doc) throws IOException { 336 Map <String , byte[]> result = new HashMap <String , byte[]>(); 337 NodeList nodes = doc.getElementsByTagNameNS(WSDLConstants.NS_XMLNS, "schema"); 338 for (int x = 0; x < nodes.getLength(); x++) { 339 Node schemaNode = nodes.item(x); 340 Element schemaEl = (Element )schemaNode; 341 String tns = schemaEl.getAttribute("targetNamespace"); 342 343 boolean clone = true; 344 NamedNodeMap defAttrs = doc.getDocumentElement().getAttributes(); 345 for (int i = 0; i < defAttrs.getLength(); ++i) { 346 Node attr = defAttrs.item(i); 347 if (attr.getNodeName().startsWith("xmlns:")) { 348 if (!schemaEl.hasAttribute(attr.getNodeName()) && clone) { 349 schemaEl = (Element )(schemaNode.cloneNode(true)); 350 clone = false; 351 schemaEl.setAttribute(attr.getNodeName(), attr.getNodeValue()); 352 } 353 354 if (!schemaEl.hasAttribute(attr.getNodeName())) { 355 schemaEl.setAttribute(attr.getNodeName(), attr.getNodeValue()); 356 } 357 358 } 359 } 360 361 ByteArrayOutputStream os = new ByteArrayOutputStream (); 364 OutputFormat of = new OutputFormat(doc); 365 XMLSerializer ser = new XMLSerializer(os, of); 366 ser.serialize(schemaEl); 367 os.flush(); 368 os.close(); 369 result.put(tns, os.toByteArray()); 370 } 371 return result; 372 } 373 374 private InputSource copyInputSource(InputSource source, byte[] stream) { 375 InputSource is = new InputSource (source.getSystemId()); 376 377 is.setPublicId(source.getPublicId()); 378 is.setEncoding(source.getEncoding()); 379 if (stream != null) { 380 is.setByteStream(new ByteArrayInputStream (stream)); 381 } 382 383 return is; 384 } 385 386 private XMLInputSource copyInputSourceXML(InputSource source, byte[] stream) { 387 return copyInputSourceXML(source, stream, source.getEncoding()); 388 } 389 390 private XMLInputSource copyInputSourceXML(InputSource source, byte[] stream, String encoding) { 391 392 XMLInputSource ret = null; 393 if (stream == null) { 394 ret = new XMLInputSource(source.getPublicId(), source.getSystemId(), null); 395 } else { 396 ret = new XMLInputSource(source.getPublicId(), source.getSystemId(), null, 397 new ByteArrayInputStream (stream), encoding); 398 } 399 return ret; 400 } 401 402 private XMLInputSource copyInputSourceXML(InputSource source, String tns, 403 byte[] stream, String encoding) { 404 405 XMLInputSource ret = null; 406 if (stream == null) { 407 ret = new XMLInputSource(source.getPublicId(), source.getSystemId(), null); 408 } else { 409 ret = new XMLInputSource(tns, tns, null, new ByteArrayInputStream (stream), encoding); 410 } 411 return ret; 412 } 413 414 private String [] getDefaultSchemas() { 415 416 String loc = schemaLocation; 417 418 if (loc == null || "".equals(loc.trim())) { 419 loc = "./"; 420 } 421 File f = new File (loc); 422 423 if (f.exists() && f.isDirectory()) { 424 FilenameFilter filter = new FilenameFilter () { 425 public boolean accept(File dir, String name) { 426 if (name.toLowerCase().endsWith(".xsd") 427 && !new File (dir.getPath() + File.separator + name).isDirectory()) { 428 return true; 429 } 430 return false; 431 } 432 }; 433 return f.list(filter); 434 } 435 return null; 436 } 437 438 private String getNamespaceOfSchemas(String [] schemas) throws IOException , SAXException { 439 440 String nsSchema = ""; 441 442 for (int i = 0; i < schemas.length; i++) { 443 String url = null; 444 url = getSchemaUrl(schemas[i]); 445 DOMParser p = new DOMParser(); 446 p.parse(url); 447 448 String tns = p.getDocument().getDocumentElement().getAttribute("targetNamespace"); 449 nsSchema += tns + " " + url + " "; 450 } 451 return nsSchema; 452 } 453 454 private void setExternalSchemaLocations(String [] schemas) throws IOException , SAXException { 455 String nsSchemas = getNamespaceOfSchemas(schemas); 456 parser.setProperty("http://apache.org/xml/properties/schema/external-schemaLocation", nsSchemas); 457 preparser.setProperty("http://apache.org/xml/properties/schema/external-schemaLocation", nsSchemas); 458 } 459 460 private StackTraceErrorHandler setErrorHandler() { 461 StackTraceErrorHandler handler = new StackTraceErrorHandler(); 462 setErrorHandler(handler); 463 return handler; 464 } 465 466 private StackTraceErrorHandler getErrorHandler() { 467 return (StackTraceErrorHandler)parser.getErrorHandler(); 468 } 469 470 private String getUrlFromString(String path) throws IOException { 471 URL url = null; 472 try { 473 url = new URL (path); 474 } catch (MalformedURLException mfex) { 475 try { 476 File f = new File (path); 477 if (f.exists() && !f.isDirectory()) { 478 url = f.toURL(); 479 } 480 } catch (Exception fnex) { 481 } 483 } catch (Exception ex) { 484 } 486 return (url == null) ? null : url.toString(); 487 } 488 489 private URL getURL(String path) throws IOException { 490 URL url = null; 491 492 if (path == null) { 493 return url; 494 } 495 try { 496 url = URLFactory.createURL(path); 497 } catch (MalformedURLException mfex) { 498 File f = new File (path); 499 url = f.toURL(); 500 } 501 502 return url; 503 } 504 505 private String getUrlFromString(String base, String path) throws IOException { 506 if (base != null) { 507 URL url = getURL(base); 508 if (path.indexOf(":") != -1) { 509 URL tmpurl = getURL(path); 510 if (tmpurl != null) { 511 path = tmpurl.toString(); 512 } 513 } 514 return new URL (url, path).toString(); 515 } 516 return getUrlFromString(path); 517 } 518 519 private String resolveUrl(String path) throws IOException { 520 return resolveUrl(null, path); 521 } 522 523 private String resolveUrl(String base, String path) throws IOException { 524 String url = getUrlFromString(base, path); 525 if (url == null) { 526 url = getUrlFromString(schemaLocation, path); 527 } 528 return url; 529 } 530 531 private String getSchemaUrl(String path) throws IOException { 532 String url = getUrlFromString(path); 533 if (url == null && schemaLocation != null && !"".equals(schemaLocation.trim())) { 534 535 url = getUrlFromString(schemaLocation, path); 536 } 537 538 if (url == null) { 539 throw new IOException ("The URL or filename for the specified schema does not exist: " + path); 540 } 541 542 return url; 543 } 544 545 private String getWsdlUrl(String path) throws IOException { 546 return getWsdlUrl(null, path); 547 } 548 549 private String getWsdlUrl(String base, String path) throws IOException { 550 String url = resolveUrl(base, path); 551 552 if (url == null) { 553 throw new IOException ("The URL or filename for the specified WSDL does not exist: " + path); 554 } 555 556 return url; 557 } 558 559 class SchemaEntityResolver implements XMLEntityResolver { 560 561 Map schemas; 562 563 public void setSchemas(Map schemaMap) { 564 schemas = schemaMap; 565 } 566 567 public XMLInputSource resolveEntity(XMLResourceIdentifier id) throws XNIException, IOException { 568 if (id.getLiteralSystemId() != null) { 569 String url = resolveUrl(id.getBaseSystemId(), id.getLiteralSystemId()); 570 if (url == null) { 571 String msg = "Can't resolve entity from publicID=" + id.getPublicId() + ", systemID=" 572 + id.getLiteralSystemId(); 573 574 throw new IOException (msg); 575 } 576 return new XMLInputSource(id.getPublicId(), url, id.getBaseSystemId()); 577 } else { 578 String ns = id.getNamespace(); 580 byte[] schemaBytes = (byte[])schemas.get(ns); 581 if (schemaBytes != null) { 582 return new XMLInputSource(id.getPublicId(), id.getLiteralSystemId(), 583 id.getBaseSystemId(), new ByteArrayInputStream (schemaBytes), 584 "UTF-8"); 585 } else { 586 return null; 587 } 588 } 589 } 590 591 } 592 593 public List <XSModel> getXSModelList() { 594 return xsmodelList; 595 } 596 597 public Document getSchemaValidatedDoc() { 598 return schemaValidatedDoc; 599 } 600 601 public Map <QName , List > getMsgPartsMap() { 602 return msgPartsMap; 603 } 604 605 public Map <QName , Map > getPortTypesMap() { 606 return portTypes; 607 } 608 609 public Map <QName , QName > getBindingMap() { 610 return bindingMap; 611 } 612 613 614 } 615 616 class StackTraceErrorHandler implements ErrorHandler , XMLErrorHandler { 617 protected boolean valid; 618 private StringBuffer buffer; 619 private int numErrors; 620 private List <SAXParseException > errors; 621 622 StackTraceErrorHandler() { 623 valid = true; 624 numErrors = 0; 625 buffer = new StringBuffer (); 626 errors = new ArrayList <SAXParseException >(); 627 } 628 629 public void error(SAXParseException ex) { 630 addError(ex); 631 } 632 633 public void fatalError(SAXParseException ex) { 634 addError(ex); 635 } 636 637 public void warning(SAXParseException ex) { 638 } 641 642 public void error(String domain, String key, XMLParseException ex) { 643 addError(getSAXParseException(ex)); 644 } 645 646 public void fatalError(String domain, String key, XMLParseException ex) { 647 addError(getSAXParseException(ex)); 648 } 649 650 public void warning(String domain, String key, XMLParseException ex) { 651 } 654 655 boolean isValid() { 656 return valid; 657 } 658 659 int getTotalErrors() { 660 return numErrors; 661 } 662 663 String getErrorMessages() { 664 return buffer.toString(); 665 } 666 667 SAXParseException [] getErrors() { 668 if (errors == null) { 669 return null; 670 } 671 return errors.toArray(new SAXParseException [errors.size()]); 672 } 673 674 void addError(String msg, SAXParseException ex) { 675 valid = false; 676 if (numErrors == 0) { 677 buffer.append("\n"); 678 } else { 679 buffer.append("\n\n"); 680 } 681 buffer.append(msg); 682 numErrors++; 683 errors.add(ex); 684 685 } 686 687 private String getErrorMessage(SAXParseException ex) { 688 return "line " + ex.getLineNumber() + " column " + ex.getColumnNumber() + " of " + ex.getSystemId() 689 + ": " + ex.getMessage(); 690 } 691 692 private void addError(SAXParseException ex) { 693 addError(getErrorMessage(ex), ex); 694 } 695 696 private static SAXParseException getSAXParseException(XMLParseException ex) { 697 return new SAXParseException (ex.getMessage(), ex.getPublicId(), ex.getLiteralSystemId(), ex 698 .getLineNumber(), ex.getColumnNumber(), ex.getException()); 699 } 700 } 701 702 class WSDLSchemaErrorHandler implements XMLErrorHandler { 703 private StackTraceErrorHandler handler; 704 private String schemaText; 705 706 WSDLSchemaErrorHandler(StackTraceErrorHandler hand, byte[] schemaBytes, String schemaEncoding) { 707 this.handler = hand; 708 try { 709 schemaText = new String (schemaBytes, schemaEncoding); 710 } catch (java.io.UnsupportedEncodingException ex) { 711 } 713 } 714 715 public void error(String domain, String key, XMLParseException ex) { 716 handler.addError(getErrorMessage(ex), getSAXParseException(ex)); 717 } 718 719 public void fatalError(String domain, String key, XMLParseException ex) { 720 handler.addError(getErrorMessage(ex), getSAXParseException(ex)); 721 } 722 723 public void warning(String domain, String key, XMLParseException ex) { 724 } 727 728 private String getErrorMessage(XMLParseException ex) { 729 String msg = null; 730 if (schemaText != null) { 731 int lineNumber = ex.getLineNumber(); 732 int start = 0; 733 int end = schemaText.indexOf('\n'); 734 int idx = 1; 735 while (end != -1 && idx < lineNumber) { 736 start = end + 1; 737 end = schemaText.indexOf('\n', start); 738 ++idx; 739 } 740 if (end == -1) { 741 end = schemaText.length(); 742 } 743 744 if (idx == lineNumber) { 745 msg = "line \"" + schemaText.substring(start, end).trim() + "\" in the schema of " 746 + ex.getLiteralSystemId() + ": " + ex.getMessage(); 747 } 748 } 749 750 if (msg == null) { 751 msg = ex.getLiteralSystemId() + ": " + ex.getMessage(); 752 } 753 754 return msg; 755 } 756 757 private static SAXParseException getSAXParseException(XMLParseException ex) { 758 return new SAXParseException (ex.getMessage(), ex.getPublicId(), ex.getLiteralSystemId(), -1, -1, ex 759 .getException()); 760 } 761 762 } 763 | Popular Tags |