KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > layoutmgr > LayoutManagerMapping


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
18 /* $Id: LayoutManagerMapping.java 426576 2006-07-28 15:44:37Z jeremias $ */
19
20 package org.apache.fop.layoutmgr;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.Map JavaDoc;
24 import java.util.HashMap JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.Iterator JavaDoc;
27
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30
31 import org.apache.fop.fo.FONode;
32 import org.apache.fop.fo.FOText;
33 import org.apache.fop.fo.FObjMixed;
34 import org.apache.fop.fo.flow.BasicLink;
35 import org.apache.fop.fo.flow.BidiOverride;
36 import org.apache.fop.fo.flow.Block;
37 import org.apache.fop.fo.flow.BlockContainer;
38 import org.apache.fop.fo.flow.Character;
39 import org.apache.fop.fo.flow.ExternalGraphic;
40 import org.apache.fop.fo.flow.Footnote;
41 import org.apache.fop.fo.flow.Inline;
42 import org.apache.fop.fo.flow.InlineLevel;
43 import org.apache.fop.fo.flow.InlineContainer;
44 import org.apache.fop.fo.flow.InstreamForeignObject;
45 import org.apache.fop.fo.flow.Leader;
46 import org.apache.fop.fo.flow.ListBlock;
47 import org.apache.fop.fo.flow.ListItem;
48 import org.apache.fop.fo.flow.PageNumber;
49 import org.apache.fop.fo.flow.PageNumberCitation;
50 import org.apache.fop.fo.flow.PageNumberCitationLast;
51 import org.apache.fop.fo.flow.RetrieveMarker;
52 import org.apache.fop.fo.flow.Table;
53 import org.apache.fop.fo.flow.TableBody;
54 import org.apache.fop.fo.flow.TableCell;
55 import org.apache.fop.fo.flow.TableColumn;
56 import org.apache.fop.fo.flow.TableFooter;
57 import org.apache.fop.fo.flow.TableHeader;
58 import org.apache.fop.fo.flow.TableRow;
59 import org.apache.fop.fo.flow.Wrapper;
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.area.AreaTreeHandler;
66
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.ICLayoutManager;
74 import org.apache.fop.layoutmgr.inline.InlineLayoutManager;
75 import org.apache.fop.layoutmgr.inline.InlineLevelLayoutManager;
76 import org.apache.fop.layoutmgr.inline.InstreamForeignObjectLM;
77 import org.apache.fop.layoutmgr.inline.LeaderLayoutManager;
78 import org.apache.fop.layoutmgr.inline.PageNumberCitationLayoutManager;
79 import org.apache.fop.layoutmgr.inline.PageNumberCitationLastLayoutManager;
80 import org.apache.fop.layoutmgr.inline.PageNumberLayoutManager;
81 import org.apache.fop.layoutmgr.inline.TextLayoutManager;
82 import org.apache.fop.layoutmgr.inline.WrapperLayoutManager;
83 import org.apache.fop.layoutmgr.list.ListBlockLayoutManager;
84 import org.apache.fop.layoutmgr.list.ListItemLayoutManager;
85 import org.apache.fop.layoutmgr.table.TableLayoutManager;
86 import org.apache.fop.util.CharUtilities;
87
88 /**
89  * The default LayoutManager maker class
90  */

