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.

GenerationHelperContentHandler.java 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 org.apache.fop.util;
  19. import org.xml.sax.Attributes;
  20. import org.xml.sax.ContentHandler;
  21. import org.xml.sax.SAXException;
  22. import org.xml.sax.helpers.AttributesImpl;
  23. import org.apache.xmlgraphics.util.QName;
  24. /**
  25. * This class is a delegating SAX ContentHandler which has the purpose to provide a few handy
  26. * methods that make life easier when generating SAX events.
  27. */
  28. public class GenerationHelperContentHandler extends DelegatingContentHandler {
  29. private static final Attributes EMPTY_ATTS = new AttributesImpl();
  30. private String mainNamespace;
  31. private Object contentHandlerContext;
  32. /**
  33. * Main constructor. If the given handler also implements any of the EntityResolver,
  34. * DTDHandler, LexicalHandler or ErrorHandler interfaces, these are set automatically.
  35. * @param handler the SAX content handler to delegate all calls to
  36. * @param mainNamespace the main namespace used for generated XML content when abbreviated
  37. * ContentHandler calls are used.
  38. * @param contentHandlerContext additional content handler context state
  39. */
  40. public GenerationHelperContentHandler(ContentHandler handler, String mainNamespace, Object contentHandlerContext) {
  41. super(handler);
  42. this.mainNamespace = mainNamespace;
  43. this.contentHandlerContext = contentHandlerContext;
  44. }
  45. /**
  46. * Returns the main namespace used for generated XML content.
  47. * @return the main namespace
  48. */
  49. public String getMainNamespace() {
  50. return this.mainNamespace;
  51. }
  52. /**
  53. * Sets the main namespace used for generated XML content when abbreviated ContentHandler
  54. * calls are used.
  55. * @param namespaceURI the new main namespace URI
  56. */
  57. public void setMainNamespace(String namespaceURI) {
  58. this.mainNamespace = namespaceURI;
  59. }
  60. /**
  61. * Returns the context object (may be null).
  62. * @return the context object
  63. */
  64. public Object getContentHandlerContext() {
  65. return this.contentHandlerContext;
  66. }
  67. /**
  68. * Convenience method to generate a startElement SAX event.
  69. * @param localName the local name of the element
  70. * @param atts the attributes
  71. * @throws SAXException if a SAX exception occurs
  72. */
  73. public void startElement(String localName, Attributes atts) throws SAXException {
  74. getDelegateContentHandler().startElement(getMainNamespace(), localName, localName, atts);
  75. }
  76. /**
  77. * Convenience method to generate a startElement SAX event.
  78. * @param localName the local name of the element
  79. * @throws SAXException if a SAX exception occurs
  80. */
  81. public void startElement(String localName) throws SAXException {
  82. startElement(localName, EMPTY_ATTS);
  83. }
  84. /**
  85. * Convenience method to generate a startElement SAX event.
  86. * @param qName the qualified name of the element
  87. * @param atts the attributes
  88. * @throws SAXException if a SAX exception occurs
  89. */
  90. public void startElement(QName qName, Attributes atts) throws SAXException {
  91. getDelegateContentHandler().startElement(qName.getNamespaceURI(), qName.getLocalName(),
  92. qName.getQName(), atts);
  93. }
  94. /**
  95. * Convenience method to generate a startElement SAX event.
  96. * @param qName the qualified name of the element
  97. * @throws SAXException if a SAX exception occurs
  98. */
  99. public void startElement(QName qName) throws SAXException {
  100. startElement(qName, EMPTY_ATTS);
  101. }
  102. /**
  103. * Convenience method to generate a endElement SAX event.
  104. * @param localName the local name of the element
  105. * @throws SAXException if a SAX exception occurs
  106. */
  107. public void endElement(String localName) throws SAXException {
  108. getDelegateContentHandler().endElement(getMainNamespace(), localName, localName);
  109. }
  110. /**
  111. * Convenience method to generate a startElement SAX event.
  112. * @param qName the qualified name of the element
  113. * @throws SAXException if a SAX exception occurs
  114. */
  115. public void endElement(QName qName) throws SAXException {
  116. getDelegateContentHandler().endElement(qName.getNamespaceURI(), qName.getLocalName(),
  117. qName.getQName());
  118. }
  119. /**
  120. * Convenience method to generate an empty element with attributes.
  121. * @param localName the local name of the element
  122. * @param atts the attributes
  123. * @throws SAXException if a SAX exception occurs
  124. */
  125. public void element(String localName, Attributes atts) throws SAXException {
  126. getDelegateContentHandler().startElement(getMainNamespace(), localName, localName, atts);
  127. getDelegateContentHandler().endElement(getMainNamespace(), localName, localName);
  128. }
  129. /**
  130. * Convenience method to generate an empty element with attributes.
  131. * @param qName the qualified name of the element
  132. * @param atts the attributes
  133. * @throws SAXException if a SAX exception occurs
  134. */
  135. public void element(QName qName, Attributes atts) throws SAXException {
  136. getDelegateContentHandler().startElement(qName.getNamespaceURI(), qName.getLocalName(),
  137. qName.getQName(), atts);
  138. getDelegateContentHandler().endElement(qName.getNamespaceURI(), qName.getLocalName(),
  139. qName.getQName());
  140. }
  141. }