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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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.IOException;
  20. import java.io.OutputStream;
  21. import java.util.Map;
  22. import org.apache.fop.afp.modca.AbstractNamedAFPObject;
  23. import org.apache.fop.afp.modca.AbstractPageObject;
  24. import org.apache.fop.afp.modca.IncludeObject;
  25. import org.apache.fop.afp.modca.PageSegment;
  26. import org.apache.fop.afp.modca.Registry;
  27. import org.apache.fop.afp.modca.ResourceGroup;
  28. /**
  29. * Manages the creation and storage of document resources
  30. */
  31. public class AFPResourceManager {
  32. /** The AFP datastream (document tree) */
  33. private DataStream dataStream;
  34. /** Resource creation factory */
  35. private final Factory factory;
  36. private final AFPStreamer streamer;
  37. private final AFPDataObjectFactory dataObjectFactory;
  38. /** Maintain a reference count of instream objects for referencing purposes */
  39. private int instreamObjectCount = 0;
  40. /** a mapping of resourceInfo --> names of includable objects */
  41. private final Map/*<AFPResourceInfo,String>*/ includableObjectsMap
  42. = new java.util.HashMap()/*<AFPResourceInfo,String>*/;
  43. private Map pageSegmentMap = new java.util.HashMap();
  44. private AFPResourceLevelDefaults resourceLevelDefaults = new AFPResourceLevelDefaults();
  45. /**
  46. * Main constructor
  47. */
  48. public AFPResourceManager() {
  49. this.factory = new Factory();
  50. this.streamer = new AFPStreamer(factory);
  51. this.dataObjectFactory = new AFPDataObjectFactory(factory);
  52. }
  53. /**
  54. * Sets the outputstream
  55. *
  56. * @param paintingState the AFP painting state
  57. * @param outputStream the outputstream
  58. * @return a new AFP DataStream
  59. * @throws IOException thrown if an I/O exception of some sort has occurred
  60. */
  61. public DataStream createDataStream(AFPPaintingState paintingState, OutputStream outputStream)
  62. throws IOException {
  63. this.dataStream = streamer.createDataStream(paintingState);
  64. streamer.setOutputStream(outputStream);
  65. return this.dataStream;
  66. }
  67. /**
  68. * Returns the AFP DataStream
  69. *
  70. * @return the AFP DataStream
  71. */
  72. public DataStream getDataStream() {
  73. return this.dataStream;
  74. }
  75. /**
  76. * Tells the streamer to write
  77. *
  78. * @throws IOException thrown if an I/O exception of some sort has occurred.
  79. */
  80. public void writeToStream() throws IOException {
  81. streamer.close();
  82. }
  83. /**
  84. * Sets the default resource group file path
  85. *
  86. * @param filePath the default resource group file path
  87. */
  88. public void setDefaultResourceGroupFilePath(String filePath) {
  89. streamer.setDefaultResourceGroupFilePath(filePath);
  90. }
  91. /**
  92. * Creates a new data object in the AFP datastream
  93. *
  94. * @param dataObjectInfo the data object info
  95. *
  96. * @throws IOException thrown if an I/O exception of some sort has occurred.
  97. */
  98. public void createObject(AFPDataObjectInfo dataObjectInfo) throws IOException {
  99. AbstractNamedAFPObject namedObj = null;
  100. AFPResourceInfo resourceInfo = dataObjectInfo.getResourceInfo();
  101. updateResourceInfoUri(resourceInfo);
  102. String objectName = (String)includableObjectsMap.get(resourceInfo);
  103. if (objectName != null) {
  104. // an existing data resource so reference it by adding an include to the current page
  105. includeObject(dataObjectInfo, objectName);
  106. return;
  107. }
  108. objectName = (String)pageSegmentMap.get(resourceInfo);
  109. if (objectName != null) {
  110. // an existing data resource so reference it by adding an include to the current page
  111. includePageSegment(dataObjectInfo, objectName);
  112. return;
  113. }
  114. boolean useInclude = true;
  115. Registry.ObjectType objectType = null;
  116. // new resource so create
  117. if (dataObjectInfo instanceof AFPImageObjectInfo) {
  118. AFPImageObjectInfo imageObjectInfo = (AFPImageObjectInfo)dataObjectInfo;
  119. namedObj = dataObjectFactory.createImage(imageObjectInfo);
  120. } else if (dataObjectInfo instanceof AFPGraphicsObjectInfo) {
  121. AFPGraphicsObjectInfo graphicsObjectInfo = (AFPGraphicsObjectInfo)dataObjectInfo;
  122. namedObj = dataObjectFactory.createGraphic(graphicsObjectInfo);
  123. } else {
  124. // natively embedded data object
  125. namedObj = dataObjectFactory.createObjectContainer(dataObjectInfo);
  126. objectType = dataObjectInfo.getObjectType();
  127. useInclude = objectType != null && objectType.isIncludable();
  128. }
  129. AFPResourceLevel resourceLevel = resourceInfo.getLevel();
  130. ResourceGroup resourceGroup = streamer.getResourceGroup(resourceLevel);
  131. useInclude &= resourceGroup != null;
  132. if (useInclude) {
  133. boolean usePageSegment = dataObjectInfo.isCreatePageSegment();
  134. // if it is to reside within a resource group at print-file or external level
  135. if (resourceLevel.isPrintFile() || resourceLevel.isExternal()) {
  136. if (usePageSegment) {
  137. String pageSegmentName = "S10" + namedObj.getName().substring(3);
  138. namedObj.setName(pageSegmentName);
  139. PageSegment seg = new PageSegment(pageSegmentName);
  140. seg.addObject(namedObj);
  141. namedObj = seg;
  142. }
  143. // wrap newly created data object in a resource object
  144. namedObj = dataObjectFactory.createResource(namedObj, resourceInfo, objectType);
  145. }
  146. // add data object into its resource group destination
  147. resourceGroup.addObject(namedObj);
  148. // create the include object
  149. objectName = namedObj.getName();
  150. if (usePageSegment) {
  151. includePageSegment(dataObjectInfo, objectName);
  152. pageSegmentMap.put(resourceInfo, objectName);
  153. } else {
  154. includeObject(dataObjectInfo, objectName);
  155. // record mapping of resource info to data object resource name
  156. includableObjectsMap.put(resourceInfo, objectName);
  157. }
  158. } else {
  159. // not to be included so inline data object directly into the current page
  160. dataStream.getCurrentPage().addObject(namedObj);
  161. }
  162. }
  163. private void updateResourceInfoUri(AFPResourceInfo resourceInfo) {
  164. String uri = resourceInfo.getUri();
  165. if (uri == null) {
  166. uri = "/";
  167. }
  168. // if this is an instream data object adjust the uri to ensure that its unique
  169. if (uri.endsWith("/")) {
  170. uri += "#" + (++instreamObjectCount);
  171. resourceInfo.setUri(uri);
  172. }
  173. }
  174. private void includeObject(AFPDataObjectInfo dataObjectInfo,
  175. String objectName) {
  176. IncludeObject includeObject
  177. = dataObjectFactory.createInclude(objectName, dataObjectInfo);
  178. dataStream.getCurrentPage().addObject(includeObject);
  179. }
  180. private void includePageSegment(AFPDataObjectInfo dataObjectInfo,
  181. String pageSegmentName) {
  182. int x = dataObjectInfo.getObjectAreaInfo().getX();
  183. int y = dataObjectInfo.getObjectAreaInfo().getY();
  184. AbstractPageObject currentPage = dataStream.getCurrentPage();
  185. boolean createHardPageSegments = true;
  186. currentPage.createIncludePageSegment(pageSegmentName, x, y, createHardPageSegments);
  187. }
  188. /**
  189. * Sets resource level defaults. The existing defaults over merged with the ones passed in
  190. * as parameter.
  191. * @param defaults the new defaults
  192. */
  193. public void setResourceLevelDefaults(AFPResourceLevelDefaults defaults) {
  194. this.resourceLevelDefaults.mergeFrom(defaults);
  195. }
  196. /**
  197. * Returns the resource level defaults in use with this resource manager.
  198. * @return the resource level defaults
  199. */
  200. public AFPResourceLevelDefaults getResourceLevelDefaults() {
  201. return this.resourceLevelDefaults;
  202. }
  203. }