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.

LayoutManagerMapping.java 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  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.layoutmgr;
  19. import java.util.ArrayList;
  20. import java.util.HashMap;
  21. import java.util.Iterator;
  22. import java.util.List;
  23. import java.util.Map;
  24. import org.apache.commons.logging.Log;
  25. import org.apache.commons.logging.LogFactory;
  26. import org.apache.fop.area.AreaTreeHandler;
  27. import org.apache.fop.fo.FOElementMapping;
  28. import org.apache.fop.fo.FONode;
  29. import org.apache.fop.fo.FONode.FONodeIterator;
  30. import org.apache.fop.fo.FOText;
  31. import org.apache.fop.fo.FObjMixed;
  32. import org.apache.fop.fo.extensions.ExternalDocument;
  33. import org.apache.fop.fo.flow.BasicLink;
  34. import org.apache.fop.fo.flow.BidiOverride;
  35. import org.apache.fop.fo.flow.Block;
  36. import org.apache.fop.fo.flow.BlockContainer;
  37. import org.apache.fop.fo.flow.Character;
  38. import org.apache.fop.fo.flow.ExternalGraphic;
  39. import org.apache.fop.fo.flow.Footnote;
  40. import org.apache.fop.fo.flow.Inline;
  41. import org.apache.fop.fo.flow.InlineContainer;
  42. import org.apache.fop.fo.flow.InlineLevel;
  43. import org.apache.fop.fo.flow.InstreamForeignObject;
  44. import org.apache.fop.fo.flow.Leader;
  45. import org.apache.fop.fo.flow.ListBlock;
  46. import org.apache.fop.fo.flow.ListItem;
  47. import org.apache.fop.fo.flow.PageNumber;
  48. import org.apache.fop.fo.flow.PageNumberCitation;
  49. import org.apache.fop.fo.flow.PageNumberCitationLast;
  50. import org.apache.fop.fo.flow.RetrieveMarker;
  51. import org.apache.fop.fo.flow.RetrieveTableMarker;
  52. import org.apache.fop.fo.flow.Wrapper;
  53. import org.apache.fop.fo.flow.table.Table;
  54. import org.apache.fop.fo.flow.table.TableBody;
  55. import org.apache.fop.fo.flow.table.TableCell;
  56. import org.apache.fop.fo.flow.table.TableColumn;
  57. import org.apache.fop.fo.flow.table.TableFooter;
  58. import org.apache.fop.fo.flow.table.TableHeader;
  59. import org.apache.fop.fo.flow.table.TableRow;
  60. import org.apache.fop.fo.pagination.Flow;
  61. import org.apache.fop.fo.pagination.PageSequence;
  62. import org.apache.fop.fo.pagination.SideRegion;
  63. import org.apache.fop.fo.pagination.StaticContent;
  64. import org.apache.fop.fo.pagination.Title;
  65. import org.apache.fop.layoutmgr.inline.BasicLinkLayoutManager;
  66. import org.apache.fop.layoutmgr.inline.BidiLayoutManager;
  67. import org.apache.fop.layoutmgr.inline.CharacterLayoutManager;
  68. import org.apache.fop.layoutmgr.inline.ContentLayoutManager;
  69. import org.apache.fop.layoutmgr.inline.ExternalGraphicLayoutManager;
  70. import org.apache.fop.layoutmgr.inline.FootnoteLayoutManager;
  71. import org.apache.fop.layoutmgr.inline.ICLayoutManager;
  72. import org.apache.fop.layoutmgr.inline.InlineLayoutManager;
  73. import org.apache.fop.layoutmgr.inline.InstreamForeignObjectLM;
  74. import org.apache.fop.layoutmgr.inline.LeaderLayoutManager;
  75. import org.apache.fop.layoutmgr.inline.PageNumberCitationLastLayoutManager;
  76. import org.apache.fop.layoutmgr.inline.PageNumberCitationLayoutManager;
  77. import org.apache.fop.layoutmgr.inline.PageNumberLayoutManager;
  78. import org.apache.fop.layoutmgr.inline.TextLayoutManager;
  79. import org.apache.fop.layoutmgr.inline.WrapperLayoutManager;
  80. import org.apache.fop.layoutmgr.list.ListBlockLayoutManager;
  81. import org.apache.fop.layoutmgr.list.ListItemLayoutManager;
  82. import org.apache.fop.layoutmgr.table.TableLayoutManager;
  83. import org.apache.fop.util.CharUtilities;
  84. /**
  85. * The default LayoutManager maker class
  86. */
  87. public class LayoutManagerMapping implements LayoutManagerMaker {
  88. /** logging instance */
  89. private static final Log LOG = LogFactory.getLog(LayoutManagerMapping.class);
  90. /** The map of LayoutManagerMakers */
  91. private Map makers = new HashMap();
  92. /** default constructor */
  93. public LayoutManagerMapping() {
  94. initialize();
  95. }
  96. /**
  97. * Initializes the set of maker objects associated with this LayoutManagerMapping
  98. */
  99. protected void initialize() {
  100. registerMaker(FOText.class, new FOTextLayoutManagerMaker());
  101. registerMaker(FObjMixed.class, new Maker());
  102. registerMaker(BidiOverride.class, new BidiOverrideLayoutManagerMaker());
  103. registerMaker(Inline.class, new InlineLayoutManagerMaker());
  104. registerMaker(Footnote.class, new FootnodeLayoutManagerMaker());
  105. registerMaker(InlineContainer.class,
  106. new InlineContainerLayoutManagerMaker());
  107. registerMaker(BasicLink.class, new BasicLinkLayoutManagerMaker());
  108. registerMaker(Block.class, new BlockLayoutManagerMaker());
  109. registerMaker(Leader.class, new LeaderLayoutManagerMaker());
  110. registerMaker(RetrieveMarker.class, new RetrieveMarkerLayoutManagerMaker());
  111. registerMaker(RetrieveTableMarker.class, new RetrieveTableMarkerLayoutManagerMaker());
  112. registerMaker(Character.class, new CharacterLayoutManagerMaker());
  113. registerMaker(ExternalGraphic.class,
  114. new ExternalGraphicLayoutManagerMaker());
  115. registerMaker(BlockContainer.class,
  116. new BlockContainerLayoutManagerMaker());
  117. registerMaker(ListItem.class, new ListItemLayoutManagerMaker());
  118. registerMaker(ListBlock.class, new ListBlockLayoutManagerMaker());
  119. registerMaker(InstreamForeignObject.class,
  120. new InstreamForeignObjectLayoutManagerMaker());
  121. registerMaker(PageNumber.class, new PageNumberLayoutManagerMaker());
  122. registerMaker(PageNumberCitation.class,
  123. new PageNumberCitationLayoutManagerMaker());
  124. registerMaker(PageNumberCitationLast.class,
  125. new PageNumberCitationLastLayoutManagerMaker());
  126. registerMaker(Table.class, new TableLayoutManagerMaker());
  127. registerMaker(TableBody.class, new Maker());
  128. registerMaker(TableColumn.class, new Maker());
  129. registerMaker(TableRow.class, new Maker());
  130. registerMaker(TableCell.class, new Maker());
  131. registerMaker(TableFooter.class, new Maker());
  132. registerMaker(TableHeader.class, new Maker());
  133. registerMaker(Wrapper.class, new WrapperLayoutManagerMaker());
  134. registerMaker(Title.class, new InlineLayoutManagerMaker());
  135. }
  136. /**
  137. * Registers a Maker class for a specific formatting object.
  138. * @param clazz the formatting object class
  139. * @param maker the maker for the layout manager
  140. */
  141. protected void registerMaker(Class clazz, Maker maker) {
  142. makers.put(clazz, maker);
  143. }
  144. /** {@inheritDoc} */
  145. public void makeLayoutManagers(FONode node, List lms) {
  146. Maker maker = (Maker) makers.get(node.getClass());
  147. if (maker == null) {
  148. if (FOElementMapping.URI.equals(node.getNamespaceURI())) {
  149. LOG.error("No LayoutManager maker for class " + node.getClass());
  150. } else {
  151. if (LOG.isDebugEnabled()) {
  152. LOG.debug("Skipping the creation of a layout manager for " + node.getClass());
  153. }
  154. }
  155. } else {
  156. maker.make(node, lms);
  157. }
  158. }
  159. /** {@inheritDoc} */
  160. public LayoutManager makeLayoutManager(FONode node) {
  161. List lms = new ArrayList();
  162. makeLayoutManagers(node, lms);
  163. if (lms.size() == 0) {
  164. throw new IllegalStateException("LayoutManager for class "
  165. + node.getClass()
  166. + " is missing.");
  167. } else if (lms.size() > 1) {
  168. throw new IllegalStateException("Duplicate LayoutManagers for class "
  169. + node.getClass()
  170. + " found, only one may be declared.");
  171. }
  172. return (LayoutManager) lms.get(0);
  173. }
  174. /** {@inheritDoc} */
  175. public PageSequenceLayoutManager makePageSequenceLayoutManager(
  176. AreaTreeHandler ath, PageSequence ps) {
  177. return new PageSequenceLayoutManager(ath, ps);
  178. }
  179. /** {@inheritDoc} */
  180. public ExternalDocumentLayoutManager makeExternalDocumentLayoutManager(
  181. AreaTreeHandler ath, ExternalDocument ed) {
  182. return new ExternalDocumentLayoutManager(ath, ed);
  183. }
  184. /** {@inheritDoc} */
  185. public FlowLayoutManager makeFlowLayoutManager(
  186. PageSequenceLayoutManager pslm, Flow flow) {
  187. return new FlowLayoutManager(pslm, flow);
  188. }
  189. /** {@inheritDoc} */
  190. public ContentLayoutManager makeContentLayoutManager(PageSequenceLayoutManager pslm,
  191. Title title) {
  192. return new ContentLayoutManager(pslm, title);
  193. }
  194. /** {@inheritDoc} */
  195. public StaticContentLayoutManager makeStaticContentLayoutManager(
  196. PageSequenceLayoutManager pslm, StaticContent sc, SideRegion reg) {
  197. return new StaticContentLayoutManager(pslm, sc, reg);
  198. }
  199. /** {@inheritDoc} */
  200. public StaticContentLayoutManager makeStaticContentLayoutManager(
  201. PageSequenceLayoutManager pslm, StaticContent sc, org.apache.fop.area.Block block) {
  202. return new StaticContentLayoutManager(pslm, sc, block);
  203. }
  204. /** a layout manager maker base class */
  205. public static class Maker {
  206. /**
  207. * Create a layout manager.
  208. * @param node the associated FO node
  209. * @param lms a list of layout managers to which new manager is to be added
  210. */
  211. public void make(FONode node, List lms) {
  212. // no layout manager
  213. }
  214. }
  215. /** a layout manager maker */
  216. public static class FOTextLayoutManagerMaker extends Maker {
  217. /** {@inheritDoc} */
  218. public void make(FONode node, List lms) {
  219. FOText foText = (FOText) node;
  220. if (foText.length() > 0) {
  221. lms.add(new TextLayoutManager(foText));
  222. }
  223. }
  224. }
  225. /** a layout manager maker */
  226. public static class BidiOverrideLayoutManagerMaker extends Maker {
  227. /** {@inheritDoc} */
  228. public void make(FONode node, List lms) {
  229. if ( node instanceof BidiOverride ) {
  230. lms.add(new BidiLayoutManager((BidiOverride) node));
  231. }
  232. }
  233. }
  234. /** a layout manager maker */
  235. public static class InlineLayoutManagerMaker extends Maker {
  236. /** {@inheritDoc} */
  237. public void make(FONode node, List lms) {
  238. lms.add(new InlineLayoutManager((InlineLevel) node));
  239. }
  240. }
  241. /** a layout manager maker */
  242. public static class FootnodeLayoutManagerMaker extends Maker {
  243. /** {@inheritDoc} */
  244. public void make(FONode node, List lms) {
  245. lms.add(new FootnoteLayoutManager((Footnote) node));
  246. }
  247. }
  248. /** a layout manager maker */
  249. public static class InlineContainerLayoutManagerMaker extends Maker {
  250. /** {@inheritDoc} */
  251. public void make(FONode node, List lms) {
  252. ArrayList childList = new ArrayList();
  253. super.make(node, childList);
  254. lms.add(new ICLayoutManager((InlineContainer) node, childList));
  255. }
  256. }
  257. /** a layout manager maker */
  258. public static class BasicLinkLayoutManagerMaker extends Maker {
  259. /** {@inheritDoc} */
  260. public void make(FONode node, List lms) {
  261. lms.add(new BasicLinkLayoutManager((BasicLink) node));
  262. }
  263. }
  264. /** a layout manager maker */
  265. public static class BlockLayoutManagerMaker extends Maker {
  266. /** {@inheritDoc} */
  267. public void make(FONode node, List lms) {
  268. lms.add(new BlockLayoutManager((Block) node));
  269. }
  270. }
  271. /** a layout manager maker */
  272. public static class LeaderLayoutManagerMaker extends Maker {
  273. /** {@inheritDoc} */
  274. public void make(FONode node, List lms) {
  275. lms.add(new LeaderLayoutManager((Leader) node));
  276. }
  277. }
  278. /** a layout manager maker */
  279. public static class CharacterLayoutManagerMaker extends Maker {
  280. /** {@inheritDoc} */
  281. public void make(FONode node, List lms) {
  282. Character foCharacter = (Character) node;
  283. if (foCharacter.getCharacter() != CharUtilities.CODE_EOT) {
  284. lms.add(new CharacterLayoutManager(foCharacter));
  285. }
  286. }
  287. }
  288. /** a layout manager maker */
  289. public static class ExternalGraphicLayoutManagerMaker extends Maker {
  290. /** {@inheritDoc} */
  291. public void make(FONode node, List lms) {
  292. ExternalGraphic eg = (ExternalGraphic) node;
  293. if (!eg.getSrc().equals("")) {
  294. lms.add(new ExternalGraphicLayoutManager(eg));
  295. }
  296. }
  297. }
  298. /** a layout manager maker */
  299. public static class BlockContainerLayoutManagerMaker extends Maker {
  300. /** {@inheritDoc} */
  301. public void make(FONode node, List lms) {
  302. lms.add(new BlockContainerLayoutManager((BlockContainer) node));
  303. }
  304. }
  305. /** a layout manager maker */
  306. public static class ListItemLayoutManagerMaker extends Maker {
  307. /** {@inheritDoc} */
  308. public void make(FONode node, List lms) {
  309. lms.add(new ListItemLayoutManager((ListItem) node));
  310. }
  311. }
  312. /** a layout manager maker */
  313. public static class ListBlockLayoutManagerMaker extends Maker {
  314. /** {@inheritDoc} */
  315. public void make(FONode node, List lms) {
  316. lms.add(new ListBlockLayoutManager((ListBlock) node));
  317. }
  318. }
  319. /** a layout manager maker */
  320. public static class InstreamForeignObjectLayoutManagerMaker extends Maker {
  321. /** {@inheritDoc} */
  322. public void make(FONode node, List lms) {
  323. lms.add(new InstreamForeignObjectLM((InstreamForeignObject) node));
  324. }
  325. }
  326. /** a layout manager maker */
  327. public static class PageNumberLayoutManagerMaker extends Maker {
  328. /** {@inheritDoc} */
  329. public void make(FONode node, List lms) {
  330. lms.add(new PageNumberLayoutManager((PageNumber) node));
  331. }
  332. }
  333. /** a layout manager maker */
  334. public static class PageNumberCitationLayoutManagerMaker extends Maker {
  335. /** {@inheritDoc} */
  336. public void make(FONode node, List lms) {
  337. lms.add(new PageNumberCitationLayoutManager((PageNumberCitation) node));
  338. }
  339. }
  340. /** a layout manager maker */
  341. public static class PageNumberCitationLastLayoutManagerMaker extends Maker {
  342. /** {@inheritDoc} */
  343. public void make(FONode node, List lms) {
  344. lms.add(new PageNumberCitationLastLayoutManager((PageNumberCitationLast) node));
  345. }
  346. }
  347. /** a layout manager maker */
  348. public static class TableLayoutManagerMaker extends Maker {
  349. /** {@inheritDoc} */
  350. public void make(FONode node, List lms) {
  351. Table table = (Table) node;
  352. TableLayoutManager tlm = new TableLayoutManager(table);
  353. lms.add(tlm);
  354. }
  355. }
  356. /** a layout manager maker */
  357. public class RetrieveMarkerLayoutManagerMaker extends Maker {
  358. /** {@inheritDoc} */
  359. public void make(FONode node, List lms) {
  360. Iterator baseIter;
  361. baseIter = node.getChildNodes();
  362. if (baseIter == null) {
  363. return;
  364. }
  365. while (baseIter.hasNext()) {
  366. FONode child = (FONode) baseIter.next();
  367. makeLayoutManagers(child, lms);
  368. }
  369. }
  370. }
  371. public class RetrieveTableMarkerLayoutManagerMaker extends Maker {
  372. public void make(FONode node, List lms) {
  373. FONodeIterator baseIter = node.getChildNodes();
  374. if (baseIter == null) {
  375. // this happens when the retrieve-table-marker cannot be resolved yet
  376. RetrieveTableMarker rtm = (RetrieveTableMarker) node;
  377. RetrieveTableMarkerLayoutManager rtmlm = new RetrieveTableMarkerLayoutManager(rtm);
  378. lms.add(rtmlm);
  379. return;
  380. }
  381. while (baseIter.hasNext()) {
  382. // this happens when the retrieve-table-marker has been resolved
  383. FONode child = (FONode) baseIter.next();
  384. makeLayoutManagers(child, lms);
  385. }
  386. }
  387. }
  388. /** a layout manager maker */
  389. public class WrapperLayoutManagerMaker extends Maker {
  390. /** {@inheritDoc} */
  391. public void make(FONode node, List lms) {
  392. //We insert the wrapper LM before it's children so an ID
  393. //on the node can be registered on a page.
  394. lms.add(new WrapperLayoutManager((Wrapper)node));
  395. Iterator baseIter;
  396. baseIter = node.getChildNodes();
  397. if (baseIter == null) {
  398. return;
  399. }
  400. while (baseIter.hasNext()) {
  401. FONode child = (FONode) baseIter.next();
  402. makeLayoutManagers(child, lms);
  403. }
  404. }
  405. }
  406. }