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.

FOTreeHandler.java 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  1. /*
  2. * Copyright 1999-2004 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /* $Id$ */
  17. package org.apache.fop.fo;
  18. // Java
  19. import java.util.HashSet;
  20. import java.util.Iterator;
  21. // SAX
  22. import org.xml.sax.SAXException;
  23. // FOP
  24. import org.apache.fop.apps.FOPException;
  25. import org.apache.fop.fo.flow.BasicLink;
  26. import org.apache.fop.fo.flow.Block;
  27. import org.apache.fop.fo.flow.ExternalGraphic;
  28. import org.apache.fop.fo.flow.Footnote;
  29. import org.apache.fop.fo.flow.FootnoteBody;
  30. import org.apache.fop.fo.flow.InstreamForeignObject;
  31. import org.apache.fop.fo.flow.Inline;
  32. import org.apache.fop.fo.flow.Leader;
  33. import org.apache.fop.fo.flow.ListBlock;
  34. import org.apache.fop.fo.flow.ListItem;
  35. import org.apache.fop.fo.flow.PageNumber;
  36. import org.apache.fop.fo.flow.Table;
  37. import org.apache.fop.fo.flow.TableColumn;
  38. import org.apache.fop.fo.flow.TableBody;
  39. import org.apache.fop.fo.flow.TableCell;
  40. import org.apache.fop.fo.flow.TableRow;
  41. import org.apache.fop.fo.pagination.Flow;
  42. import org.apache.fop.fo.pagination.PageSequence;
  43. /**
  44. * Defines how SAX events specific to XSL-FO input should be handled when
  45. * an FO Tree needs to be built.
  46. * This initiates layout processes and corresponding
  47. * rendering processes such as start/end.
  48. * @see FOInputHandler
  49. */
  50. public class FOTreeHandler extends FOInputHandler {
  51. // TODO: Collecting of statistics should be configurable
  52. private final boolean collectStatistics = true;
  53. private static final boolean MEM_PROFILE_WITH_GC = false;
  54. /**
  55. * Somewhere to get our stats from.
  56. */
  57. private Runtime runtime;
  58. /**
  59. * Keep track of the number of pages rendered.
  60. */
  61. private int pageCount;
  62. /**
  63. * Keep track of heap memory allocated,
  64. * for statistical purposes.
  65. */
  66. private long initialMemory;
  67. /**
  68. * Keep track of time used by renderer.
  69. */
  70. private long startTime;
  71. /**
  72. * Collection of objects that have registered to be notified about
  73. * FOTreeEvent firings.
  74. */
  75. private HashSet foTreeListeners = new HashSet();
  76. /**
  77. * Main constructor
  78. * @param foTreeControl the FOTreeControl implementation that governs this
  79. * FO Tree
  80. * @param store if true then use the store pages model and keep the
  81. * area tree in memory
  82. */
  83. public FOTreeHandler(FOTreeControl foTreeControl, boolean store) {
  84. super(foTreeControl);
  85. if (collectStatistics) {
  86. runtime = Runtime.getRuntime();
  87. }
  88. }
  89. /**
  90. * Start the document.
  91. * This starts the document in the renderer.
  92. *
  93. * @throws SAXException if there is an error
  94. */
  95. public void startDocument() throws SAXException {
  96. //Initialize statistics
  97. if (collectStatistics) {
  98. pageCount = 0;
  99. if (MEM_PROFILE_WITH_GC) {
  100. System.gc(); // This takes time but gives better results
  101. }
  102. initialMemory = runtime.totalMemory() - runtime.freeMemory();
  103. startTime = System.currentTimeMillis();
  104. }
  105. }
  106. /**
  107. * End the document.
  108. *
  109. * @throws SAXException if there is some error
  110. */
  111. public void endDocument() throws SAXException {
  112. notifyDocumentComplete();
  113. if (collectStatistics) {
  114. if (MEM_PROFILE_WITH_GC) {
  115. // This takes time but gives better results
  116. System.gc();
  117. }
  118. long memoryNow = runtime.totalMemory() - runtime.freeMemory();
  119. long memoryUsed = (memoryNow - initialMemory) / 1024L;
  120. long timeUsed = System.currentTimeMillis() - startTime;
  121. if (getLogger().isDebugEnabled()) {
  122. getLogger().debug("Initial heap size: " + (initialMemory / 1024L) + "Kb");
  123. getLogger().debug("Current heap size: " + (memoryNow / 1024L) + "Kb");
  124. getLogger().debug("Total memory used: " + memoryUsed + "Kb");
  125. if (!MEM_PROFILE_WITH_GC) {
  126. getLogger().debug(" Memory use is indicative; no GC was performed");
  127. getLogger().debug(" These figures should not be used comparatively");
  128. }
  129. getLogger().debug("Total time used: " + timeUsed + "ms");
  130. getLogger().debug("Pages rendered: " + pageCount);
  131. if (pageCount > 0) {
  132. getLogger().debug("Avg render time: " + (timeUsed / pageCount) + "ms/page");
  133. }
  134. }
  135. }
  136. }
  137. /**
  138. * Start a page sequence.
  139. * At the start of a page sequence it can start the page sequence
  140. * on the area tree with the page sequence title.
  141. *
  142. * @param pageSeq the page sequence starting
  143. */
  144. public void startPageSequence(PageSequence pageSeq) {
  145. }
  146. /**
  147. * End the PageSequence.
  148. * The PageSequence formats Pages and adds them to the AreaTree.
  149. * The area tree then handles what happens with the pages.
  150. *
  151. * @param pageSequence the page sequence ending
  152. * @throws FOPException if there is an error formatting the pages
  153. */
  154. public void endPageSequence(PageSequence pageSequence)
  155. throws FOPException {
  156. //areaTree.setFontInfo(fontInfo);
  157. if (collectStatistics) {
  158. if (MEM_PROFILE_WITH_GC) {
  159. // This takes time but gives better results
  160. System.gc();
  161. }
  162. long memoryNow = runtime.totalMemory() - runtime.freeMemory();
  163. if (getLogger().isDebugEnabled()) {
  164. getLogger().debug("Current heap size: " + (memoryNow / 1024L) + "Kb");
  165. }
  166. }
  167. notifyPageSequenceComplete(pageSequence);
  168. }
  169. /**
  170. * @see org.apache.fop.fo.FOInputHandler#startFlow(Flow)
  171. */
  172. public void startFlow(Flow fl) {
  173. }
  174. /**
  175. * @see org.apache.fop.fo.FOInputHandler#endFlow(Flow)
  176. */
  177. public void endFlow(Flow fl) {
  178. }
  179. /**
  180. * @see org.apache.fop.fo.FOInputHandler#startBlock(Block)
  181. */
  182. public void startBlock(Block bl) {
  183. }
  184. /**
  185. * @see org.apache.fop.fo.FOInputHandler#endBlock(Block)
  186. */
  187. public void endBlock(Block bl) {
  188. }
  189. /**
  190. *
  191. * @param inl Inline that is starting.
  192. */
  193. public void startInline(Inline inl){
  194. }
  195. /**
  196. *
  197. * @param inl Inline that is ending.
  198. */
  199. public void endInline(Inline inl){
  200. }
  201. /**
  202. * @see org.apache.fop.fo.FOInputHandler#startTable(Table)
  203. */
  204. public void startTable(Table tbl) {
  205. }
  206. /**
  207. * @see org.apache.fop.fo.FOInputHandler#endTable(Table)
  208. */
  209. public void endTable(Table tbl) {
  210. }
  211. /**
  212. *
  213. * @param tc TableColumn that is starting;
  214. */
  215. public void startColumn(TableColumn tc) {
  216. }
  217. /**
  218. *
  219. * @param tc TableColumn that is ending;
  220. */
  221. public void endColumn(TableColumn tc) {
  222. }
  223. /**
  224. * @see org.apache.fop.fo.FOInputHandler#startHeader(TableBody)
  225. */
  226. public void startHeader(TableBody th) {
  227. }
  228. /**
  229. * @see org.apache.fop.fo.FOInputHandler#endHeader(TableBody)
  230. */
  231. public void endHeader(TableBody th) {
  232. }
  233. /**
  234. * @see org.apache.fop.fo.FOInputHandler#startFooter(TableBody)
  235. */
  236. public void startFooter(TableBody tf) {
  237. }
  238. /**
  239. * @see org.apache.fop.fo.FOInputHandler#endFooter(TableBody)
  240. */
  241. public void endFooter(TableBody tf) {
  242. }
  243. /**
  244. * @see org.apache.fop.fo.FOInputHandler#startBody(TableBody)
  245. */
  246. public void startBody(TableBody tb) {
  247. }
  248. /**
  249. * @see org.apache.fop.fo.FOInputHandler#endBody(TableBody)
  250. */
  251. public void endBody(TableBody tb) {
  252. }
  253. /**
  254. * @see org.apache.fop.fo.FOInputHandler#startRow(TableRow)
  255. */
  256. public void startRow(TableRow tr) {
  257. }
  258. /**
  259. * @see org.apache.fop.fo.FOInputHandler#endRow(TableRow)
  260. */
  261. public void endRow(TableRow tr) {
  262. }
  263. /**
  264. * @see org.apache.fop.fo.FOInputHandler#startCell(TableCell)
  265. */
  266. public void startCell(TableCell tc) {
  267. }
  268. /**
  269. * @see org.apache.fop.fo.FOInputHandler#endCell(TableCell)
  270. */
  271. public void endCell(TableCell tc) {
  272. }
  273. // Lists
  274. /**
  275. * @see org.apache.fop.fo.FOInputHandler#startList(ListBlock)
  276. */
  277. public void startList(ListBlock lb) {
  278. }
  279. /**
  280. * @see org.apache.fop.fo.FOInputHandler#endList(ListBlock)
  281. */
  282. public void endList(ListBlock lb) {
  283. }
  284. /**
  285. * @see org.apache.fop.fo.FOInputHandler#startListItem(ListItem)
  286. */
  287. public void startListItem(ListItem li) {
  288. }
  289. /**
  290. * @see org.apache.fop.fo.FOInputHandler#endListItem(ListItem)
  291. */
  292. public void endListItem(ListItem li) {
  293. }
  294. /**
  295. * @see org.apache.fop.fo.FOInputHandler#startListLabel()
  296. */
  297. public void startListLabel() {
  298. }
  299. /**
  300. * @see org.apache.fop.fo.FOInputHandler#endListLabel()
  301. */
  302. public void endListLabel() {
  303. }
  304. /**
  305. * @see org.apache.fop.fo.FOInputHandler#startListBody()
  306. */
  307. public void startListBody() {
  308. }
  309. /**
  310. * @see org.apache.fop.fo.FOInputHandler#endListBody()
  311. */
  312. public void endListBody() {
  313. }
  314. // Static Regions
  315. /**
  316. * @see org.apache.fop.fo.FOInputHandler#startStatic()
  317. */
  318. public void startStatic() {
  319. }
  320. /**
  321. * @see org.apache.fop.fo.FOInputHandler#endStatic()
  322. */
  323. public void endStatic() {
  324. }
  325. /**
  326. * @see org.apache.fop.fo.FOInputHandler#startMarkup()
  327. */
  328. public void startMarkup() {
  329. }
  330. /**
  331. * @see org.apache.fop.fo.FOInputHandler#endMarkup()
  332. */
  333. public void endMarkup() {
  334. }
  335. /**
  336. * @see org.apache.fop.fo.FOInputHandler#startLink(BasicLink basicLink)
  337. */
  338. public void startLink(BasicLink basicLink) {
  339. }
  340. /**
  341. * @see org.apache.fop.fo.FOInputHandler#endLink()
  342. */
  343. public void endLink() {
  344. }
  345. /**
  346. * @see org.apache.fop.fo.FOInputHandler#image(ExternalGraphic)
  347. */
  348. public void image(ExternalGraphic eg) {
  349. }
  350. /**
  351. * @see org.apache.fop.fo.FOInputHandler#pageRef()
  352. */
  353. public void pageRef() {
  354. }
  355. /**
  356. * @see org.apache.fop.fo.FOInputHandler#foreignObject(InstreamForeignObject)
  357. */
  358. public void foreignObject(InstreamForeignObject ifo) {
  359. }
  360. /**
  361. * @see org.apache.fop.fo.FOInputHandler#startFootnote(Footnote)
  362. */
  363. public void startFootnote(Footnote footnote) {
  364. }
  365. /**
  366. * @see org.apache.fop.fo.FOInputHandler#endFootnote(Footnote)
  367. */
  368. public void endFootnote(Footnote footnote) {
  369. }
  370. /**
  371. * @see org.apache.fop.fo.FOInputHandler#startFootnoteBody(FootnoteBody)
  372. */
  373. public void startFootnoteBody(FootnoteBody body) {
  374. }
  375. /**
  376. * @see org.apache.fop.fo.FOInputHandler#endFootnoteBody(FootnoteBody)
  377. */
  378. public void endFootnoteBody(FootnoteBody body) {
  379. }
  380. /**
  381. * @see org.apache.fop.fo.FOInputHandler#leader(Leader)
  382. */
  383. public void leader(Leader l) {
  384. }
  385. /**
  386. * @see org.apache.fop.fo.FOInputHandler#characters(char[], int, int)
  387. */
  388. public void characters(char[] data, int start, int length) {
  389. }
  390. /**
  391. * Get the font information for the layout handler.
  392. *
  393. * @return the font information
  394. */
  395. public FOTreeControl getFontInfo() {
  396. return foTreeControl;
  397. }
  398. /**
  399. * Add an object to the collection of objects that should be notified about
  400. * FOTreeEvent firings.
  401. * @param listener the Object which should be notified
  402. */
  403. public void addFOTreeListener (FOTreeListener listener) {
  404. if (listener == null) {
  405. return;
  406. }
  407. foTreeListeners.add(listener);
  408. }
  409. /**
  410. * Remove an object from the collection of objects that should be notified
  411. * about FOTreeEvent firings.
  412. * @param listener the Object which should no longer be notified
  413. */
  414. public void removeFOTreeListener (FOTreeListener listener) {
  415. if (listener == null) {
  416. return;
  417. }
  418. foTreeListeners.remove(listener);
  419. }
  420. /**
  421. * Notify all objects in the foTreeListeners that a "Page Sequence Complete"
  422. * FOTreeEvent has been fired.
  423. * @param eventType integer indicating which type of event is created
  424. * @param event the Event object that should be passed to the listeners
  425. */
  426. private void notifyPageSequenceComplete(PageSequence pageSequence)
  427. throws FOPException {
  428. FOTreeEvent event = new FOTreeEvent(this);
  429. event.setPageSequence(pageSequence);
  430. Iterator iterator = foTreeListeners.iterator();
  431. FOTreeListener foTreeListenerItem = null;
  432. while (iterator.hasNext()) {
  433. foTreeListenerItem = (FOTreeListener)iterator.next();
  434. foTreeListenerItem.foPageSequenceComplete(event);
  435. }
  436. }
  437. /**
  438. * Notify all objects in the foTreeListeners that a "Document Complete"
  439. * FOTreeEvent has been fired.
  440. * @param eventType integer indicating which type of event is created
  441. * @param event the Event object that should be passed to the listeners
  442. */
  443. private void notifyDocumentComplete()
  444. throws SAXException {
  445. FOTreeEvent event = new FOTreeEvent(this);
  446. Iterator iterator = foTreeListeners.iterator();
  447. FOTreeListener foTreeListenerItem = null;
  448. while (iterator.hasNext()) {
  449. foTreeListenerItem = (FOTreeListener)iterator.next();
  450. foTreeListenerItem.foDocumentComplete(event);
  451. }
  452. }
  453. /**
  454. *
  455. * @param pagenum PageNumber that is starting.
  456. */
  457. public void startPageNumber(PageNumber pagenum) {
  458. }
  459. /**
  460. *
  461. * @param pagenum PageNumber that is ending.
  462. */
  463. public void endPageNumber(PageNumber pagenum) {
  464. }
  465. }