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.

SVGDocumentHandler.java 9.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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.svg;
  19. import java.awt.Dimension;
  20. import java.io.IOException;
  21. import java.io.OutputStream;
  22. import javax.xml.parsers.DocumentBuilder;
  23. import javax.xml.parsers.DocumentBuilderFactory;
  24. import javax.xml.parsers.ParserConfigurationException;
  25. import javax.xml.transform.Result;
  26. import javax.xml.transform.Source;
  27. import javax.xml.transform.Transformer;
  28. import javax.xml.transform.TransformerConfigurationException;
  29. import javax.xml.transform.TransformerException;
  30. import javax.xml.transform.dom.DOMResult;
  31. import javax.xml.transform.dom.DOMSource;
  32. import javax.xml.transform.sax.SAXResult;
  33. import javax.xml.transform.sax.TransformerHandler;
  34. import javax.xml.transform.stream.StreamResult;
  35. import org.w3c.dom.Document;
  36. import org.xml.sax.ContentHandler;
  37. import org.xml.sax.SAXException;
  38. import org.xml.sax.helpers.AttributesImpl;
  39. import org.apache.commons.io.IOUtils;
  40. import org.apache.fop.render.bitmap.BitmapRendererEventProducer;
  41. import org.apache.fop.render.bitmap.MultiFileRenderingUtil;
  42. import org.apache.fop.render.intermediate.DelegatingFragmentContentHandler;
  43. import org.apache.fop.render.intermediate.IFContext;
  44. import org.apache.fop.render.intermediate.IFException;
  45. import org.apache.fop.render.intermediate.IFPainter;
  46. import org.apache.fop.util.GenerationHelperContentHandler;
  47. import org.apache.fop.util.XMLUtil;
  48. /**
  49. * {@link org.apache.fop.render.intermediate.IFDocumentHandler} implementation
  50. * that writes SVG 1.1.
  51. */
  52. public class SVGDocumentHandler extends AbstractSVGDocumentHandler {
  53. /** Helper class for generating multiple files */
  54. private MultiFileRenderingUtil multiFileUtil;
  55. private StreamResult firstStream;
  56. private StreamResult currentStream;
  57. /** Used for single-page documents rendered to a DOM or SAX. */
  58. private Result simpleResult;
  59. private Document reusedParts;
  60. /**
  61. * Default constructor.
  62. */
  63. public SVGDocumentHandler(IFContext context) {
  64. super(context);
  65. }
  66. /** {@inheritDoc} */
  67. public boolean supportsPagesOutOfOrder() {
  68. return true;
  69. }
  70. /** {@inheritDoc} */
  71. public String getMimeType() {
  72. return MIME_TYPE;
  73. }
  74. /** {@inheritDoc} */
  75. public void setResult(Result result) throws IFException {
  76. if (result instanceof StreamResult) {
  77. multiFileUtil = new MultiFileRenderingUtil(FILE_EXTENSION_SVG,
  78. getUserAgent().getOutputFile());
  79. this.firstStream = (StreamResult)result;
  80. } else {
  81. this.simpleResult = result;
  82. }
  83. }
  84. /** {@inheritDoc} */
  85. public void startDocument() throws IFException {
  86. super.startDocument();
  87. DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
  88. builderFactory.setNamespaceAware(true);
  89. builderFactory.setValidating(false);
  90. try {
  91. DocumentBuilder builder = builderFactory.newDocumentBuilder();
  92. this.reusedParts = builder.newDocument();
  93. } catch (ParserConfigurationException e) {
  94. throw new IFException("Error while setting up a DOM for SVG generation", e);
  95. }
  96. try {
  97. TransformerHandler toDOMHandler = tFactory.newTransformerHandler();
  98. toDOMHandler.setResult(new DOMResult(this.reusedParts));
  99. this.handler = decorate(toDOMHandler);
  100. this.handler.startDocument();
  101. } catch (SAXException se) {
  102. throw new IFException("SAX error in startDocument()", se);
  103. } catch (TransformerConfigurationException e) {
  104. throw new IFException(
  105. "Error while setting up a TransformerHandler for SVG generation", e);
  106. }
  107. }
  108. /** {@inheritDoc} */
  109. public void endDocument() throws IFException {
  110. //nop
  111. }
  112. /** {@inheritDoc} */
  113. public void endDocumentHeader() throws IFException {
  114. super.endDocumentHeader();
  115. try {
  116. //Stop recording parts reused for each page
  117. this.handler.endDocument();
  118. this.handler = null;
  119. } catch (SAXException e) {
  120. throw new IFException("SAX error in endDocumentHeader()", e);
  121. }
  122. }
  123. /** {@inheritDoc} */
  124. public void startPageSequence(String id) throws IFException {
  125. //nop
  126. }
  127. /** {@inheritDoc} */
  128. public void endPageSequence() throws IFException {
  129. //nop
  130. }
  131. /** {@inheritDoc} */
  132. public void startPage(int index, String name, String pageMasterName, Dimension size)
  133. throws IFException {
  134. if (this.multiFileUtil != null) {
  135. prepareHandlerWithOutputStream(index);
  136. } else {
  137. if (this.simpleResult == null) {
  138. //Only one page is supported with this approach at the moment
  139. throw new IFException(
  140. "Only one page is supported for output with the given Result instance!",
  141. null);
  142. }
  143. super.setResult(this.simpleResult);
  144. this.simpleResult = null;
  145. }
  146. try {
  147. handler.startDocument();
  148. handler.startPrefixMapping("", NAMESPACE);
  149. handler.startPrefixMapping(XLINK_PREFIX, XLINK_NAMESPACE);
  150. AttributesImpl atts = new AttributesImpl();
  151. XMLUtil.addAttribute(atts, "version", "1.1"); //SVG 1.1
  152. /*
  153. XMLUtil.addAttribute(atts, "index", Integer.toString(index));
  154. XMLUtil.addAttribute(atts, "name", name);
  155. */
  156. XMLUtil.addAttribute(atts, "width", SVGUtil.formatMptToPt(size.width) + "pt");
  157. XMLUtil.addAttribute(atts, "height", SVGUtil.formatMptToPt(size.height) + "pt");
  158. XMLUtil.addAttribute(atts, "viewBox",
  159. "0 0 " + SVGUtil.formatMptToPt(size.width)
  160. + " " + SVGUtil.formatMptToPt(size.height));
  161. handler.startElement("svg", atts);
  162. try {
  163. Transformer transformer = tFactory.newTransformer();
  164. Source src = new DOMSource(this.reusedParts.getDocumentElement());
  165. Result res = new SAXResult(new DelegatingFragmentContentHandler(this.handler));
  166. transformer.transform(src, res);
  167. } catch (TransformerConfigurationException tce) {
  168. throw new IFException("Error setting up a Transformer", tce);
  169. } catch (TransformerException te) {
  170. if (te.getCause() instanceof SAXException) {
  171. throw (SAXException)te.getCause();
  172. } else {
  173. throw new IFException("Error while serializing reused parts", te);
  174. }
  175. }
  176. } catch (SAXException e) {
  177. throw new IFException("SAX error in startPage()", e);
  178. }
  179. }
  180. private void prepareHandlerWithOutputStream(int index) throws IFException {
  181. OutputStream out;
  182. try {
  183. if (index == 0) {
  184. out = null;
  185. } else {
  186. out = this.multiFileUtil.createOutputStream(index);
  187. if (out == null) {
  188. BitmapRendererEventProducer eventProducer
  189. = BitmapRendererEventProducer.Provider.get(
  190. getUserAgent().getEventBroadcaster());
  191. eventProducer.stoppingAfterFirstPageNoFilename(this);
  192. }
  193. }
  194. } catch (IOException ioe) {
  195. throw new IFException("I/O exception while setting up output file", ioe);
  196. }
  197. if (out == null) {
  198. this.handler = decorate(createContentHandler(this.firstStream));
  199. } else {
  200. this.currentStream = new StreamResult(out);
  201. this.handler = decorate(createContentHandler(this.currentStream));
  202. }
  203. }
  204. private GenerationHelperContentHandler decorate(ContentHandler contentHandler) {
  205. return new GenerationHelperContentHandler(contentHandler, getMainNamespace());
  206. }
  207. private void closeCurrentStream() {
  208. if (this.currentStream != null) {
  209. IOUtils.closeQuietly(currentStream.getOutputStream());
  210. currentStream.setOutputStream(null);
  211. IOUtils.closeQuietly(currentStream.getWriter());
  212. currentStream.setWriter(null);
  213. this.currentStream = null;
  214. }
  215. }
  216. /** {@inheritDoc} */
  217. public IFPainter startPageContent() throws IFException {
  218. try {
  219. handler.startElement("g");
  220. } catch (SAXException e) {
  221. throw new IFException("SAX error in startPageContent()", e);
  222. }
  223. return new SVGPainter(this, handler);
  224. }
  225. /** {@inheritDoc} */
  226. public void endPageContent() throws IFException {
  227. try {
  228. handler.endElement("g");
  229. } catch (SAXException e) {
  230. throw new IFException("SAX error in endPageContent()", e);
  231. }
  232. }
  233. /** {@inheritDoc} */
  234. public void endPage() throws IFException {
  235. try {
  236. handler.endElement("svg");
  237. this.handler.endDocument();
  238. } catch (SAXException e) {
  239. throw new IFException("SAX error in endPage()", e);
  240. }
  241. closeCurrentStream();
  242. }
  243. }