1 17 18 19 20 21 27 28 package org.apache.fop.render.rtf.rtflib.rtfdoc; 29 30 import java.util.Map ; 31 import java.util.HashMap ; 32 import java.io.IOException ; 33 import java.io.Writer ; 34 35 38 39 public class RtfStringConverter { 40 private static final RtfStringConverter INSTANCE = new RtfStringConverter(); 41 42 private static final Map SPECIAL_CHARS; 43 private static final Character DBLQUOTE = new Character ('\"'); 44 private static final Character QUOTE = new Character ('\''); 45 private static final Character SPACE = new Character (' '); 46 47 48 static { 49 SPECIAL_CHARS = new HashMap (); 50 SPECIAL_CHARS.put(new Character ('\t'), "tab"); 51 SPECIAL_CHARS.put(new Character ('\n'), "line"); 52 SPECIAL_CHARS.put(new Character ('\''), "rquote"); 53 SPECIAL_CHARS.put(new Character ('\"'), "rdblquote"); 54 SPECIAL_CHARS.put(new Character ('\\'), "\\"); 55 SPECIAL_CHARS.put(new Character ('{'), "{"); 56 SPECIAL_CHARS.put(new Character ('}'), "}"); 57 } 58 59 60 private RtfStringConverter() { 61 } 62 63 67 public static RtfStringConverter getInstance() { 68 return INSTANCE; 69 } 70 71 78 public void writeRtfString(Writer w, String str) throws IOException { 79 if (str == null) { 80 return; 81 } 82 w.write(escape(str)); 83 } 84 85 90 public String escape(String str) { 91 if (str == null) { 92 return null; 93 } 94 95 StringBuffer sb = new StringBuffer (Math.max(16, str.length())); 96 for (int i = 0; i < str.length(); i++) { 98 final Character c = new Character (str.charAt(i)); 99 Character d; 100 String replacement; 101 if (i != 0) { 102 d = new Character (str.charAt(i - 1)); 103 } else { 104 d = new Character (str.charAt(i)); 105 } 106 107 if (c.equals((Object )DBLQUOTE) && d.equals((Object )SPACE)) { 110 replacement = "ldblquote"; 111 } else if (c.equals((Object )QUOTE) && d.equals((Object )SPACE)) { 112 replacement = "lquote"; 113 } else { 114 replacement = (String )SPECIAL_CHARS.get(c); 115 } 116 117 if (replacement != null) { 118 sb.append('\\'); 120 sb.append(replacement); 121 sb.append(' '); 122 } else if (c.charValue() > 127) { 123 sb.append("\\u"); 126 sb.append(Integer.toString((int)c.charValue())); 127 sb.append("\\\'3f"); 128 } else { 129 sb.append(c.charValue()); 131 } 132 } 133 return sb.toString(); 134 } 135 136 } 137 | Popular Tags |