Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

AreaTree.java 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. /*
  2. * $Id$
  3. * Copyright (C) 2002 The Apache Software Foundation. All rights reserved.
  4. * For details on use and redistribution please refer to the
  5. * LICENSE file included with these sources.
  6. */
  7. package org.apache.fop.area;
  8. import org.apache.fop.render.Renderer;
  9. import java.util.ArrayList;
  10. import java.util.List;
  11. import java.util.HashMap;
  12. import java.util.Iterator;
  13. /**
  14. * Area tree for formatting objects.
  15. *
  16. * Concepts:
  17. * The area tree is to be as small as possible. With minimal classes
  18. * and data to fully represent an area tree for formatting objects.
  19. * The area tree needs to be simple to render and follow the spec
  20. * closely.
  21. * This area tree has the concept of page sequences.
  22. * Where ever possible information is discarded or optimised to
  23. * keep memory use low. The data is also organised to make it
  24. * possible for renderers to minimise their output.
  25. * A page can be saved if not fully resolved and once rendered
  26. * a page contains only size and id reference information.
  27. * The area tree pages are organised in a model that depends on the
  28. * type of renderer.
  29. */
  30. public class AreaTree {
  31. // allows for different models to deal with adding/rendering
  32. // in different situations
  33. private AreaTreeModel model;
  34. // hashmap of arraylists containing pages with id area
  35. private HashMap idLocations = new HashMap();
  36. // list of id's yet to be resolved and arraylists of pages
  37. private HashMap resolve = new HashMap();
  38. private ArrayList treeExtensions = new ArrayList();
  39. /**
  40. * Create a render pages area tree model.
  41. * @param rend the renderer that will be used
  42. * @return RenderPagesModel the new area tree model
  43. */
  44. public RenderPagesModel createRenderPagesModel(Renderer rend) {
  45. return new RenderPagesModel(rend);
  46. }
  47. /**
  48. * Create a new store pages model.
  49. * @return StorePagesModel the new model
  50. */
  51. public static StorePagesModel createStorePagesModel() {
  52. return new StorePagesModel();
  53. }
  54. /**
  55. * Set the tree model to use for this area tree.
  56. * The different models can have different behaviour
  57. * when pages area added and other changes.
  58. * @param m the area tree model
  59. */
  60. public void setTreeModel(AreaTreeModel m) {
  61. model = m;
  62. }
  63. /**
  64. * Start a new page sequence.
  65. * This signals that a new page sequence has started in the document.
  66. * @param title the title of the new page sequence or null if no title
  67. */
  68. public void startPageSequence(Title title) {
  69. model.startPageSequence(title);
  70. }
  71. /**
  72. * Add a new page to the area tree.
  73. * @param page the page to add
  74. */
  75. public void addPage(PageViewport page) {
  76. model.addPage(page);
  77. }
  78. /**
  79. * Add an id reference pointing to a page viewport.
  80. * @param id the id of the reference
  81. * @param pv the page viewport that contains the id reference
  82. */
  83. public void addIDRef(String id, PageViewport pv) {
  84. List list = (List)idLocations.get(id);
  85. if (list == null) {
  86. list = new ArrayList();
  87. idLocations.put(id, list);
  88. }
  89. list.add(pv);
  90. List todo = (List)resolve.get(id);
  91. if (todo != null) {
  92. for (int count = 0; count < todo.size(); count++) {
  93. Resolveable res = (Resolveable)todo.get(count);
  94. res.resolve(id, list);
  95. }
  96. resolve.remove(id);
  97. }
  98. }
  99. /**
  100. * Get the list of id references for an id.
  101. * @param id the id to lookup
  102. * @return the list of id references.
  103. */
  104. public List getIDReferences(String id) {
  105. return (List)idLocations.get(id);
  106. }
  107. /**
  108. * Add an unresolved object with a given id.
  109. * @param id the id reference that needs resolving
  110. * @param res the Resolveable object to resolve
  111. */
  112. public void addUnresolvedID(String id, Resolveable res) {
  113. ArrayList todo = (ArrayList)resolve.get(id);
  114. if (todo == null) {
  115. todo = new ArrayList();
  116. resolve.put(id, todo);
  117. }
  118. todo.add(res);
  119. }
  120. /**
  121. * Add a tree extension.
  122. * This checks if the extension is resolveable and attempts
  123. * to resolve or add the resolveable ids for later resolution.
  124. * @param ext the tree extension to add.
  125. */
  126. public void addTreeExtension(TreeExt ext) {
  127. treeExtensions.add(ext);
  128. if (ext.isResolveable()) {
  129. Resolveable res = (Resolveable)ext;
  130. String[] ids = res.getIDs();
  131. for (int count = 0; count < ids.length; count++) {
  132. if (idLocations.containsKey(ids[count])) {
  133. res.resolve(ids[count], (ArrayList)idLocations.get(ids[count]));
  134. } else {
  135. ArrayList todo = (ArrayList)resolve.get(ids[count]);
  136. if (todo == null) {
  137. todo = new ArrayList();
  138. resolve.put(ids[count], todo);
  139. }
  140. todo.add(ext);
  141. }
  142. }
  143. } else {
  144. handleTreeExtension(ext, TreeExt.IMMEDIATELY);
  145. }
  146. }
  147. /**
  148. * Handle a tree extension.
  149. * This sends the extension to the model for handling.
  150. * @param ext the tree extension to handle
  151. * @param when when the extension should be handled by the model
  152. */
  153. public void handleTreeExtension(TreeExt ext, int when) {
  154. // queue tree extension according to the when
  155. model.addExtension(ext, when);
  156. }
  157. /**
  158. * Signal end of document.
  159. * This indicates that the document is complete and any unresolved
  160. * reference can be dealt with.
  161. */
  162. public void endDocument() {
  163. for (Iterator iter = resolve.keySet().iterator(); iter.hasNext();) {
  164. String id = (String)iter.next();
  165. ArrayList list = (ArrayList)resolve.get(id);
  166. for (int count = 0; count < list.size(); count++) {
  167. Resolveable res = (Resolveable)list.get(count);
  168. if (!res.isResolved()) {
  169. res.resolve(id, null);
  170. }
  171. }
  172. }
  173. model.endDocument();
  174. }
  175. /**
  176. * This is the model for the area tree object.
  177. * The model implementation can handle the page sequence,
  178. * page and extensions.
  179. */
  180. public abstract static class AreaTreeModel {
  181. /**
  182. * Start a page sequence on this model.
  183. * @param title the title of the new page sequence
  184. */
  185. public abstract void startPageSequence(Title title);
  186. /**
  187. * Add a page to this moel.
  188. * @param page the page to add to the model.
  189. */
  190. public abstract void addPage(PageViewport page);
  191. /**
  192. * Add an extension to this model.
  193. * @param ext the extension to add
  194. * @param when when the extension should be handled
  195. */
  196. public abstract void addExtension(TreeExt ext, int when);
  197. /**
  198. * Signal the end of the document for any processing.
  199. */
  200. public abstract void endDocument();
  201. }
  202. /**
  203. * This class stores all the pages in the document
  204. * for interactive agents.
  205. * The pages are stored and can be retrieved in any order.
  206. */
  207. public static class StorePagesModel extends AreaTreeModel {
  208. private ArrayList pageSequence = null;
  209. private ArrayList titles = new ArrayList();
  210. private ArrayList currSequence;
  211. private ArrayList extensions = new ArrayList();
  212. /**
  213. * Create a new store pages model
  214. */
  215. public StorePagesModel() {
  216. }
  217. /**
  218. * Start a new page sequence.
  219. * This creates a new list for the pages in the new page sequence.
  220. * @param title the title of the page sequence.
  221. */
  222. public void startPageSequence(Title title) {
  223. titles.add(title);
  224. if (pageSequence == null) {
  225. pageSequence = new ArrayList();
  226. }
  227. currSequence = new ArrayList();
  228. pageSequence.add(currSequence);
  229. }
  230. /**
  231. * Add a page.
  232. * @param page the page to add to the current page sequence
  233. */
  234. public void addPage(PageViewport page) {
  235. currSequence.add(page);
  236. }
  237. /**
  238. * Get the page sequence count.
  239. * @return the number of page sequences in the document.
  240. */
  241. public int getPageSequenceCount() {
  242. return pageSequence.size();
  243. }
  244. /**
  245. * Get the title for a page sequence.
  246. * @param count the page sequence count
  247. * @return the title of the page sequence
  248. */
  249. public Title getTitle(int count) {
  250. return (Title) titles.get(count);
  251. }
  252. /**
  253. * Get the page count.
  254. * @param seq the page sequence to count.
  255. * @return returns the number of pages in a page sequence
  256. */
  257. public int getPageCount(int seq) {
  258. ArrayList sequence = (ArrayList) pageSequence.get(seq);
  259. return sequence.size();
  260. }
  261. /**
  262. * Get the page for a position in the document.
  263. * @param seq the page sequence number
  264. * @param count the page count in the sequence
  265. * @return the PageViewport for the particular page
  266. */
  267. public PageViewport getPage(int seq, int count) {
  268. ArrayList sequence = (ArrayList) pageSequence.get(seq);
  269. return (PageViewport) sequence.get(count);
  270. }
  271. /**
  272. * Add an extension to the store page model.
  273. * The extension is stored so that it can be retrieved in the
  274. * appropriate position.
  275. * @param ext the extension to add
  276. * @param when when the extension should be handled
  277. */
  278. public void addExtension(TreeExt ext, int when) {
  279. int seq, page;
  280. switch(when) {
  281. case TreeExt.IMMEDIATELY:
  282. seq = pageSequence == null ? 0 : pageSequence.size();
  283. page = currSequence == null ? 0 : currSequence.size();
  284. break;
  285. case TreeExt.AFTER_PAGE:
  286. break;
  287. case TreeExt.END_OF_DOC:
  288. break;
  289. }
  290. extensions.add(ext);
  291. }
  292. /**
  293. * Get the list of extensions that apply at a particular
  294. * position in the document.
  295. * @param seq the page sequence number
  296. * @param count the page count in the sequence
  297. * @return the list of extensions
  298. */
  299. public List getExtensions(int seq, int count) {
  300. return null;
  301. }
  302. /**
  303. * Get the end of document extensions for this stroe pages model.
  304. * @return the list of end extensions
  305. */
  306. public List getEndExtensions() {
  307. return extensions;
  308. }
  309. /**
  310. * End document, do nothing.
  311. */
  312. public void endDocument() {
  313. }
  314. }
  315. /**
  316. * This uses the store pages model to store the pages
  317. * each page is either rendered if ready or prepared
  318. * for later rendering.
  319. * Once a page is rendered it is cleared to release the
  320. * contents but the PageViewport is retained.
  321. */
  322. public static class RenderPagesModel extends StorePagesModel {
  323. private Renderer renderer;
  324. private ArrayList prepared = new ArrayList();
  325. private ArrayList pendingExt = new ArrayList();
  326. private ArrayList endDocExt = new ArrayList();
  327. /**
  328. * Create a new render pages model with the given renderer.
  329. * @param rend the renderer to render pages to
  330. */
  331. public RenderPagesModel(Renderer rend) {
  332. renderer = rend;
  333. }
  334. /**
  335. * Start a new page sequence.
  336. * This tells the renderer that a new page sequence has
  337. * started with the given title.
  338. * @param title the title of the new page sequence
  339. */
  340. public void startPageSequence(Title title) {
  341. super.startPageSequence(title);
  342. renderer.startPageSequence(title);
  343. }
  344. /**
  345. * Add a page to the render page model.
  346. * If the page is finished it can be rendered immediately.
  347. * If the page needs resolving then if the renderer supports
  348. * out of order rendering it can prepare the page. Otherwise
  349. * the page is added to a queue.
  350. * @param page the page to add to the model
  351. */
  352. public void addPage(PageViewport page) {
  353. super.addPage(page);
  354. // if page finished
  355. try {
  356. renderer.renderPage(page);
  357. } catch (Exception e) {
  358. // use error handler to handle this FOP or IO Exception
  359. }
  360. page.clear();
  361. renderExtensions(pendingExt);
  362. pendingExt.clear();
  363. // else prepare
  364. //renderer.preparePage(page);
  365. prepared.add(page);
  366. }
  367. /**
  368. * Add an extension to this model.
  369. * If handle immediately then send directly to the renderer.
  370. * The after page ones are handled after the next page is added.
  371. * End of document extensions are added to a list to be
  372. * handled at the end.
  373. * @param ext the extension
  374. * @param when when to render the extension
  375. */
  376. public void addExtension(TreeExt ext, int when) {
  377. switch(when) {
  378. case TreeExt.IMMEDIATELY:
  379. renderer.renderExtension(ext);
  380. break;
  381. case TreeExt.AFTER_PAGE:
  382. pendingExt.add(ext);
  383. break;
  384. case TreeExt.END_OF_DOC:
  385. endDocExt.add(ext);
  386. break;
  387. }
  388. }
  389. private void renderExtensions(ArrayList list) {
  390. for (int count = 0; count < list.size(); count++) {
  391. TreeExt ext = (TreeExt)list.get(count);
  392. renderer.renderExtension(ext);
  393. }
  394. }
  395. /**
  396. * End the document. Render any end document extensions.
  397. */
  398. public void endDocument() {
  399. renderExtensions(endDocExt);
  400. }
  401. }
  402. }