SAXParser.java 34 KB

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