001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: /* $Id: LayoutManagerMapping.java 525526 2007-04-04 15:05:30Z jeremias $ */
019:
020: package org.apache.fop.layoutmgr;
021:
022: import java.util.ArrayList;
023: import java.util.Map;
024: import java.util.HashMap;
025: import java.util.List;
026: import java.util.Iterator;
027:
028: import org.apache.commons.logging.Log;
029: import org.apache.commons.logging.LogFactory;
030:
031: import org.apache.fop.fo.FOElementMapping;
032: import org.apache.fop.fo.FONode;
033: import org.apache.fop.fo.FOText;
034: import org.apache.fop.fo.FObjMixed;
035: import org.apache.fop.fo.flow.BasicLink;
036: import org.apache.fop.fo.flow.BidiOverride;
037: import org.apache.fop.fo.flow.Block;
038: import org.apache.fop.fo.flow.BlockContainer;
039: import org.apache.fop.fo.flow.Character;
040: import org.apache.fop.fo.flow.ExternalGraphic;
041: import org.apache.fop.fo.flow.Footnote;
042: import org.apache.fop.fo.flow.Inline;
043: import org.apache.fop.fo.flow.InlineLevel;
044: import org.apache.fop.fo.flow.InlineContainer;
045: import org.apache.fop.fo.flow.InstreamForeignObject;
046: import org.apache.fop.fo.flow.Leader;
047: import org.apache.fop.fo.flow.ListBlock;
048: import org.apache.fop.fo.flow.ListItem;
049: import org.apache.fop.fo.flow.PageNumber;
050: import org.apache.fop.fo.flow.PageNumberCitation;
051: import org.apache.fop.fo.flow.PageNumberCitationLast;
052: import org.apache.fop.fo.flow.RetrieveMarker;
053: import org.apache.fop.fo.flow.Table;
054: import org.apache.fop.fo.flow.TableBody;
055: import org.apache.fop.fo.flow.TableCell;
056: import org.apache.fop.fo.flow.TableColumn;
057: import org.apache.fop.fo.flow.TableFooter;
058: import org.apache.fop.fo.flow.TableHeader;
059: import org.apache.fop.fo.flow.TableRow;
060: import org.apache.fop.fo.flow.Wrapper;
061: import org.apache.fop.fo.pagination.Flow;
062: import org.apache.fop.fo.pagination.PageSequence;
063: import org.apache.fop.fo.pagination.SideRegion;
064: import org.apache.fop.fo.pagination.StaticContent;
065: import org.apache.fop.fo.pagination.Title;
066: import org.apache.fop.area.AreaTreeHandler;
067:
068: import org.apache.fop.layoutmgr.inline.BasicLinkLayoutManager;
069: import org.apache.fop.layoutmgr.inline.BidiLayoutManager;
070: import org.apache.fop.layoutmgr.inline.CharacterLayoutManager;
071: import org.apache.fop.layoutmgr.inline.ContentLayoutManager;
072: import org.apache.fop.layoutmgr.inline.ExternalGraphicLayoutManager;
073: import org.apache.fop.layoutmgr.inline.FootnoteLayoutManager;
074: import org.apache.fop.layoutmgr.inline.ICLayoutManager;
075: import org.apache.fop.layoutmgr.inline.InlineLayoutManager;
076: import org.apache.fop.layoutmgr.inline.InlineLevelLayoutManager;
077: import org.apache.fop.layoutmgr.inline.InstreamForeignObjectLM;
078: import org.apache.fop.layoutmgr.inline.LeaderLayoutManager;
079: import org.apache.fop.layoutmgr.inline.PageNumberCitationLayoutManager;
080: import org.apache.fop.layoutmgr.inline.PageNumberCitationLastLayoutManager;
081: import org.apache.fop.layoutmgr.inline.PageNumberLayoutManager;
082: import org.apache.fop.layoutmgr.inline.TextLayoutManager;
083: import org.apache.fop.layoutmgr.inline.WrapperLayoutManager;
084: import org.apache.fop.layoutmgr.list.ListBlockLayoutManager;
085: import org.apache.fop.layoutmgr.list.ListItemLayoutManager;
086: import org.apache.fop.layoutmgr.table.TableLayoutManager;
087: import org.apache.fop.util.CharUtilities;
088:
089: import org.apache.fop.fo.extensions.destination.Destination;
090:
091: /**
092: * The default LayoutManager maker class
093: */
094: public class LayoutManagerMapping implements LayoutManagerMaker {
095:
096: /** logging instance */
097: protected static Log log = LogFactory
098: .getLog(LayoutManagerMapping.class);
099:
100: /** The map of LayoutManagerMakers */
101: private Map makers = new HashMap();
102:
103: public LayoutManagerMapping() {
104: initialize();
105: }
106:
107: /**
108: * Initializes the set of maker objects associated with this LayoutManagerMapping
109: */
110: protected void initialize() {
111: makers.put(FOText.class, new FOTextLayoutManagerMaker());
112: makers.put(FObjMixed.class, new Maker());
113: makers.put(BidiOverride.class,
114: new BidiOverrideLayoutManagerMaker());
115: makers.put(Inline.class, new InlineLayoutManagerMaker());
116: makers.put(Footnote.class, new FootnodeLayoutManagerMaker());
117: makers.put(InlineContainer.class,
118: new InlineContainerLayoutManagerMaker());
119: makers.put(BasicLink.class, new BasicLinkLayoutManagerMaker());
120: makers.put(Block.class, new BlockLayoutManagerMaker());
121: makers.put(Leader.class, new LeaderLayoutManagerMaker());
122: makers.put(RetrieveMarker.class,
123: new RetrieveMarkerLayoutManagerMaker());
124: makers.put(Character.class, new CharacterLayoutManagerMaker());
125: makers.put(ExternalGraphic.class,
126: new ExternalGraphicLayoutManagerMaker());
127: makers.put(BlockContainer.class,
128: new BlockContainerLayoutManagerMaker());
129: makers.put(ListItem.class, new ListItemLayoutManagerMaker());
130: makers.put(ListBlock.class, new ListBlockLayoutManagerMaker());
131: makers.put(InstreamForeignObject.class,
132: new InstreamForeignObjectLayoutManagerMaker());
133: makers
134: .put(PageNumber.class,
135: new PageNumberLayoutManagerMaker());
136: makers.put(PageNumberCitation.class,
137: new PageNumberCitationLayoutManagerMaker());
138: makers.put(PageNumberCitationLast.class,
139: new PageNumberCitationLastLayoutManagerMaker());
140: makers.put(Table.class, new TableLayoutManagerMaker());
141: makers.put(TableBody.class, new Maker());
142: makers.put(TableColumn.class, new Maker());
143: makers.put(TableRow.class, new Maker());
144: makers.put(TableCell.class, new Maker());
145: makers.put(TableFooter.class, new Maker());
146: makers.put(TableHeader.class, new Maker());
147: makers.put(Wrapper.class, new WrapperLayoutManagerMaker());
148: makers.put(Title.class, new InlineLayoutManagerMaker());
149: }
150:
151: /**
152: * @see org.apache.fop.layoutmgr.LayoutManagerMaker#makeLayoutManagers(FONode, List)
153: */
154: public void makeLayoutManagers(FONode node, List lms) {
155: Maker maker = (Maker) makers.get(node.getClass());
156: if (maker == null) {
157: if (FOElementMapping.URI.equals(node.getNamespaceURI())) {
158: log.error("No LayoutManager maker for class "
159: + node.getClass());
160: } else {
161: if (log.isDebugEnabled()) {
162: log
163: .debug("Skipping the creation of a layout manager for "
164: + node.getClass());
165: }
166: }
167: } else {
168: maker.make(node, lms);
169: }
170: }
171:
172: /**
173: * @see org.apache.fop.layoutmgr.LayoutManagerMaker#makeLayoutManager(FONode)
174: */
175: public LayoutManager makeLayoutManager(FONode node) {
176: List lms = new ArrayList();
177: makeLayoutManagers(node, lms);
178: if (lms.size() == 0) {
179: throw new IllegalStateException("LayoutManager for class "
180: + node.getClass() + " is missing.");
181: } else if (lms.size() > 1) {
182: throw new IllegalStateException(
183: "Duplicate LayoutManagers for class "
184: + node.getClass()
185: + " found, only one may be declared.");
186: }
187: return (LayoutManager) lms.get(0);
188: }
189:
190: public PageSequenceLayoutManager makePageSequenceLayoutManager(
191: AreaTreeHandler ath, PageSequence ps) {
192: return new PageSequenceLayoutManager(ath, ps);
193: }
194:
195: /*
196: * @see org.apache.fop.layoutmgr.LayoutManagerMaker#makeFlowLayoutManager(PageSequenceLayoutManager, Flow)
197: */
198: public FlowLayoutManager makeFlowLayoutManager(
199: PageSequenceLayoutManager pslm, Flow flow) {
200: return new FlowLayoutManager(pslm, flow);
201: }
202:
203: /*
204: * @see org.apache.fop.layoutmgr.LayoutManagerMaker#makeContentLayoutManager(PageSequenceLayoutManager, Title)
205: */
206: public ContentLayoutManager makeContentLayoutManager(
207: PageSequenceLayoutManager pslm, Title title) {
208: return new ContentLayoutManager(pslm, title);
209: }
210:
211: /*
212: * @see org.apache.fop.layoutmgr.LayoutManagerMaker#makeStaticContentLayoutManager(PageSequenceLayoutManager, StaticContent, Region)
213: */
214: public StaticContentLayoutManager makeStaticContentLayoutManager(
215: PageSequenceLayoutManager pslm, StaticContent sc,
216: SideRegion reg) {
217: return new StaticContentLayoutManager(pslm, sc, reg);
218: }
219:
220: /*
221: * @see org.apache.fop.layoutmgr.LayoutManagerMaker#makeStaticContentLayoutManager(PageSequenceLayoutManager, StaticContent, Block)
222: */
223: public StaticContentLayoutManager makeStaticContentLayoutManager(
224: PageSequenceLayoutManager pslm, StaticContent sc,
225: org.apache.fop.area.Block block) {
226: return new StaticContentLayoutManager(pslm, sc, block);
227: }
228:
229: public static class Maker {
230: public void make(FONode node, List lms) {
231: // no layout manager
232: return;
233: }
234: }
235:
236: public static class FOTextLayoutManagerMaker extends Maker {
237: public void make(FONode node, List lms) {
238: FOText foText = (FOText) node;
239: if (foText.endIndex - foText.startIndex > 0) {
240: lms.add(new TextLayoutManager(foText));
241: }
242: }
243: }
244:
245: /*
246: public static class FObjMixedLayoutManagerMaker extends Maker {
247: public void make(FONode node, List lms) {
248: if (node.getChildNodes() != null) {
249: InlineStackingLayoutManager lm;
250: lm = new InlineStackingLayoutManager((FObjMixed) node);
251: lms.add(lm);
252: }
253: }
254: }
255: */
256:
257: public static class BidiOverrideLayoutManagerMaker extends Maker {
258: // public static class BidiOverrideLayoutManagerMaker extends FObjMixedLayoutManagerMaker {
259: public void make(BidiOverride node, List lms) {
260: if (false) {
261: // this is broken; it does nothing
262: // it should make something like an InlineStackingLM
263: super .make(node, lms);
264: } else {
265: ArrayList childList = new ArrayList();
266: // this is broken; it does nothing
267: // it should make something like an InlineStackingLM
268: super .make(node, childList);
269: for (int count = childList.size() - 1; count >= 0; count--) {
270: LayoutManager lm = (LayoutManager) childList
271: .get(count);
272: if (lm instanceof InlineLevelLayoutManager) {
273: LayoutManager blm = new BidiLayoutManager(node,
274: (InlineLayoutManager) lm);
275: lms.add(blm);
276: } else {
277: lms.add(lm);
278: }
279: }
280: }
281: }
282: }
283:
284: public static class InlineLayoutManagerMaker extends Maker {
285: public void make(FONode node, List lms) {
286: if (node.getChildNodes() != null) {
287: lms.add(new InlineLayoutManager((InlineLevel) node));
288: }
289: }
290: }
291:
292: public static class FootnodeLayoutManagerMaker extends Maker {
293: public void make(FONode node, List lms) {
294: lms.add(new FootnoteLayoutManager((Footnote) node));
295: }
296: }
297:
298: public static class InlineContainerLayoutManagerMaker extends Maker {
299: public void make(FONode node, List lms) {
300: ArrayList childList = new ArrayList();
301: super .make(node, childList);
302: lms.add(new ICLayoutManager((InlineContainer) node,
303: childList));
304: }
305: }
306:
307: public static class BasicLinkLayoutManagerMaker extends Maker {
308: public void make(FONode node, List lms) {
309: lms.add(new BasicLinkLayoutManager((BasicLink) node));
310: }
311: }
312:
313: public static class BlockLayoutManagerMaker extends Maker {
314: public void make(FONode node, List lms) {
315: lms.add(new BlockLayoutManager((Block) node));
316: }
317: }
318:
319: public static class LeaderLayoutManagerMaker extends Maker {
320: public void make(FONode node, List lms) {
321: lms.add(new LeaderLayoutManager((Leader) node));
322: }
323: }
324:
325: public static class CharacterLayoutManagerMaker extends Maker {
326: public void make(FONode node, List lms) {
327: Character foCharacter = (Character) node;
328: if (foCharacter.getCharacter() != CharUtilities.CODE_EOT) {
329: lms.add(new CharacterLayoutManager(foCharacter));
330: }
331: }
332: }
333:
334: public static class ExternalGraphicLayoutManagerMaker extends Maker {
335: public void make(FONode node, List lms) {
336: ExternalGraphic eg = (ExternalGraphic) node;
337: if (!eg.getSrc().equals("")) {
338: lms.add(new ExternalGraphicLayoutManager(eg));
339: }
340: }
341: }
342:
343: public static class BlockContainerLayoutManagerMaker extends Maker {
344: public void make(FONode node, List lms) {
345: lms.add(new BlockContainerLayoutManager(
346: (BlockContainer) node));
347: }
348: }
349:
350: public static class ListItemLayoutManagerMaker extends Maker {
351: public void make(FONode node, List lms) {
352: lms.add(new ListItemLayoutManager((ListItem) node));
353: }
354: }
355:
356: public static class ListBlockLayoutManagerMaker extends Maker {
357: public void make(FONode node, List lms) {
358: lms.add(new ListBlockLayoutManager((ListBlock) node));
359: }
360: }
361:
362: public static class InstreamForeignObjectLayoutManagerMaker extends
363: Maker {
364: public void make(FONode node, List lms) {
365: lms.add(new InstreamForeignObjectLM(
366: (InstreamForeignObject) node));
367: }
368: }
369:
370: public static class PageNumberLayoutManagerMaker extends Maker {
371: public void make(FONode node, List lms) {
372: lms.add(new PageNumberLayoutManager((PageNumber) node));
373: }
374: }
375:
376: public static class PageNumberCitationLayoutManagerMaker extends
377: Maker {
378: public void make(FONode node, List lms) {
379: lms.add(new PageNumberCitationLayoutManager(
380: (PageNumberCitation) node));
381: }
382: }
383:
384: public static class PageNumberCitationLastLayoutManagerMaker extends
385: Maker {
386: public void make(FONode node, List lms) {
387: lms.add(new PageNumberCitationLastLayoutManager(
388: (PageNumberCitationLast) node));
389: }
390: }
391:
392: public static class TableLayoutManagerMaker extends Maker {
393: public void make(FONode node, List lms) {
394: Table table = (Table) node;
395: TableLayoutManager tlm = new TableLayoutManager(table);
396: lms.add(tlm);
397: }
398: }
399:
400: public class RetrieveMarkerLayoutManagerMaker extends Maker {
401: public void make(FONode node, List lms) {
402: Iterator baseIter;
403: baseIter = node.getChildNodes();
404: if (baseIter == null) {
405: return;
406: }
407: while (baseIter.hasNext()) {
408: FONode child = (FONode) baseIter.next();
409: makeLayoutManagers(child, lms);
410: }
411: }
412: }
413:
414: public class WrapperLayoutManagerMaker extends Maker {
415: public void make(FONode node, List lms) {
416: //We insert the wrapper LM before it's children so an ID
417: //on the node can be registered on a page.
418: lms.add(new WrapperLayoutManager((Wrapper) node));
419: Iterator baseIter;
420: baseIter = node.getChildNodes();
421: if (baseIter == null) {
422: return;
423: }
424: while (baseIter.hasNext()) {
425: FONode child = (FONode) baseIter.next();
426: makeLayoutManagers(child, lms);
427: }
428: }
429: }
430:
431: }
|