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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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.net.URI;
  22. import java.net.URISyntaxException;
  23. import java.util.Map;
  24. import org.apache.commons.logging.Log;
  25. import org.apache.commons.logging.LogFactory;
  26. import org.apache.fop.afp.modca.AbstractNamedAFPObject;
  27. import org.apache.fop.afp.modca.AbstractPageObject;
  28. import org.apache.fop.afp.modca.IncludeObject;
  29. import org.apache.fop.afp.modca.IncludedResourceObject;
  30. import org.apache.fop.afp.modca.PageSegment;
  31. import org.apache.fop.afp.modca.Registry;
  32. import org.apache.fop.afp.modca.ResourceGroup;
  33. import org.apache.fop.afp.modca.ResourceObject;
  34. import org.apache.fop.afp.util.ResourceAccessor;
  35. import org.apache.fop.afp.util.SimpleResourceAccessor;
  36. /**
  37. * Manages the creation and storage of document resources
  38. */
  39. public class AFPResourceManager {
  40. /** logging instance */
  41. private static Log log = LogFactory.getLog(AFPResourceManager.class);
  42. /** The AFP datastream (document tree) */
  43. private DataStream dataStream;
  44. /** Resource creation factory */
  45. private final Factory factory;
  46. private final AFPStreamer streamer;
  47. private final AFPDataObjectFactory dataObjectFactory;
  48. /** Maintain a reference count of instream objects for referencing purposes */
  49. private int instreamObjectCount = 0;
  50. /** a mapping of resourceInfo --> include name */
  51. private final Map/*<AFPResourceInfo,String>*/ includeNameMap
  52. = new java.util.HashMap()/*<AFPResourceInfo,String>*/;
  53. private Map pageSegmentMap = new java.util.HashMap();
  54. private AFPResourceLevelDefaults resourceLevelDefaults = new AFPResourceLevelDefaults();
  55. /**
  56. * Main constructor
  57. */
  58. public AFPResourceManager() {
  59. this.factory = new Factory();
  60. this.streamer = new AFPStreamer(factory);
  61. this.dataObjectFactory = new AFPDataObjectFactory(factory);
  62. }
  63. /**
  64. * Sets the outputstream
  65. *
  66. * @param paintingState the AFP painting state
  67. * @param outputStream the outputstream
  68. * @return a new AFP DataStream
  69. * @throws IOException thrown if an I/O exception of some sort has occurred
  70. */
  71. public DataStream createDataStream(AFPPaintingState paintingState, OutputStream outputStream)
  72. throws IOException {
  73. this.dataStream = streamer.createDataStream(paintingState);
  74. streamer.setOutputStream(outputStream);
  75. return this.dataStream;
  76. }
  77. /**
  78. * Returns the AFP DataStream
  79. *
  80. * @return the AFP DataStream
  81. */
  82. public DataStream getDataStream() {
  83. return this.dataStream;
  84. }
  85. /**
  86. * Tells the streamer to write
  87. *
  88. * @throws IOException thrown if an I/O exception of some sort has occurred.
  89. */
  90. public void writeToStream() throws IOException {
  91. streamer.close();
  92. }
  93. /**
  94. * Sets the default resource group file path
  95. *
  96. * @param filePath the default resource group file path
  97. */
  98. public void setDefaultResourceGroupFilePath(String filePath) {
  99. streamer.setDefaultResourceGroupFilePath(filePath);
  100. }
  101. /**
  102. * Creates a new data object in the AFP datastream
  103. *
  104. * @param dataObjectInfo the data object info
  105. *
  106. * @throws IOException thrown if an I/O exception of some sort has occurred.
  107. */
  108. public void createObject(AFPDataObjectInfo dataObjectInfo) throws IOException {
  109. AbstractNamedAFPObject namedObj = null;
  110. AFPResourceInfo resourceInfo = dataObjectInfo.getResourceInfo();
  111. updateResourceInfoUri(resourceInfo);
  112. String objectName = (String)includeNameMap.get(resourceInfo);
  113. if (objectName != null) {
  114. // an existing data resource so reference it by adding an include to the current page
  115. includeObject(dataObjectInfo, objectName);
  116. return;
  117. }
  118. objectName = (String)pageSegmentMap.get(resourceInfo);
  119. if (objectName != null) {
  120. // an existing data resource so reference it by adding an include to the current page
  121. includePageSegment(dataObjectInfo, objectName);
  122. return;
  123. }
  124. boolean useInclude = true;
  125. Registry.ObjectType objectType = null;
  126. // new resource so create
  127. if (dataObjectInfo instanceof AFPImageObjectInfo) {
  128. AFPImageObjectInfo imageObjectInfo = (AFPImageObjectInfo)dataObjectInfo;
  129. namedObj = dataObjectFactory.createImage(imageObjectInfo);
  130. } else if (dataObjectInfo instanceof AFPGraphicsObjectInfo) {
  131. AFPGraphicsObjectInfo graphicsObjectInfo = (AFPGraphicsObjectInfo)dataObjectInfo;
  132. namedObj = dataObjectFactory.createGraphic(graphicsObjectInfo);
  133. } else {
  134. // natively embedded data object
  135. namedObj = dataObjectFactory.createObjectContainer(dataObjectInfo);
  136. objectType = dataObjectInfo.getObjectType();
  137. useInclude = objectType != null && objectType.isIncludable();
  138. }
  139. AFPResourceLevel resourceLevel = resourceInfo.getLevel();
  140. ResourceGroup resourceGroup = streamer.getResourceGroup(resourceLevel);
  141. useInclude &= resourceGroup != null;
  142. if (useInclude) {
  143. boolean usePageSegment = dataObjectInfo.isCreatePageSegment();
  144. // if it is to reside within a resource group at print-file or external level
  145. if (resourceLevel.isPrintFile() || resourceLevel.isExternal()) {
  146. if (usePageSegment) {
  147. String pageSegmentName = "S10" + namedObj.getName().substring(3);
  148. namedObj.setName(pageSegmentName);
  149. PageSegment seg = new PageSegment(pageSegmentName);
  150. seg.addObject(namedObj);
  151. namedObj = seg;
  152. }
  153. // wrap newly created data object in a resource object
  154. namedObj = dataObjectFactory.createResource(namedObj, resourceInfo, objectType);
  155. }
  156. // add data object into its resource group destination
  157. resourceGroup.addObject(namedObj);
  158. // create the include object
  159. objectName = namedObj.getName();
  160. if (usePageSegment) {
  161. includePageSegment(dataObjectInfo, objectName);
  162. pageSegmentMap.put(resourceInfo, objectName);
  163. } else {
  164. includeObject(dataObjectInfo, objectName);
  165. // record mapping of resource info to data object resource name
  166. includeNameMap.put(resourceInfo, objectName);
  167. }
  168. } else {
  169. // not to be included so inline data object directly into the current page
  170. dataStream.getCurrentPage().addObject(namedObj);
  171. }
  172. }
  173. private void updateResourceInfoUri(AFPResourceInfo resourceInfo) {
  174. String uri = resourceInfo.getUri();
  175. if (uri == null) {
  176. uri = "/";
  177. }
  178. // if this is an instream data object adjust the uri to ensure that its unique
  179. if (uri.endsWith("/")) {
  180. uri += "#" + (++instreamObjectCount);
  181. resourceInfo.setUri(uri);
  182. }
  183. }
  184. private void includeObject(AFPDataObjectInfo dataObjectInfo,
  185. String objectName) {
  186. IncludeObject includeObject
  187. = dataObjectFactory.createInclude(objectName, dataObjectInfo);
  188. dataStream.getCurrentPage().addObject(includeObject);
  189. }
  190. private void includePageSegment(AFPDataObjectInfo dataObjectInfo,
  191. String pageSegmentName) {
  192. int x = dataObjectInfo.getObjectAreaInfo().getX();
  193. int y = dataObjectInfo.getObjectAreaInfo().getY();
  194. AbstractPageObject currentPage = dataStream.getCurrentPage();
  195. boolean createHardPageSegments = true;
  196. currentPage.createIncludePageSegment(pageSegmentName, x, y, createHardPageSegments);
  197. }
  198. /**
  199. * Creates an included resource object by loading the contained object from a file.
  200. * @param resourceName the name of the resource
  201. * @param basePath the base path in which to look for the resource files
  202. * @param resourceObjectType the resource object type ({@link ResourceObject}.*)
  203. * @throws IOException if an I/O error occurs while loading the resource
  204. */
  205. public void createIncludedResource(String resourceName, String basePath,
  206. byte resourceObjectType) throws IOException {
  207. AFPResourceLevel resourceLevel = new AFPResourceLevel(AFPResourceLevel.PRINT_FILE);
  208. URI uri;
  209. try {
  210. uri = new URI(resourceName.trim());
  211. } catch (URISyntaxException e) {
  212. throw new IOException("Could not create URI from resource name: " + resourceName
  213. + " (" + e.getMessage() + ")");
  214. }
  215. AFPResourceInfo resourceInfo = new AFPResourceInfo();
  216. resourceInfo.setLevel(resourceLevel);
  217. resourceInfo.setName(resourceName);
  218. resourceInfo.setUri(uri.toASCIIString());
  219. String objectName = (String)includeNameMap.get(resourceInfo);
  220. if (objectName == null) {
  221. if (log.isDebugEnabled()) {
  222. log.debug("Adding included resource: " + resourceName);
  223. }
  224. //TODO This works with local filenames only. In the long term, this
  225. //should work through FOP's URI resolver.
  226. ResourceAccessor accessor = new SimpleResourceAccessor(basePath);
  227. IncludedResourceObject resourceContent = new IncludedResourceObject(
  228. resourceName, accessor, uri);
  229. ResourceObject resourceObject = factory.createResource(resourceName);
  230. resourceObject.setDataObject(resourceContent);
  231. resourceObject.setType(resourceObjectType);
  232. ResourceGroup resourceGroup = streamer.getResourceGroup(resourceLevel);
  233. resourceGroup.addObject(resourceObject);
  234. // record mapping of resource info to data object resource name
  235. includeNameMap.put(resourceInfo, resourceName);
  236. } else {
  237. //skip, already created
  238. }
  239. }
  240. /**
  241. * Sets resource level defaults. The existing defaults over merged with the ones passed in
  242. * as parameter.
  243. * @param defaults the new defaults
  244. */
  245. public void setResourceLevelDefaults(AFPResourceLevelDefaults defaults) {
  246. this.resourceLevelDefaults.mergeFrom(defaults);
  247. }
  248. /**
  249. * Returns the resource level defaults in use with this resource manager.
  250. * @return the resource level defaults
  251. */
  252. public AFPResourceLevelDefaults getResourceLevelDefaults() {
  253. return this.resourceLevelDefaults;
  254. }
  255. }