1 53 54 package com.lowagie.text.rtf.field; 55 56 import java.io.ByteArrayOutputStream ; 57 import java.io.IOException ; 58 import java.io.OutputStream ; 59 60 import com.lowagie.text.Chunk; 61 import com.lowagie.text.Font; 62 import com.lowagie.text.rtf.RtfBasicElement; 63 import com.lowagie.text.rtf.document.RtfDocument; 64 import com.lowagie.text.rtf.style.RtfFont; 65 66 67 78 public abstract class RtfField extends Chunk implements RtfBasicElement { 79 80 83 private static final byte[] FIELD = "\\field".getBytes(); 84 87 private static final byte[] FIELD_DIRTY = "\\flddirty".getBytes(); 88 91 private static final byte[] FIELD_PRIVATE = "\\fldpriv".getBytes(); 92 95 private static final byte[] FIELD_LOCKED = "\\fldlock".getBytes(); 96 99 private static final byte[] FIELD_EDIT = "\\fldedit".getBytes(); 100 103 private static final byte[] FIELD_ALT = "\\fldalt".getBytes(); 104 107 private static final byte[] FIELD_INSTRUCTIONS = "\\*\\fldinst".getBytes(); 108 111 private static final byte[] FIELD_RESULT = "\\fldrslt".getBytes(); 112 113 116 private boolean fieldDirty = false; 117 120 private boolean fieldEdit = false; 121 124 private boolean fieldLocked = false; 125 128 private boolean fieldPrivate = false; 129 132 private boolean fieldAlt = false; 133 136 private boolean inTable = false; 137 140 private boolean inHeader = false; 141 144 protected RtfDocument document = null; 145 148 private RtfFont font = null; 149 150 157 protected RtfField(RtfDocument doc) { 158 this(doc, new Font()); 159 } 160 161 169 protected RtfField(RtfDocument doc, Font font) { 170 super("", font); 171 this.document = doc; 172 this.font = new RtfFont(this.document, font); 173 } 174 175 180 public void setRtfDocument(RtfDocument doc) { 181 this.document = doc; 182 this.font.setRtfDocument(this.document); 183 } 184 185 192 private byte[] writeFieldBegin() throws IOException 193 { 194 ByteArrayOutputStream result = new ByteArrayOutputStream (); 195 writeFieldBegin(result); 196 return result.toByteArray(); 197 } 198 204 private void writeFieldBegin(OutputStream result) throws IOException 205 { 206 result.write(OPEN_GROUP); 207 result.write(FIELD); 208 if(fieldDirty) result.write(FIELD_DIRTY); 209 if(fieldEdit) result.write(FIELD_EDIT); 210 if(fieldLocked) result.write(FIELD_LOCKED); 211 if(fieldPrivate) result.write(FIELD_PRIVATE); 212 } 213 214 221 private byte[] writeFieldInstBegin() throws IOException 222 { 223 ByteArrayOutputStream result = new ByteArrayOutputStream (); 224 writeFieldInstBegin(result); 225 return result.toByteArray(); 226 } 227 233 private void writeFieldInstBegin(OutputStream result) throws IOException 234 { 235 result.write(OPEN_GROUP); 236 result.write(FIELD_INSTRUCTIONS); 237 result.write(DELIMITER); 238 } 239 240 248 protected abstract byte[] writeFieldInstContent() throws IOException ; 249 250 254 protected void writeFieldInstContent(OutputStream out) throws IOException 255 { 256 byte[] b = writeFieldInstContent(); 257 if(b != null) out.write(b); 258 } 259 260 267 private byte[] writeFieldInstEnd() throws IOException 268 { 269 ByteArrayOutputStream result = new ByteArrayOutputStream (); 270 writeFieldInstEnd(result); 271 return result.toByteArray(); 272 } 273 276 private void writeFieldInstEnd(OutputStream result) throws IOException 277 { 278 if(fieldAlt) { 279 result.write(DELIMITER); 280 result.write(FIELD_ALT); 281 } 282 result.write(CLOSE_GROUP); 283 } 284 285 292 private byte[] writeFieldResultBegin() throws IOException 293 { 294 ByteArrayOutputStream result = new ByteArrayOutputStream (); 295 writeFieldResultBegin(result); 296 return result.toByteArray(); 297 } 298 301 private void writeFieldResultBegin(final OutputStream result) throws IOException 302 { 303 result.write(OPEN_GROUP); 304 result.write(FIELD_RESULT); 305 result.write(DELIMITER); 306 } 307 308 316 protected abstract byte[] writeFieldResultContent() throws IOException ; 317 321 protected void writeFieldResultContent(OutputStream out) throws IOException 322 { 323 byte[] b = writeFieldResultContent(); 324 if(b != null) out.write(b); 325 } 326 327 334 private byte[] writeFieldResultEnd() throws IOException 335 { 336 ByteArrayOutputStream result = new ByteArrayOutputStream (); 337 writeFieldResultEnd(result); 338 return result.toByteArray(); 339 } 340 343 private void writeFieldResultEnd(final OutputStream result) throws IOException 344 { 345 result.write(DELIMITER); 346 result.write(CLOSE_GROUP); 347 } 348 349 356 private byte[] writeFieldEnd() throws IOException 357 { 358 ByteArrayOutputStream result = new ByteArrayOutputStream (); 359 writeFieldEnd(result); 360 return result.toByteArray(); 361 } 362 365 private void writeFieldEnd(OutputStream result) throws IOException 366 { 367 result.write(CLOSE_GROUP); 368 } 369 370 371 377 public byte[] write() 378 { 379 ByteArrayOutputStream result = new ByteArrayOutputStream (); 380 try { 381 writeContent(result); 382 } catch(IOException ioe) { 383 ioe.printStackTrace(); 384 } 385 return result.toByteArray(); 386 } 387 390 public void writeContent(final OutputStream result) throws IOException 391 { 392 result.write(this.font.writeBegin()); 393 394 writeFieldBegin(result); 403 writeFieldInstBegin(result); 404 writeFieldInstContent(result); 405 writeFieldInstEnd(result); 406 writeFieldResultBegin(result); 407 writeFieldResultContent(result); 408 writeFieldResultEnd(result); 409 writeFieldEnd(result); 410 411 result.write(this.font.writeEnd()); 412 } 413 414 419 public boolean isFieldAlt() { 420 return fieldAlt; 421 } 422 423 428 public void setFieldAlt(boolean fieldAlt) { 429 this.fieldAlt = fieldAlt; 430 } 431 432 437 public boolean isFieldDirty() { 438 return fieldDirty; 439 } 440 441 446 public void setFieldDirty(boolean fieldDirty) { 447 this.fieldDirty = fieldDirty; 448 } 449 450 455 public boolean isFieldEdit() { 456 return fieldEdit; 457 } 458 459 464 public void setFieldEdit(boolean fieldEdit) { 465 this.fieldEdit = fieldEdit; 466 } 467 468 473 public boolean isFieldLocked() { 474 return fieldLocked; 475 } 476 477 481 public void setFieldLocked(boolean fieldLocked) { 482 this.fieldLocked = fieldLocked; 483 } 484 485 490 public boolean isFieldPrivate() { 491 return fieldPrivate; 492 } 493 494 499 public void setFieldPrivate(boolean fieldPrivate) { 500 this.fieldPrivate = fieldPrivate; 501 } 502 503 508 public void setInTable(boolean inTable) { 509 this.inTable = inTable; 510 } 511 512 517 public void setInHeader(boolean inHeader) { 518 this.inHeader = inHeader; 519 } 520 521 524 public boolean isEmpty() { 525 return false; 526 } 527 528 531 public void setFont(Font font) { 532 super.setFont(font); 533 this.font = new RtfFont(this.document, font); 534 } 535 } 536 | Popular Tags |