1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041
|
/* SAXParser.java --
Copyright (C) 2005, 2006, 2007 Free Software Foundation, Inc.
This file is part of GNU Classpath.
GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING. If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA.
Linking this library statically or dynamically with other modules is
making a combined work based on this library. Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.
As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module. An independent module is a module which is not derived from
or based on this library. If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so. If you do not wish to do so, delete this
exception statement from your version. */
package gnu.xml.stream;
import java.io.InputStream;
import java.io.IOException;
import java.io.Reader;
import java.net.URL;
import java.util.Iterator;
import java.util.Map;
import javax.xml.XMLConstants;
import javax.xml.namespace.QName;
import javax.xml.stream.Location;
import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLReporter;
import javax.xml.stream.XMLResolver;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import org.xml.sax.ContentHandler;
import org.xml.sax.DTDHandler;
import org.xml.sax.EntityResolver;
import org.xml.sax.ErrorHandler;
import org.xml.sax.InputSource;
import org.xml.sax.Parser;
import org.xml.sax.SAXException;
import org.xml.sax.SAXNotRecognizedException;
import org.xml.sax.SAXNotSupportedException;
import org.xml.sax.SAXParseException;
import org.xml.sax.XMLReader;
import org.xml.sax.ext.Attributes2;
import org.xml.sax.ext.DeclHandler;
import org.xml.sax.ext.LexicalHandler;
import org.xml.sax.ext.Locator2;
/**
* JAXP SAX parser using an underlying StAX parser.
* This parser supports the following additional SAX features and
* properties:
* <table>
* <tr><th colspan='4'>Features</th></tr>
* <tr><td>http://gnu.org/sax/features/xml-base</td>
* <td colspan='2'>read/write</td>
* <td>Indicates or sets whether XML Base processing is enabled</td></tr>
* <tr><th colspan='4'>Properties</th></tr>
* <tr><td>http://gnu.org/sax/properties/base-uri</td>
* <td>read-only</td><td>String</td>
* <td>Returns the base URI of the current event</td></tr>
* <tr><td>http://gnu.org/sax/properties/document-xml-encoding</td>
* <td>read-only</td><td>String</td>
* <td>Returns the encoding specified in the XML declaration</td></tr>
* </table>
*
* @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
*/
public class SAXParser
extends javax.xml.parsers.SAXParser
implements XMLReader, Attributes2, Locator2, XMLReporter, XMLResolver
{
ContentHandler contentHandler;
DeclHandler declHandler;
DTDHandler dtdHandler;
EntityResolver entityResolver;
ErrorHandler errorHandler;
LexicalHandler lexicalHandler;
boolean validating = false;
boolean namespaceAware = true;
boolean xIncludeAware = false;
boolean stringInterning = true;
boolean coalescing = true;
boolean replaceERefs = true;
boolean externalEntities = true;
boolean supportDTD = true;
boolean baseAware = true;
XMLParser parser;
XMLStreamReader reader;
String encoding;
String xmlVersion;
boolean xmlStandalone;
String xmlEncoding;
String baseURI;
public SAXParser()
{
}
SAXParser(boolean validating, boolean namespaceAware, boolean xIncludeAware)
{
this.validating = validating;
this.namespaceAware = namespaceAware;
this.xIncludeAware = xIncludeAware;
}
// -- SAXParser --
public Parser getParser()
throws SAXException
{
return null;
}
public XMLReader getXMLReader()
throws SAXException
{
return this;
}
public boolean isNamespaceAware()
{
return namespaceAware;
}
public boolean isValidating()
{
return validating;
}
public void setProperty(String name, Object value)
throws SAXNotRecognizedException, SAXNotSupportedException
{
if (parser != null)
throw new IllegalStateException("parsing in progress");
final String FEATURES = "http://xml.org/sax/features/";
final String PROPERTIES = "http://xml.org/sax/properties/";
final String GNU_FEATURES = "http://gnu.org/sax/features/";
if ((FEATURES + "namespaces").equals(name))
namespaceAware = Boolean.TRUE.equals(value);
else if ((FEATURES + "namespace-prefixes").equals(name))
{
// NOOP
}
else if ((FEATURES + "string-interning").equals(name))
stringInterning = Boolean.TRUE.equals(value);
else if ((FEATURES + "use-attributes2").equals(name))
{
// NOOP
}
else if ((FEATURES + "validation").equals(name))
validating = Boolean.TRUE.equals(value);
else if ((FEATURES + "external-general-entities").equals(name))
externalEntities = Boolean.TRUE.equals(value);
else if ((FEATURES + "external-parameter-entities").equals(name))
externalEntities = Boolean.TRUE.equals(value);
else if ((PROPERTIES + "declaration-handler").equals(name))
declHandler = (DeclHandler) value;
else if ((PROPERTIES + "lexical-handler").equals(name))
lexicalHandler = (LexicalHandler) value;
else if ((GNU_FEATURES + "xml-base").equals(name))
baseAware = Boolean.TRUE.equals(value);
else if ((GNU_FEATURES + "coalescing").equals(name))
coalescing = Boolean.TRUE.equals(value);
else
throw new SAXNotSupportedException(name);
}
public Object getProperty(String name)
throws SAXNotRecognizedException, SAXNotSupportedException
{
final String FEATURES = "http://xml.org/sax/features/";
final String PROPERTIES = "http://xml.org/sax/properties/";
final String GNU_FEATURES = "http://gnu.org/sax/features/";
final String GNU_PROPERTIES = "http://gnu.org/sax/properties/";
if ((GNU_FEATURES + "base-uri").equals(name))
return baseURI;
if ((FEATURES + "is-standalone").equals(name))
return xmlStandalone ? Boolean.TRUE : Boolean.FALSE;
if ((FEATURES + "namespaces").equals(name))
return namespaceAware ? Boolean.TRUE : Boolean.FALSE;
if ((FEATURES + "namespace-prefixes").equals(name))
return Boolean.TRUE;
if ((FEATURES + "string-interning").equals(name))
return stringInterning ? Boolean.TRUE : Boolean.FALSE;
if ((FEATURES + "use-attributes2").equals(name))
return Boolean.TRUE;
if ((FEATURES + "use-locator2").equals(name))
return Boolean.TRUE;
if ((FEATURES + "use-entity-resolver2").equals(name))
return Boolean.FALSE;
if ((FEATURES + "validation").equals(name))
return validating ? Boolean.TRUE : Boolean.FALSE;
if ((FEATURES + "external-general-entities").equals(name))
return externalEntities ? Boolean.TRUE : Boolean.FALSE;
if ((FEATURES + "external-parameter-entities").equals(name))
return externalEntities ? Boolean.TRUE : Boolean.FALSE;
if ((FEATURES + "xml-1.1").equals(name))
return Boolean.TRUE;
if ((PROPERTIES + "declaration-handler").equals(name))
return declHandler;
if ((PROPERTIES + "document-xml-version").equals(name))
return xmlVersion;
if ((PROPERTIES + "lexical-handler").equals(name))
return lexicalHandler;
if ((GNU_FEATURES + "xml-base").equals(name))
return baseAware ? Boolean.TRUE : Boolean.FALSE;
if ((GNU_PROPERTIES + "document-xml-encoding").equals(name))
return xmlEncoding;
throw new SAXNotSupportedException(name);
}
public boolean isXIncludeAware()
{
return xIncludeAware;
}
public void reset()
{
parser = null;
encoding = null;
xmlVersion = null;
xmlStandalone = false;
}
// -- XMLReader --
public boolean getFeature(String name)
throws SAXNotRecognizedException, SAXNotSupportedException
{
Object ret = getProperty(name);
if (ret instanceof Boolean)
return ((Boolean) ret).booleanValue();
throw new SAXNotSupportedException(name);
}
public void setFeature(String name, boolean value)
throws SAXNotRecognizedException, SAXNotSupportedException
{
setProperty(name, value ? Boolean.TRUE : Boolean.FALSE);
}
public void setEntityResolver(EntityResolver resolver)
{
entityResolver = resolver;
}
public EntityResolver getEntityResolver()
{
return entityResolver;
}
public void setDTDHandler(DTDHandler handler)
{
dtdHandler = handler;
}
public DTDHandler getDTDHandler()
{
return dtdHandler;
}
public void setContentHandler(ContentHandler handler)
{
contentHandler = handler;
}
public ContentHandler getContentHandler()
{
return contentHandler;
}
public void setErrorHandler(ErrorHandler handler)
{
errorHandler = handler;
}
public ErrorHandler getErrorHandler()
{
return errorHandler;
}
public synchronized void parse(InputSource input)
throws IOException, SAXException
{
reset();
String systemId = input.getSystemId();
InputStream in = input.getByteStream();
boolean opened = false;
if (in != null)
parser = new XMLParser(in, systemId,
validating,
namespaceAware,
coalescing,
replaceERefs,
externalEntities,
supportDTD,
baseAware,
stringInterning,
true,
this,
this);
else
{
Reader r = input.getCharacterStream();
if (r != null)
parser = new XMLParser(r, systemId,
validating,
namespaceAware,
coalescing,
replaceERefs,
externalEntities,
supportDTD,
baseAware,
stringInterning,
true,
this,
this);
}
if (parser == null)
{
if (systemId == null)
throw new SAXException("No stream or system ID specified");
systemId = XMLParser.absolutize(null, systemId);
in = new URL(systemId).openStream();
opened = true;
parser = new XMLParser(in, systemId,
validating,
namespaceAware,
coalescing,
replaceERefs,
externalEntities,
supportDTD,
baseAware,
stringInterning,
true,
this,
this);
}
reader = parser;
baseURI = systemId;
if (xIncludeAware)
reader = new XIncludeFilter(parser, systemId, namespaceAware,
validating, true);
if (contentHandler != null)
contentHandler.setDocumentLocator(this);
boolean startDocumentDone = false;
try
{
while (parser.hasNext())
{
int event = parser.next();
if (baseAware)
baseURI = parser.getXMLBase();
switch (event)
{
case XMLStreamConstants.CHARACTERS:
if (contentHandler != null)
{
char[] b = reader.getTextCharacters();
contentHandler.characters(b, 0, b.length);
}
break;
case XMLStreamConstants.SPACE:
if (contentHandler != null)
{
char[] b = reader.getTextCharacters();
if (isIgnorableWhitespace(parser, b, false))
contentHandler.ignorableWhitespace(b, 0, b.length);
else
contentHandler.characters(b, 0, b.length);
}
break;
case XMLStreamConstants.CDATA:
if (lexicalHandler != null)
lexicalHandler.startCDATA();
if (contentHandler != null)
{
char[] b = reader.getTextCharacters();
if (isIgnorableWhitespace(parser, b, true))
contentHandler.ignorableWhitespace(b, 0, b.length);
else
contentHandler.characters(b, 0, b.length);
}
if (lexicalHandler != null)
lexicalHandler.endCDATA();
break;
case XMLStreamConstants.START_ELEMENT:
if (contentHandler != null)
{
QName name = reader.getName();
String uri = name.getNamespaceURI();
String localName = name.getLocalPart();
String prefix = name.getPrefix();
String qName = localName;
if (!"".equals(prefix))
qName = prefix + ":" + localName;
if (!namespaceAware)
{
uri = "";
localName = "";
}
else
{
int nc = reader.getNamespaceCount();
for (int i = 0; i < nc; i++)
{
String nsuri = reader.getNamespaceURI(i);
String nsprefix = reader.getNamespacePrefix(i);
if ("xml".equals(nsprefix))
continue;
contentHandler.startPrefixMapping(nsprefix, nsuri);
}
}
contentHandler.startElement(uri, localName, qName, this);
}
break;
case XMLStreamConstants.END_ELEMENT:
if (contentHandler != null)
{
QName name = reader.getName();
String uri = name.getNamespaceURI();
String localName = name.getLocalPart();
String prefix = name.getPrefix();
String qName = localName;
if (!"".equals(prefix))
qName = prefix + ":" + localName;
if (!namespaceAware)
{
uri = "";
localName = "";
}
contentHandler.endElement(uri, localName, qName);
if (namespaceAware)
{
int nc = reader.getNamespaceCount();
for (int i = 0; i < nc; i++)
{
String nsprefix = reader.getNamespacePrefix(i);
if ("xml".equals(nsprefix))
continue;
contentHandler.endPrefixMapping(nsprefix);
}
}
}
break;
case XMLStreamConstants.COMMENT:
if (lexicalHandler != null)
{
char[] b = reader.getTextCharacters();
lexicalHandler.comment(b, 0, b.length);
}
break;
case XMLStreamConstants.PROCESSING_INSTRUCTION:
if (contentHandler != null)
{
String target = reader.getPITarget();
String data = reader.getPIData();
if (data == null)
data = "";
contentHandler.processingInstruction(target, data);
}
break;
case XMLParser.START_ENTITY:
if (lexicalHandler != null)
{
String name = reader.getText();
lexicalHandler.startEntity(name);
}
break;
case XMLParser.END_ENTITY:
if (lexicalHandler != null)
{
String name = reader.getText();
lexicalHandler.endEntity(name);
}
break;
case XMLStreamConstants.START_DOCUMENT:
encoding = reader.getEncoding();
xmlVersion = reader.getVersion();
xmlStandalone = reader.isStandalone();
xmlEncoding = reader.getCharacterEncodingScheme();
if (contentHandler != null)
contentHandler.startDocument();
startDocumentDone = true;
break;
case XMLStreamConstants.END_DOCUMENT:
if (contentHandler != null)
contentHandler.endDocument();
break;
case XMLStreamConstants.DTD:
XMLParser.Doctype doctype = parser.doctype;
if (lexicalHandler != null)
{
String rootName = doctype.rootName;
String publicId = doctype.publicId;
String systemId2 = doctype.systemId;
lexicalHandler.startDTD(rootName, publicId, systemId2);
}
for (Iterator i = doctype.entryIterator(); i.hasNext(); )
{
String entry = (String) i.next();
char c = entry.charAt(0);
String name = entry.substring(1);
if ('E' == c)
{
// Element decl
if (declHandler != null)
{
XMLParser.ContentModel model =
doctype.getElementModel(name);
declHandler.elementDecl(name, model.text);
}
}
else if ('A' == c)
{
// Attlist decl
if (declHandler != null)
{
for (Iterator j = doctype.attlistIterator(name);
j.hasNext(); )
{
Map.Entry att = (Map.Entry) j.next();
String aname = (String) att.getKey();
XMLParser.AttributeDecl decl =
(XMLParser.AttributeDecl) att.getValue();
String type = decl.type;
String value = decl.value;
String mode = null;
switch (decl.valueType)
{
case XMLParser.ATTRIBUTE_DEFAULT_FIXED:
mode = "#FIXED";
break;
case XMLParser.ATTRIBUTE_DEFAULT_REQUIRED:
mode = "#REQUIRED";
break;
case XMLParser.ATTRIBUTE_DEFAULT_IMPLIED:
mode = "#IMPLIED";
break;
}
declHandler.attributeDecl(name, aname,
type, mode, value);
}
}
}
else if ('e' == c)
{
// Entity decl
Object entity = doctype.getEntity(name);
if (entity instanceof String)
{
if (declHandler != null)
declHandler.internalEntityDecl(name,
(String) entity);
}
else
{
XMLParser.ExternalIds ids =
(XMLParser.ExternalIds) entity;
if (ids.notationName != null)
{
if (dtdHandler != null)
{
String pub = ids.publicId;
String url = ids.systemId;
String not = ids.notationName;
dtdHandler.unparsedEntityDecl(name,
pub,
url,
not);
}
}
else
{
if (declHandler != null)
{
String pub = ids.publicId;
String url = ids.systemId;
declHandler.externalEntityDecl(name,
pub,
url);
}
}
}
}
else if ('n' == c)
{
// Notation decl
if (dtdHandler != null)
{
XMLParser.ExternalIds ids =
doctype.getNotation(name);
String pub = ids.publicId;
String url = ids.systemId;
dtdHandler.notationDecl(name, pub, url);
}
}
else if ('c' == c)
{
// Comment
if (lexicalHandler != null)
{
String comment = doctype.getComment(name);
char[] b = comment.toCharArray();
lexicalHandler.comment(b, 0, b.length);
}
}
else if ('p' == c)
{
// Processing instruction
if (contentHandler != null)
{
String[] pi = doctype.getPI(name);
String target = pi[0];
String data = pi[1];
if (data == null)
data = "";
contentHandler.processingInstruction(target, data);
}
}
}
if (lexicalHandler != null)
lexicalHandler.endDTD();
}
}
reset();
if (opened)
in.close();
}
catch (Exception e)
{
SAXParseException e2 = new SAXParseException(e.getMessage(), this);
e2.initCause(e);
try
{
if (!startDocumentDone && contentHandler != null)
contentHandler.startDocument();
if (errorHandler != null)
errorHandler.fatalError(e2);
if (contentHandler != null)
contentHandler.endDocument();
}
catch (SAXException sex)
{
// Ignored, we will rethrow the original exception.
}
reset();
if (opened)
in.close();
if (e instanceof SAXException)
throw (SAXException) e;
if (e instanceof IOException)
throw (IOException) e;
else
throw e2;
}
}
/**
* Indicates whether the specified characters are ignorable whitespace.
*/
private boolean isIgnorableWhitespace(XMLParser reader, char[] b,
boolean testCharacters)
throws XMLStreamException
{
XMLParser.Doctype doctype = reader.doctype;
if (doctype == null)
return false;
String currentElement = reader.getCurrentElement();
// check for xml:space
int ac = reader.getAttributeCount();
for (int i = 0; i < ac; i++)
{
QName aname = reader.getAttributeName(i);
if ("space".equals(aname.getLocalPart()) &&
XMLConstants.XML_NS_URI.equals(aname.getNamespaceURI()))
{
if ("preserve".equals(reader.getAttributeValue(i)))
return false;
}
}
XMLParser.ContentModel model = doctype.getElementModel(currentElement);
if (model == null || model.type != XMLParser.ContentModel.ELEMENT)
return false;
if (model.external && xmlStandalone)
return false;
boolean white = true;
if (testCharacters)
{
for (int i = 0; i < b.length; i++)
{
if (b[i] != ' ' && b[i] != '\t' && b[i] != '\n' && b[i] != '\r')
{
white = false;
break;
}
}
}
return white;
}
public void parse(String systemId)
throws IOException, SAXException
{
parse(new InputSource(systemId));
}
// -- Attributes2 --
public int getIndex(String qName)
{
int len = reader.getAttributeCount();
for (int i = 0; i < len; i++)
{
QName q = reader.getAttributeName(i);
String localName = q.getLocalPart();
String prefix = q.getPrefix();
String qn = ("".equals(prefix)) ? localName : prefix + ":" + localName;
if (qName.equals(qn))
return i;
}
return -1;
}
public int getIndex(String uri, String localName)
{
int len = reader.getAttributeCount();
for (int i = 0; i < len; i++)
{
QName q = reader.getAttributeName(i);
String ln = q.getLocalPart();
String u = q.getNamespaceURI();
if (u == null && uri != null)
continue;
if (u != null && !u.equals(uri))
continue;
if (ln.equals(localName))
return i;
}
return -1;
}
public int getLength()
{
return reader.getAttributeCount();
}
public String getLocalName(int index)
{
return reader.getAttributeLocalName(index);
}
public String getQName(int index)
{
QName q = reader.getAttributeName(index);
String localName = q.getLocalPart();
String prefix = q.getPrefix();
return ("".equals(prefix)) ? localName : prefix + ":" + localName;
}
public String getType(int index)
{
String ret = reader.getAttributeType(index);
// SAX doesn't permit ENUMERATION?
return ("ENUMERATION".equals(ret)) ? "NMTOKEN" : ret;
}
public String getType(String qName)
{
int index = getIndex(qName);
return (index == -1) ? null : getType(index);
}
public String getType(String uri, String localName)
{
int index = getIndex(uri, localName);
return (index == -1) ? null : getType(index);
}
public String getURI(int index)
{
String ret = reader.getAttributeNamespace(index);
return (ret == null) ? "" : ret;
}
public String getValue(int index)
{
return reader.getAttributeValue(index);
}
public String getValue(String qName)
{
int index = getIndex(qName);
return (index == -1) ? null : getValue(index);
}
public String getValue(String uri, String localName)
{
int index = getIndex(uri, localName);
return (index == -1) ? null : getValue(index);
}
public boolean isDeclared(int index)
{
return parser.isAttributeDeclared(index);
}
public boolean isDeclared(String qName)
{
int index = getIndex(qName);
return (index == -1) ? false : isDeclared(index);
}
public boolean isDeclared(String uri, String localName)
{
int index = getIndex(uri, localName);
return (index == -1) ? false : isDeclared(index);
}
public boolean isSpecified(int index)
{
return reader.isAttributeSpecified(index);
}
public boolean isSpecified(String qName)
{
int index = getIndex(qName);
return (index == -1) ? false : isSpecified(index);
}
public boolean isSpecified(String uri, String localName)
{
int index = getIndex(uri, localName);
return (index == -1) ? false : isSpecified(index);
}
// -- Locator2 --
public int getColumnNumber()
{
Location l = reader.getLocation();
return l.getColumnNumber();
}
public int getLineNumber()
{
Location l = reader.getLocation();
return l.getLineNumber();
}
public String getPublicId()
{
Location l = reader.getLocation();
return l.getPublicId();
}
public String getSystemId()
{
Location l = reader.getLocation();
return l.getSystemId();
}
public String getEncoding()
{
return encoding;
}
public String getXMLVersion()
{
return xmlVersion;
}
// -- XMLResolver --
public Object resolveEntity(String publicId, String systemId,
String baseURI, String namespace)
throws XMLStreamException
{
if (entityResolver != null)
{
try
{
InputSource input =
entityResolver.resolveEntity(publicId, systemId);
if (input != null)
{
InputStream in = input.getByteStream();
if (in == null)
{
String newSystemId = input.getSystemId();
if (newSystemId != null && !newSystemId.equals(systemId))
in = XMLParser.resolve(newSystemId);
}
return in;
}
}
catch (SAXException e)
{
XMLStreamException e2 = new XMLStreamException(e.getMessage());
e2.initCause(e);
throw e2;
}
catch (IOException e)
{
XMLStreamException e2 = new XMLStreamException(e.getMessage());
e2.initCause(e);
throw e2;
}
}
return null;
}
public XMLEventReader resolveAsXMLEventReader(String uri)
throws XMLStreamException
{
// unused
return null;
}
public XMLStreamReader resolveAsXMLStreamReader(String uri)
throws XMLStreamException
{
// unused
return null;
}
// -- XMLReporter --
public void report(String message, String errorType,
Object relatedInformation, Location location)
throws XMLStreamException
{
if (errorHandler != null)
{
try
{
errorHandler.warning(new SAXParseException(message, this));
}
catch (SAXException e)
{
XMLStreamException e2 = new XMLStreamException(e.getMessage());
e2.initCause(e);
throw e2;
}
}
}
public static void main(String[] args)
throws Exception
{
boolean validating = false;
boolean namespaceAware = false;
boolean xIncludeAware = false;
boolean expectCallbackClass = false;
String callbackClass = null;
int pos = 0;
while (pos < args.length && (args[pos].startsWith("-") || expectCallbackClass))
{
if ("-x".equals(args[pos]))
xIncludeAware = true;
else if ("-v".equals(args[pos]))
validating = true;
else if ("-n".equals(args[pos]))
namespaceAware = true;
else if ("-c".equals(args[pos]))
expectCallbackClass = true;
else if (expectCallbackClass)
{
callbackClass = args[pos];
expectCallbackClass = false;
}
pos++;
}
if (pos >= args.length || expectCallbackClass)
{
System.out.println("Syntax: SAXParser [-n] [-v] [-x] [-c <class>] <file> [<file2> [...]]");
System.out.println("\t-n: use namespace aware mode");
System.out.println("\t-v: use validating parser");
System.out.println("\t-x: use XInclude aware mode");
System.out.println("\t-c <class>: use specified class as callback handler (must have a no-arg public constructor)");
System.exit(2);
}
while (pos < args.length)
{
ContentHandler handler = null;
if (callbackClass != null)
{
Class t = Class.forName(callbackClass);
handler = (ContentHandler) t.newInstance();
}
else
handler = new org.xml.sax.helpers.DefaultHandler();
SAXParser parser = new SAXParser(validating, namespaceAware,
xIncludeAware);
InputSource input = new InputSource(args[pos]);
java.io.FileReader fr = new java.io.FileReader(args[pos]);
input.setCharacterStream(fr);
try
{
XMLReader reader = parser.getXMLReader();
reader.setContentHandler(handler);
reader.parse(input);
}
finally
{
fr.close();
}
pos++;
}
}
}
|