KickJava   Java API By Example, From Geeks To Geeks.

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


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

27
28 package org.apache.fop.render.rtf.rtflib.rtfdoc;
29
30 import java.io.IOException JavaDoc;
31 import java.io.Writer JavaDoc;
32
33 /** Model of a text run (a piece of text with attributes) in an RTF document
34  * @author Bertrand Delacretaz bdelacretaz@codeconsult.ch
35  */

36
37 public class RtfText extends RtfElement {
38     // char code for non-breakable space
39
private static final int CHAR_NBSP = 160;
40     private static final int CHAR_TAB = 137;
41     private static final int CHAR_NEW_LINE = 141;
42     /* these next two variables are used to encode bold formating in the
43      * raw xml text. Usefull when specific words or phrases are to be bolded
44      * but their placement and length change. Thus the bold formatting becomes
45      * part of the data. The same method can be used for implementing other types
46      * of raw text formatting.
47      */

48     private static final int CHAR_BOLD_START = 130;
49     private static final int CHAR_BOLD_END = 131;
50
51     /** members */
52     private String JavaDoc text;
53     private final RtfAttributes attr;
54
55
56     /** RtfText attributes: attribute names are RTF control word names to avoid
57      * additional mapping */

58     /** constant for bold */
59     public static final String JavaDoc ATTR_BOLD = "b";
60     /** constant for italic */
61     public static final String JavaDoc ATTR_ITALIC = "i";
62     /** constant for underline */
63     public static final String JavaDoc ATTR_UNDERLINE = "ul";
64     /** constant for underline */
65     public static final String JavaDoc ATTR_STRIKETHROUGH = "strike";
66     /** constant for font size */
67     public static final String JavaDoc ATTR_FONT_SIZE = "fs";
68     /** constant for font family */
69     public static final String JavaDoc ATTR_FONT_FAMILY = "f";
70     /** constant for font color */
71     public static final String JavaDoc ATTR_FONT_COLOR = "cf";
72     /** constant for background color */
73     public static final String JavaDoc ATTR_BACKGROUND_COLOR = "chcbpat"; // Added by Boris on 06/25//02
74
/** constant for superscript */
75     public static final String JavaDoc ATTR_SUPERSCRIPT = "super";
76     /** constant for subscript */
77     public static final String JavaDoc ATTR_SUBSCRIPT = "sub";
78
79     /** RtfText attributes: paragraph shading attributes */
80     /** Constant for the shading of the paragraph */
81     public static final String JavaDoc SHADING = "shading";
82     /** Constant for the document's color tableshading of the paragraph */
83     public static final String JavaDoc SHADING_FRONT_COLOR = "cfpat";
84     /** Constant for the 100% shading of the paragraph */
85     public static final int FULL_SHADING = 10000;
86
87     /** RtfText attributes: alignment attributes */
88     /** constant for align center */
89     public static final String JavaDoc ALIGN_CENTER = "qc";
90     /** constant for align left */
91     public static final String JavaDoc ALIGN_LEFT = "ql";
92     /** constant for align right */
93     public static final String JavaDoc ALIGN_RIGHT = "qr";
94     /** constant for align justified */
95     public static final String JavaDoc ALIGN_JUSTIFIED = "qj";
96     /** constant for align distributed */
97     public static final String JavaDoc ALIGN_DISTRIBUTED = "qd";
98
99     /** RtfText attributes: border attributes */
100     //added by Chris Scott
101
/** constant for bottom single border */
102     public static final String JavaDoc BDR_BOTTOM_SINGLE = "brdrb\\brsp40\\brdrs";
103     /** constant for bottom double border */
104     public static final String JavaDoc BDR_BOTTOM_DOUBLE = "brdrb\\brsp40\\brdrdb";
105     /** constant for bottom embossed border */
106     public static final String JavaDoc BDR_BOTTOM_EMBOSS = "brdrb\\brsp40\\brdremboss";
107     /** constant for bottom dotted border */
108     public static final String JavaDoc BDR_BOTTOM_DOTTED = "brdrb\\brsp40\\brdrdot";
109     /** constant for bottom dashed border */
110     public static final String JavaDoc BDR_BOTTOM_DASH = "brdrb\\brsp40\\brdrdash";
111
112     /** RtfText attributes: fields */
113     //must be carefull of group markings and star control
114
//ie page field:
115
// "{\field {\*\fldinst {PAGE}} {\fldrslt}}"
116
/** constant for field */
117     public static final String JavaDoc RTF_FIELD = "field";
118     /** constant for field page */
119     public static final String JavaDoc RTF_FIELD_PAGE = "fldinst { PAGE }";
120     /** constant for field result */
121     public static final String JavaDoc RTF_FIELD_RESULT = "fldrslt";
122
123     /**RtfText attributes: indentation attributes */
124     //added by Chris Scott
125
/** constant for left indent body */
126     public static final String JavaDoc LEFT_INDENT_BODY = "li";
127     /** constant for left indent first */
128     public static final String JavaDoc LEFT_INDENT_FIRST = "fi-";
129     /** constant for right indent body */
130     public static final String JavaDoc RIGHT_INDENT_BODY = "ri";
131
132     /** constant for center tab */
133     public static final String JavaDoc TAB_CENTER = "tqc\\tx";
134     /** constant for right tab */
135     public static final String JavaDoc TAB_RIGHT = "tqr\\tx";
136     /** constant for tab leader dots */
137     public static final String JavaDoc TAB_LEADER_DOTS = "tldot";
138     /** constant for tab leader hyphens */
139     public static final String JavaDoc TAB_LEADER_HYPHEN = "tlhyph";
140     /** constant for tab leader underscores */
141     public static final String JavaDoc TAB_LEADER_UNDER = "tlul";
142     /** constant for tab leader thick */
143     public static final String JavaDoc TAB_LEADER_THICK = "tlth";
144     /** constant for tab leader equals */
145     public static final String JavaDoc TAB_LEADER_EQUALS = "tleq";
146
147     /** Space before/after a paragraph */
148     //these lines were added by Boris Pouderous
149
public static final String JavaDoc SPACE_BEFORE = "sb";
150     /** Space after a paragraph */
151     public static final String JavaDoc SPACE_AFTER = "sa";
152
153     /** RtfText attributes: this must contain all allignment attributes names */
154     public static final String JavaDoc[] ALIGNMENT = new String JavaDoc []
155     {
156         ALIGN_CENTER, ALIGN_LEFT, ALIGN_RIGHT, ALIGN_JUSTIFIED, ALIGN_DISTRIBUTED
157     };
158
159     /** RtfText attributes:: this must contain all border attribute names*/
160     //this line added by Chris Scott, Westinghouse
161
public static final String JavaDoc[] BORDER = new String JavaDoc []
162     {
163         BDR_BOTTOM_SINGLE, BDR_BOTTOM_DOUBLE, BDR_BOTTOM_EMBOSS, BDR_BOTTOM_DOTTED,
164         BDR_BOTTOM_DASH
165     };
166
167     /** String array of indent constants */
168     public static final String JavaDoc[] INDENT = new String JavaDoc []
169     {
170         LEFT_INDENT_BODY, LEFT_INDENT_FIRST
171     };
172
173     /** String array of tab constants */
174     public static final String JavaDoc[] TABS = new String JavaDoc []
175     {
176         TAB_CENTER, TAB_RIGHT, TAB_LEADER_DOTS, TAB_LEADER_HYPHEN, TAB_LEADER_UNDER,
177         TAB_LEADER_THICK, TAB_LEADER_EQUALS
178     };
179
180
181     /** RtfText attributes: this must contain all attribute names */
182     public static final String JavaDoc [] ATTR_NAMES = {
183         ATTR_BOLD,
184         ATTR_ITALIC,
185         ATTR_UNDERLINE,
186         ATTR_FONT_SIZE,
187         ATTR_FONT_FAMILY,
188         ATTR_FONT_COLOR,
189         ATTR_BACKGROUND_COLOR
190     };
191
192     /** Create an RtfText in given IRtfTextContainer.
193      * @param str optional initial text content
194      */

195     RtfText(IRtfTextContainer parent, Writer JavaDoc w, String JavaDoc str, RtfAttributes attr)
196            throws IOException JavaDoc {
197         super((RtfContainer)parent, w);
198         this.text = str;
199         this.attr = attr;
200     }
201
202     /**
203      * Write our text to the RTF stream
204      * @throws IOException for I/O problems
205      */

206     public void writeRtfContent() throws IOException JavaDoc {
207         writeChars: {
208
209             //these lines were added by Boris Pouderous
210
if (attr != null) {
211                 writeAttributes(attr, new String JavaDoc[] {RtfText.SPACE_BEFORE});
212                 writeAttributes(attr, new String JavaDoc[] {RtfText.SPACE_AFTER});
213             }
214
215             if (isTab()) {
216                 writeControlWord("tab");
217             } else if (isNewLine()) {
218                 break writeChars;
219             } else if (isBold(true)) {
220                 writeControlWord("b");
221             } else if (isBold(false)) {
222                 writeControlWord("b0");
223             // TODO not optimal, consecutive RtfText with same attributes
224
// could be written without group marks
225
} else {
226                 writeGroupMark(true);
227                 if (attr != null && mustWriteAttributes()) {
228                     writeAttributes(attr, RtfText.ATTR_NAMES);
229                 }
230                 RtfStringConverter.getInstance().writeRtfString(writer, text);
231                 writeGroupMark(false);
232             }
233         }
234     }
235
236     /** true if our text attributes must be written */
237     private boolean mustWriteAttributes() {
238         return !isEmpty() && !isNbsp();
239     }
240
241     /** IRtfTextContainer requirement:
242      * @return a copy of our attributes */

243     public RtfAttributes getTextContainerAttributes() {
244         if (attrib == null) {
245             return null;
246         }
247         return (RtfAttributes)this.attrib.clone();
248     }
249
250     /** direct access to our text */
251     String JavaDoc getText() {
252         return text;
253     }
254
255     /** direct access to our text */
256     void setText(String JavaDoc str) {
257         text = str;
258     }
259
260     /**
261      * Checks whether the text is empty.
262      *
263      * @return true If m_text is null\n
264      * false m_text is set
265      */

266     public boolean isEmpty () {
267         return text == null || text.trim().length() == 0;
268     }
269
270     /**
271      * True if text contains a single non-breaking space (#160).
272      * TODO make this more general and/or merge with isEmpty? -- what happen
273      * with empty paragraphs, if they will be removed, than NO, else ok
274      *
275      * @return true If m_text is character 160\n
276      * false m_text is not a nbsp
277      */

278     public boolean isNbsp () {
279         if (!isEmpty ()) {
280             if (text.trim ().length () == 1 && text.charAt (0) == CHAR_NBSP) {
281                 return true;
282             }
283         }
284         return false;
285     }
286
287     /**
288      * @return true if the text is a tab character
289      */

290     public boolean isTab() {
291         return (text.trim().length() == 1 && text.charAt(0) == CHAR_TAB);
292     }
293
294     /**
295      * @return true if text is a newline character
296      */

297     public boolean isNewLine() {
298         return (text.trim().length() == 1 && text.charAt(0) == CHAR_NEW_LINE);
299     }
300
301     /**
302      * @param isStart set to true if processing the start of the text (??)
303      * @return true if text is bold
304      */

305     public boolean isBold(boolean isStart) {
306         if (isStart) {
307             return (text.trim().length() == 1 && text.charAt(0) == CHAR_BOLD_START);
308         } else {
309             return (text.trim().length() == 1 && text.charAt(0) == CHAR_BOLD_END);
310         }
311     }
312
313     /** @return the attributes of our text */
314     public RtfAttributes getTextAttributes() {
315         return attr;
316     }
317 }
318
Popular Tags