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

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