You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

EasyGenerationContentHandlerProxy.java 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*
  2. * Copyright 1999-2004 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /* $Id$ */
  17. package embedding.tools;
  18. //SAX
  19. import org.xml.sax.ContentHandler;
  20. import org.xml.sax.Locator;
  21. import org.xml.sax.Attributes;
  22. import org.xml.sax.SAXException;
  23. import org.xml.sax.helpers.AttributesImpl;
  24. /**
  25. * This class is an implementation of ContentHandler which acts as a proxy to
  26. * another ContentHandler and has the purpose to provide a few handy methods
  27. * that make life easier when generating SAX events.
  28. * <br>
  29. * Note: This class is only useful for simple cases with no namespaces.
  30. */
  31. public class EasyGenerationContentHandlerProxy implements ContentHandler {
  32. /** An empty Attributes object used when no attributes are needed. */
  33. public static final Attributes EMPTY_ATTS = new AttributesImpl();
  34. private ContentHandler target;
  35. /**
  36. * Main constructor.
  37. * @param forwardTo ContentHandler to forward the SAX event to.
  38. */
  39. public EasyGenerationContentHandlerProxy(ContentHandler forwardTo) {
  40. this.target = forwardTo;
  41. }
  42. /**
  43. * Sends the notification of the beginning of an element.
  44. * @param name Name for the element.
  45. * @throws SAXException Any SAX exception, possibly wrapping another exception.
  46. */
  47. public void startElement(String name) throws SAXException {
  48. startElement(name, EMPTY_ATTS);
  49. }
  50. /**
  51. * Sends the notification of the beginning of an element.
  52. * @param name Name for the element.
  53. * @param atts The attributes attached to the element. If there are no
  54. * attributes, it shall be an empty Attributes object.
  55. * @throws SAXException Any SAX exception, possibly wrapping another exception.
  56. */
  57. public void startElement(String name, Attributes atts) throws SAXException {
  58. startElement(null, name, name, atts);
  59. }
  60. /**
  61. * Send a String of character data.
  62. * @param s The content String
  63. * @throws SAXException Any SAX exception, possibly wrapping another exception.
  64. */
  65. public void characters(String s) throws SAXException {
  66. target.characters(s.toCharArray(), 0, s.length());
  67. }
  68. /**
  69. * Send the notification of the end of an element.
  70. * @param name Name for the element.
  71. * @throws SAXException Any SAX exception, possibly wrapping another exception.
  72. */
  73. public void endElement(String name) throws SAXException {
  74. endElement(null, name, name);
  75. }
  76. /**
  77. * Sends notifications for a whole element with some String content.
  78. * @param name Name for the element.
  79. * @param value Content of the element.
  80. * @throws SAXException Any SAX exception, possibly wrapping another exception.
  81. */
  82. public void element(String name, String value) throws SAXException {
  83. element(name, value, EMPTY_ATTS);
  84. }
  85. /**
  86. * Sends notifications for a whole element with some String content.
  87. * @param name Name for the element.
  88. * @param value Content of the element.
  89. * @param atts The attributes attached to the element. If there are no
  90. * attributes, it shall be an empty Attributes object.
  91. * @throws SAXException Any SAX exception, possibly wrapping another exception.
  92. */
  93. public void element(String name, String value, Attributes atts) throws SAXException {
  94. startElement(name, atts);
  95. if (value != null) {
  96. characters(value.toCharArray(), 0, value.length());
  97. }
  98. endElement(name);
  99. }
  100. /* =========== ContentHandler interface =========== */
  101. /**
  102. * @see org.xml.sax.ContentHandler#setDocumentLocator(Locator)
  103. */
  104. public void setDocumentLocator(Locator locator) {
  105. target.setDocumentLocator(locator);
  106. }
  107. /**
  108. * @see org.xml.sax.ContentHandler#startDocument()
  109. */
  110. public void startDocument() throws SAXException {
  111. target.startDocument();
  112. }
  113. /**
  114. * @see org.xml.sax.ContentHandler#endDocument()
  115. */
  116. public void endDocument() throws SAXException {
  117. target.endDocument();
  118. }
  119. /**
  120. * @see org.xml.sax.ContentHandler#startPrefixMapping(String, String)
  121. */
  122. public void startPrefixMapping(String prefix, String uri) throws SAXException {
  123. target.startPrefixMapping(prefix, uri);
  124. }
  125. /**
  126. * @see org.xml.sax.ContentHandler#endPrefixMapping(String)
  127. */
  128. public void endPrefixMapping(String prefix) throws SAXException {
  129. target.endPrefixMapping(prefix);
  130. }
  131. /**
  132. * @see org.xml.sax.ContentHandler#startElement(String, String, String, Attributes)
  133. */
  134. public void startElement(String namespaceURI, String localName,
  135. String qName, Attributes atts) throws SAXException {
  136. target.startElement(namespaceURI, localName, qName, atts);
  137. }
  138. /**
  139. * @see org.xml.sax.ContentHandler#endElement(String, String, String)
  140. */
  141. public void endElement(String namespaceURI, String localName, String qName)
  142. throws SAXException {
  143. target.endElement(namespaceURI, localName, qName);
  144. }
  145. /**
  146. * @see org.xml.sax.ContentHandler#characters(char[], int, int)
  147. */
  148. public void characters(char[] ch, int start, int length) throws SAXException {
  149. target.characters(ch, start, length);
  150. }
  151. /**
  152. * @see org.xml.sax.ContentHandler#ignorableWhitespace(char[], int, int)
  153. */
  154. public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException {
  155. target.ignorableWhitespace(ch, start, length);
  156. }
  157. /**
  158. * @see org.xml.sax.ContentHandler#processingInstruction(String, String)
  159. */
  160. public void processingInstruction(String target, String data) throws SAXException {
  161. this.target.processingInstruction(target, data);
  162. }
  163. /**
  164. * @see org.xml.sax.ContentHandler#skippedEntity(String)
  165. */
  166. public void skippedEntity(String name) throws SAXException {
  167. target.skippedEntity(name);
  168. }
  169. }