Skip to content

Commit 324d733

Browse files
committed
Minor refactoring in lyout.hyphenation subpackage.
Preparing for itextsharp
1 parent 7d63a17 commit 324d733

File tree

6 files changed

+56
-253
lines changed

6 files changed

+56
-253
lines changed

layout/src/main/java/com/itextpdf/layout/hyphenation/CharVector.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
*
2626
* <p>This work was authored by Carlos Villegas ([email protected]).</p>
2727
*/
28-
public class CharVector implements Cloneable, Serializable {
28+
public class CharVector implements Serializable {
2929

3030
private static final long serialVersionUID = 4263472982169004048L;
3131

@@ -91,20 +91,19 @@ public CharVector(char[] a, int capacity) {
9191
n = a.length;
9292
}
9393

94+
public CharVector(CharVector cv) {
95+
this.array = (char[])cv.array.clone();
96+
this.blockSize = cv.blockSize;
97+
this.n = cv.n;
98+
}
99+
94100
/**
95101
* Reset length of vector, but don't clear contents.
96102
*/
97103
public void clear() {
98104
n = 0;
99105
}
100106

101-
/** {@inheritDoc} */
102-
public Object clone() throws CloneNotSupportedException {
103-
CharVector cv = (CharVector) super.clone();
104-
cv.array = array.clone();
105-
return cv;
106-
}
107-
108107
/**
109108
* Obtain char vector array.
110109
* @return char array
@@ -174,5 +173,4 @@ public void trimToSize() {
174173
array = aux;
175174
}
176175
}
177-
178176
}

layout/src/main/java/com/itextpdf/layout/hyphenation/HyphenationTree.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,19 @@
1717

1818
package com.itextpdf.layout.hyphenation;
1919

20+
import java.io.BufferedInputStream;
2021
import java.io.File;
2122
import java.io.IOException;
23+
import java.io.InputStream;
2224
import java.io.ObjectInputStream;
2325
import java.net.MalformedURLException;
26+
import java.net.URL;
2427
import java.util.ArrayList;
2528
import java.util.Collections;
2629
import java.util.HashMap;
2730
import java.util.List;
2831
import java.util.Map;
2932

30-
import org.xml.sax.InputSource;
31-
3233
/**
3334
* <p>This tree structure stores the hyphenation patterns in an efficient
3435
* way for fast lookup. It provides the provides the method to
@@ -130,24 +131,27 @@ protected String unpackValues(int k) {
130131
public void loadPatterns(String filename) throws HyphenationException {
131132
File f = new File(filename);
132133
try {
133-
InputSource src = new InputSource(f.toURI().toURL().toExternalForm());
134-
loadPatterns(src);
134+
URL url = f.toURI().toURL();
135+
loadPatterns(new BufferedInputStream(url.openStream()), url.toExternalForm());
135136
} catch (MalformedURLException e) {
136137
throw new HyphenationException("Error converting the File '" + f + "' to a URL: "
137138
+ e.getMessage());
139+
} catch (IOException e) {
140+
throw new HyphenationException("Error opening the File '" + f + "'");
138141
}
139142
}
140143

141144
/**
142145
* Read hyphenation patterns from an XML file.
143-
* @param source the InputSource for the file
146+
* @param stream the InputSource for the file
147+
* @param name unique key representing country-language combination
144148
* @throws HyphenationException In case the parsing fails
145149
*/
146-
public void loadPatterns(InputSource source) throws HyphenationException {
150+
public void loadPatterns(InputStream stream, String name) throws HyphenationException {
147151
PatternParser pp = new PatternParser(this);
148152
ivalues = new TernaryTree();
149153

150-
pp.parse(source);
154+
pp.parse(stream, name);
151155

152156
// patterns/values should be now in the tree
153157
// let's optimize a bit

layout/src/main/java/com/itextpdf/layout/hyphenation/HyphenationTreeCache.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818

1919
package com.itextpdf.layout.hyphenation;
2020

21-
import java.util.Hashtable;
21+
import java.util.HashMap;
22+
import java.util.HashSet;
2223
import java.util.Map;
2324
import java.util.Set;
2425

@@ -28,9 +29,9 @@
2829
public class HyphenationTreeCache {
2930

3031
/** Contains the cached hyphenation trees */
31-
private Hashtable hyphenTrees = new Hashtable();
32+
private Map<String, HyphenationTree> hyphenTrees = new HashMap<>();
3233
/** Used to avoid multiple error messages for the same language if a pattern file is missing. */
33-
private Set missingHyphenationTrees;
34+
private Set<String> missingHyphenationTrees;
3435

3536
/**
3637
* Looks in the cache if a hyphenation tree is available and returns it if it is found.
@@ -47,9 +48,9 @@ public HyphenationTree getHyphenationTree(String lang, String country) {
4748

4849
// first try to find it in the cache
4950
if (hyphenTrees.containsKey(key)) {
50-
return (HyphenationTree)hyphenTrees.get(key);
51+
return hyphenTrees.get(key);
5152
} else if (hyphenTrees.containsKey(lang)) {
52-
return (HyphenationTree)hyphenTrees.get(lang);
53+
return hyphenTrees.get(lang);
5354
} else {
5455
return null;
5556
}
@@ -83,7 +84,7 @@ public static String constructUserKey(String lang, String country, Map<String, S
8384
if (hyphPatNames != null) {
8485
String key = constructLlccKey(lang, country);
8586
key = key.replace('_', '-');
86-
userKey = (String) hyphPatNames.get(key);
87+
userKey = hyphPatNames.get(key);
8788
}
8889
return userKey;
8990
}
@@ -105,7 +106,7 @@ public void cache(String key, HyphenationTree hTree) {
105106
*/
106107
public void noteMissing(String key) {
107108
if (missingHyphenationTrees == null) {
108-
missingHyphenationTrees = new java.util.HashSet();
109+
missingHyphenationTrees = new HashSet<>();
109110
}
110111
missingHyphenationTrees.add(key);
111112
}
@@ -120,5 +121,4 @@ public void noteMissing(String key) {
120121
public boolean isMissing(String key) {
121122
return (missingHyphenationTrees != null && missingHyphenationTrees.contains(key));
122123
}
123-
124124
}

layout/src/main/java/com/itextpdf/layout/hyphenation/Hyphenator.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
package com.itextpdf.layout.hyphenation;
1919

20-
import java.io.BufferedInputStream;
2120
import java.io.File;
2221
import java.io.FileInputStream;
2322
import java.io.IOException;
@@ -29,7 +28,6 @@
2928
import com.itextpdf.io.util.ResourceUtil;
3029
import org.slf4j.Logger;
3130
import org.slf4j.LoggerFactory;
32-
import org.xml.sax.InputSource;
3331

3432
/**
3533
* <p>This class is the main entry point to the hyphenation package.
@@ -198,7 +196,7 @@ public static HyphenationTree getHyphenationTree2(String lang, String country, M
198196

199197
if (additionalHyphenationFileDirectories != null) {
200198
for (String dir : additionalHyphenationFileDirectories) {
201-
hTree = getHyphenationTree(new File(dir), key);
199+
hTree = getHyphenationTree(dir, key);
202200
if (hTree != null) {
203201
break;
204202
}
@@ -228,11 +226,11 @@ public static HyphenationTree getHyphenationTree2(String lang, String country, M
228226
* @param key language key for the requested hyphenation file
229227
* @return the requested HyphenationTree or null if it is not available
230228
*/
231-
public static HyphenationTree getHyphenationTree(File searchDirectory, String key) {
229+
public static HyphenationTree getHyphenationTree(String searchDirectory, String key) {
232230
// try the raw XML file
233231
String name = key + ".xml";
234232
try {
235-
InputStream fis = new FileInputStream(new File(searchDirectory, name));
233+
InputStream fis = new FileInputStream(new File(new File(searchDirectory), name));
236234
return getHyphenationTree(fis, name);
237235
} catch (IOException ioe) {
238236
if (log.isDebugEnabled()) {
@@ -255,10 +253,8 @@ public static HyphenationTree getHyphenationTree(InputStream in, String name) {
255253
}
256254
HyphenationTree hTree;
257255
try {
258-
InputSource src = new InputSource(new BufferedInputStream(in));
259-
src.setSystemId(name);
260256
hTree = new HyphenationTree();
261-
hTree.loadPatterns(src);
257+
hTree.loadPatterns(in, name);
262258
}
263259
catch (HyphenationException ex) {
264260
log.error("Can't load user patterns from XML file " + name + ": " + ex.getMessage());

layout/src/main/java/com/itextpdf/layout/hyphenation/PatternParser.java

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.io.IOException;
2323
import java.io.InputStream;
2424
import java.net.MalformedURLException;
25+
import java.net.URL;
2526
import java.util.ArrayList;
2627

2728
import javax.xml.parsers.SAXParserFactory;
@@ -83,30 +84,27 @@ public PatternParser(IPatternConsumer consumer) throws HyphenationException {
8384
* @throws HyphenationException In case of an exception while parsing
8485
*/
8586
public void parse(String filename) throws HyphenationException {
86-
parse(new File(filename));
87-
}
88-
89-
/**
90-
* Parses a hyphenation pattern file.
91-
* @param file the pattern file
92-
* @throws HyphenationException In case of an exception while parsing
93-
*/
94-
public void parse(File file) throws HyphenationException {
9587
try {
96-
InputSource src = new InputSource(file.toURI().toURL().toExternalForm());
97-
parse(src);
88+
URL url = new File(filename).toURI().toURL();
89+
parse(url.openStream(), url.toExternalForm());
9890
} catch (MalformedURLException e) {
99-
throw new HyphenationException("Error converting the File '" + file + "' to a URL: "
91+
throw new HyphenationException("Error converting the File '" + filename + "' to a URL: "
10092
+ e.getMessage());
93+
} catch (IOException e) {
94+
throw new HyphenationException("Error opening the File '" + filename + "'");
10195
}
10296
}
10397

10498
/**
10599
* Parses a hyphenation pattern file.
106-
* @param source the InputSource for the file
100+
* @param stream the InputStream for the file
101+
* @param name unique key representing country-language combination
102+
*
107103
* @throws HyphenationException In case of an exception while parsing
108104
*/
109-
public void parse(InputSource source) throws HyphenationException {
105+
public void parse(InputStream stream, String name) throws HyphenationException {
106+
InputSource source = new InputSource(stream);
107+
source.setSystemId(name);
110108
try {
111109
parser.parse(source);
112110
} catch (FileNotFoundException fnfe) {

0 commit comments

Comments
 (0)