001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: /* $Id: RtfStyleSheetTable.java 426576 2006-07-28 15:44:37Z jeremias $ */
019:
020: /*
021: * This file is part of the RTF library of the FOP project, which was originally
022: * created by Bertrand Delacretaz <bdelacretaz@codeconsult.ch> and by other
023: * contributors to the jfor project (www.jfor.org), who agreed to donate jfor to
024: * the FOP project.
025: */
026:
027: package org.apache.fop.render.rtf.rtflib.rtfdoc;
028:
029: import java.util.Vector;
030: import java.util.Hashtable;
031: import java.io.IOException;
032: import java.util.Iterator;
033:
034: /**
035: * Singelton of the RTF style sheet table.
036: * This class belongs to the <jfor:stylesheet> tag processing.
037: * @author <a href="mailto:a.putz@skynamics.com">Andreas Putz</a>
038: */
039: public class RtfStyleSheetTable {
040: //////////////////////////////////////////////////
041: // @@ Symbolic constants
042: //////////////////////////////////////////////////
043:
044: /** Start index number for the stylesheet reference table */
045: private static int startIndex = 15;
046:
047: /** OK status value for attribute handling */
048: public static final int STATUS_OK = 0;
049: /** Status value for attribute handling, if the stylesheet not found and
050: * the stylesheet set to the default stylesheet */
051: public static final int STATUS_DEFAULT = 1;
052:
053: /** Standard style name */
054: private static final String STANDARD_STYLE = "Standard";
055:
056: //////////////////////////////////////////////////
057: // @@ Singleton
058: //////////////////////////////////////////////////
059:
060: /** Singelton instance */
061: private static RtfStyleSheetTable instance = null;
062:
063: //////////////////////////////////////////////////
064: // @@ Members
065: //////////////////////////////////////////////////
066:
067: /** Table of styles */
068: private Hashtable styles = null;
069:
070: /** Used, style attributes to this vector */
071: private Hashtable attrTable = null;
072:
073: /** Used, style names to this vector */
074: private Vector nameTable = null;
075:
076: /** Default style */
077: private String defaultStyleName = STANDARD_STYLE;
078:
079: //////////////////////////////////////////////////
080: // @@ Construction
081: //////////////////////////////////////////////////
082:
083: /**
084: * Constructor.
085: */
086: private RtfStyleSheetTable() {
087: styles = new Hashtable();
088: attrTable = new Hashtable();
089: nameTable = new Vector();
090: }
091:
092: /**
093: * Singelton.
094: *
095: * @return The instance of RtfStyleSheetTable
096: */
097: public static RtfStyleSheetTable getInstance() {
098: if (instance == null) {
099: instance = new RtfStyleSheetTable();
100: }
101:
102: return instance;
103: }
104:
105: //////////////////////////////////////////////////
106: // @@ Member access
107: //////////////////////////////////////////////////
108:
109: /**
110: * Sets the default style.
111: * @param styleName Name of the default style, defined in the stylesheet
112: */
113: public void setDefaultStyle(String styleName) {
114: this .defaultStyleName = styleName;
115: }
116:
117: /**
118: * Gets the name of the default style.
119: * @return Default style name.
120: */
121: public String getDefaultStyleName() {
122: if (attrTable.get(defaultStyleName) != null) {
123: return defaultStyleName;
124: }
125:
126: if (attrTable.get(STANDARD_STYLE) != null) {
127: defaultStyleName = STANDARD_STYLE;
128: return defaultStyleName;
129: }
130:
131: return null;
132: }
133:
134: //////////////////////////////////////////////////
135: // @@ Public methods
136: //////////////////////////////////////////////////
137:
138: /**
139: * Adds a style to the table.
140: * @param name Name of style to add
141: * @param attrs Rtf attributes which defines the style
142: */
143: public void addStyle(String name, RtfAttributes attrs) {
144: nameTable.addElement(name);
145: if (attrs != null) {
146: attrTable.put(name, attrs);
147: }
148: styles
149: .put(name, new Integer(nameTable.size() - 1
150: + startIndex));
151: }
152:
153: /**
154: * Adds the style attributes to the given attributes.
155: * @param name Name of style, of which the attributes will copied to attr
156: * @param attr Default rtf attributes
157: * @return Status value
158: */
159: public int addStyleToAttributes(String name, RtfAttributes attr) {
160: // Sets status to ok
161: int status = STATUS_OK;
162:
163: // Gets the style number from table
164: Integer style = (Integer) styles.get(name);
165:
166: if (style == null && !name.equals(defaultStyleName)) {
167: // If style not found, and style was not the default style, try the default style
168: name = defaultStyleName;
169: style = (Integer) styles.get(name);
170: // set status for default style setting
171: status = STATUS_DEFAULT;
172: }
173:
174: // Returns the status for invalid styles
175: if (style == null) {
176: return status;
177: }
178:
179: // Adds the attributes to default attributes, if not available in default attributes
180: attr.set("cs", style.intValue());
181:
182: Object o = attrTable.get(name);
183: if (o != null) {
184: RtfAttributes rtfAttr = (RtfAttributes) o;
185:
186: for (Iterator names = rtfAttr.nameIterator(); names
187: .hasNext();) {
188: String attrName = (String) names.next();
189: if (!attr.isSet(attrName)) {
190: Integer i = (Integer) rtfAttr.getValue(attrName);
191: if (i == null) {
192: attr.set(attrName);
193: } else {
194: attr.set(attrName, i.intValue());
195: }
196: }
197: }
198: }
199: return status;
200: }
201:
202: /**
203: * Writes the rtf style sheet table.
204: * @param header Rtf header is the parent
205: * @throws IOException On write error
206: */
207: public void writeStyleSheet(RtfHeader header) throws IOException {
208: if (styles == null || styles.size() == 0) {
209: return;
210: }
211: header.writeGroupMark(true);
212: header.writeControlWord("stylesheet");
213:
214: int number = nameTable.size();
215: for (int i = 0; i < number; i++) {
216: String name = (String) nameTable.elementAt(i);
217: header.writeGroupMark(true);
218: header.writeControlWord("*\\"
219: + this .getRtfStyleReference(name));
220:
221: Object o = attrTable.get(name);
222: if (o != null) {
223: header.writeAttributes((RtfAttributes) o,
224: RtfText.ATTR_NAMES);
225: header.writeAttributes((RtfAttributes) o,
226: RtfText.ALIGNMENT);
227: }
228:
229: header.write(name + ";");
230: header.writeGroupMark(false);
231: }
232: header.writeGroupMark(false);
233: }
234:
235: /**
236: * Gets the rtf style reference from the table.
237: * @param name Name of Style
238: * @return Rtf attribute of the style reference
239: */
240: private String getRtfStyleReference(String name) {
241: return "cs" + styles.get(name).toString();
242: }
243: }
|