Source code: org/jfor/jfor/rtflib/rtfdoc/RtfStyleSheetTable.java
1 /**
2 * File: RtfStyleSheetTable.java
3 *
4 *
5 * Date Author Changes
6 * Aug 28 01 Andreas Putz Created
7 */
8 package org.jfor.jfor.rtflib.rtfdoc;
9
10 import java.util.Vector;
11 import java.util.Hashtable;
12 import java.io.IOException;
13 import java.util.Iterator;
14
15 /*-----------------------------------------------------------------------------
16 * jfor - Open-Source XSL-FO to RTF converter - see www.jfor.org
17 *
18 * ====================================================================
19 * jfor Apache-Style Software License.
20 * Copyright (c) 2002 by the jfor project. All rights reserved.
21 *
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 *
26 * 1. Redistributions of source code must retain the above copyright
27 * notice, this list of conditions and the following disclaimer.
28 *
29 * 2. Redistributions in binary form must reproduce the above copyright
30 * notice, this list of conditions and the following disclaimer in
31 * the documentation and/or other materials provided with the
32 * distribution.
33 *
34 * 3. The end-user documentation included with the redistribution,
35 * if any, must include the following acknowledgment:
36 * "This product includes software developed
37 * by the jfor project (http://www.jfor.org)."
38 * Alternately, this acknowledgment may appear in the software itself,
39 * if and wherever such third-party acknowledgments normally appear.
40 *
41 * 4. The name "jfor" must not be used to endorse
42 * or promote products derived from this software without prior written
43 * permission. For written permission, please contact info@jfor.org.
44 *
45 * 5. Products derived from this software may not be called "jfor",
46 * nor may "jfor" appear in their name, without prior written
47 * permission of info@jfor.org.
48 *
49 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
50 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
51 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
52 * DISCLAIMED. IN NO EVENT SHALL THE JFOR PROJECT OR ITS CONTRIBUTORS BE
53 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
54 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
55 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
56 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
57 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
58 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
59 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
60 * ====================================================================
61 * Contributor(s):
62 -----------------------------------------------------------------------------*/
63
64 /**
65 * Singelton of the RTF style sheet table.
66 * This class belongs to the <jfor:stylesheet> tag processing.
67 * @author <a href="mailto:a.putz@skynamics.com">Andreas Putz</a>
68 */
69 public class RtfStyleSheetTable
70 {
71 //////////////////////////////////////////////////
72 // @@ Symbolic constants
73 //////////////////////////////////////////////////
74
75 /** Start index number for the stylesheet reference table */
76 private static int startIndex = 15;
77
78 /** OK status value for attribute handling */
79 public static int STATUS_OK = 0;
80 /** Status value for attribute handling, if the stylesheet not found and
81 * the stylesheet set to the default stylesheet */
82 public static int STATUS_DEFAULT = 1;
83
84 /** Standard style name */
85 private static String STANDARD_STYLE = "Standard";
86
87
88 //////////////////////////////////////////////////
89 // @@ Singleton
90 //////////////////////////////////////////////////
91
92 /** Singelton instance */
93 private static RtfStyleSheetTable instance = null;
94
95
96 //////////////////////////////////////////////////
97 // @@ Members
98 //////////////////////////////////////////////////
99
100
101 /** Table of styles */
102 private Hashtable styles = null;
103
104 /** Used, style attributes to this vector */
105 private Hashtable attrTable = null;
106
107 /** Used, style names to this vector */
108 private Vector nameTable = null;
109
110 /** Default style */
111 private String defaultStyleName = STANDARD_STYLE;
112
113
114 //////////////////////////////////////////////////
115 // @@ Construction
116 //////////////////////////////////////////////////
117
118 /**
119 * Constructor.
120 */
121 private RtfStyleSheetTable ()
122 {
123 styles = new Hashtable ();
124 attrTable = new Hashtable ();
125 nameTable = new Vector ();
126 }
127
128 /**
129 * Singelton.
130 *
131 * @return The instance of RtfStyleSheetTable
132 */
133 public static RtfStyleSheetTable getInstance ()
134 {
135 if (instance == null)
136 {
137 instance = new RtfStyleSheetTable ();
138 }
139
140 return instance;
141 }
142
143
144 //////////////////////////////////////////////////
145 // @@ Member access
146 //////////////////////////////////////////////////
147
148 /**
149 * Sets the default style.
150 * @param styleName Name of the default style, defined in the stylesheet
151 */
152 public void setDefaultStyle (String styleName)
153 {
154 this.defaultStyleName = styleName;
155 }
156
157 /**
158 * Gets the name of the default style.
159 * @return Default style name.
160 */
161 public String getDefaultStyleName ()
162 {
163 if (attrTable.get (defaultStyleName) != null)
164 return defaultStyleName;
165
166 if (attrTable.get (STANDARD_STYLE) != null)
167 {
168 defaultStyleName = STANDARD_STYLE;
169 return defaultStyleName;
170 }
171
172 return null;
173 }
174
175
176 //////////////////////////////////////////////////
177 // @@ Public methods
178 //////////////////////////////////////////////////
179
180 /**
181 * Adds a style to the table.
182 * @param name Name of style to add
183 * @param attrs Rtf attributes which defines the style
184 */
185 public void addStyle (String name, RtfAttributes attrs)
186 {
187 nameTable.addElement (name);
188 if (attrs != null)
189 attrTable.put (name, attrs);
190 styles.put (name, new Integer (nameTable.size () - 1 + startIndex));
191 }
192
193 /**
194 * Adds the style attributes to the given attributes.
195 * @param name Name of style, of which the attributes will copied to attr
196 * @param attrs Default rtf attributes
197 * @return Status value
198 */
199 public int addStyleToAttributes (String name, RtfAttributes attr)
200 {
201 // Sets status to ok
202 int status = STATUS_OK;
203
204 // Gets the style number from table
205 Integer style = (Integer) styles.get (name);
206
207 if (style == null && !name.equals (defaultStyleName))
208 {
209 // If style not found, and style was not the default style, try the default style
210 name = defaultStyleName;
211 style = (Integer) styles.get (name);
212 // set status for default style setting
213 status = STATUS_DEFAULT;
214 }
215
216 // Returns the status for invalid styles
217 if (style == null)
218 return status;
219
220 // Adds the attributes to default attributes, if not available in default attributes
221 attr.set ("cs", style.intValue ());
222
223 Object o = attrTable.get (name);
224 if (o != null)
225 {
226 RtfAttributes rtfAttr = (RtfAttributes) o;
227
228 for (Iterator names = rtfAttr.nameIterator (); names.hasNext ();)
229 {
230 String attrName = (String) names.next ();
231 if (! attr.isSet (attrName))
232 {
233 Integer i = (Integer) rtfAttr.getValue (attrName);
234 if (i == null)
235 attr.set (attrName);
236 else
237 attr.set (attrName, i.intValue ());
238 }
239 }
240 }
241 return status;
242 }
243
244 /**
245 * Writes the rtf style sheet table.
246 * @param header Rtf header is the parent
247 * @throws IOException On write error
248 */
249 public void writeStyleSheet (RtfHeader header) throws IOException
250 {
251 if (styles == null || styles.size () == 0)
252 {
253 return;
254 }
255 header.writeGroupMark (true);
256 header.writeControlWord ("stylesheet");
257
258 int number = nameTable.size ();
259 for (int i = 0; i < number; i++)
260 {
261 String name = (String) nameTable.elementAt (i);
262 header.writeGroupMark (true);
263 header.writeControlWord ("*\\" + this.getRtfStyleReference (name));
264
265 Object o = attrTable.get (name);
266 if (o != null)
267 {
268 header.writeAttributes ((RtfAttributes) o, RtfText.ATTR_NAMES);
269 header.writeAttributes ((RtfAttributes) o, RtfText.ALIGNMENT);
270 }
271
272 header.write (name + ";");
273 header.writeGroupMark (false);
274 }
275 header.writeGroupMark (false);
276 }
277
278 /**
279 * Gets the rtf style reference from the table.
280 * @param name Name of Style
281 * @return Rtf attribute of the style reference
282 */
283 private String getRtfStyleReference (String name)
284 {
285 return "cs" + styles.get (name).toString ();
286 }
287 }