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.

AbstractBinaryWritingIFDocumentHandler.java 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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.intermediate;
  19. import java.io.BufferedOutputStream;
  20. import java.io.IOException;
  21. import java.io.OutputStream;
  22. import java.net.URI;
  23. import javax.xml.transform.Result;
  24. import javax.xml.transform.stream.StreamResult;
  25. import org.apache.commons.io.IOUtils;
  26. import org.apache.fop.fonts.FontCollection;
  27. import org.apache.fop.fonts.FontEventAdapter;
  28. import org.apache.fop.fonts.FontInfo;
  29. import org.apache.fop.fonts.FontManager;
  30. import org.apache.fop.fonts.base14.Base14FontCollection;
  31. /**
  32. * Abstract base class for binary-writing {@link IFDocumentHandler} implementations.
  33. */
  34. public abstract class AbstractBinaryWritingIFDocumentHandler extends AbstractIFDocumentHandler {
  35. /** The output stream to write the document to */
  36. protected OutputStream outputStream;
  37. private boolean ownOutputStream;
  38. /** Font configuration */
  39. protected FontInfo fontInfo;
  40. public AbstractBinaryWritingIFDocumentHandler(IFContext ifContext) {
  41. super(ifContext);
  42. }
  43. /** {@inheritDoc} */
  44. public void setResult(Result result) throws IFException {
  45. if (result instanceof StreamResult) {
  46. StreamResult streamResult = (StreamResult) result;
  47. OutputStream out = streamResult.getOutputStream();
  48. if (out == null) {
  49. if (streamResult.getWriter() != null) {
  50. throw new IllegalArgumentException(
  51. "FOP cannot use a Writer. Please supply an OutputStream!");
  52. }
  53. try {
  54. URI resultURI = URI.create(streamResult.getSystemId());
  55. out = new BufferedOutputStream(getUserAgent().getResourceResolver().getOutputStream(resultURI));
  56. } catch (IOException ioe) {
  57. throw new IFException("I/O error while opening output stream" , ioe);
  58. }
  59. this.ownOutputStream = true;
  60. }
  61. this.outputStream = out;
  62. } else {
  63. throw new UnsupportedOperationException(
  64. "Unsupported Result subclass: " + result.getClass().getName());
  65. }
  66. }
  67. /** {@inheritDoc} */
  68. public FontInfo getFontInfo() {
  69. return this.fontInfo;
  70. }
  71. /** {@inheritDoc} */
  72. public void setFontInfo(FontInfo fontInfo) {
  73. this.fontInfo = fontInfo;
  74. }
  75. /** {@inheritDoc} */
  76. public void setDefaultFontInfo(FontInfo fontInfo) {
  77. FontManager fontManager = getUserAgent().getFontManager();
  78. FontCollection[] fontCollections = new FontCollection[] {
  79. new Base14FontCollection(fontManager.isBase14KerningEnabled())
  80. };
  81. FontInfo fi = (fontInfo != null ? fontInfo : new FontInfo());
  82. fi.setEventListener(new FontEventAdapter(getUserAgent().getEventBroadcaster()));
  83. fontManager.setup(fi, fontCollections);
  84. setFontInfo(fi);
  85. }
  86. /** {@inheritDoc} */
  87. public void startDocument() throws IFException {
  88. super.startDocument();
  89. if (this.outputStream == null) {
  90. throw new IllegalStateException("OutputStream hasn't been set through setResult()");
  91. }
  92. }
  93. /** {@inheritDoc} */
  94. public void endDocument() throws IFException {
  95. if (this.ownOutputStream) {
  96. IOUtils.closeQuietly(this.outputStream);
  97. this.outputStream = null;
  98. }
  99. }
  100. }