2
* Copyright 1999-2004 The Apache Software Foundation.
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
8
* http://www.apache.org/licenses/LICENSE-2.0
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
17
package com.lowagie.text.pdf.hyphenation;
20
import java.io.FileInputStream;
21
import java.io.InputStream;
22
import java.util.Hashtable;
24
import com.lowagie.text.pdf.BaseFont;
27
* This class is the main entry point to the hyphenation package.
28
* You can use only the static methods or create an instance.
30
* @author Carlos Villegas <cav@uniscope.co.jp>
32
public class Hyphenator {
34
/** TODO: Don't use statics */
35
private static Hashtable hyphenTrees = new Hashtable();
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/";
42
/** Holds value of property hyphenDir. */
43
private static String hyphenDir = "";
51
public Hyphenator(String lang, String country, int leftMin,
53
hyphenTree = getHyphenationTree(lang, country);
54
remainCharCount = leftMin;
55
pushCharCount = rightMin;
61
* @return the hyphenation tree
63
public static HyphenationTree getHyphenationTree(String lang,
66
// check whether the country code has been used
67
if (country != null && !country.equals("none")) {
70
// first try to find it in the cache
71
if (hyphenTrees.containsKey(key)) {
72
return (HyphenationTree)hyphenTrees.get(key);
74
if (hyphenTrees.containsKey(lang)) {
75
return (HyphenationTree)hyphenTrees.get(lang);
78
HyphenationTree hTree = getResourceHyphenationTree(key);
80
hTree = getFileHyphenationTree(key);
81
// put it into the pattern cache
83
hyphenTrees.put(key, hTree);
90
* @return a hyphenation tree
92
public static HyphenationTree getResourceHyphenationTree(String key) {
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");
99
HyphenationTree hTree = new HyphenationTree();
100
hTree.loadSimplePatterns(stream);
103
catch (Exception e) {
110
* @return a hyphenation tree
112
public static HyphenationTree getFileHyphenationTree(String key) {
114
if (hyphenDir == 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);
127
HyphenationTree hTree = new HyphenationTree();
128
hTree.loadSimplePatterns(stream);
131
catch (Exception e) {
142
* @return a hyphenation object
144
public static Hyphenation hyphenate(String lang, String country,
145
String word, int leftMin,
147
HyphenationTree hTree = getHyphenationTree(lang, country);
149
//log.error("Error building hyphenation tree for language "
153
return hTree.hyphenate(word, leftMin, rightMin);
164
* @return a hyphenation object
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);
171
//log.error("Error building hyphenation tree for language "
175
return hTree.hyphenate(word, offset, len, leftMin, rightMin);
181
public void setMinRemainCharCount(int min) {
182
remainCharCount = min;
188
public void setMinPushCharCount(int min) {
196
public void setLanguage(String lang, String country) {
197
hyphenTree = getHyphenationTree(lang, country);
204
* @return a hyphenation object
206
public Hyphenation hyphenate(char[] word, int offset, int len) {
207
if (hyphenTree == null) {
210
return hyphenTree.hyphenate(word, offset, len, remainCharCount,
216
* @return a hyphenation object
218
public Hyphenation hyphenate(String word) {
219
if (hyphenTree == null) {
222
return hyphenTree.hyphenate(word, remainCharCount, pushCharCount);
225
/** Getter for property hyphenDir.
226
* @return Value of property hyphenDir.
228
public static String getHyphenDir() {
232
/** Setter for property hyphenDir.
233
* @param _hyphenDir New value of property hyphenDir.
235
public static void setHyphenDir(String _hyphenDir) {
236
hyphenDir = _hyphenDir;