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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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.commons.logging.Log;
  23. import org.apache.commons.logging.LogFactory;
  24. import org.apache.fop.afp.modca.AbstractDataObject;
  25. import org.apache.fop.afp.modca.AbstractNamedAFPObject;
  26. import org.apache.fop.afp.modca.DataStream;
  27. import org.apache.fop.afp.modca.IncludeObject;
  28. import org.apache.fop.afp.modca.Registry;
  29. import org.apache.fop.afp.modca.ResourceGroup;
  30. /**
  31. * Manages the creation and storage of document resources
  32. */
  33. public class AFPResourceManager {
  34. /** Static logging instance */
  35. private static final Log log = LogFactory.getLog(AFPResourceManager.class);
  36. /** The AFP datastream (document tree) */
  37. private DataStream dataStream;
  38. /** Resource creation factory */
  39. private final Factory factory;
  40. private final AFPStreamer streamer;
  41. private final AFPDataObjectFactory dataObjectFactory;
  42. /** Maintain a reference count of instream objects for referencing purposes */
  43. private int instreamObjectCount = 0;
  44. /** a mapping of resourceInfo --> include name */
  45. private final Map/*<AFPResourceInfo,String>*/ includeNameMap
  46. = new java.util.HashMap()/*<AFPResourceInfo,String>*/;
  47. /**
  48. * Main constructor
  49. */
  50. public AFPResourceManager() {
  51. this.factory = new Factory();
  52. this.streamer = new AFPStreamer(factory);
  53. this.dataObjectFactory = new AFPDataObjectFactory(factory);
  54. }
  55. /**
  56. * Sets the outputstream
  57. *
  58. * @param state the AFP painting state
  59. * @param outputStream the outputstream
  60. */
  61. public void createDataStream(AFPPaintingState state, OutputStream outputStream) {
  62. this.dataStream = streamer.createDataStream(state);
  63. streamer.setOutputStream(outputStream);
  64. }
  65. /**
  66. * Returns the AFP DataStream
  67. *
  68. * @return the AFP DataStream
  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 a new data object in the AFP datastream
  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 the uri to ensure that its unique
  104. if (uri.endsWith("/")) {
  105. uri += "#" + (++instreamObjectCount);
  106. resourceInfo.setUri(uri);
  107. }
  108. String objectName = (String)includeNameMap.get(resourceInfo);
  109. if (objectName == null) {
  110. boolean useInclude = true;
  111. Registry.ObjectType objectType = null;
  112. // new resource so create
  113. if (dataObjectInfo instanceof AFPImageObjectInfo) {
  114. AFPImageObjectInfo imageObjectInfo = (AFPImageObjectInfo)dataObjectInfo;
  115. namedObj = dataObjectFactory.createImage(imageObjectInfo);
  116. } else if (dataObjectInfo instanceof AFPGraphicsObjectInfo) {
  117. AFPGraphicsObjectInfo graphicsObjectInfo = (AFPGraphicsObjectInfo)dataObjectInfo;
  118. namedObj = dataObjectFactory.createGraphic(graphicsObjectInfo);
  119. } else {
  120. // natively embedded object
  121. namedObj = dataObjectFactory.createObjectContainer(dataObjectInfo);
  122. objectType = dataObjectInfo.getObjectType();
  123. useInclude = objectType != null && objectType.isIncludable();
  124. }
  125. // set data object viewport (i.e. position, rotation, dimension, resolution)
  126. if (namedObj instanceof AbstractDataObject) {
  127. AbstractDataObject dataObj = (AbstractDataObject)namedObj;
  128. dataObj.setViewport(dataObjectInfo);
  129. }
  130. AFPResourceLevel resourceLevel = resourceInfo.getLevel();
  131. ResourceGroup resourceGroup = streamer.getResourceGroup(resourceLevel);
  132. useInclude &= resourceGroup != null;
  133. if (useInclude) {
  134. // if it is to reside within a resource group at print-file or external level
  135. if (resourceLevel.isPrintFile() || resourceLevel.isExternal()) {
  136. // wrap newly created data object in a resource object
  137. namedObj = dataObjectFactory.createResource(namedObj, resourceInfo, objectType);
  138. }
  139. // add data object into its resource group destination
  140. resourceGroup.addObject(namedObj);
  141. // create the include object
  142. objectName = namedObj.getName();
  143. IncludeObject includeObject
  144. = dataObjectFactory.createInclude(objectName, dataObjectInfo);
  145. // add an include to the current page
  146. dataStream.getCurrentPage().addObject(includeObject);
  147. // record mapping of resource info to data object resource name
  148. includeNameMap.put(resourceInfo, objectName);
  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(objectName, dataObjectInfo);
  157. dataStream.getCurrentPage().addObject(includeObject);
  158. }
  159. }
  160. }