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

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