KickJava   Java API By Example, From Geeks To Geeks.

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


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: WhitespaceCollapser.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.util.Iterator JavaDoc;
31 import java.util.StringTokenizer JavaDoc;
32
33 /** Collapses whitespace of an RtfContainer that contains RtfText elements
34  * @author Bertrand Delacretaz bdelacretaz@codeconsult.ch
35  */

36
37 class WhitespaceCollapser {
38     private static final String JavaDoc SPACE = " ";
39     private boolean lastEndSpace = true;
40
41     /** remove extra whitespace in RtfText elements that are inside c */
42     WhitespaceCollapser(RtfContainer c) {
43         // process all texts
44
for (Iterator JavaDoc it = c.getChildren().iterator(); it.hasNext();) {
45             final Object JavaDoc kid = it.next();
46             if (kid instanceof RtfText) {
47                 RtfText current = (RtfText)kid;
48                 processText(current);
49             } else if (kid instanceof RtfString) {
50                 RtfString current = (RtfString)kid;
51                 processString(current);
52             } else {
53                 // if there is something between two texts, it counts for a space
54
lastEndSpace = true;
55             }
56         }
57     }
58
59     /** process one RtfText from our container */
60     private void processText(RtfText txt) {
61         final String JavaDoc newString = processString(txt.getText());
62         if (newString != null) {
63             txt.setText(newString);
64         }
65     }
66     
67     /** process one RtfString from our container */
68     private void processString(RtfString txt) {
69         final String JavaDoc newString = processString(txt.getText());
70         if (newString != null) {
71             txt.setText(newString);
72         }
73     }
74     
75     /** process one String */
76     private String JavaDoc processString(String JavaDoc txt) {
77         final String JavaDoc orig = txt;
78
79         // tokenize the text based on whitespace and regenerate it so as
80
// to collapse multiple spaces into one
81
if (orig == null) {
82             return null;
83         } else if (orig.length() > 0) {
84             final boolean allSpaces = orig.trim().length() == 0;
85             final boolean endSpace = allSpaces
86                                      || Character.isWhitespace(orig.charAt(orig.length() - 1));
87             final boolean beginSpace = Character.isWhitespace(orig.charAt(0));
88             final StringBuffer JavaDoc sb = new StringBuffer JavaDoc(orig.length());
89
90             // if text contains spaces only, keep at most one
91
if (allSpaces) {
92                 if (!lastEndSpace) {
93                     sb.append(SPACE);
94                 }
95             } else {
96                 // TODO to be compatible with different Locales, should use Character.isWhitespace
97
// instead of this limited list
98
boolean first = true;
99                 final StringTokenizer JavaDoc stk = new StringTokenizer JavaDoc(txt, " \t\n\r");
100                 while (stk.hasMoreTokens()) {
101                     if (first && beginSpace && !lastEndSpace) {
102                         sb.append(SPACE);
103                     }
104                     first = false;
105
106                     sb.append(stk.nextToken());
107                     if (stk.hasMoreTokens() || endSpace) {
108                         sb.append(SPACE);
109                     }
110                 }
111             }
112
113             lastEndSpace = endSpace;
114             return sb.toString();
115         } else {
116             return "";
117         }
118     }
119 }
120
Popular Tags