1. /*
  2. * Copyright 2001-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. package org.apache.commons.betwixt.io;
  17. import org.apache.commons.logging.Log;
  18. import org.apache.commons.logging.LogFactory;
  19. import org.xml.sax.Attributes;
  20. import org.xml.sax.ContentHandler;
  21. import org.xml.sax.SAXException;
  22. // FIX ME
  23. // At the moment, namespaces are NOT supported!
  24. /**
  25. * The SAXBeanwriter will send events to a ContentHandler
  26. *
  27. * @author <a href="mailto:rdonkin@apache.org">Robert Burrell Donkin</a>
  28. * @author <a href="mailto:martin@mvdb.net">Martin van den Bemt</a>
  29. */
  30. public class SAXBeanWriter extends AbstractBeanWriter {
  31. /** Where the output goes */
  32. private ContentHandler contentHandler;
  33. /** Log used for logging (Doh!) */
  34. private Log log = LogFactory.getLog( SAXBeanWriter.class );
  35. /** Should document events (ie. start and end) be called? */
  36. private boolean callDocumentEvents = true;
  37. /**
  38. * <p> Constructor sets writer used for output.</p>
  39. *
  40. * @param contentHandler feed events to this content handler
  41. */
  42. public SAXBeanWriter(ContentHandler contentHandler) {
  43. this.contentHandler = contentHandler;
  44. }
  45. /**
  46. * Should document events (ie start and end) be called?
  47. *
  48. * @return true if this SAXWriter should call start
  49. * and end of the content handler
  50. * @since 0.5
  51. */
  52. public boolean getCallDocumentEvents() {
  53. return callDocumentEvents;
  54. }
  55. /**
  56. * Sets whether the document events (ie start and end) should be called.
  57. *
  58. * @param callDocumentEvents should document events be called
  59. * @since 0.5
  60. */
  61. public void setCallDocumentEvents(boolean callDocumentEvents) {
  62. this.callDocumentEvents = callDocumentEvents;
  63. }
  64. /**
  65. * <p> Set the log implementation used. </p>
  66. *
  67. * @return <code>Log</code> implementation that this class logs to
  68. */
  69. public Log getLog() {
  70. return log;
  71. }
  72. /**
  73. * <p> Set the log implementation used. </p>
  74. *
  75. * @param log <code>Log</code> implementation to use
  76. */
  77. public void setLog(Log log) {
  78. this.log = log;
  79. }
  80. // Expression methods
  81. //-------------------------------------------------------------------------
  82. // Replaced by new API
  83. // New API
  84. // -------------------------------------------------------------------------
  85. /**
  86. * Writes the start tag for an element.
  87. *
  88. * @param uri the element's namespace uri
  89. * @param localName the element's local name
  90. * @param qName the element's qualified name
  91. * @param attributes the element's attributes
  92. * @throws SAXException if an SAX problem occurs during writing
  93. * @since 0.5
  94. */
  95. protected void startElement(
  96. WriteContext context,
  97. String uri,
  98. String localName,
  99. String qName,
  100. Attributes attributes)
  101. throws
  102. SAXException {
  103. contentHandler.startElement(
  104. uri,
  105. localName,
  106. qName,
  107. attributes);
  108. }
  109. /**
  110. * Writes the end tag for an element
  111. *
  112. * @param uri the element's namespace uri
  113. * @param localName the element's local name
  114. * @param qName the element's qualified name
  115. * @throws SAXException if an SAX problem occurs during writing
  116. * @since 0.5
  117. */
  118. protected void endElement(
  119. WriteContext context,
  120. String uri,
  121. String localName,
  122. String qName)
  123. throws
  124. SAXException {
  125. contentHandler.endElement(
  126. uri,
  127. localName,
  128. qName);
  129. }
  130. /**
  131. * Express body text
  132. * @param text the element body text
  133. * @throws SAXException if the <code>ContentHandler</code> has a problem
  134. * @since 0.5
  135. */
  136. protected void bodyText(WriteContext context, String text) throws SAXException {
  137. //TODO:
  138. // FIX ME
  139. // CHECK UNICODE->CHAR CONVERSION!
  140. // THIS WILL QUITE POSSIBLY BREAK FOR NON-ROMAN
  141. char[] body = text.toCharArray();
  142. contentHandler.characters(body, 0, body.length);
  143. }
  144. /**
  145. * This will announce the start of the document
  146. * to the contenthandler.
  147. *
  148. * @see org.apache.commons.betwixt.io.AbstractBeanWriter#end()
  149. */
  150. public void start() throws SAXException {
  151. if ( callDocumentEvents ) {
  152. contentHandler.startDocument();
  153. }
  154. }
  155. /**
  156. * This method will announce the end of the document to
  157. * the contenthandler.
  158. *
  159. * @see org.apache.commons.betwixt.io.AbstractBeanWriter#start()
  160. */
  161. public void end() throws SAXException {
  162. if ( callDocumentEvents ) {
  163. contentHandler.endDocument();
  164. }
  165. }
  166. }