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.6KB

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