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

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