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

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