91 public class LayoutManagerMapping implements LayoutManagerMaker {
92
93     /** logging instance */
94     protected static Log log = LogFactory.getLog(LayoutManagerMapping.class);
95
96     /** The map of LayoutManagerMakers */
97     private Map JavaDoc makers = new HashMap JavaDoc();
98
99     public LayoutManagerMapping() {
100         initialize();
101     }
102
103     /**
104      * Initializes the set of maker objects associated with this LayoutManagerMapping
105      */

106     protected void initialize() {
107         makers.put(FOText.class, new FOTextLayoutManagerMaker());
108         makers.put(FObjMixed.class, new Maker());
109         makers.put(BidiOverride.class, new BidiOverrideLayoutManagerMaker());
110         makers.put(Inline.class, new InlineLayoutManagerMaker());
111         makers.put(Footnote.class, new FootnodeLayoutManagerMaker());
112         makers.put(InlineContainer.class,
113                    new InlineContainerLayoutManagerMaker());
114         makers.put(BasicLink.class, new BasicLinkLayoutManagerMaker());
115         makers.put(Block.class, new BlockLayoutManagerMaker());
116         makers.put(Leader.class, new LeaderLayoutManagerMaker());
117         makers.put(RetrieveMarker.class, new RetrieveMarkerLayoutManagerMaker());
118         makers.put(Character JavaDoc.class, new CharacterLayoutManagerMaker());
119         makers.put(ExternalGraphic.class,
120                    new ExternalGraphicLayoutManagerMaker());
121         makers.put(BlockContainer.class,
122                    new BlockContainerLayoutManagerMaker());
123         makers.put(ListItem.class, new ListItemLayoutManagerMaker());
124         makers.put(ListBlock.class, new ListBlockLayoutManagerMaker());
125         makers.put(InstreamForeignObject.class,
126                    new InstreamForeignObjectLayoutManagerMaker());
127         makers.put(PageNumber.class, new PageNumberLayoutManagerMaker());
128         makers.put(PageNumberCitation.class,
129                    new PageNumberCitationLayoutManagerMaker());
130         makers.put(PageNumberCitationLast.class,
131                 new PageNumberCitationLastLayoutManagerMaker());
132         makers.put(Table.class, new TableLayoutManagerMaker());
133         makers.put(TableBody.class, new Maker());
134         makers.put(TableColumn.class, new Maker());
135         makers.put(TableRow.class, new Maker());
136         makers.put(TableCell.class, new Maker());
137         makers.put(TableFooter.class, new Maker());
138         makers.put(TableHeader.class, new Maker());
139         makers.put(Wrapper.class, new WrapperLayoutManagerMaker());
140         makers.put(Title.class, new InlineLayoutManagerMaker());
141     }
142
143     /**
144      * @see org.apache.fop.layoutmgr.LayoutManagerMaker#makeLayoutManagers(FONode, List)
145      */

146     public void makeLayoutManagers(FONode node, List JavaDoc lms) {
147         Maker maker = (Maker) makers.get(node.getClass());
148         if (maker == null) {
149             log.error("No LayoutManager maker for class " + node.getClass());
150         } else {
151             maker.make(node, lms);
152         }
153     }
154
155     /**
156      * @see org.apache.fop.layoutmgr.LayoutManagerMaker#makeLayoutManager(FONode)
157      */

158     public LayoutManager makeLayoutManager(FONode node) {
159         List JavaDoc lms = new ArrayList JavaDoc();
160         makeLayoutManagers(node, lms);
161         if (lms.size() == 0) {
162             throw new IllegalStateException JavaDoc("LayoutManager for class "
163                                    + node.getClass()
164                                    + " is missing.");
165         } else if (lms.size() > 1) {
166             throw new IllegalStateException JavaDoc("Duplicate LayoutManagers for class "
167                                    + node.getClass()
168                                    + " found, only one may be declared.");
169         }
170         return (LayoutManager) lms.get(0);
171     }
172
173     public PageSequenceLayoutManager makePageSequenceLayoutManager(
174         AreaTreeHandler ath, PageSequence ps) {
175         return new PageSequenceLayoutManager(ath, ps);
176     }
177
178     /*
179      * @see org.apache.fop.layoutmgr.LayoutManagerMaker#makeFlowLayoutManager(PageSequenceLayoutManager, Flow)
180      */

181     public FlowLayoutManager makeFlowLayoutManager(
182             PageSequenceLayoutManager pslm, Flow flow) {
183         return new FlowLayoutManager(pslm, flow);
184     }
185
186     /*
187      * @see org.apache.fop.layoutmgr.LayoutManagerMaker#makeContentLayoutManager(PageSequenceLayoutManager, Title)
188      */

189     public ContentLayoutManager makeContentLayoutManager(PageSequenceLayoutManager pslm,
190                                                          Title title) {
191         return new ContentLayoutManager(pslm, title);
192     }
193     
194     /*
195      * @see org.apache.fop.layoutmgr.LayoutManagerMaker#makeStaticContentLayoutManager(PageSequenceLayoutManager, StaticContent, Region)
196      */

197     public StaticContentLayoutManager makeStaticContentLayoutManager(
198             PageSequenceLayoutManager pslm, StaticContent sc, SideRegion reg) {
199         return new StaticContentLayoutManager(pslm, sc, reg);
200     }
201     
202     /*
203      * @see org.apache.fop.layoutmgr.LayoutManagerMaker#makeStaticContentLayoutManager(PageSequenceLayoutManager, StaticContent, Block)
204      */

205     public StaticContentLayoutManager makeStaticContentLayoutManager(
206         PageSequenceLayoutManager pslm, StaticContent sc, org.apache.fop.area.Block block) {
207         return new StaticContentLayoutManager(pslm, sc, block);
208     }
209
210     public static class Maker {
211         public void make(FONode node, List JavaDoc lms) {
212             // no layout manager
213
return;
214         }
215     }
216
217     public static class FOTextLayoutManagerMaker extends Maker {
218         public void make(FONode node, List JavaDoc lms) {
219             FOText foText = (FOText) node;
220             if (foText.endIndex - foText.startIndex > 0) {
221                 lms.add(new TextLayoutManager(foText));
222             }
223         }
224     }
225
226     /*
227     public static class FObjMixedLayoutManagerMaker extends Maker {
228         public void make(FONode node, List lms) {
229             if (node.getChildNodes() != null) {
230                 InlineStackingLayoutManager lm;
231                 lm = new InlineStackingLayoutManager((FObjMixed) node);
232                 lms.add(lm);
233             }
234         }
235     }
236     */

237
238     public static class BidiOverrideLayoutManagerMaker extends Maker {
239         // public static class BidiOverrideLayoutManagerMaker extends FObjMixedLayoutManagerMaker {
240
public void make(BidiOverride node, List JavaDoc lms) {
241             if (false) {
242                 // this is broken; it does nothing
243
// it should make something like an InlineStackingLM
244
super.make(node, lms);
245             } else {
246                 ArrayList JavaDoc childList = new ArrayList JavaDoc();
247                 // this is broken; it does nothing
248
// it should make something like an InlineStackingLM
249
super.make(node, childList);
250                 for (int count = childList.size() - 1; count >= 0; count--) {
251                     LayoutManager lm = (LayoutManager) childList.get(count);
252                     if (lm instanceof InlineLevelLayoutManager) {
253                         LayoutManager blm = new BidiLayoutManager
254                             (node, (InlineLayoutManager) lm);
255                         lms.add(blm);
256                     } else {
257                         lms.add(lm);
258                     }
259                 }
260             }
261         }
262     }
263
264     public static class InlineLayoutManagerMaker extends Maker {
265          public void make(FONode node, List JavaDoc lms) {
266              if (node.getChildNodes() != null) {
267                  lms.add(new InlineLayoutManager((InlineLevel) node));
268              }
269          }
270     }
271
272     public static class FootnodeLayoutManagerMaker extends Maker {
273         public void make(FONode node, List JavaDoc lms) {
274             lms.add(new FootnoteLayoutManager((Footnote) node));
275         }
276     }
277
278     public static class InlineContainerLayoutManagerMaker extends Maker {
279         public void make(FONode node, List JavaDoc lms) {
280             ArrayList JavaDoc childList = new ArrayList JavaDoc();
281             super.make(node, childList);
282             lms.add(new ICLayoutManager((InlineContainer) node, childList));
283         }
284     }
285
286     public static class BasicLinkLayoutManagerMaker extends Maker {
287         public void make(FONode node, List JavaDoc lms) {
288             lms.add(new BasicLinkLayoutManager((BasicLink) node));
289         }
290     }
291
292     public static class BlockLayoutManagerMaker extends Maker {
293          public void make(FONode node, List JavaDoc lms) {
294              lms.add(new BlockLayoutManager((Block) node));
295          }
296     }
297
298     public static class LeaderLayoutManagerMaker extends Maker {
299         public void make(FONode node, List JavaDoc lms) {
300             lms.add(new LeaderLayoutManager((Leader) node));
301         }
302     }
303
304     public static class CharacterLayoutManagerMaker extends Maker {
305         public void make(FONode node, List JavaDoc lms) {
306             Character JavaDoc foCharacter = (Character JavaDoc) node;
307             if (foCharacter.getCharacter() != CharUtilities.CODE_EOT) {
308                 lms.add(new CharacterLayoutManager(foCharacter));
309             }
310         }
311     }
312
313     public static class ExternalGraphicLayoutManagerMaker extends Maker {
314         public void make(FONode node, List JavaDoc lms) {
315             ExternalGraphic eg = (ExternalGraphic) node;
316             if (!eg.getSrc().equals("")) {
317                 lms.add(new ExternalGraphicLayoutManager(eg));
318             }
319         }
320     }
321
322     public static class BlockContainerLayoutManagerMaker extends Maker {
323         public void make(FONode node, List JavaDoc lms) {
324             lms.add(new BlockContainerLayoutManager((BlockContainer) node));
325          }
326     }
327
328     public static class ListItemLayoutManagerMaker extends Maker {
329          public void make(FONode node, List JavaDoc lms) {
330              lms.add(new ListItemLayoutManager((ListItem) node));
331          }
332     }
333
334     public static class ListBlockLayoutManagerMaker extends Maker {
335         public void make(FONode node, List JavaDoc lms) {
336             lms.add(new ListBlockLayoutManager((ListBlock) node));
337         }
338     }
339
340     public static class InstreamForeignObjectLayoutManagerMaker extends Maker {
341         public void make(FONode node, List JavaDoc lms) {
342             lms.add(new InstreamForeignObjectLM((InstreamForeignObject) node));
343         }
344     }
345
346     public static class PageNumberLayoutManagerMaker extends Maker {
347          public void make(FONode node, List JavaDoc lms) {
348              lms.add(new PageNumberLayoutManager((PageNumber) node));
349          }
350     }
351
352     public static class PageNumberCitationLayoutManagerMaker extends Maker {
353          public void make(FONode node, List JavaDoc lms) {
354             lms.add(new PageNumberCitationLayoutManager((PageNumberCitation) node));
355          }
356     }
357
358     public static class PageNumberCitationLastLayoutManagerMaker extends Maker {
359         public void make(FONode node, List JavaDoc lms) {
360            lms.add(new PageNumberCitationLastLayoutManager((PageNumberCitationLast) node));
361         }
362     }
363     
364     public static class TableLayoutManagerMaker extends Maker {
365         public void make(FONode node, List JavaDoc lms) {
366             Table table = (Table) node;
367             TableLayoutManager tlm = new TableLayoutManager(table);
368             lms.add(tlm);
369         }
370     }
371      
372     public class RetrieveMarkerLayoutManagerMaker extends Maker {
373         public void make(FONode node, List JavaDoc lms) {
374             Iterator JavaDoc baseIter;
375             baseIter = node.getChildNodes();
376             if (baseIter == null) {
377                 return;
378             }
379             while (baseIter.hasNext()) {
380                 FONode child = (FONode) baseIter.next();
381                 makeLayoutManagers(child, lms);
382             }
383         }
384     }
385
386     public class WrapperLayoutManagerMaker extends Maker {
387         public void make(FONode node, List JavaDoc lms) {
388             //We insert the wrapper LM before it's children so an ID
389
//on the node can be registered on a page.
390
lms.add(new WrapperLayoutManager((Wrapper)node));
391             Iterator JavaDoc baseIter;
392             baseIter = node.getChildNodes();
393             if (baseIter == null) {
394                 return;
395             }
396             while (baseIter.hasNext()) {
397                 FONode child = (FONode) baseIter.next();
398                 makeLayoutManagers(child, lms);
399             }
400         }
401     }
402
403 }
404
Popular Tags