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

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