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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  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.InlineContainerLayoutManager;
  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. lms.add(new InlineContainerLayoutManager((InlineContainer) node));
  253. }
  254. }
  255. /** a layout manager maker */
  256. public static class BasicLinkLayoutManagerMaker extends Maker {
  257. /** {@inheritDoc} */
  258. public void make(FONode node, List lms) {
  259. lms.add(new BasicLinkLayoutManager((BasicLink) node));
  260. }
  261. }
  262. /** a layout manager maker */
  263. public static class BlockLayoutManagerMaker extends Maker {
  264. /** {@inheritDoc} */
  265. public void make(FONode node, List lms) {
  266. lms.add(new BlockLayoutManager((Block) node));
  267. }
  268. }
  269. /** a layout manager maker */
  270. public static class LeaderLayoutManagerMaker extends Maker {
  271. /** {@inheritDoc} */
  272. public void make(FONode node, List lms) {
  273. lms.add(new LeaderLayoutManager((Leader) node));
  274. }
  275. }
  276. /** a layout manager maker */
  277. public static class CharacterLayoutManagerMaker extends Maker {
  278. /** {@inheritDoc} */
  279. public void make(FONode node, List lms) {
  280. Character foCharacter = (Character) node;
  281. if (foCharacter.getCharacter() != CharUtilities.CODE_EOT) {
  282. lms.add(new CharacterLayoutManager(foCharacter));
  283. }
  284. }
  285. }
  286. /** a layout manager maker */
  287. public static class ExternalGraphicLayoutManagerMaker extends Maker {
  288. /** {@inheritDoc} */
  289. public void make(FONode node, List lms) {
  290. ExternalGraphic eg = (ExternalGraphic) node;
  291. if (!eg.getSrc().equals("")) {
  292. lms.add(new ExternalGraphicLayoutManager(eg));
  293. }
  294. }
  295. }
  296. /** a layout manager maker */
  297. public static class BlockContainerLayoutManagerMaker extends Maker {
  298. /** {@inheritDoc} */
  299. public void make(FONode node, List lms) {
  300. lms.add(new BlockContainerLayoutManager((BlockContainer) node));
  301. }
  302. }
  303. /** a layout manager maker */
  304. public static class ListItemLayoutManagerMaker extends Maker {
  305. /** {@inheritDoc} */
  306. public void make(FONode node, List lms) {
  307. lms.add(new ListItemLayoutManager((ListItem) node));
  308. }
  309. }
  310. /** a layout manager maker */
  311. public static class ListBlockLayoutManagerMaker extends Maker {
  312. /** {@inheritDoc} */
  313. public void make(FONode node, List lms) {
  314. lms.add(new ListBlockLayoutManager((ListBlock) node));
  315. }
  316. }
  317. /** a layout manager maker */
  318. public static class InstreamForeignObjectLayoutManagerMaker extends Maker {
  319. /** {@inheritDoc} */
  320. public void make(FONode node, List lms) {
  321. lms.add(new InstreamForeignObjectLM((InstreamForeignObject) node));
  322. }
  323. }
  324. /** a layout manager maker */
  325. public static class PageNumberLayoutManagerMaker extends Maker {
  326. /** {@inheritDoc} */
  327. public void make(FONode node, List lms) {
  328. lms.add(new PageNumberLayoutManager((PageNumber) node));
  329. }
  330. }
  331. /** a layout manager maker */
  332. public static class PageNumberCitationLayoutManagerMaker extends Maker {
  333. /** {@inheritDoc} */
  334. public void make(FONode node, List lms) {
  335. lms.add(new PageNumberCitationLayoutManager((PageNumberCitation) node));
  336. }
  337. }
  338. /** a layout manager maker */
  339. public static class PageNumberCitationLastLayoutManagerMaker extends Maker {
  340. /** {@inheritDoc} */
  341. public void make(FONode node, List lms) {
  342. lms.add(new PageNumberCitationLastLayoutManager((PageNumberCitationLast) node));
  343. }
  344. }
  345. /** a layout manager maker */
  346. public static class TableLayoutManagerMaker extends Maker {
  347. /** {@inheritDoc} */
  348. public void make(FONode node, List lms) {
  349. Table table = (Table) node;
  350. TableLayoutManager tlm = new TableLayoutManager(table);
  351. lms.add(tlm);
  352. }
  353. }
  354. /** a layout manager maker */
  355. public class RetrieveMarkerLayoutManagerMaker extends Maker {
  356. /** {@inheritDoc} */
  357. public void make(FONode node, List lms) {
  358. Iterator baseIter;
  359. baseIter = node.getChildNodes();
  360. if (baseIter == null) {
  361. return;
  362. }
  363. while (baseIter.hasNext()) {
  364. FONode child = (FONode) baseIter.next();
  365. makeLayoutManagers(child, lms);
  366. }
  367. }
  368. }
  369. public class RetrieveTableMarkerLayoutManagerMaker extends Maker {
  370. public void make(FONode node, List lms) {
  371. FONodeIterator baseIter = node.getChildNodes();
  372. if (baseIter == null) {
  373. // this happens when the retrieve-table-marker cannot be resolved yet
  374. RetrieveTableMarker rtm = (RetrieveTableMarker) node;
  375. RetrieveTableMarkerLayoutManager rtmlm = new RetrieveTableMarkerLayoutManager(rtm);
  376. lms.add(rtmlm);
  377. return;
  378. }
  379. while (baseIter.hasNext()) {
  380. // this happens when the retrieve-table-marker has been resolved
  381. FONode child = (FONode) baseIter.next();
  382. makeLayoutManagers(child, lms);
  383. }
  384. }
  385. }
  386. /** a layout manager maker */
  387. public class WrapperLayoutManagerMaker extends Maker {
  388. /** {@inheritDoc} */
  389. public void make(FONode node, List lms) {
  390. //We insert the wrapper LM before it's children so an ID
  391. //on the node can be registered on a page.
  392. lms.add(new WrapperLayoutManager((Wrapper)node));
  393. Iterator baseIter;
  394. baseIter = node.getChildNodes();
  395. if (baseIter == null) {
  396. return;
  397. }
  398. while (baseIter.hasNext()) {
  399. FONode child = (FONode) baseIter.next();
  400. makeLayoutManagers(child, lms);
  401. }
  402. }
  403. }
  404. }