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.

AFPResourceManager.java 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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.afp;
  19. import java.io.IOException;
  20. import java.io.OutputStream;
  21. import java.util.Map;
  22. import org.apache.commons.logging.Log;
  23. import org.apache.commons.logging.LogFactory;
  24. import org.apache.fop.render.afp.modca.AbstractDataObject;
  25. import org.apache.fop.render.afp.modca.AbstractNamedAFPObject;
  26. import org.apache.fop.render.afp.modca.DataStream;
  27. import org.apache.fop.render.afp.modca.Factory;
  28. import org.apache.fop.render.afp.modca.IncludeObject;
  29. import org.apache.fop.render.afp.modca.Registry;
  30. import org.apache.fop.render.afp.modca.ResourceGroup;
  31. /**
  32. * Manages the creation and storage of document resources
  33. */
  34. public class AFPResourceManager {
  35. /** Static logging instance */
  36. private static final Log log = LogFactory.getLog(AFPResourceManager.class);
  37. /** The AFP datastream (document tree) */
  38. private DataStream dataStream;
  39. /** Resource creation factory */
  40. private final Factory factory;
  41. private final AFPStreamer streamer;
  42. private final AFPDataObjectFactory dataObjectFactory;
  43. /** Maintain a reference count of instream objects for referencing purposes */
  44. private int instreamObjectCount = 0;
  45. /** a mapping of resourceInfo --> include name */
  46. private final Map/*<ResourceInfo,String>*/ includeNameMap
  47. = new java.util.HashMap()/*<ResourceInfo,String>*/;
  48. /**
  49. * Main constructor
  50. */
  51. public AFPResourceManager() {
  52. this.factory = new Factory();
  53. this.streamer = new AFPStreamer(factory);
  54. this.dataObjectFactory = new AFPDataObjectFactory(factory);
  55. }
  56. /**
  57. * Sets the outputstream
  58. *
  59. * @param outputStream the outputstream
  60. */
  61. public void setOutputStream(OutputStream outputStream) {
  62. this.dataStream = streamer.createDataStream();
  63. streamer.setOutputStream(outputStream);
  64. }
  65. /**
  66. * Returns the AFPDocumentStream
  67. *
  68. * @return the AFPDocumentStream
  69. */
  70. public DataStream getDataStream() {
  71. return this.dataStream;
  72. }
  73. /**
  74. * Tells the streamer to write
  75. *
  76. * @throws IOException thrown if an I/O exception of some sort has occurred.
  77. */
  78. public void writeToStream() throws IOException {
  79. streamer.close();
  80. }
  81. /**
  82. * Sets the default resource group file path
  83. *
  84. * @param filePath the default resource group file path
  85. */
  86. public void setDefaultResourceGroupFilePath(String filePath) {
  87. streamer.setDefaultResourceGroupFilePath(filePath);
  88. }
  89. /**
  90. * Creates and returns a new data object
  91. *
  92. * @param dataObjectInfo the data object info
  93. *
  94. * @throws IOException thrown if an I/O exception of some sort has occurred.
  95. */
  96. public void createObject(AFPDataObjectInfo dataObjectInfo) throws IOException {
  97. AbstractNamedAFPObject namedObj = null;
  98. AFPResourceInfo resourceInfo = dataObjectInfo.getResourceInfo();
  99. String uri = resourceInfo.getUri();
  100. if (uri == null) {
  101. uri = "/";
  102. }
  103. // if this is an instream data object adjust uri to ensure that it is
  104. // unique
  105. if (uri.endsWith("/")) {
  106. uri += "#" + (++instreamObjectCount);
  107. resourceInfo.setUri(uri);
  108. }
  109. // try and find an include name for the same resource
  110. String includeName = (String)includeNameMap.get(resourceInfo);
  111. if (includeName == null) {
  112. boolean canInclude = false;
  113. Registry.ObjectType objectType = null;
  114. // new resource so create
  115. if (dataObjectInfo instanceof AFPImageObjectInfo) {
  116. AFPImageObjectInfo imageObjectInfo = (AFPImageObjectInfo)dataObjectInfo;
  117. namedObj = dataObjectFactory.createImage(imageObjectInfo);
  118. canInclude = true;
  119. } else if (dataObjectInfo instanceof AFPGraphicsObjectInfo) {
  120. namedObj = dataObjectFactory.createGraphic((AFPGraphicsObjectInfo)dataObjectInfo);
  121. } else {
  122. namedObj = dataObjectFactory.createObjectContainer(dataObjectInfo);
  123. objectType = dataObjectInfo.getObjectType();
  124. canInclude = objectType != null && objectType.isIncludable();
  125. }
  126. // set data object viewport (i.e. position, rotation, dimension, resolution)
  127. if (namedObj instanceof AbstractDataObject) {
  128. AbstractDataObject dataObj = (AbstractDataObject)namedObj;
  129. dataObj.setViewport(dataObjectInfo);
  130. }
  131. AFPResourceLevel resourceLevel = resourceInfo.getLevel();
  132. ResourceGroup resourceGroup = streamer.getResourceGroup(resourceLevel);
  133. canInclude &= resourceGroup != null;
  134. if (canInclude) {
  135. // if it is to reside within a resource group at print-file or external level
  136. if (resourceLevel.isPrintFile() || resourceLevel.isExternal()) {
  137. // wrap newly created data object in a resource object
  138. namedObj = dataObjectFactory.createResource(namedObj, resourceInfo, objectType);
  139. }
  140. // add data object into its resource group destination
  141. resourceGroup.addObject(namedObj);
  142. // add an include to the current page
  143. includeName = namedObj.getName();
  144. IncludeObject includeObject
  145. = dataObjectFactory.createInclude(includeName, dataObjectInfo);
  146. dataStream.getCurrentPage().addObject(includeObject);
  147. // record name of data object for the resource
  148. includeNameMap.put(resourceInfo, namedObj.getName());
  149. } else {
  150. // not to be included so inline data object directly into the current page
  151. dataStream.getCurrentPage().addObject(namedObj);
  152. }
  153. } else {
  154. // an existing data resource so reference it by adding an include to the current page
  155. IncludeObject includeObject
  156. = dataObjectFactory.createInclude(includeName, dataObjectInfo);
  157. dataStream.getCurrentPage().addObject(includeObject);
  158. }
  159. }
  160. }