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.

AFPStreamer.java 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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.afp;
  19. import java.io.BufferedOutputStream;
  20. import java.io.IOException;
  21. import java.io.InputStream;
  22. import java.io.OutputStream;
  23. import java.net.URI;
  24. import java.util.HashMap;
  25. import java.util.Map;
  26. import org.apache.commons.io.IOUtils;
  27. import org.apache.commons.logging.Log;
  28. import org.apache.commons.logging.LogFactory;
  29. import org.apache.xmlgraphics.io.TempResourceURIGenerator;
  30. import org.apache.fop.afp.modca.ResourceGroup;
  31. import org.apache.fop.afp.modca.StreamedResourceGroup;
  32. import org.apache.fop.apps.io.InternalResourceResolver;
  33. /**
  34. * Manages the streaming of the AFP output
  35. */
  36. public class AFPStreamer implements Streamable {
  37. /** Static logging instance */
  38. private static final Log LOG = LogFactory.getLog(AFPStreamer.class);
  39. private static final String DEFAULT_EXTERNAL_RESOURCE_FILENAME = "resources.afp";
  40. private static final TempResourceURIGenerator TEMP_URI_GENERATOR
  41. = new TempResourceURIGenerator("AFPDataStream_");
  42. private final Factory factory;
  43. private final InternalResourceResolver resourceResolver;
  44. /** A mapping of external resource destinations to resource groups */
  45. private final Map<URI, ResourceGroup> pathResourceGroupMap = new HashMap<URI, ResourceGroup>();
  46. private StreamedResourceGroup printFileResourceGroup;
  47. /** Sets the default resource group file path */
  48. private URI defaultResourceGroupUri;
  49. private final URI tempUri;
  50. /** temporary document outputstream */
  51. private OutputStream tempOutputStream;
  52. /** the final outputstream */
  53. private OutputStream outputStream;
  54. private DataStream dataStream;
  55. /**
  56. * Main constructor
  57. *
  58. * @param factory a factory
  59. * @param resourceResolver resource resolver
  60. */
  61. public AFPStreamer(Factory factory, InternalResourceResolver resourceResolver) {
  62. this.factory = factory;
  63. this.resourceResolver = resourceResolver;
  64. this.tempUri = TEMP_URI_GENERATOR.generate();
  65. defaultResourceGroupUri = URI.create(DEFAULT_EXTERNAL_RESOURCE_FILENAME);
  66. }
  67. /**
  68. * Creates a new DataStream
  69. *
  70. * @param paintingState the AFP painting state
  71. * @return a new {@link DataStream}
  72. * @throws IOException thrown if an I/O exception of some sort has occurred
  73. */
  74. public DataStream createDataStream(AFPPaintingState paintingState) throws IOException {
  75. this.tempOutputStream = new BufferedOutputStream(resourceResolver.getOutputStream(tempUri));
  76. this.dataStream = factory.createDataStream(paintingState, tempOutputStream);
  77. return dataStream;
  78. }
  79. /**
  80. * Sets the default resource group URI.
  81. *
  82. * @param uri the default resource group URI
  83. */
  84. public void setDefaultResourceGroupUri(URI uri) {
  85. this.defaultResourceGroupUri = uri;
  86. }
  87. /**
  88. * Returns the resource group for a given resource info
  89. *
  90. * @param level a resource level
  91. * @return a resource group for the given resource info
  92. */
  93. public ResourceGroup getResourceGroup(AFPResourceLevel level) {
  94. ResourceGroup resourceGroup = null;
  95. if (level.isInline()) { // no resource group for inline level
  96. return null;
  97. }
  98. if (level.isExternal()) {
  99. URI uri = level.getExternalURI();
  100. if (uri == null) {
  101. LOG.warn("No file path provided for external resource, using default.");
  102. uri = defaultResourceGroupUri;
  103. }
  104. resourceGroup = pathResourceGroupMap.get(uri);
  105. if (resourceGroup == null) {
  106. OutputStream os = null;
  107. try {
  108. os = new BufferedOutputStream(resourceResolver.getOutputStream(uri));
  109. } catch (IOException ioe) {
  110. LOG.error("Failed to create/open external resource group for uri '"
  111. + uri + "'");
  112. } finally {
  113. if (os != null) {
  114. resourceGroup = factory.createStreamedResourceGroup(os);
  115. pathResourceGroupMap.put(uri, resourceGroup);
  116. }
  117. }
  118. }
  119. } else if (level.isPrintFile()) {
  120. if (printFileResourceGroup == null) {
  121. // use final outputstream for print-file resource group
  122. printFileResourceGroup = factory.createStreamedResourceGroup(outputStream);
  123. }
  124. resourceGroup = printFileResourceGroup;
  125. } else {
  126. // resource group in afp document datastream
  127. resourceGroup = dataStream.getResourceGroup(level);
  128. }
  129. return resourceGroup;
  130. }
  131. /**
  132. * Closes off the AFP stream writing the document stream
  133. *
  134. * @throws IOException if an an I/O exception of some sort has occurred
  135. */
  136. // write out any external resource groups
  137. public void close() throws IOException {
  138. for (ResourceGroup resourceGroup : pathResourceGroupMap.values()) {
  139. // TODO - Why not a Map<URI, StreamedResourceGroup>, if all the elements are expected to be of that type?
  140. assert (resourceGroup instanceof StreamedResourceGroup);
  141. ((StreamedResourceGroup) resourceGroup).close();
  142. }
  143. // close any open print-file resource group
  144. if (printFileResourceGroup != null) {
  145. printFileResourceGroup.close();
  146. }
  147. // write out document
  148. writeToStream(outputStream);
  149. outputStream.close();
  150. }
  151. /**
  152. * Sets the final outputstream
  153. *
  154. * @param outputStream an outputstream
  155. */
  156. public void setOutputStream(OutputStream outputStream) {
  157. this.outputStream = outputStream;
  158. }
  159. /** {@inheritDoc} */
  160. public void writeToStream(OutputStream os) throws IOException {
  161. tempOutputStream.close();
  162. InputStream tempInputStream = resourceResolver.getResource(tempUri);
  163. IOUtils.copy(tempInputStream, os);
  164. //TODO this should notify the stream provider that it is safe to delete the temp data
  165. tempInputStream.close();
  166. os.flush();
  167. }
  168. }