1 17 18 19 20 21 27 28 package org.apache.fop.render.rtf.rtflib.rtfdoc; 29 30 import java.util.Vector ; 31 import java.util.Hashtable ; 32 import java.io.IOException ; 33 import java.util.Iterator ; 34 35 40 public class RtfStyleSheetTable { 41 45 46 private static int startIndex = 15; 47 48 49 public static final int STATUS_OK = 0; 50 52 public static final int STATUS_DEFAULT = 1; 53 54 55 private static final String STANDARD_STYLE = "Standard"; 56 57 58 62 63 private static RtfStyleSheetTable instance = null; 64 65 66 70 71 72 private Hashtable styles = null; 73 74 75 private Hashtable attrTable = null; 76 77 78 private Vector nameTable = null; 79 80 81 private String defaultStyleName = STANDARD_STYLE; 82 83 84 88 91 private RtfStyleSheetTable () { 92 styles = new Hashtable (); 93 attrTable = new Hashtable (); 94 nameTable = new Vector (); 95 } 96 97 102 public static RtfStyleSheetTable getInstance () { 103 if (instance == null) { 104 instance = new RtfStyleSheetTable (); 105 } 106 107 return instance; 108 } 109 110 111 115 119 public void setDefaultStyle (String styleName) { 120 this.defaultStyleName = styleName; 121 } 122 123 127 public String 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 145 150 public void addStyle (String name, RtfAttributes attrs) { 151 nameTable.addElement (name); 152 if (attrs != null) { 153 attrTable.put (name, attrs); 154 } 155 styles.put (name, new Integer (nameTable.size () - 1 + startIndex)); 156 } 157 158 164 public int addStyleToAttributes (String name, RtfAttributes attr) { 165 int status = STATUS_OK; 167 168 Integer style = (Integer ) styles.get (name); 170 171 if (style == null && !name.equals (defaultStyleName)) { 172 name = defaultStyleName; 174 style = (Integer ) styles.get (name); 175 status = STATUS_DEFAULT; 177 } 178 179 if (style == null) { 181 return status; 182 } 183 184 attr.set ("cs", style.intValue ()); 186 187 Object o = attrTable.get (name); 188 if (o != null) { 189 RtfAttributes rtfAttr = (RtfAttributes) o; 190 191 for (Iterator names = rtfAttr.nameIterator (); names.hasNext ();) { 192 String attrName = (String ) names.next (); 193 if (!attr.isSet (attrName)) { 194 Integer i = (Integer ) 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 211 public void writeStyleSheet (RtfHeader header) throws IOException { 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 name = (String ) nameTable.elementAt (i); 221 header.writeGroupMark (true); 222 header.writeControlWord ("*\\" + this.getRtfStyleReference (name)); 223 224 Object 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 241 private String getRtfStyleReference (String name) { 242 return "cs" + styles.get (name).toString (); 243 } 244 } | Popular Tags |