KickJava   Java API By Example, From Geeks To Geeks.

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


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: RtfStringConverter.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.Map JavaDoc;
31 import java.util.HashMap JavaDoc;
32 import java.io.IOException JavaDoc;
33 import java.io.Writer JavaDoc;
34
35 /** Converts java Strings according to RTF conventions
36  * @author Bertrand Delacretaz bdelacretaz@codeconsult.ch
37  */

38
39 public class RtfStringConverter {
40     private static final RtfStringConverter INSTANCE = new RtfStringConverter();
41
42     private static final Map JavaDoc SPECIAL_CHARS;
43     private static final Character JavaDoc DBLQUOTE = new Character JavaDoc('\"');
44     private static final Character JavaDoc QUOTE = new Character JavaDoc('\'');
45     private static final Character JavaDoc SPACE = new Character JavaDoc(' ');
46
47     /** List of characters to escape with corresponding replacement strings */
48     static {
49         SPECIAL_CHARS = new HashMap JavaDoc();
50         SPECIAL_CHARS.put(new Character JavaDoc('\t'), "tab");
51         SPECIAL_CHARS.put(new Character JavaDoc('\n'), "line");
52         SPECIAL_CHARS.put(new Character JavaDoc('\''), "rquote");
53         SPECIAL_CHARS.put(new Character JavaDoc('\"'), "rdblquote");
54         SPECIAL_CHARS.put(new Character JavaDoc('\\'), "\\");
55         SPECIAL_CHARS.put(new Character JavaDoc('{'), "{");
56         SPECIAL_CHARS.put(new Character JavaDoc('}'), "}");
57     }
58
59     /** singleton pattern */
60     private RtfStringConverter() {
61     }
62
63     /**
64      * use this to get an object of this class
65      * @return the singleton instance
66      */

67     public static RtfStringConverter getInstance() {
68         return INSTANCE;
69     }
70
71     /**
72      * Write given String to given Writer, converting characters as required by
73      * RTF spec
74      * @param w Writer
75      * @param str String to be written
76      * @throws IOException for I/O problems
77      */

78     public void writeRtfString(Writer JavaDoc w, String JavaDoc str) throws IOException JavaDoc {
79         if (str == null) {
80             return;
81         }
82         w.write(escape(str));
83     }
84
85     /**
86      * Escapes a String as required by the RTF spec.
87      * @param str String to be escaped
88      * @return the escaped string
89      */

90     public String JavaDoc escape(String JavaDoc str) {
91         if (str == null) {
92             return null;
93         }
94
95         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(Math.max(16, str.length()));
96         // TODO: could be made more efficient (binary lookup, etc.)
97
for (int i = 0; i < str.length(); i++) {
98             final Character JavaDoc c = new Character JavaDoc(str.charAt(i));
99             Character JavaDoc d;
100             String JavaDoc replacement;
101             if (i != 0) {
102                 d = new Character JavaDoc(str.charAt(i - 1));
103             } else {
104                 d = new Character JavaDoc(str.charAt(i));
105             }
106
107             //This section modified by Chris Scott
108
//add "smart" quote recognition
109
if (c.equals((Object JavaDoc)DBLQUOTE) && d.equals((Object JavaDoc)SPACE)) {
110                 replacement = "ldblquote";
111             } else if (c.equals((Object JavaDoc)QUOTE) && d.equals((Object JavaDoc)SPACE)) {
112                 replacement = "lquote";
113             } else {
114                 replacement = (String JavaDoc)SPECIAL_CHARS.get(c);
115             }
116
117             if (replacement != null) {
118                 // RTF-escaped char
119
sb.append('\\');
120                 sb.append(replacement);
121                 sb.append(' ');
122             } else if (c.charValue() > 127) {
123                 // write unicode representation - contributed by Michel Jacobson
124
// <jacobson@idf.ext.jussieu.fr>
125
sb.append("\\u");
126                 sb.append(Integer.toString((int)c.charValue()));
127                 sb.append("\\\'3f");
128             } else {
129                 // plain char that is understood by RTF natively
130
sb.append(c.charValue());
131             }
132         }
133         return sb.toString();
134     }
135     
136 }
137
Popular Tags