1 16 17 package com.lowagie.text.pdf.hyphenation; 18 19 import java.io.File ; 20 import java.io.FileInputStream ; 21 import java.io.InputStream ; 22 import java.util.Hashtable ; 23 24 import com.lowagie.text.pdf.BaseFont; 25 26 32 public class Hyphenator { 33 34 35 private static Hashtable hyphenTrees = new Hashtable (); 36 37 private HyphenationTree hyphenTree = null; 38 private int remainCharCount = 2; 39 private int pushCharCount = 2; 40 private static final String defaultHyphLocation = "com/lowagie/text/pdf/hyphenation/hyph/"; 41 42 43 private static String hyphenDir = ""; 44 45 51 public Hyphenator(String lang, String country, int leftMin, 52 int rightMin) { 53 hyphenTree = getHyphenationTree(lang, country); 54 remainCharCount = leftMin; 55 pushCharCount = rightMin; 56 } 57 58 63 public static HyphenationTree getHyphenationTree(String lang, 64 String country) { 65 String key = lang; 66 if (country != null && !country.equals("none")) { 68 key += "_" + country; 69 } 70 if (hyphenTrees.containsKey(key)) { 72 return (HyphenationTree)hyphenTrees.get(key); 73 } 74 if (hyphenTrees.containsKey(lang)) { 75 return (HyphenationTree)hyphenTrees.get(lang); 76 } 77 78 HyphenationTree hTree = getResourceHyphenationTree(key); 79 if (hTree == null) 80 hTree = getFileHyphenationTree(key); 81 if (hTree != null) { 83 hyphenTrees.put(key, hTree); 84 } 85 return hTree; 86 } 87 88 92 public static HyphenationTree getResourceHyphenationTree(String key) { 93 try { 94 InputStream stream = BaseFont.getResourceStream(defaultHyphLocation + key + ".xml"); 95 if (stream == null && key.length() > 2) 96 stream = BaseFont.getResourceStream(defaultHyphLocation + key.substring(0, 2) + ".xml"); 97 if (stream == null) 98 return null; 99 HyphenationTree hTree = new HyphenationTree(); 100 hTree.loadSimplePatterns(stream); 101 return hTree; 102 } 103 catch (Exception e) { 104 return null; 105 } 106 } 107 108 112 public static HyphenationTree getFileHyphenationTree(String key) { 113 try { 114 if (hyphenDir == null) 115 return null; 116 InputStream stream = null; 117 File hyphenFile = new File (hyphenDir, key + ".xml"); 118 if (hyphenFile.canRead()) 119 stream = new FileInputStream (hyphenFile); 120 if (stream == null && key.length() > 2) { 121 hyphenFile = new File (hyphenDir, key.substring(0, 2) + ".xml"); 122 if (hyphenFile.canRead()) 123 stream = new FileInputStream (hyphenFile); 124 } 125 if (stream == null) 126 return null; 127 HyphenationTree hTree = new HyphenationTree(); 128 hTree.loadSimplePatterns(stream); 129 return hTree; 130 } 131 catch (Exception e) { 132 return null; 133 } 134 } 135 136 144 public static Hyphenation hyphenate(String lang, String country, 145 String word, int leftMin, 146 int rightMin) { 147 HyphenationTree hTree = getHyphenationTree(lang, country); 148 if (hTree == null) { 149 return null; 152 } 153 return hTree.hyphenate(word, leftMin, rightMin); 154 } 155 156 166 public static Hyphenation hyphenate(String lang, String country, 167 char[] word, int offset, int len, 168 int leftMin, int rightMin) { 169 HyphenationTree hTree = getHyphenationTree(lang, country); 170 if (hTree == null) { 171 return null; 174 } 175 return hTree.hyphenate(word, offset, len, leftMin, rightMin); 176 } 177 178 181 public void setMinRemainCharCount(int min) { 182 remainCharCount = min; 183 } 184 185 188 public void setMinPushCharCount(int min) { 189 pushCharCount = min; 190 } 191 192 196 public void setLanguage(String lang, String country) { 197 hyphenTree = getHyphenationTree(lang, country); 198 } 199 200 206 public Hyphenation hyphenate(char[] word, int offset, int len) { 207 if (hyphenTree == null) { 208 return null; 209 } 210 return hyphenTree.hyphenate(word, offset, len, remainCharCount, 211 pushCharCount); 212 } 213 214 218 public Hyphenation hyphenate(String word) { 219 if (hyphenTree == null) { 220 return null; 221 } 222 return hyphenTree.hyphenate(word, remainCharCount, pushCharCount); 223 } 224 225 228 public static String getHyphenDir() { 229 return hyphenDir; 230 } 231 232 235 public static void setHyphenDir(String _hyphenDir) { 236 hyphenDir = _hyphenDir; 237 } 238 239 } 240 | Popular Tags |