1 50 51 package com.lowagie.text.rtf.document; 52 53 import java.io.IOException ; 54 import java.io.OutputStream ; 55 import java.util.ArrayList ; 56 57 import com.lowagie.text.rtf.RtfBasicElement; 58 import com.lowagie.text.rtf.RtfElement; 59 import com.lowagie.text.rtf.RtfMapper; 60 import com.lowagie.text.rtf.document.output.*; 61 import com.lowagie.text.rtf.graphic.RtfImage; 62 63 72 public class RtfDocument extends RtfElement { 73 76 private RtfDataCache data = null; 77 80 private RtfMapper mapper = null; 81 84 private RtfDocumentHeader documentHeader = null; 85 88 private ArrayList previousRandomInts = null; 89 92 private boolean autogenerateTOCEntries = false; 93 96 private RtfDocumentSettings documentSettings = null; 97 100 private RtfBasicElement lastElementWritten = null; 101 102 105 private static final byte[] RTF_DOCUMENT = "\\rtf1".getBytes(); 106 107 private final static byte[] FSC_LINE = "\\line ".getBytes(); 108 private final static byte[] FSC_PAR = "\\par ".getBytes(); 109 private final static byte[] FSC_TAB = "\\tab ".getBytes(); 110 private final static byte[] FSC_PAGE_PAR = "\\page\\par ".getBytes(); 111 private final static byte[] FSC_NEWPAGE = "$newpage$".getBytes(); 112 private final static byte[] FSC_BACKSLASH = "\\".getBytes(); 113 private final static byte[] FSC_HEX_PREFIX = "\\\'".getBytes(); 114 private final static byte[] FSC_UNI_PREFIX = "\\u".getBytes(); 115 116 119 public RtfDocument() { 120 super(null); 121 this.data = new RtfMemoryCache(); 122 this.mapper = new RtfMapper(this); 123 this.documentHeader = new RtfDocumentHeader(this); 124 this.documentHeader.init(); 125 this.previousRandomInts = new ArrayList (); 126 this.documentSettings = new RtfDocumentSettings(this); 127 } 128 129 133 public byte[] write() 134 { 135 return(new byte[0]); 136 } 137 140 public void writeContent(final OutputStream out) throws IOException 141 { 142 } 143 144 149 public void writeDocument(OutputStream out) { 150 try { 151 out.write(OPEN_GROUP); 152 out.write(RtfDocument.RTF_DOCUMENT); 153 this.documentHeader.writeContent(out); 155 this.data.writeTo(out); 156 out.write(CLOSE_GROUP); 157 } catch(IOException ioe) { 158 ioe.printStackTrace(); 159 } 160 } 161 162 167 public void open() { 168 try { 169 switch(this.documentSettings.getDataCacheStyle()) { 170 case RtfDataCache.CACHE_MEMORY_EFFICIENT: 171 this.data = new RtfEfficientMemoryCache(); 172 break; 173 case RtfDataCache.CACHE_MEMORY: 174 this.data = new RtfMemoryCache(); 175 break; 176 case RtfDataCache.CACHE_DISK: 177 this.data = new RtfDiskCache(); 178 break; 179 default: 180 throw(new RuntimeException ("unknown")); 181 } 182 183 } catch(IOException ioe) { 184 System.err.println("Could not initialise disk cache. Using memory cache."); 185 ioe.printStackTrace(); 186 this.data = new RtfMemoryCache(); 187 } 188 } 189 190 195 public void add(RtfBasicElement element) { 196 try { 197 if(element instanceof RtfInfoElement) { 198 this.documentHeader.addInfoElement((RtfInfoElement) element); 199 } else { 200 if(element instanceof RtfImage) { 201 ((RtfImage) element).setTopLevelElement(true); 202 } 203 element.writeContent( this.data.getOutputStream() ); 204 this.lastElementWritten = element; 205 } 206 } catch(IOException ioe) { 207 ioe.printStackTrace(); 208 } 209 } 210 211 216 public RtfMapper getMapper() { 217 return this.mapper; 218 } 219 220 225 public int getRandomInt() { 226 Integer newInt = null; 227 do { 228 newInt = new Integer ((int) (Math.random() * Integer.MAX_VALUE)); 229 } while(this.previousRandomInts.contains(newInt)); 230 this.previousRandomInts.add(newInt); 231 return newInt.intValue(); 232 } 233 234 239 public RtfDocumentHeader getDocumentHeader() { 240 return this.documentHeader; 241 } 242 243 252 public String filterSpecialChar(String str, boolean useHex, boolean softLineBreaks) { 253 if(str == null) { 254 return ""; 255 } 256 int length = str.length(); 257 int z = 'z'; 258 StringBuffer ret = new StringBuffer (length); 259 for (int i = 0; i < length; i++) { 260 char ch = str.charAt(i); 261 262 if (ch == '\\') { 263 ret.append("\\\\"); 264 } else if (ch == '\n') { 265 if(softLineBreaks) { 266 ret.append("\\line "); 267 } else { 268 ret.append("\\par "); 269 } 270 } else if (ch == '\t') { 271 ret.append("\\tab "); 272 } else if ((ch) > z && this.documentSettings.isAlwaysUseUnicode()) { 273 if(useHex) { 274 ret.append("\\\'").append(Long.toHexString(ch)); 275 } else { 276 ret.append("\\u").append((long) ch).append('?'); 277 } 278 } else { 279 ret.append(ch); 280 } 281 } 282 String s = ret.toString(); 283 if(s.indexOf("$newpage$") >= 0) { 284 String before = s.substring(0, s.indexOf("$newpage$")); 285 String after = s.substring(s.indexOf("$newpage$") + 9); 286 ret = new StringBuffer (before); 287 ret.append("\\page\\par "); 288 ret.append(after); 289 return ret.toString(); 290 } 291 return s; 292 } 293 294 304 public void filterSpecialChar(final OutputStream out, final String str, final boolean useHex, final boolean softLineBreaks) throws IOException 305 { 306 if(out == null) { 307 throw(new NullPointerException ("null OutpuStream")); 308 } 309 310 final boolean alwaysUseUniCode = this.documentSettings.isAlwaysUseUnicode(); 311 if(str == null) { 312 return; 313 } 314 final int len = str.length(); 315 if(len == 0) { 316 return; 317 } 318 319 for(int k = 0; k < len; k++) { 320 final char c = str.charAt(k); 321 if(c < 0x20) { 322 if(c == '\n') { 324 out.write(softLineBreaks ? FSC_LINE : FSC_PAR); 325 } else 326 if(c == '\t') { 327 out.write(FSC_TAB); 328 } else { 329 out.write('?'); 330 } 331 } else 332 if((c == '\\') || (c == '{') || (c == '}')) { 333 out.write(FSC_BACKSLASH); 335 out.write(c); 336 } else 337 if((c == '$') && (len-k >= FSC_NEWPAGE.length) && subMatch(str, k, FSC_NEWPAGE)) { 338 out.write(FSC_PAGE_PAR); 340 k += FSC_NEWPAGE.length-1; 341 } else { 342 if((c > 0xff) || ((c > 'z') && alwaysUseUniCode)) { 343 if(useHex && (c <= 0xff)) { 344 out.write(FSC_HEX_PREFIX); 346 out.write(RtfImage.byte2charLUT, c*2, 2); 347 } else { 348 out.write(FSC_UNI_PREFIX); 350 String s = Short.toString((short)c); 351 for(int x = 0; x < s.length(); x++) { 352 out.write(s.charAt(x)); 353 } 354 out.write('?'); 355 } 356 } else { 357 out.write(c); 358 } 359 } 360 } 361 } 362 371 private static boolean subMatch(final String str, int soff, final byte[] m) 372 { 373 for(int k = 0; k < m.length; k++) { 374 if(str.charAt(soff++) != m[k]) { 375 return(false); 376 } 377 } 378 return(true); 379 } 380 381 387 public void setAutogenerateTOCEntries(boolean autogenerate) { 388 this.autogenerateTOCEntries = autogenerate; 389 } 390 391 396 public boolean getAutogenerateTOCEntries() { 397 return this.autogenerateTOCEntries; 398 } 399 400 405 public RtfDocumentSettings getDocumentSettings() { 406 return this.documentSettings; 407 } 408 409 414 public RtfBasicElement getLastElementWritten() { 415 return this.lastElementWritten; 416 } 417 } | Popular Tags |