Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

LayoutManagerMapping.java 18KB

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