You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RtfStyleSheetTable.java 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. /* $Id$ */
  18. /*
  19. * This file is part of the RTF library of the FOP project, which was originally
  20. * created by Bertrand Delacretaz bdelacretaz@codeconsult.ch and by other
  21. * contributors to the jfor project (www.jfor.org), who agreed to donate jfor to
  22. * the FOP project.
  23. */
  24. package org.apache.fop.render.rtf.rtflib.rtfdoc;
  25. import java.io.IOException;
  26. import java.util.Hashtable;
  27. import java.util.Iterator;
  28. import java.util.Vector;
  29. /**
  30. * Singelton of the RTF style sheet table.
  31. * This class belongs to the jfor:stylesheet tag processing.
  32. * This work was originally authored by <a href="mailto:a.putz@skynamics.com">Andreas Putz</a>
  33. */
  34. public final class RtfStyleSheetTable {
  35. //////////////////////////////////////////////////
  36. // @@ Symbolic constants
  37. //////////////////////////////////////////////////
  38. /** Start index number for the stylesheet reference table */
  39. private static int startIndex = 15;
  40. /** OK status value for attribute handling */
  41. public static final int STATUS_OK = 0;
  42. /** Status value for attribute handling, if the stylesheet not found and
  43. * the stylesheet set to the default stylesheet */
  44. public static final int STATUS_DEFAULT = 1;
  45. /** Standard style name */
  46. private static final String STANDARD_STYLE = "Standard";
  47. //////////////////////////////////////////////////
  48. // @@ Singleton
  49. //////////////////////////////////////////////////
  50. /** Singelton instance */
  51. private static RtfStyleSheetTable instance = new RtfStyleSheetTable();
  52. //////////////////////////////////////////////////
  53. // @@ Members
  54. //////////////////////////////////////////////////
  55. /** Table of styles */
  56. private Hashtable styles;
  57. /** Used, style attributes to this vector */
  58. private Hashtable attrTable;
  59. /** Used, style names to this vector */
  60. private Vector nameTable;
  61. /** Default style */
  62. private String defaultStyleName = STANDARD_STYLE;
  63. //////////////////////////////////////////////////
  64. // @@ Construction
  65. //////////////////////////////////////////////////
  66. /**
  67. * Constructor.
  68. */
  69. private RtfStyleSheetTable() {
  70. styles = new Hashtable();
  71. attrTable = new Hashtable();
  72. nameTable = new Vector();
  73. }
  74. /**
  75. * Singelton.
  76. *
  77. * @return The instance of RtfStyleSheetTable
  78. */
  79. public static RtfStyleSheetTable getInstance() {
  80. return instance;
  81. }
  82. //////////////////////////////////////////////////
  83. // @@ Member access
  84. //////////////////////////////////////////////////
  85. /**
  86. * Sets the default style.
  87. * @param styleName Name of the default style, defined in the stylesheet
  88. */
  89. public void setDefaultStyle(String styleName) {
  90. this.defaultStyleName = styleName;
  91. }
  92. /**
  93. * Gets the name of the default style.
  94. * @return Default style name.
  95. */
  96. public String getDefaultStyleName() {
  97. if (attrTable.get(defaultStyleName) != null) {
  98. return defaultStyleName;
  99. }
  100. if (attrTable.get(STANDARD_STYLE) != null) {
  101. defaultStyleName = STANDARD_STYLE;
  102. return defaultStyleName;
  103. }
  104. return null;
  105. }
  106. //////////////////////////////////////////////////
  107. // @@ Public methods
  108. //////////////////////////////////////////////////
  109. /**
  110. * Adds a style to the table.
  111. * @param name Name of style to add
  112. * @param attrs Rtf attributes which defines the style
  113. */
  114. public void addStyle(String name, RtfAttributes attrs) {
  115. nameTable.addElement(name);
  116. if (attrs != null) {
  117. attrTable.put(name, attrs);
  118. }
  119. styles.put(name, nameTable.size() - 1 + startIndex);
  120. }
  121. /**
  122. * Adds the style attributes to the given attributes.
  123. * @param name Name of style, of which the attributes will copied to attr
  124. * @param attr Default rtf attributes
  125. * @return Status value
  126. */
  127. public int addStyleToAttributes(String name, RtfAttributes attr) {
  128. // Sets status to ok
  129. int status = STATUS_OK;
  130. // Gets the style number from table
  131. Integer style = (Integer) styles.get(name);
  132. if (style == null && !name.equals(defaultStyleName)) {
  133. // If style not found, and style was not the default style, try the default style
  134. name = defaultStyleName;
  135. style = (Integer) styles.get(name);
  136. // set status for default style setting
  137. status = STATUS_DEFAULT;
  138. }
  139. // Returns the status for invalid styles
  140. if (style == null) {
  141. return status;
  142. }
  143. // Adds the attributes to default attributes, if not available in default attributes
  144. attr.set("cs", style);
  145. Object o = attrTable.get(name);
  146. if (o != null) {
  147. RtfAttributes rtfAttr = (RtfAttributes) o;
  148. for (Iterator names = rtfAttr.nameIterator(); names.hasNext();) {
  149. String attrName = (String) names.next();
  150. if (!attr.isSet(attrName)) {
  151. Integer i = (Integer) rtfAttr.getValue(attrName);
  152. if (i == null) {
  153. attr.set(attrName);
  154. } else {
  155. attr.set(attrName, i);
  156. }
  157. }
  158. }
  159. }
  160. return status;
  161. }
  162. /**
  163. * Writes the rtf style sheet table.
  164. * @param header Rtf header is the parent
  165. * @throws IOException On write error
  166. */
  167. public void writeStyleSheet(RtfHeader header) throws IOException {
  168. if (styles == null || styles.size() == 0) {
  169. return;
  170. }
  171. header.writeGroupMark(true);
  172. header.writeControlWord("stylesheet");
  173. int number = nameTable.size();
  174. for (int i = 0; i < number; i++) {
  175. String name = (String) nameTable.elementAt(i);
  176. header.writeGroupMark(true);
  177. header.writeControlWord("*\\" + this.getRtfStyleReference(name));
  178. Object o = attrTable.get(name);
  179. if (o != null) {
  180. header.writeAttributes((RtfAttributes) o, RtfText.ATTR_NAMES);
  181. header.writeAttributes((RtfAttributes) o, RtfText.ALIGNMENT);
  182. }
  183. header.write(name + ";");
  184. header.writeGroupMark(false);
  185. }
  186. header.writeGroupMark(false);
  187. }
  188. /**
  189. * Gets the rtf style reference from the table.
  190. * @param name Name of Style
  191. * @return Rtf attribute of the style reference
  192. */
  193. private String getRtfStyleReference(String name) {
  194. return "cs" + styles.get(name).toString();
  195. }
  196. }