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

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