1 17 18 19 20 package org.apache.fop.render.rtf.rtflib.rtfdoc; 21 22 28 29 import java.io.Writer ; 30 import java.io.IOException ; 31 import java.util.Iterator ; 32 34 38 public abstract class RtfElement { 39 40 protected final Writer writer; 41 42 protected final RtfContainer parent; 43 44 protected final RtfAttributes attrib; 45 private boolean written; 46 private boolean closed; 47 private final int id; 48 private static int idCounter; 49 50 51 RtfElement(RtfContainer parent, Writer w) throws IOException { 52 this(parent, w, null); 53 } 54 55 56 RtfElement(RtfContainer parent, Writer w, RtfAttributes attr) throws IOException { 57 58 id = idCounter++; 59 this.parent = parent; 60 attrib = (attr != null ? attr : new RtfAttributes()); 61 if (this.parent != null) { 62 this.parent.addChild(this); 63 } 64 writer = w; 65 written = false; 66 } 67 68 73 public final void close() throws IOException { 74 closed = true; 75 } 76 77 81 public final void writeRtf() throws IOException { 82 if (!written) { 83 written = true; 84 if (okToWriteRtf()) { 85 writeRtfPrefix(); 86 writeRtfContent(); 87 writeRtfSuffix(); 88 } 89 } 90 } 91 92 97 public void newLine() throws IOException { 98 writer.write("\n"); 99 } 100 101 106 protected final void writeControlWord(String word) 107 throws IOException { 108 writer.write('\\'); 109 writer.write(word); 110 writer.write(' '); 111 } 112 113 119 protected final void writeStarControlWord(String word) 120 throws IOException { 121 writer.write("\\*\\"); 122 writer.write(word); 123 writer.write(' '); 124 } 125 126 131 protected final void writeStarControlWordNS(String word) 132 throws IOException { 133 writer.write("\\*\\"); 134 writer.write(word); 135 } 136 137 142 protected final void writeControlWordNS(String word) 143 throws IOException { 144 writer.write('\\'); 145 writer.write(word); 146 } 147 148 152 protected void writeRtfPrefix() throws IOException { 153 } 154 155 159 protected abstract void writeRtfContent() throws IOException ; 160 161 165 protected void writeRtfSuffix() throws IOException { 166 } 167 168 173 protected final void writeGroupMark(boolean isStart) 174 throws IOException { 175 writer.write(isStart ? "{" : "}"); 176 } 177 178 184 protected void writeAttributes(RtfAttributes attr, String [] nameList) 185 throws IOException { 186 if (attr == null) { 187 return; 188 } 189 190 if (nameList != null) { 191 for (int i = 0; i < nameList.length; i++) { 193 final String name = nameList[i]; 194 if (attr.isSet(name)) { 195 writeOneAttribute(name, attr.getValue(name)); 196 } 197 } 198 } else { 199 for (Iterator it = attr.nameIterator(); it.hasNext();) { 201 final String name = (String )it.next(); 202 if (attr.isSet(name)) { 203 writeOneAttribute(name, attr.getValue(name)); 204 } 205 } 206 } 207 } 208 209 215 protected void writeOneAttribute(String name, Object value) 216 throws IOException { 217 String cw = name; 218 if (value instanceof Integer ) { 219 cw += value; 221 } else if (value instanceof String ) { 222 cw += value; 223 } else if (value instanceof RtfAttributes) { 224 writeControlWord(cw); 225 writeAttributes((RtfAttributes) value, null); 226 return; 227 } 228 writeControlWord(cw); 229 } 230 231 237 protected void writeOneAttributeNS(String name, Object value) 238 throws IOException { 239 String cw = name; 240 if (value instanceof Integer ) { 241 cw += value; 243 } else if (value instanceof String ) { 244 cw += value; 245 } else if (value instanceof RtfAttributes) { 246 writeControlWord(cw); 247 writeAttributes((RtfAttributes) value, null); 248 return; 249 } 250 writeControlWordNS(cw); 251 } 252 253 257 protected boolean okToWriteRtf() { 258 return true; 259 } 260 261 262 void dump(Writer w, int indent) 263 throws IOException { 264 for (int i = 0; i < indent; i++) { 265 w.write(' '); 266 } 267 w.write(this.toString()); 268 w.write('\n'); 269 w.flush(); 270 } 271 272 276 public String toString() { 277 return (this == null) ? "null" : (this.getClass().getName() + " #" + id); 278 } 279 280 281 boolean isClosed() { 282 return closed; 283 } 284 285 286 RtfFile getRtfFile() { 287 RtfElement result = this; 289 while (result.parent != null) { 290 result = result.parent; 291 } 292 293 return (RtfFile)result; 296 } 297 298 301 RtfElement getParentOfClass(Class c) { 302 RtfElement result = null; 303 RtfElement current = this; 304 while (current.parent != null) { 305 current = current.parent; 306 if (c.isAssignableFrom(current.getClass())) { 307 result = current; 308 break; 309 } 310 } 311 return result; 312 } 313 314 317 public abstract boolean isEmpty(); 318 319 324 protected void writeExceptionInRtf(Exception ie) 325 throws IOException { 326 writeGroupMark(true); 327 writeControlWord("par"); 328 329 writeControlWord("fs48"); 331 RtfStringConverter.getInstance().writeRtfString(writer, ie.getClass().getName()); 334 335 writeControlWord("fs20"); 336 RtfStringConverter.getInstance().writeRtfString(writer, " " + ie.toString()); 337 338 writeControlWord("par"); 339 writeGroupMark(false); 340 } 341 342 347 public RtfAttributes getRtfAttributes() { 348 return attrib; 349 } 350 } | Popular Tags |