KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > render > rtf > rtflib > rtfdoc > RtfContainer


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: RtfContainer.java 426576 2006-07-28 15:44:37Z jeremias $ */
19
20 package org.apache.fop.render.rtf.rtflib.rtfdoc;
21
22 /*
23  * This file is part of the RTF library of the FOP project, which was originally
24  * created by Bertrand Delacretaz <bdelacretaz@codeconsult.ch> and by other
25  * contributors to the jfor project (www.jfor.org), who agreed to donate jfor to
26  * the FOP project.
27  */

28
29 import java.io.Writer JavaDoc;
30 import java.util.LinkedList JavaDoc;
31 import java.util.List JavaDoc;
32 import java.util.Iterator JavaDoc;
33 import java.io.IOException JavaDoc;
34 import org.apache.fop.render.rtf.rtflib.exceptions.RtfStructureException;
35
36 /** An RtfElement that can contain other elements.
37  * @author Bertrand Delacretaz bdelacretaz@codeconsult.ch
38  */

39
40 public class RtfContainer extends RtfElement {
41     private LinkedList JavaDoc children; // 'final' removed by Boris Poudérous on 07/22/2002
42
private RtfOptions options = new RtfOptions();
43     private RtfElement lastChild;
44
45     /** Create an RTF container as a child of given container */
46     RtfContainer(RtfContainer parent, Writer JavaDoc w) throws IOException JavaDoc {
47         this(parent, w, null);
48     }
49
50     /** Create an RTF container as a child of given container with given attributes */
51     RtfContainer(RtfContainer parent, Writer JavaDoc w, RtfAttributes attr) throws IOException JavaDoc {
52         super(parent, w, attr);
53         children = new LinkedList JavaDoc();
54     }
55
56     /**
57      * set options
58      * @param opt options to set
59      */

60     public void setOptions(RtfOptions opt) {
61         options = opt;
62     }
63
64     /**
65      * add a child element to this
66      * @param e child element to add
67      * @throws RtfStructureException for trying to add an invalid child (??)
68      */

69     protected void addChild(RtfElement e)
70     throws RtfStructureException {
71         if (isClosed()) {
72             // No childs should be added to a container that has been closed
73
final StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
74             sb.append("addChild: container already closed (parent=");
75             sb.append(this.getClass().getName());
76             sb.append(" child=");
77             sb.append(e.getClass().getName());
78             sb.append(")");
79             final String JavaDoc msg = sb.toString();
80
81             // warn of this problem
82
final RtfFile rf = getRtfFile();
83 // if(rf.getLog() != null) {
84
// rf.getLog().logWarning(msg);
85
// }
86

87             // TODO this should be activated to help detect XSL-FO constructs
88
// that we do not handle properly.
89
/*
90             throw new RtfStructureException(msg);
91              */

92         }
93
94         children.add(e);
95         lastChild = e;
96     }
97
98     /**
99      * @return a copy of our children's list
100      */

101     public List JavaDoc getChildren() {
102         return (List JavaDoc)children.clone();
103     }
104
105     /**
106      * @return the number of children
107      */

108     public int getChildCount() {
109         return children.size();
110     }
111
112     /**
113      * Add by Boris Poudérous on 07/22/2002
114      * Set the children list
115      * @param list list of child objects
116      * @return true if process succeeded
117      */

118     public boolean setChildren (List JavaDoc list) {
119       if (list instanceof LinkedList JavaDoc) {
120           this.children = (LinkedList JavaDoc) list;
121           return true;
122         }
123
124       return false;
125     }
126
127     /**
128      * write RTF code of all our children
129      * @throws IOException for I/O problems
130      */

131     protected void writeRtfContent()
132     throws IOException JavaDoc {
133         for (Iterator JavaDoc it = children.iterator(); it.hasNext();) {
134             final RtfElement e = (RtfElement)it.next();
135             e.writeRtf();
136         }
137     }
138
139     /** return our options */
140     RtfOptions getOptions() {
141         return options;
142     }
143
144     /** true if this (recursively) contains at least one RtfText object */
145     boolean containsText() {
146         boolean result = false;
147         for (Iterator JavaDoc it = children.iterator(); it.hasNext();) {
148             final RtfElement e = (RtfElement)it.next();
149             if (e instanceof RtfText) {
150                 result = !e.isEmpty();
151             } else if (e instanceof RtfContainer) {
152                 if (((RtfContainer)e).containsText()) {
153                     result = true;
154                 }
155             }
156             if (result) {
157                 break;
158             }
159         }
160         return result;
161     }
162
163     /** debugging to given Writer */
164     void dump(Writer JavaDoc w, int indent)
165     throws IOException JavaDoc {
166         super.dump(w, indent);
167         for (Iterator JavaDoc it = children.iterator(); it.hasNext();) {
168             final RtfElement e = (RtfElement)it.next();
169             e.dump(w, indent + 1);
170         }
171     }
172
173     /**
174      * minimal debugging display
175      * @return String representation of object contents
176      */

177     public String JavaDoc toString() {
178         return super.toString() + " (" + getChildCount() + " children)";
179     }
180
181     /**
182      * @return false if empty or if our options block writing
183      */

184     protected boolean okToWriteRtf() {
185         boolean result = super.okToWriteRtf() && !isEmpty();
186         if (result && !options.renderContainer(this)) {
187             result = false;
188         }
189         return result;
190     }
191
192     /**
193      * @return true if this element would generate no "useful" RTF content,
194      * i.e. (for RtfContainer) true if it has no children where isEmpty() is false
195      */

196     public boolean isEmpty() {
197         boolean result = true;
198         for (Iterator JavaDoc it = children.iterator(); it.hasNext();) {
199             final RtfElement e = (RtfElement)it.next();
200             if (!e.isEmpty()) {
201                 result = false;
202                 break;
203             }
204         }
205         return result;
206     }
207 }
Popular Tags