You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Hyphenator.java 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /* $Id$
  2. * Copyright (C) 2001 The Apache Software Foundation. All rights reserved.
  3. * For details on use and redistribution please refer to the
  4. * LICENSE file included with these sources.
  5. */
  6. package org.apache.fop.layout.hyphenation;
  7. import java.io.*;
  8. import java.util.Hashtable;
  9. import org.apache.fop.configuration.*;
  10. import org.apache.fop.messaging.MessageHandler;
  11. /**
  12. * This class is the main entry point to the hyphenation package.
  13. * You can use only the static methods or create an instance.
  14. *
  15. * @author Carlos Villegas <cav@uniscope.co.jp>
  16. */
  17. public class Hyphenator {
  18. static Hashtable hyphenTrees = new Hashtable();
  19. private HyphenationTree hyphenTree = null;
  20. private int remainCharCount = 2;
  21. private int pushCharCount = 2;
  22. private static boolean errorDump = false;
  23. public Hyphenator(String lang, String country, int leftMin,
  24. int rightMin) {
  25. hyphenTree = getHyphenationTree(lang, country);
  26. remainCharCount = leftMin;
  27. pushCharCount = rightMin;
  28. }
  29. public static HyphenationTree getHyphenationTree(String lang,
  30. String country) {
  31. String key = lang;
  32. //check whether the country code has been used
  33. if (country != null && !country.equals("none"))
  34. key += "_" + country;
  35. // first try to find it in the cache
  36. if (hyphenTrees.containsKey(key))
  37. return (HyphenationTree) hyphenTrees.get(key);
  38. if (hyphenTrees.containsKey(lang))
  39. return (HyphenationTree) hyphenTrees.get(lang);
  40. HyphenationTree hTree = getFopHyphenationTree(key);
  41. if (hTree == null) {
  42. String hyphenDir = Configuration.getStringValue("hyphenation-dir");
  43. if (hyphenDir != null){
  44. hTree = getUserHyphenationTree(key,hyphenDir);
  45. }
  46. }
  47. //put it into the pattern cache
  48. if (hTree != null) {
  49. hyphenTrees.put(key, hTree);
  50. } else {
  51. MessageHandler.errorln("Couldn't find hyphenation pattern " + key);
  52. }
  53. return hTree;
  54. }
  55. public static HyphenationTree getFopHyphenationTree (String key) {
  56. HyphenationTree hTree = null;
  57. ObjectInputStream ois = null;
  58. InputStream is = null;
  59. try {
  60. is = Hyphenator.class.getResourceAsStream("/hyph/" + key + ".hyp");
  61. if (is == null) {
  62. if (key.length() == 5) {
  63. is = Hyphenator.class.getResourceAsStream("/hyph/" +
  64. key.substring(0,2) + ".hyp");
  65. if (is != null) {
  66. MessageHandler.errorln(
  67. "Couldn't find hyphenation pattern " + key
  68. + "\nusing general language pattern " +
  69. key.substring(0,2) + " instead.");
  70. } else {
  71. if (errorDump){
  72. MessageHandler.errorln("Couldn't find precompiled "
  73. + "fop hyphenation pattern " + key + ".hyp");
  74. }
  75. return null;
  76. }
  77. } else {
  78. if (errorDump){
  79. MessageHandler.errorln("Couldn't find precompiled "
  80. + "fop hyphenation pattern " + key + ".hyp");
  81. }
  82. return null;
  83. }
  84. }
  85. ois = new ObjectInputStream(is);
  86. hTree = (HyphenationTree) ois.readObject();
  87. } catch (Exception e) {
  88. e.printStackTrace();
  89. } finally {
  90. if (ois != null) {
  91. try {
  92. ois.close();
  93. } catch (IOException e) {
  94. MessageHandler.errorln("can't close hyphenation object stream");
  95. }
  96. }
  97. }
  98. return hTree;
  99. }
  100. /**
  101. * load tree from serialized file or xml file
  102. * using configuration settings
  103. */
  104. public static HyphenationTree getUserHyphenationTree (String key,String hyphenDir) {
  105. HyphenationTree hTree = null;
  106. // I use here the following convention. The file name specified in
  107. // the configuration is taken as the base name. First we try
  108. // name + ".hyp" assuming a serialized HyphenationTree. If that fails
  109. // we try name + ".xml", assumming a raw hyphenation pattern file.
  110. // first try serialized object
  111. File hyphenFile = new File(hyphenDir, key + ".hyp");
  112. if (hyphenFile.exists()) {
  113. ObjectInputStream ois = null;
  114. try {
  115. ois = new ObjectInputStream(
  116. new FileInputStream(hyphenFile));
  117. hTree = (HyphenationTree) ois.readObject();
  118. } catch (Exception e) {
  119. e.printStackTrace();
  120. } finally {
  121. if (ois != null) {
  122. try {
  123. ois.close();
  124. } catch (IOException e) {
  125. }
  126. }
  127. }
  128. return hTree;
  129. } else {
  130. // try the raw XML file
  131. hyphenFile = new File(hyphenDir, key + ".xml");
  132. if (hyphenFile.exists()) {
  133. hTree = new HyphenationTree();
  134. if (errorDump) {
  135. MessageHandler.errorln("reading " +
  136. hyphenDir + key +".xml");
  137. }
  138. try {
  139. hTree.loadPatterns(hyphenFile.getPath());
  140. if (errorDump) {
  141. System.out.println("Stats: ");
  142. hTree.printStats();
  143. }
  144. return hTree;
  145. } catch (HyphenationException ex) {
  146. if (errorDump) {
  147. MessageHandler.errorln("Can't load user patterns "
  148. + "from xml file " + hyphenDir + key +".xml");
  149. }
  150. return null;
  151. }
  152. } else {
  153. if (errorDump) {
  154. MessageHandler.errorln("Tried to load " +
  155. hyphenFile.toString() +
  156. "\nCannot find compiled nor xml file for "
  157. + "hyphenation pattern" + key );
  158. }
  159. return null;
  160. }
  161. }
  162. }
  163. public static Hyphenation hyphenate(String lang, String country,
  164. String word, int leftMin, int rightMin) {
  165. HyphenationTree hTree = getHyphenationTree(lang, country);
  166. if (hTree == null) {
  167. MessageHandler.errorln(
  168. "Error building hyphenation tree for language " + lang);
  169. return null;
  170. }
  171. return hTree.hyphenate(word, leftMin, rightMin);
  172. }
  173. public static Hyphenation hyphenate(String lang, String country,
  174. char[] word, int offset, int len, int leftMin, int rightMin) {
  175. HyphenationTree hTree = getHyphenationTree(lang, country);
  176. if (hTree == null) {
  177. MessageHandler.errorln(
  178. "Error building hyphenation tree for language " + lang);
  179. return null;
  180. }
  181. return hTree.hyphenate(word, offset, len, leftMin, rightMin);
  182. }
  183. public void setMinRemainCharCount(int min) {
  184. remainCharCount = min;
  185. }
  186. public void setMinPushCharCount(int min) {
  187. pushCharCount = min;
  188. }
  189. public void setLanguage(String lang, String country) {
  190. hyphenTree = getHyphenationTree(lang, country);
  191. }
  192. public Hyphenation hyphenate(char[] word, int offset, int len) {
  193. if (hyphenTree == null)
  194. return null;
  195. return hyphenTree.hyphenate(word, offset, len, remainCharCount,
  196. pushCharCount);
  197. }
  198. public Hyphenation hyphenate(String word) {
  199. if (hyphenTree == null)
  200. return null;
  201. return hyphenTree.hyphenate(word, remainCharCount, pushCharCount);
  202. }
  203. }