KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > render > rtf > rtflib > tools > BuilderContext


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: BuilderContext.java 473975 2006-11-12 15:28:15Z jeremias $ */
19
20 package org.apache.fop.render.rtf.rtflib.tools;
21
22 import java.util.Stack JavaDoc;
23
24 import org.apache.fop.render.rtf.rtflib.exceptions.RtfException;
25 import org.apache.fop.render.rtf.rtflib.rtfdoc.IRtfOptions;
26 import org.apache.fop.render.rtf.rtflib.rtfdoc.RtfContainer;
27
28
29 /** A BuilderContext holds context information when building an RTF document
30  *
31  * @author Bertrand Delacretaz <bdelacretaz@codeconsult.ch>
32  * @author putzi
33  * @author Peter Herweg <pherweg@web.de>
34  *
35  * This class was originally developed by Bertrand Delacretaz bdelacretaz@codeconsult.ch
36  * for the JFOR project and is now integrated into FOP.
37  */

38
39 public class BuilderContext {
40     /** stack of RtfContainers */
41     private final Stack JavaDoc containers = new Stack JavaDoc();
42
43     /** stack of TableContexts */
44     private final Stack JavaDoc tableContexts = new Stack JavaDoc();
45
46     /** stack of IBuilders */
47     private final Stack JavaDoc builders = new Stack JavaDoc();
48
49     /** Rtf options */
50     private IRtfOptions options;
51
52     public BuilderContext(IRtfOptions rtfOptions) {
53         options = rtfOptions;
54     }
55
56     /** find first object of given class from top of stack s
57      * @return null if not found
58      */

59     private Object JavaDoc getObjectFromStack(Stack JavaDoc s, Class JavaDoc desiredClass) {
60         Object JavaDoc result = null;
61         final Stack JavaDoc copy = (Stack JavaDoc)s.clone();
62         while (!copy.isEmpty()) {
63             final Object JavaDoc o = copy.pop();
64             if (desiredClass.isAssignableFrom(o.getClass())) {
65                 result = o;
66                 break;
67             }
68         }
69         return result;
70     }
71
72     /* find the "nearest" IBuilder of given class /
73     public Object getBuilder(Class builderClass,boolean required)
74     throws Exception
75     {
76         final IBuilder result = (IBuilder)getObjectFromStack(builders,builderClass);
77         if(result == null && required) {
78             throw new Exception(
79                 "IBuilder of class '" + builderClass.getName() + "' not found on builders stack"
80                );
81         }
82         return result;
83     }*/

84
85     /** find the "nearest" container that implements the given interface on our stack
86      * @param required if true, ConverterException is thrown if no container found
87      * @param forWhichBuilder used in error message if container not found
88      */

89     public RtfContainer getContainer(Class JavaDoc containerClass, boolean required,
90                               Object JavaDoc /*IBuilder*/ forWhichBuilder) throws RtfException {
91         // TODO what to do if the desired container is not at the top of the stack?
92
// close top-of-stack container?
93
final RtfContainer result = (RtfContainer)getObjectFromStack(containers,
94                 containerClass);
95
96         if (result == null && required) {
97             throw new RtfException(
98                 "No RtfContainer of class '" + containerClass.getName()
99                 + "' available for '" + forWhichBuilder.getClass().getName() + "' builder"
100                );
101         }
102
103         return result;
104     }
105
106     /** push an RtfContainer on our stack */
107     public void pushContainer(RtfContainer c) {
108         containers.push(c);
109     }
110
111     /**
112      * In some cases an RtfContainer must be replaced by another one on the
113      * stack. This happens when handling nested fo:blocks for example: after
114      * handling a nested block the enclosing block must switch to a new
115      * paragraph container to handle what follows the nested block.
116      * TODO: what happens to elements that are "more on top" than oldC on the
117      * stack? shouldn't they be closed or something?
118      */

119     public void replaceContainer(RtfContainer oldC, RtfContainer newC)
120     throws Exception JavaDoc {
121         // treating the Stack as a Vector allows such manipulations (yes, I hear you screaming ;-)
122
final int index = containers.indexOf(oldC);
123         if (index < 0) {
124             throw new Exception JavaDoc("container to replace not found:" + oldC);
125         }
126         containers.setElementAt(newC, index);
127     }
128
129     /** pop the topmost RtfContainer from our stack */
130     public void popContainer() {
131         containers.pop();
132     }
133
134     /* push an IBuilder to our stack /
135     public void pushBuilder(IBuilder b)
136     {
137         builders.push(b);
138     }*/

139
140     /** pop the topmost IBuilder from our stack and return previous builder on stack
141      * @return null if builders stack is empty
142
143     public IBuilder popBuilderAndGetPreviousOne()
144     {
145         IBuilder result = null;
146         builders.pop();
147         if(!builders.isEmpty()) {
148             result = (IBuilder)builders.peek();
149         }
150         return result;
151     }
152     */

153     /** return the current TableContext */
154     public TableContext getTableContext() {
155         return (TableContext)tableContexts.peek();
156     }
157
158     /** push a TableContext to our stack */
159     public void pushTableContext(TableContext tc) {
160         tableContexts.push(tc);
161     }
162
163     /** pop a TableContext from our stack */
164     public void popTableContext() {
165         tableContexts.pop();
166     }
167
168 }
169
Popular Tags