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

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