GnomeXMLReader.java 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066
  1. /* GnomeXMLReader.java -
  2. Copyright (C) 2004 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.libxmlj.sax;
  32. import java.io.File;
  33. import java.io.FileNotFoundException;
  34. import java.io.InputStream;
  35. import java.io.IOException;
  36. import java.net.MalformedURLException;
  37. import java.net.URL;
  38. import java.util.Arrays;
  39. import java.util.ArrayList;
  40. import java.util.Iterator;
  41. import java.util.List;
  42. import org.xml.sax.Attributes;
  43. import org.xml.sax.ContentHandler;
  44. import org.xml.sax.DTDHandler;
  45. import org.xml.sax.EntityResolver;
  46. import org.xml.sax.ErrorHandler;
  47. import org.xml.sax.InputSource;
  48. import org.xml.sax.Locator;
  49. import org.xml.sax.SAXException;
  50. import org.xml.sax.SAXNotRecognizedException;
  51. import org.xml.sax.SAXNotSupportedException;
  52. import org.xml.sax.SAXParseException;
  53. import org.xml.sax.XMLReader;
  54. import org.xml.sax.ext.DeclHandler;
  55. import org.xml.sax.ext.LexicalHandler;
  56. import gnu.xml.libxmlj.util.NamedInputStream;
  57. import gnu.xml.libxmlj.util.StandaloneLocator;
  58. import gnu.xml.libxmlj.util.XMLJ;
  59. /**
  60. * A SAX2 parser that uses libxml2.
  61. *
  62. * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
  63. */
  64. public class GnomeXMLReader
  65. implements XMLReader
  66. {
  67. static
  68. {
  69. XMLJ.init ();
  70. }
  71. private static final String FEATURES_PREFIX =
  72. "http://xml.org/sax/features/";
  73. private static final List RECOGNIZED_FEATURES =
  74. Arrays.asList (new String[]
  75. {
  76. "external-general-entities",
  77. "external-parameter-entities",
  78. "is-standalone",
  79. "lexical-handler/parameter-entities",
  80. "namespaces",
  81. "namespace-prefixes",
  82. "resolve-dtd-uris",
  83. "string-interning",
  84. "use-attributes2",
  85. "use-locator2",
  86. "use-entity-resolver2",
  87. "validation"
  88. });
  89. private static final String PROPERTIES_PREFIX =
  90. "http://xml.org/sax/properties/";
  91. private static final List RECOGNIZED_PROPERTIES =
  92. Arrays.asList (new String[]
  93. {
  94. "declaration-handler",
  95. "dom-node",
  96. "lexical-handler",
  97. "xml-string"
  98. });
  99. // Features
  100. private transient boolean standalone;
  101. private boolean namespaces;
  102. private boolean namespacePrefixes;
  103. private boolean validation;
  104. // Callback handlers
  105. private ContentHandler contentHandler;
  106. private DTDHandler dtdHandler;
  107. private EntityResolver entityResolver;
  108. private ErrorHandler errorHandler;
  109. private DeclHandler declarationHandler;
  110. private LexicalHandler lexicalHandler;
  111. private GnomeLocator locator;
  112. // Namespace helper for handling callbacks
  113. private transient Namespaces ns;
  114. // If true, do not invoke callback methods except endDocument
  115. private transient boolean seenFatalError;
  116. private transient boolean seenStartDocument;
  117. private transient String base;
  118. public GnomeXMLReader ()
  119. {
  120. this (true, true);
  121. }
  122. public GnomeXMLReader (boolean namespaces, boolean validation)
  123. {
  124. this.namespaces = namespaces;
  125. this.validation = validation;
  126. ns = new Namespaces ();
  127. }
  128. public ContentHandler getContentHandler ()
  129. {
  130. return contentHandler;
  131. }
  132. public void setContentHandler (ContentHandler handler)
  133. {
  134. contentHandler = handler;
  135. }
  136. public DTDHandler getDTDHandler ()
  137. {
  138. return dtdHandler;
  139. }
  140. public void setDTDHandler (DTDHandler handler)
  141. {
  142. dtdHandler = handler;
  143. }
  144. public EntityResolver getEntityResolver ()
  145. {
  146. return entityResolver;
  147. }
  148. public void setEntityResolver (EntityResolver resolver)
  149. {
  150. entityResolver = resolver;
  151. }
  152. public ErrorHandler getErrorHandler ()
  153. {
  154. return errorHandler;
  155. }
  156. public void setErrorHandler (ErrorHandler handler)
  157. {
  158. errorHandler = handler;
  159. }
  160. // Features
  161. public boolean getFeature (String name)
  162. throws SAXNotRecognizedException, SAXNotSupportedException
  163. {
  164. checkFeatureName (name);
  165. String key = name.substring (FEATURES_PREFIX.length ());
  166. if ("external-general-entities".equals (key))
  167. {
  168. return validation; // TODO check this
  169. }
  170. else if ("external-parameter-entities".equals (key))
  171. {
  172. return validation; // TODO check this
  173. }
  174. else if ("is-standalone".equals (key))
  175. {
  176. return standalone;
  177. }
  178. else if ("namespaces".equals (key))
  179. {
  180. return namespaces;
  181. }
  182. else if ("namespace-prefixes".equals (key))
  183. {
  184. return namespacePrefixes;
  185. }
  186. else if ("resolve-dtd-uris".equals (key))
  187. {
  188. return true;
  189. }
  190. else if ("validation".equals (key))
  191. {
  192. return validation;
  193. }
  194. else
  195. {
  196. return false;
  197. }
  198. }
  199. public void setFeature (String name, boolean value)
  200. throws SAXNotRecognizedException, SAXNotSupportedException
  201. {
  202. checkFeatureName (name);
  203. String key = name.substring (FEATURES_PREFIX.length ());
  204. if ("namespaces".equals (key))
  205. {
  206. namespaces = value;
  207. }
  208. else if ("namespace-prefixes".equals (key))
  209. {
  210. namespacePrefixes = value;
  211. }
  212. else if ("validation".equals (key))
  213. {
  214. validation = value;
  215. }
  216. }
  217. /**
  218. * Check that the specified feature name is recognized.
  219. */
  220. static void checkFeatureName (String name)
  221. throws SAXNotRecognizedException
  222. {
  223. if (name == null || !name.startsWith (FEATURES_PREFIX))
  224. {
  225. throw new SAXNotRecognizedException (name);
  226. }
  227. String key = name.substring (FEATURES_PREFIX.length ());
  228. if (!RECOGNIZED_FEATURES.contains (key))
  229. {
  230. throw new SAXNotRecognizedException (name);
  231. }
  232. }
  233. // Properties
  234. public Object getProperty (String name)
  235. throws SAXNotRecognizedException, SAXNotSupportedException
  236. {
  237. checkPropertyName (name);
  238. String key = name.substring (PROPERTIES_PREFIX.length ());
  239. if ("declaration-handler".equals (key))
  240. {
  241. return getDeclarationHandler ();
  242. }
  243. else if ("lexical-handler".equals (key))
  244. {
  245. return getLexicalHandler ();
  246. }
  247. else
  248. {
  249. throw new SAXNotSupportedException (name);
  250. }
  251. }
  252. public void setProperty (String name, Object value)
  253. throws SAXNotRecognizedException, SAXNotSupportedException
  254. {
  255. checkPropertyName (name);
  256. String key = name.substring (PROPERTIES_PREFIX.length ());
  257. if ("declaration-handler".equals (key))
  258. {
  259. setDeclarationHandler ((DeclHandler) value);
  260. }
  261. else if ("lexical-handler".equals (key))
  262. {
  263. setLexicalHandler ((LexicalHandler) value);
  264. }
  265. }
  266. public DeclHandler getDeclarationHandler ()
  267. {
  268. return declarationHandler;
  269. }
  270. public void setDeclarationHandler (DeclHandler declarationHandler)
  271. {
  272. this.declarationHandler = declarationHandler;
  273. }
  274. public LexicalHandler getLexicalHandler ()
  275. {
  276. return lexicalHandler;
  277. }
  278. public void setLexicalHandler (LexicalHandler lexicalHandler)
  279. {
  280. this.lexicalHandler = lexicalHandler;
  281. }
  282. /**
  283. * Check that the specified property name is recognized.
  284. */
  285. static void checkPropertyName (String name)
  286. throws SAXNotRecognizedException
  287. {
  288. if (!name.startsWith (PROPERTIES_PREFIX))
  289. {
  290. throw new SAXNotRecognizedException (name);
  291. }
  292. String key = name.substring (PROPERTIES_PREFIX.length ());
  293. if (!RECOGNIZED_PROPERTIES.contains (key))
  294. {
  295. throw new SAXNotRecognizedException (name);
  296. }
  297. }
  298. // Parse
  299. public void parse (String systemId)
  300. throws IOException, SAXException
  301. {
  302. URL url = null;
  303. try
  304. {
  305. url = new URL (systemId);
  306. }
  307. catch (MalformedURLException e)
  308. {
  309. File file = new File(systemId);
  310. if (!file.exists ())
  311. {
  312. throw new FileNotFoundException (systemId);
  313. }
  314. String path = file.getAbsolutePath();
  315. if (File.separatorChar != '/')
  316. {
  317. path = path.replace (File.separatorChar, '/');
  318. }
  319. if (!path.startsWith ("/"))
  320. {
  321. path = "/" + path;
  322. }
  323. if (!path.endsWith ("/") && file.isDirectory ())
  324. {
  325. path = path + "/";
  326. }
  327. url = new URL ("file:" + path);
  328. }
  329. InputSource source = new InputSource(url.toString ());
  330. source.setByteStream (url.openStream ());
  331. parse (source);
  332. }
  333. public synchronized void parse (InputSource input)
  334. throws IOException, SAXException
  335. {
  336. NamedInputStream in = XMLJ.getInputStream (input);
  337. byte[] detectBuffer = in.getDetectBuffer ();
  338. String publicId = input.getPublicId ();
  339. String systemId = input.getSystemId ();
  340. base = XMLJ.getBaseURI (systemId);
  341. // Reset state
  342. standalone = false;
  343. seenFatalError = false;
  344. seenStartDocument = false;
  345. if (systemId != null)
  346. {
  347. int lsi = systemId.lastIndexOf ('/');
  348. if (lsi != -1)
  349. {
  350. base = systemId.substring (0, lsi + 1);
  351. }
  352. }
  353. // Handle zero-length document
  354. if (detectBuffer == null)
  355. {
  356. startDocument (true);
  357. fatalError ("No document element", 0, 0, publicId, systemId);
  358. endDocument ();
  359. return;
  360. }
  361. // Parse
  362. parseStream(in,
  363. detectBuffer,
  364. publicId,
  365. systemId,
  366. base,
  367. validation,
  368. contentHandler != null,
  369. dtdHandler != null,
  370. entityResolver != null,
  371. errorHandler != null,
  372. declarationHandler != null,
  373. lexicalHandler != null);
  374. in.close ();
  375. }
  376. native void parseStream (InputStream in,
  377. byte[] detectBuffer,
  378. String publicId,
  379. String systemId,
  380. String base,
  381. boolean validate,
  382. boolean contentHandler,
  383. boolean dtdHandler,
  384. boolean entityResolver,
  385. boolean errorHandler,
  386. boolean declarationHandler,
  387. boolean lexicalHandler)
  388. throws IOException, SAXException;
  389. String getURI (String prefix)
  390. {
  391. if (!namespaces)
  392. {
  393. return null;
  394. }
  395. return ns.getURI (prefix);
  396. }
  397. // Callbacks from libxmlj
  398. private void startDTD (String name, String publicId, String systemId)
  399. throws SAXException
  400. {
  401. if (seenFatalError || lexicalHandler == null)
  402. {
  403. return;
  404. }
  405. try
  406. {
  407. systemId = XMLJ.getAbsoluteURI (base, systemId);
  408. lexicalHandler.startDTD (name, publicId, systemId);
  409. }
  410. catch (Exception e)
  411. {
  412. if (e instanceof SAXException)
  413. {
  414. throw (SAXException) e;
  415. }
  416. else
  417. {
  418. throw new SAXException (e);
  419. }
  420. }
  421. }
  422. private void externalEntityDecl (String name, String publicId,
  423. String systemId)
  424. throws SAXException
  425. {
  426. if (seenFatalError || declarationHandler == null)
  427. {
  428. return;
  429. }
  430. try
  431. {
  432. systemId = XMLJ.getAbsoluteURI (base, systemId);
  433. declarationHandler.externalEntityDecl (name, publicId, systemId);
  434. }
  435. catch (Exception e)
  436. {
  437. if (e instanceof SAXException)
  438. {
  439. throw (SAXException) e;
  440. }
  441. else
  442. {
  443. throw new SAXException (e);
  444. }
  445. }
  446. }
  447. private void internalEntityDecl (String name, String value)
  448. throws SAXException
  449. {
  450. if (seenFatalError || declarationHandler == null)
  451. {
  452. return;
  453. }
  454. try
  455. {
  456. declarationHandler.internalEntityDecl (name, value);
  457. }
  458. catch (Exception e)
  459. {
  460. if (e instanceof SAXException)
  461. {
  462. throw (SAXException) e;
  463. }
  464. else
  465. {
  466. throw new SAXException (e);
  467. }
  468. }
  469. }
  470. private InputStream resolveEntity (String publicId, String systemId)
  471. throws SAXException, IOException
  472. {
  473. if (entityResolver == null)
  474. {
  475. return null;
  476. }
  477. try
  478. {
  479. systemId = XMLJ.getAbsoluteURI (base, systemId);
  480. InputSource source = entityResolver.resolveEntity (publicId, systemId);
  481. return (source == null) ? null : XMLJ.getInputStream (source);
  482. }
  483. catch (Exception e)
  484. {
  485. if (e instanceof SAXException)
  486. {
  487. throw (SAXException) e;
  488. }
  489. else
  490. {
  491. throw new SAXException (e);
  492. }
  493. }
  494. }
  495. private void notationDecl (String name, String publicId, String systemId)
  496. throws SAXException
  497. {
  498. if (seenFatalError || dtdHandler == null)
  499. {
  500. return;
  501. }
  502. try
  503. {
  504. systemId = XMLJ.getAbsoluteURI (base, systemId);
  505. dtdHandler.notationDecl (name, publicId, systemId);
  506. }
  507. catch (Exception e)
  508. {
  509. if (e instanceof SAXException)
  510. {
  511. throw (SAXException) e;
  512. }
  513. else
  514. {
  515. throw new SAXException (e);
  516. }
  517. }
  518. }
  519. private void attributeDecl (String eName, String aName, String type,
  520. String mode, String value)
  521. throws SAXException
  522. {
  523. if (seenFatalError || declarationHandler == null)
  524. {
  525. return;
  526. }
  527. try
  528. {
  529. declarationHandler.attributeDecl (eName, aName, type, mode, value);
  530. }
  531. catch (Exception e)
  532. {
  533. if (e instanceof SAXException)
  534. {
  535. throw (SAXException) e;
  536. }
  537. else
  538. {
  539. throw new SAXException (e);
  540. }
  541. }
  542. }
  543. private void elementDecl (String name, String model)
  544. throws SAXException
  545. {
  546. if (seenFatalError || declarationHandler == null)
  547. {
  548. return;
  549. }
  550. try
  551. {
  552. declarationHandler.elementDecl (name, model);
  553. }
  554. catch (Exception e)
  555. {
  556. if (e instanceof SAXException)
  557. {
  558. throw (SAXException) e;
  559. }
  560. else
  561. {
  562. throw new SAXException (e);
  563. }
  564. }
  565. }
  566. private void unparsedEntityDecl (String name, String publicId,
  567. String systemId, String notationName)
  568. throws SAXException
  569. {
  570. if (seenFatalError || dtdHandler == null)
  571. {
  572. return;
  573. }
  574. try
  575. {
  576. systemId = XMLJ.getAbsoluteURI (base, systemId);
  577. dtdHandler.unparsedEntityDecl (name, publicId, systemId,
  578. notationName);
  579. }
  580. catch (Exception e)
  581. {
  582. if (e instanceof SAXException)
  583. {
  584. throw (SAXException) e;
  585. }
  586. else
  587. {
  588. throw new SAXException (e);
  589. }
  590. }
  591. }
  592. private void setDocumentLocator (Object ctx, Object loc)
  593. {
  594. locator = new GnomeLocator (ctx, loc);
  595. if (seenFatalError || contentHandler == null)
  596. {
  597. return;
  598. }
  599. try
  600. {
  601. contentHandler.setDocumentLocator (locator);
  602. }
  603. catch (Exception e)
  604. {
  605. }
  606. }
  607. private void startDocument (boolean standalone)
  608. throws SAXException
  609. {
  610. this.standalone = standalone;
  611. seenStartDocument = true;
  612. if (contentHandler == null)
  613. {
  614. return;
  615. }
  616. try
  617. {
  618. contentHandler.startDocument ();
  619. }
  620. catch (Exception e)
  621. {
  622. if (e instanceof SAXException)
  623. {
  624. throw (SAXException) e;
  625. }
  626. else
  627. {
  628. throw new SAXException (e);
  629. }
  630. }
  631. }
  632. private void endDocument ()
  633. throws SAXException
  634. {
  635. if (contentHandler == null)
  636. {
  637. return;
  638. }
  639. try
  640. {
  641. contentHandler.endDocument();
  642. }
  643. catch (Exception e)
  644. {
  645. if (e instanceof SAXException)
  646. {
  647. throw (SAXException) e;
  648. }
  649. else
  650. {
  651. throw new SAXException (e);
  652. }
  653. }
  654. }
  655. private void startElement(String name, String[] attrs)
  656. throws SAXException
  657. {
  658. if (seenFatalError || contentHandler == null)
  659. {
  660. return;
  661. }
  662. try
  663. {
  664. XMLName xName = new XMLName (this, name);
  665. if (namespaces)
  666. {
  667. // Handle defined namespaces
  668. ns.push ();
  669. int len = (attrs == null) ? 0 : attrs.length;
  670. if (len > 0)
  671. {
  672. ArrayList filtered = new ArrayList (len);
  673. for (int i = 0; i < len; i += 2)
  674. {
  675. String attName = attrs[i];
  676. String attValue = attrs[i + 1];
  677. if (attName.equals ("xmlns"))
  678. {
  679. startPrefixMapping ("", attValue);
  680. }
  681. else if (attName.startsWith ("xmlns:"))
  682. {
  683. startPrefixMapping (attName.substring (6), attValue);
  684. }
  685. else
  686. {
  687. filtered.add (attName);
  688. filtered.add (attValue);
  689. }
  690. }
  691. // Remove xmlns attributes
  692. attrs = new String[filtered.size ()];
  693. filtered.toArray (attrs);
  694. }
  695. }
  696. // Construct attributes
  697. Attributes atts = new StringArrayAttributes (this, attrs);
  698. contentHandler.startElement (xName.uri, xName.localName, xName.qName,
  699. atts);
  700. }
  701. catch (Exception e)
  702. {
  703. if (e instanceof SAXException)
  704. {
  705. throw (SAXException) e;
  706. }
  707. else
  708. {
  709. throw new SAXException (e);
  710. }
  711. }
  712. }
  713. private void endElement (String name)
  714. throws SAXException
  715. {
  716. if (seenFatalError || contentHandler == null)
  717. {
  718. return;
  719. }
  720. try
  721. {
  722. XMLName xName = new XMLName (this, name);
  723. String uri = (xName.uri == null) ? "" : xName.uri;
  724. contentHandler.endElement (uri, xName.localName, xName.qName);
  725. // Handle undefining namespaces
  726. if (namespaces)
  727. {
  728. for (Iterator i = ns.currentPrefixes (); i.hasNext (); )
  729. {
  730. endPrefixMapping ((String) i.next ());
  731. }
  732. ns.pop (); // releases current depth
  733. }
  734. }
  735. catch (Exception e)
  736. {
  737. if (e instanceof SAXException)
  738. {
  739. throw (SAXException) e;
  740. }
  741. else
  742. {
  743. throw new SAXException (e);
  744. }
  745. }
  746. }
  747. private void startPrefixMapping (String prefix, String uri)
  748. throws SAXException
  749. {
  750. if (seenFatalError || contentHandler == null)
  751. {
  752. return;
  753. }
  754. ns.define (prefix, uri);
  755. contentHandler.startPrefixMapping (prefix, uri);
  756. }
  757. private void endPrefixMapping (String prefix)
  758. throws SAXException
  759. {
  760. if (seenFatalError || contentHandler == null)
  761. {
  762. return;
  763. }
  764. contentHandler.endPrefixMapping (prefix);
  765. }
  766. private void characters (String text)
  767. throws SAXException
  768. {
  769. if (seenFatalError || contentHandler == null || text == null)
  770. {
  771. return;
  772. }
  773. try
  774. {
  775. char[] ch = text.toCharArray ();
  776. contentHandler.characters (ch, 0, ch.length);
  777. }
  778. catch (Exception e)
  779. {
  780. if (e instanceof SAXException)
  781. {
  782. throw (SAXException) e;
  783. }
  784. else
  785. {
  786. throw new SAXException (e);
  787. }
  788. }
  789. }
  790. private void ignorableWhitespace (String text)
  791. throws SAXException
  792. {
  793. if (seenFatalError || contentHandler == null || text == null)
  794. {
  795. return;
  796. }
  797. try
  798. {
  799. char[] ch = text.toCharArray ();
  800. contentHandler.ignorableWhitespace (ch, 0, ch.length);
  801. }
  802. catch (Exception e)
  803. {
  804. if (e instanceof SAXException)
  805. {
  806. throw (SAXException) e;
  807. }
  808. else
  809. {
  810. throw new SAXException (e);
  811. }
  812. }
  813. }
  814. private void processingInstruction (String target, String data)
  815. throws SAXException
  816. {
  817. if (seenFatalError || contentHandler == null)
  818. {
  819. return;
  820. }
  821. try
  822. {
  823. if (data == null)
  824. {
  825. data = "";
  826. }
  827. contentHandler.processingInstruction (target, data);
  828. }
  829. catch (Exception e)
  830. {
  831. if (e instanceof SAXException)
  832. {
  833. throw (SAXException) e;
  834. }
  835. else
  836. {
  837. throw new SAXException (e);
  838. }
  839. }
  840. }
  841. private void comment (String text)
  842. throws SAXException
  843. {
  844. if (seenFatalError || lexicalHandler == null || text == null)
  845. {
  846. return;
  847. }
  848. try
  849. {
  850. char[] ch = text.toCharArray ();
  851. lexicalHandler.comment (ch, 0, ch.length);
  852. }
  853. catch (Exception e)
  854. {
  855. if (e instanceof SAXException)
  856. {
  857. throw (SAXException) e;
  858. }
  859. else
  860. {
  861. throw new SAXException (e);
  862. }
  863. }
  864. }
  865. private void cdataBlock (String text)
  866. throws SAXException
  867. {
  868. if (seenFatalError || text == null)
  869. {
  870. return;
  871. }
  872. try
  873. {
  874. if (lexicalHandler == null)
  875. {
  876. characters(text);
  877. }
  878. else
  879. {
  880. lexicalHandler.startCDATA();
  881. characters(text);
  882. lexicalHandler.endCDATA();
  883. }
  884. }
  885. catch (Exception e)
  886. {
  887. if (e instanceof SAXException)
  888. {
  889. throw (SAXException) e;
  890. }
  891. else
  892. {
  893. throw new SAXException (e);
  894. }
  895. }
  896. }
  897. private void warning (String message,
  898. int lineNumber, int columnNumber,
  899. String publicId, String systemId)
  900. throws SAXException
  901. {
  902. if (seenFatalError || errorHandler == null)
  903. {
  904. return;
  905. }
  906. try
  907. {
  908. Locator l = new StandaloneLocator (lineNumber, columnNumber,
  909. publicId, systemId);
  910. errorHandler.warning (new SAXParseException (message, l));
  911. }
  912. catch (Exception e)
  913. {
  914. if (e instanceof SAXException)
  915. {
  916. throw (SAXException) e;
  917. }
  918. else
  919. {
  920. throw new SAXException (e);
  921. }
  922. }
  923. }
  924. private void error (String message,
  925. int lineNumber, int columnNumber,
  926. String publicId, String systemId)
  927. throws SAXException
  928. {
  929. if (seenFatalError || errorHandler == null)
  930. {
  931. return;
  932. }
  933. try
  934. {
  935. Locator l = new StandaloneLocator (lineNumber, columnNumber,
  936. publicId, systemId);
  937. errorHandler.error (new SAXParseException (message, l));
  938. }
  939. catch (Exception e)
  940. {
  941. if (e instanceof SAXException)
  942. {
  943. throw (SAXException) e;
  944. }
  945. else
  946. {
  947. throw new SAXException (e);
  948. }
  949. }
  950. }
  951. private void fatalError (String message,
  952. int lineNumber, int columnNumber,
  953. String publicId, String systemId)
  954. throws SAXException
  955. {
  956. if (seenFatalError || errorHandler == null)
  957. {
  958. return;
  959. }
  960. try
  961. {
  962. if (!seenStartDocument)
  963. {
  964. startDocument (false);
  965. }
  966. seenFatalError = true;
  967. Locator l = new StandaloneLocator (lineNumber, columnNumber,
  968. publicId, systemId);
  969. errorHandler.fatalError (new SAXParseException (message, l));
  970. }
  971. catch (Exception e)
  972. {
  973. if (e instanceof SAXException)
  974. {
  975. throw (SAXException) e;
  976. }
  977. else
  978. {
  979. throw new SAXException (e);
  980. }
  981. }
  982. }
  983. }