KickJava   Java API By Example, From Geeks To Geeks.

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


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: RtfStyleSheetTable.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.Vector JavaDoc;
31 import java.util.Hashtable JavaDoc;
32 import java.io.IOException JavaDoc;
33 import java.util.Iterator JavaDoc;
34
35 /**
36  * Singelton of the RTF style sheet table.
37  * This class belongs to the <jfor:stylesheet> tag processing.
38  * @author <a HREF="mailto:a.putz@skynamics.com">Andreas Putz</a>
39  */

40 public class RtfStyleSheetTable {
41     //////////////////////////////////////////////////
42
// @@ Symbolic constants
43
//////////////////////////////////////////////////
44

45     /** Start index number for the stylesheet reference table */
46     private static int startIndex = 15;
47
48     /** OK status value for attribute handling */
49     public static final int STATUS_OK = 0;
50     /** Status value for attribute handling, if the stylesheet not found and
51      * the stylesheet set to the default stylesheet */

52     public static final int STATUS_DEFAULT = 1;
53
54     /** Standard style name */
55     private static final String JavaDoc STANDARD_STYLE = "Standard";
56
57
58     //////////////////////////////////////////////////
59
// @@ Singleton
60
//////////////////////////////////////////////////
61

62     /** Singelton instance */
63     private static RtfStyleSheetTable instance = null;
64
65
66     //////////////////////////////////////////////////
67
// @@ Members
68
//////////////////////////////////////////////////
69

70
71     /** Table of styles */
72     private Hashtable JavaDoc styles = null;
73
74     /** Used, style attributes to this vector */
75     private Hashtable JavaDoc attrTable = null;
76
77     /** Used, style names to this vector */
78     private Vector JavaDoc nameTable = null;
79
80     /** Default style */
81     private String JavaDoc defaultStyleName = STANDARD_STYLE;
82
83
84     //////////////////////////////////////////////////
85
// @@ Construction
86
//////////////////////////////////////////////////
87

88     /**
89      * Constructor.
90      */

91     private RtfStyleSheetTable () {
92         styles = new Hashtable JavaDoc ();
93         attrTable = new Hashtable JavaDoc ();
94         nameTable = new Vector JavaDoc ();
95     }
96
97     /**
98      * Singelton.
99      *
100      * @return The instance of RtfStyleSheetTable
101      */

102     public static RtfStyleSheetTable getInstance () {
103         if (instance == null) {
104             instance = new RtfStyleSheetTable ();
105         }
106
107         return instance;
108     }
109
110
111     //////////////////////////////////////////////////
112
// @@ Member access
113
//////////////////////////////////////////////////
114

115     /**
116      * Sets the default style.
117      * @param styleName Name of the default style, defined in the stylesheet
118      */

119     public void setDefaultStyle (String JavaDoc styleName) {
120         this.defaultStyleName = styleName;
121     }
122
123     /**
124      * Gets the name of the default style.
125      * @return Default style name.
126      */

127     public String JavaDoc getDefaultStyleName () {
128         if (attrTable.get (defaultStyleName) != null) {
129             return defaultStyleName;
130         }
131
132         if (attrTable.get (STANDARD_STYLE) != null) {
133             defaultStyleName = STANDARD_STYLE;
134             return defaultStyleName;
135         }
136
137         return null;
138     }
139
140
141     //////////////////////////////////////////////////
142
// @@ Public methods
143
//////////////////////////////////////////////////
144

145     /**
146      * Adds a style to the table.
147      * @param name Name of style to add
148      * @param attrs Rtf attributes which defines the style
149      */

150     public void addStyle (String JavaDoc name, RtfAttributes attrs) {
151         nameTable.addElement (name);
152         if (attrs != null) {
153             attrTable.put (name, attrs);
154         }
155         styles.put (name, new Integer JavaDoc (nameTable.size () - 1 + startIndex));
156     }
157
158     /**
159      * Adds the style attributes to the given attributes.
160      * @param name Name of style, of which the attributes will copied to attr
161      * @param attr Default rtf attributes
162      * @return Status value
163      */

164     public int addStyleToAttributes (String JavaDoc name, RtfAttributes attr) {
165         // Sets status to ok
166
int status = STATUS_OK;
167
168         // Gets the style number from table
169
Integer JavaDoc style = (Integer JavaDoc) styles.get (name);
170
171         if (style == null && !name.equals (defaultStyleName)) {
172             // If style not found, and style was not the default style, try the default style
173
name = defaultStyleName;
174             style = (Integer JavaDoc) styles.get (name);
175             // set status for default style setting
176
status = STATUS_DEFAULT;
177         }
178
179         // Returns the status for invalid styles
180
if (style == null) {
181             return status;
182         }
183
184         // Adds the attributes to default attributes, if not available in default attributes
185
attr.set ("cs", style.intValue ());
186
187         Object JavaDoc o = attrTable.get (name);
188         if (o != null) {
189             RtfAttributes rtfAttr = (RtfAttributes) o;
190
191             for (Iterator JavaDoc names = rtfAttr.nameIterator (); names.hasNext ();) {
192                 String JavaDoc attrName = (String JavaDoc) names.next ();
193                 if (!attr.isSet (attrName)) {
194                     Integer JavaDoc i = (Integer JavaDoc) rtfAttr.getValue (attrName);
195                     if (i == null) {
196                         attr.set (attrName);
197                     } else {
198                         attr.set (attrName, i.intValue ());
199                     }
200                 }
201             }
202         }
203         return status;
204     }
205
206     /**
207      * Writes the rtf style sheet table.
208      * @param header Rtf header is the parent
209      * @throws IOException On write error
210      */

211     public void writeStyleSheet (RtfHeader header) throws IOException JavaDoc {
212         if (styles == null || styles.size () == 0) {
213             return;
214         }
215         header.writeGroupMark (true);
216         header.writeControlWord ("stylesheet");
217
218         int number = nameTable.size ();
219         for (int i = 0; i < number; i++) {
220             String JavaDoc name = (String JavaDoc) nameTable.elementAt (i);
221             header.writeGroupMark (true);
222             header.writeControlWord ("*\\" + this.getRtfStyleReference (name));
223
224             Object JavaDoc o = attrTable.get (name);
225             if (o != null) {
226                 header.writeAttributes ((RtfAttributes) o, RtfText.ATTR_NAMES);
227                 header.writeAttributes ((RtfAttributes) o, RtfText.ALIGNMENT);
228             }
229
230             header.write (name + ";");
231             header.writeGroupMark (false);
232         }
233         header.writeGroupMark (false);
234     }
235
236     /**
237      * Gets the rtf style reference from the table.
238      * @param name Name of Style
239      * @return Rtf attribute of the style reference
240      */

241     private String JavaDoc getRtfStyleReference (String JavaDoc name) {
242         return "cs" + styles.get (name).toString ();
243     }
244 }
Popular Tags