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.

AbstractXMLRenderer.java 8.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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.render.xml;
  19. import java.awt.geom.Rectangle2D;
  20. import java.io.IOException;
  21. import java.io.OutputStream;
  22. import java.util.List;
  23. import javax.xml.transform.TransformerConfigurationException;
  24. import javax.xml.transform.sax.SAXTransformerFactory;
  25. import javax.xml.transform.sax.TransformerHandler;
  26. import javax.xml.transform.stream.StreamResult;
  27. import org.xml.sax.Attributes;
  28. import org.xml.sax.ContentHandler;
  29. import org.xml.sax.SAXException;
  30. import org.xml.sax.ext.LexicalHandler;
  31. import org.xml.sax.helpers.AttributesImpl;
  32. import org.apache.xmlgraphics.util.QName;
  33. import org.apache.fop.apps.FOUserAgent;
  34. import org.apache.fop.area.BookmarkData;
  35. import org.apache.fop.area.OffDocumentExtensionAttachment;
  36. import org.apache.fop.area.OffDocumentItem;
  37. import org.apache.fop.area.PageViewport;
  38. import org.apache.fop.fo.extensions.ExtensionAttachment;
  39. import org.apache.fop.render.PrintRenderer;
  40. import org.apache.fop.render.RendererContext;
  41. /** Abstract xml renderer base class. */
  42. public abstract class AbstractXMLRenderer extends PrintRenderer {
  43. public AbstractXMLRenderer(FOUserAgent userAgent) {
  44. super(userAgent);
  45. }
  46. /** Main namespace in use. */
  47. public static final String NS = "";
  48. /** CDATA type */
  49. public static final String CDATA = "CDATA";
  50. /** An empty Attributes object used when no attributes are needed. */
  51. public static final Attributes EMPTY_ATTS = new AttributesImpl();
  52. /** AttributesImpl instance that can be used during XML generation. */
  53. protected AttributesImpl atts = new AttributesImpl();
  54. /** ContentHandler that the generated XML is written to */
  55. protected ContentHandler handler;
  56. /** The OutputStream to write the generated XML to. */
  57. protected OutputStream out;
  58. /** The renderer context. */
  59. protected RendererContext context;
  60. /** A list of ExtensionAttachements received through processOffDocumentItem() */
  61. protected List extensionAttachments;
  62. /**
  63. * Handles SAXExceptions.
  64. * @param saxe the SAXException to handle
  65. */
  66. protected void handleSAXException(SAXException saxe) {
  67. throw new RuntimeException(saxe.getMessage());
  68. }
  69. /**
  70. * Handles page extension attachments
  71. * @param page the page viewport
  72. */
  73. protected void handlePageExtensionAttachments(PageViewport page) {
  74. handleExtensionAttachments(page.getExtensionAttachments());
  75. }
  76. /**
  77. * Writes a comment to the generated XML.
  78. * @param comment the comment
  79. */
  80. protected void comment(String comment) {
  81. if (handler instanceof LexicalHandler) {
  82. try {
  83. ((LexicalHandler) handler).comment(comment.toCharArray(), 0, comment.length());
  84. } catch (SAXException saxe) {
  85. handleSAXException(saxe);
  86. }
  87. }
  88. }
  89. /**
  90. * Starts a new element (without attributes).
  91. * @param tagName tag name of the element
  92. */
  93. protected void startElement(String tagName) {
  94. startElement(tagName, EMPTY_ATTS);
  95. }
  96. /**
  97. * Starts a new element.
  98. * @param tagName tag name of the element
  99. * @param atts attributes to add
  100. */
  101. protected void startElement(String tagName, Attributes atts) {
  102. try {
  103. handler.startElement(NS, tagName, tagName, atts);
  104. } catch (SAXException saxe) {
  105. handleSAXException(saxe);
  106. }
  107. }
  108. /**
  109. * Ends an element.
  110. * @param tagName tag name of the element
  111. */
  112. protected void endElement(String tagName) {
  113. try {
  114. handler.endElement(NS, tagName, tagName);
  115. } catch (SAXException saxe) {
  116. handleSAXException(saxe);
  117. }
  118. }
  119. /**
  120. * Sends plain text to the XML
  121. * @param text the text
  122. */
  123. protected void characters(String text) {
  124. try {
  125. char[] ca = text.toCharArray();
  126. handler.characters(ca, 0, ca.length);
  127. } catch (SAXException saxe) {
  128. handleSAXException(saxe);
  129. }
  130. }
  131. /**
  132. * Adds a new attribute to the protected member variable "atts".
  133. * @param name name of the attribute
  134. * @param value value of the attribute
  135. */
  136. protected void addAttribute(String name, String value) {
  137. atts.addAttribute(NS, name, name, CDATA, value);
  138. }
  139. /**
  140. * Adds a new attribute to the protected member variable "atts".
  141. * @param name name of the attribute
  142. * @param value value of the attribute
  143. */
  144. protected void addAttribute(QName name, String value) {
  145. atts.addAttribute(name.getNamespaceURI(), name.getLocalName(), name.getQName(),
  146. CDATA, value);
  147. }
  148. /**
  149. * Adds a new attribute to the protected member variable "atts".
  150. * @param name name of the attribute
  151. * @param value value of the attribute
  152. */
  153. protected void addAttribute(String name, int value) {
  154. addAttribute(name, Integer.toString(value));
  155. }
  156. private String createString(Rectangle2D rect) {
  157. return "" + (int) rect.getX() + " " + (int) rect.getY() + " "
  158. + (int) rect.getWidth() + " " + (int) rect.getHeight();
  159. }
  160. /**
  161. * Adds a new attribute to the protected member variable "atts".
  162. * @param name name of the attribute
  163. * @param rect a Rectangle2D to format and use as attribute value
  164. */
  165. protected void addAttribute(String name, Rectangle2D rect) {
  166. addAttribute(name, createString(rect));
  167. }
  168. /** {@inheritDoc} */
  169. public void startRenderer(OutputStream outputStream)
  170. throws IOException {
  171. if (this.handler == null) {
  172. SAXTransformerFactory factory
  173. = (SAXTransformerFactory)SAXTransformerFactory.newInstance();
  174. try {
  175. TransformerHandler transformerHandler = factory.newTransformerHandler();
  176. setContentHandler(transformerHandler);
  177. StreamResult res = new StreamResult(outputStream);
  178. transformerHandler.setResult(res);
  179. } catch (TransformerConfigurationException tce) {
  180. throw new RuntimeException(tce.getMessage());
  181. }
  182. this.out = outputStream;
  183. }
  184. try {
  185. handler.startDocument();
  186. } catch (SAXException saxe) {
  187. handleSAXException(saxe);
  188. }
  189. }
  190. /** {@inheritDoc} */
  191. public void stopRenderer() throws IOException {
  192. try {
  193. handler.endDocument();
  194. } catch (SAXException saxe) {
  195. handleSAXException(saxe);
  196. }
  197. if (this.out != null) {
  198. this.out.flush();
  199. }
  200. }
  201. /** {@inheritDoc} */
  202. public void processOffDocumentItem(OffDocumentItem oDI) {
  203. if (oDI instanceof BookmarkData) {
  204. renderBookmarkTree((BookmarkData) oDI);
  205. } else if (oDI instanceof OffDocumentExtensionAttachment) {
  206. ExtensionAttachment attachment = ((OffDocumentExtensionAttachment)oDI).getAttachment();
  207. if (extensionAttachments == null) {
  208. extensionAttachments = new java.util.ArrayList();
  209. }
  210. extensionAttachments.add(attachment);
  211. } else {
  212. String warn = "Ignoring OffDocumentItem: " + oDI;
  213. log.warn(warn);
  214. }
  215. }
  216. /** Handle document extension attachments. */
  217. protected void handleDocumentExtensionAttachments() {
  218. if (extensionAttachments != null && extensionAttachments.size() > 0) {
  219. handleExtensionAttachments(extensionAttachments);
  220. extensionAttachments.clear();
  221. }
  222. }
  223. /**
  224. * Sets an outside TransformerHandler to use instead of the default one
  225. * create in this class in startRenderer().
  226. * @param handler Overriding TransformerHandler
  227. */
  228. public void setContentHandler(ContentHandler handler) {
  229. this.handler = handler;
  230. }
  231. /**
  232. * Handles a list of extension attachments
  233. * @param attachments a list of extension attachments
  234. */
  235. protected abstract void handleExtensionAttachments(List attachments);
  236. /**
  237. * Renders a bookmark tree
  238. * @param odi the bookmark data
  239. */
  240. protected abstract void renderBookmarkTree(BookmarkData odi);
  241. }