1 | /* | |
2 | * Copyright 1999-2004 The Apache Software Foundation. | |
3 | * | |
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 | |
7 | * | |
8 | * http://www.apache.org/licenses/LICENSE-2.0 | |
9 | * | |
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. | |
15 | */ | |
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 | /** | |
27 | * This class is the main entry point to the hyphenation package. | |
28 | * You can use only the static methods or create an instance. | |
29 | * | |
30 | * @author Carlos Villegas <cav@uniscope.co.jp> | |
31 | */ | |
32 | public class Hyphenator { | |
33 | | |
34 | /** TODO: Don't use statics */ | |
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 | /** Holds value of property hyphenDir. */ | |
43 | private static String hyphenDir = ""; | |
44 | ||
45 | /** | |
46 | * @param lang | |
47 | * @param country | |
48 | * @param leftMin | |
49 | * @param rightMin | |
50 | */ | |
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 | /** | |
59 | * @param lang | |
60 | * @param country | |
61 | * @return the hyphenation tree | |
62 | */ | |
63 | public static HyphenationTree getHyphenationTree(String lang, | |
64 | String country) { | |
65 | String key = lang; | |
66 | // check whether the country code has been used | |
67 |
2
1. getHyphenationTree : negated conditional → NO_COVERAGE 2. getHyphenationTree : negated conditional → NO_COVERAGE |
if (country != null && !country.equals("none")) { |
68 | key += "_" + country; | |
69 | } | |
70 | // first try to find it in the cache | |
71 |
1
1. getHyphenationTree : negated conditional → NO_COVERAGE |
if (hyphenTrees.containsKey(key)) { |
72 |
1
1. getHyphenationTree : mutated return of Object value for com/lowagie/text/pdf/hyphenation/Hyphenator::getHyphenationTree to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE |
return (HyphenationTree)hyphenTrees.get(key); |
73 | } | |
74 |
1
1. getHyphenationTree : negated conditional → NO_COVERAGE |
if (hyphenTrees.containsKey(lang)) { |
75 |
1
1. getHyphenationTree : mutated return of Object value for com/lowagie/text/pdf/hyphenation/Hyphenator::getHyphenationTree to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE |
return (HyphenationTree)hyphenTrees.get(lang); |
76 | } | |
77 | ||
78 | HyphenationTree hTree = getResourceHyphenationTree(key); | |
79 |
1
1. getHyphenationTree : negated conditional → NO_COVERAGE |
if (hTree == null) |
80 | hTree = getFileHyphenationTree(key); | |
81 | // put it into the pattern cache | |
82 |
1
1. getHyphenationTree : negated conditional → NO_COVERAGE |
if (hTree != null) { |
83 | hyphenTrees.put(key, hTree); | |
84 | } | |
85 |
1
1. getHyphenationTree : mutated return of Object value for com/lowagie/text/pdf/hyphenation/Hyphenator::getHyphenationTree to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE |
return hTree; |
86 | } | |
87 | ||
88 | /** | |
89 | * @param key | |
90 | * @return a hyphenation tree | |
91 | */ | |
92 | public static HyphenationTree getResourceHyphenationTree(String key) { | |
93 | try { | |
94 | InputStream stream = BaseFont.getResourceStream(defaultHyphLocation + key + ".xml"); | |
95 |
3
1. getResourceHyphenationTree : changed conditional boundary → NO_COVERAGE 2. getResourceHyphenationTree : negated conditional → NO_COVERAGE 3. getResourceHyphenationTree : negated conditional → NO_COVERAGE |
if (stream == null && key.length() > 2) |
96 | stream = BaseFont.getResourceStream(defaultHyphLocation + key.substring(0, 2) + ".xml"); | |
97 |
1
1. getResourceHyphenationTree : negated conditional → NO_COVERAGE |
if (stream == null) |
98 |
1
1. getResourceHyphenationTree : mutated return of Object value for com/lowagie/text/pdf/hyphenation/Hyphenator::getResourceHyphenationTree to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE |
return null; |
99 | HyphenationTree hTree = new HyphenationTree(); | |
100 |
1
1. getResourceHyphenationTree : removed call to com/lowagie/text/pdf/hyphenation/HyphenationTree::loadSimplePatterns → NO_COVERAGE |
hTree.loadSimplePatterns(stream); |
101 |
1
1. getResourceHyphenationTree : mutated return of Object value for com/lowagie/text/pdf/hyphenation/Hyphenator::getResourceHyphenationTree to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE |
return hTree; |
102 | } | |
103 | catch (Exception e) { | |
104 |
1
1. getResourceHyphenationTree : mutated return of Object value for com/lowagie/text/pdf/hyphenation/Hyphenator::getResourceHyphenationTree to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE |
return null; |
105 | } | |
106 | } | |
107 | ||
108 | /** | |
109 | * @param key | |
110 | * @return a hyphenation tree | |
111 | */ | |
112 | public static HyphenationTree getFileHyphenationTree(String key) { | |
113 | try { | |
114 |
1
1. getFileHyphenationTree : negated conditional → NO_COVERAGE |
if (hyphenDir == null) |
115 |
1
1. getFileHyphenationTree : mutated return of Object value for com/lowagie/text/pdf/hyphenation/Hyphenator::getFileHyphenationTree to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE |
return null; |
116 | InputStream stream = null; | |
117 | File hyphenFile = new File(hyphenDir, key + ".xml"); | |
118 |
1
1. getFileHyphenationTree : negated conditional → NO_COVERAGE |
if (hyphenFile.canRead()) |
119 | stream = new FileInputStream(hyphenFile); | |
120 |
3
1. getFileHyphenationTree : changed conditional boundary → NO_COVERAGE 2. getFileHyphenationTree : negated conditional → NO_COVERAGE 3. getFileHyphenationTree : negated conditional → NO_COVERAGE |
if (stream == null && key.length() > 2) { |
121 | hyphenFile = new File(hyphenDir, key.substring(0, 2) + ".xml"); | |
122 |
1
1. getFileHyphenationTree : negated conditional → NO_COVERAGE |
if (hyphenFile.canRead()) |
123 | stream = new FileInputStream(hyphenFile); | |
124 | } | |
125 |
1
1. getFileHyphenationTree : negated conditional → NO_COVERAGE |
if (stream == null) |
126 |
1
1. getFileHyphenationTree : mutated return of Object value for com/lowagie/text/pdf/hyphenation/Hyphenator::getFileHyphenationTree to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE |
return null; |
127 | HyphenationTree hTree = new HyphenationTree(); | |
128 |
1
1. getFileHyphenationTree : removed call to com/lowagie/text/pdf/hyphenation/HyphenationTree::loadSimplePatterns → NO_COVERAGE |
hTree.loadSimplePatterns(stream); |
129 |
1
1. getFileHyphenationTree : mutated return of Object value for com/lowagie/text/pdf/hyphenation/Hyphenator::getFileHyphenationTree to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE |
return hTree; |
130 | } | |
131 | catch (Exception e) { | |
132 |
1
1. getFileHyphenationTree : mutated return of Object value for com/lowagie/text/pdf/hyphenation/Hyphenator::getFileHyphenationTree to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE |
return null; |
133 | } | |
134 | } | |
135 | ||
136 | /** | |
137 | * @param lang | |
138 | * @param country | |
139 | * @param word | |
140 | * @param leftMin | |
141 | * @param rightMin | |
142 | * @return a hyphenation object | |
143 | */ | |
144 | public static Hyphenation hyphenate(String lang, String country, | |
145 | String word, int leftMin, | |
146 | int rightMin) { | |
147 | HyphenationTree hTree = getHyphenationTree(lang, country); | |
148 |
1
1. hyphenate : negated conditional → NO_COVERAGE |
if (hTree == null) { |
149 | //log.error("Error building hyphenation tree for language " | |
150 | // + lang); | |
151 |
1
1. hyphenate : mutated return of Object value for com/lowagie/text/pdf/hyphenation/Hyphenator::hyphenate to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE |
return null; |
152 | } | |
153 |
1
1. hyphenate : mutated return of Object value for com/lowagie/text/pdf/hyphenation/Hyphenator::hyphenate to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE |
return hTree.hyphenate(word, leftMin, rightMin); |
154 | } | |
155 | ||
156 | /** | |
157 | * @param lang | |
158 | * @param country | |
159 | * @param word | |
160 | * @param offset | |
161 | * @param len | |
162 | * @param leftMin | |
163 | * @param rightMin | |
164 | * @return a hyphenation object | |
165 | */ | |
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 |
1
1. hyphenate : negated conditional → NO_COVERAGE |
if (hTree == null) { |
171 | //log.error("Error building hyphenation tree for language " | |
172 | // + lang); | |
173 |
1
1. hyphenate : mutated return of Object value for com/lowagie/text/pdf/hyphenation/Hyphenator::hyphenate to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE |
return null; |
174 | } | |
175 |
1
1. hyphenate : mutated return of Object value for com/lowagie/text/pdf/hyphenation/Hyphenator::hyphenate to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE |
return hTree.hyphenate(word, offset, len, leftMin, rightMin); |
176 | } | |
177 | ||
178 | /** | |
179 | * @param min | |
180 | */ | |
181 | public void setMinRemainCharCount(int min) { | |
182 | remainCharCount = min; | |
183 | } | |
184 | ||
185 | /** | |
186 | * @param min | |
187 | */ | |
188 | public void setMinPushCharCount(int min) { | |
189 | pushCharCount = min; | |
190 | } | |
191 | ||
192 | /** | |
193 | * @param lang | |
194 | * @param country | |
195 | */ | |
196 | public void setLanguage(String lang, String country) { | |
197 | hyphenTree = getHyphenationTree(lang, country); | |
198 | } | |
199 | ||
200 | /** | |
201 | * @param word | |
202 | * @param offset | |
203 | * @param len | |
204 | * @return a hyphenation object | |
205 | */ | |
206 | public Hyphenation hyphenate(char[] word, int offset, int len) { | |
207 |
1
1. hyphenate : negated conditional → NO_COVERAGE |
if (hyphenTree == null) { |
208 |
1
1. hyphenate : mutated return of Object value for com/lowagie/text/pdf/hyphenation/Hyphenator::hyphenate to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE |
return null; |
209 | } | |
210 |
1
1. hyphenate : mutated return of Object value for com/lowagie/text/pdf/hyphenation/Hyphenator::hyphenate to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE |
return hyphenTree.hyphenate(word, offset, len, remainCharCount, |
211 | pushCharCount); | |
212 | } | |
213 | ||
214 | /** | |
215 | * @param word | |
216 | * @return a hyphenation object | |
217 | */ | |
218 | public Hyphenation hyphenate(String word) { | |
219 |
1
1. hyphenate : negated conditional → NO_COVERAGE |
if (hyphenTree == null) { |
220 |
1
1. hyphenate : mutated return of Object value for com/lowagie/text/pdf/hyphenation/Hyphenator::hyphenate to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE |
return null; |
221 | } | |
222 |
1
1. hyphenate : mutated return of Object value for com/lowagie/text/pdf/hyphenation/Hyphenator::hyphenate to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE |
return hyphenTree.hyphenate(word, remainCharCount, pushCharCount); |
223 | } | |
224 | ||
225 | /** Getter for property hyphenDir. | |
226 | * @return Value of property hyphenDir. | |
227 | */ | |
228 | public static String getHyphenDir() { | |
229 | return hyphenDir; | |
230 | } | |
231 | | |
232 | /** Setter for property hyphenDir. | |
233 | * @param _hyphenDir New value of property hyphenDir. | |
234 | */ | |
235 | public static void setHyphenDir(String _hyphenDir) { | |
236 | hyphenDir = _hyphenDir; | |
237 | } | |
238 | | |
239 | } | |
Mutations | ||
67 |
1.1 2.2 |
|
71 |
1.1 |
|
72 |
1.1 |
|
74 |
1.1 |
|
75 |
1.1 |
|
79 |
1.1 |
|
82 |
1.1 |
|
85 |
1.1 |
|
95 |
1.1 2.2 3.3 |
|
97 |
1.1 |
|
98 |
1.1 |
|
100 |
1.1 |
|
101 |
1.1 |
|
104 |
1.1 |
|
114 |
1.1 |
|
115 |
1.1 |
|
118 |
1.1 |
|
120 |
1.1 2.2 3.3 |
|
122 |
1.1 |
|
125 |
1.1 |
|
126 |
1.1 |
|
128 |
1.1 |
|
129 |
1.1 |
|
132 |
1.1 |
|
148 |
1.1 |
|
151 |
1.1 |
|
153 |
1.1 |
|
170 |
1.1 |
|
173 |
1.1 |
|
175 |
1.1 |
|
207 |
1.1 |
|
208 |
1.1 |
|
210 |
1.1 |
|
219 |
1.1 |
|
220 |
1.1 |
|
222 |
1.1 |