KickJava   Java API By Example, From Geeks To Geeks.

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


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: RtfParagraph.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.io.IOException JavaDoc;
31 import java.util.List JavaDoc;
32
33 /** Model of an RTF paragraph, which can contain RTF text elements.
34  * @author Bertrand Delacretaz bdelacretaz@codeconsult.ch
35  * @author Andreas Putz a.putz@skynamics.com
36  * @author Boris Poudérous, boris.pouderous@free.fr
37  */

38
39 public class RtfParagraph extends RtfBookmarkContainerImpl
40 implements IRtfTextContainer, IRtfPageBreakContainer, IRtfHyperLinkContainer,
41         IRtfExternalGraphicContainer, IRtfPageNumberContainer,
42         IRtfPageNumberCitationContainer {
43     private RtfText text;
44     private RtfHyperLink hyperlink;
45     private RtfExternalGraphic externalGraphic;
46     private RtfPageNumber pageNumber;
47     private RtfPageNumberCitation pageNumberCitation;
48     // Above line added by Boris POUDEROUS on 2002/07/09
49
private boolean keepn = false;
50     private boolean resetProperties = false;
51
52     /* needed for importing Rtf into FrameMaker
53        FrameMaker is not as forgiving as word in rtf
54            thus /pard/par must be written in a page break directly
55            after a table. /pard is probably needed in other places
56            also, this is just a hack to make FrameMaker import Jfor rtf
57            correctly */

58     private boolean writeForBreak = false;
59
60     /** Set of attributes that must be copied at the start of a paragraph */
61     private static final String JavaDoc[] PARA_ATTRIBUTES = {"intbl"};
62
63     /** Create an RTF paragraph as a child of given container with default attributes */
64     RtfParagraph(IRtfParagraphContainer parent, Writer JavaDoc w) throws IOException JavaDoc {
65         super((RtfContainer)parent, w);
66     }
67
68     /** Create an RTF paragraph as a child of given container with given attributes */
69     RtfParagraph(IRtfParagraphContainer parent, Writer JavaDoc w, RtfAttributes attr) throws IOException JavaDoc {
70         super((RtfContainer)parent, w, attr);
71     }
72
73     /**
74      * Accessor for the paragraph text
75      * @return the paragraph text
76      */

77     public String JavaDoc getText() {
78         return (text.getText());
79     }
80
81     /** Set the keepn attribute for this paragraph */
82     public void setKeepn() {
83         this.keepn = true;
84     }
85
86     /** Force reset properties */
87     public void setResetProperties() {
88         this.resetProperties = true;
89     }
90
91     /**
92      * IRtfTextContainer requirement: return a copy of our attributes
93      * @return a copy of this paragraphs attributes
94      */

95     public RtfAttributes getTextContainerAttributes() {
96         if (attrib == null) {
97             return null;
98         }
99         return (RtfAttributes)this.attrib.clone();
100     }
101
102     /**
103      * Overridden to write our attributes before our content
104      * @throws IOException for I/O problems
105      */

106     protected void writeRtfPrefix() throws IOException JavaDoc {
107
108         //Reset paragraph properties if needed
109
if (resetProperties) {
110                writeControlWord("pard");
111            }
112
113         /*
114          * Original comment said "do not write text attributes here, they are
115          * handled by RtfText." However, the text attributes appear to be
116          * relevant to paragraphs as well.
117          */

118         writeAttributes(attrib, RtfText.ATTR_NAMES);
119         writeAttributes(attrib, PARA_ATTRIBUTES);
120         // Added by Normand Masse
121
// Write alignment attributes after \intbl for cells
122
if (attrib.isSet("intbl") && mustWriteAttributes()) {
123             writeAttributes(attrib, RtfText.ALIGNMENT);
124         }
125
126         //Set keepn if needed (Keep paragraph with the next paragraph)
127
if (keepn) {
128             writeControlWord("keepn");
129         }
130
131         // start a group for this paragraph and write our own attributes if needed
132
if (mustWriteGroupMark()) {
133             writeGroupMark(true);
134         }
135
136
137         if (mustWriteAttributes()) {
138             // writeAttributes(m_attrib, new String [] {"cs"});
139
// Added by Normand Masse
140
// If \intbl then attributes have already been written (see higher in method)
141
if (!attrib.isSet("intbl")) {
142                 writeAttributes(attrib, RtfText.ALIGNMENT);
143             }
144             //this line added by Chris Scott, Westinghouse
145
writeAttributes(attrib, RtfText.BORDER);
146             writeAttributes(attrib, RtfText.INDENT);
147             writeAttributes(attrib, RtfText.TABS);
148             if (writeForBreak) {
149                 writeControlWord("pard\\par");
150             }
151         }
152
153     }
154
155     /**
156      * Overridden to close paragraph
157      * @throws IOException for I/O problems
158      */

159     protected void writeRtfSuffix() throws IOException JavaDoc {
160         // sometimes the end of paragraph mark must be suppressed in table cells
161
boolean writeMark = true;
162         if (parent instanceof RtfTableCell) {
163             writeMark = ((RtfTableCell)parent).paragraphNeedsPar(this);
164         }
165         if (writeMark) {
166             writeControlWord("par");
167         }
168
169         if (mustWriteGroupMark()) {
170             writeGroupMark(false);
171         }
172
173
174     }
175
176     /**
177      * Close current text run if any and start a new one with default attributes
178      * @param str if not null, added to the RtfText created
179      * @return the new RtfText object
180      * @throws IOException for I/O problems
181      */

182     public RtfText newText(String JavaDoc str) throws IOException JavaDoc {
183         return newText(str, null);
184     }
185
186     /**
187      * Close current text run if any and start a new one
188      * @param str if not null, added to the RtfText created
189      * @param attr attributes of the text
190      * @return the new RtfText object
191      * @throws IOException for I/O problems
192      */

193     public RtfText newText(String JavaDoc str, RtfAttributes attr) throws IOException JavaDoc {
194         closeAll();
195         text = new RtfText(this, writer, str, attr);
196         return text;
197     }
198
199     /**
200      * add a page break
201      * @throws IOException for I/O problems
202      */

203     public void newPageBreak() throws IOException JavaDoc {
204         writeForBreak = true;
205         new RtfPageBreak(this, writer);
206     }
207
208     /**
209      * add a line break
210      * @throws IOException for I/O problems
211      */

212     public void newLineBreak() throws IOException JavaDoc {
213         new RtfLineBreak(this, writer);
214     }
215
216     /**
217      * Add a page number
218      * @return new RtfPageNumber object
219      * @throws IOException for I/O problems
220      */

221     public RtfPageNumber newPageNumber()throws IOException JavaDoc {
222         pageNumber = new RtfPageNumber(this, writer);
223         return pageNumber;
224     }
225
226     /**
227      * Added by Boris POUDEROUS on 2002/07/09
228      * @param id string containing the citation text
229      * @return the new RtfPageNumberCitation object
230      * @throws IOException for I/O problems
231      */

232     public RtfPageNumberCitation newPageNumberCitation(String JavaDoc id) throws IOException JavaDoc {
233        pageNumberCitation = new RtfPageNumberCitation(this, writer, id);
234        return pageNumberCitation;
235     }
236
237     /**
238      * Creates a new hyperlink.
239      * @param str string containing the hyperlink text
240      * @param attr attributes of new hyperlink
241      * @return the new RtfHyperLink object
242      * @throws IOException for I/O problems
243      */

244     public RtfHyperLink newHyperLink(String JavaDoc str, RtfAttributes attr) throws IOException JavaDoc {
245         hyperlink = new RtfHyperLink(this, writer, str, attr);
246         return hyperlink;
247     }
248
249     /**
250      * Start a new external graphic after closing all other elements
251      * @return the new RtfExternalGraphic
252      * @throws IOException for I/O problems
253      */

254     public RtfExternalGraphic newImage() throws IOException JavaDoc {
255         closeAll();
256         externalGraphic = new RtfExternalGraphic(this, writer);
257         return externalGraphic;
258     }
259
260     private void closeCurrentText() throws IOException JavaDoc {
261         if (text != null) {
262             text.close();
263         }
264     }
265
266     private void closeCurrentHyperLink() throws IOException JavaDoc {
267         if (hyperlink != null) {
268             hyperlink.close();
269         }
270     }
271
272     private void closeAll() throws IOException JavaDoc {
273         closeCurrentText();
274         closeCurrentHyperLink();
275     }
276
277     /**
278      * Depending on RtfOptions, do not emit any RTF for empty paragraphs
279      * @return true if RTF should be written
280      */

281     protected boolean okToWriteRtf() {
282         boolean result = super.okToWriteRtf();
283
284         if (parent.getOptions().ignoreEmptyParagraphs() && getChildCount() == 0) {
285             // TODO should test that this is the last RtfParagraph in the cell instead
286
// of simply testing for last child??
287
result = false;
288         }
289
290         return result;
291     }
292
293     /** true if we must write our own (non-text) attributes in the RTF */
294     private boolean mustWriteAttributes() {
295         boolean writeAttributes = false;
296         final int children = getChildCount();
297         if (children > 0) {
298             final List JavaDoc childList = getChildren();
299             for (int i = 0; i < children; i++) {
300                 final RtfElement el = (RtfElement) childList.get(i);
301                 if (!el.isEmpty()) {
302                     if (el.getClass() == RtfText.class) {
303                         boolean tmp = ((RtfText) el).isNbsp();
304                         if (!tmp) {
305                             writeAttributes = true;
306                             break;
307                         }
308                     } else {
309                         writeAttributes = true;
310                         break;
311                     }
312                 }
313             }
314         }
315         return writeAttributes;
316     }
317
318     /** true if we must write a group mark around this paragraph
319      * TODO is this correct, study interaction with mustWriteAttributes()
320      * <-- On implementation i have noticed if the groupmark set, the
321      * format attributes are only for this content, i think this
322      * implementation is ok
323      */

324     private boolean mustWriteGroupMark() {
325         return getChildCount() > 0;
326     }
327
328     /**
329      * accessor for text attributes
330      * @return attributes of the text
331      */

332     public RtfAttributes getTextAttributes() {
333         if (text == null) {
334             return null;
335         }
336         return text.getTextAttributes();
337     }
338 }
339
Popular Tags