KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > hyphenation > HyphenationTreeCache


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 /* $Id$ */
19
20 package org.apache.fop.hyphenation;
21
22 import java.util.Hashtable JavaDoc;
23 import java.util.Set JavaDoc;
24
25 /**
26  * This is a cache for HyphenationTree instances.
27  */

28 public class HyphenationTreeCache {
29
30     /** Contains the cached hyphenation trees */
31     private Hashtable JavaDoc hyphenTrees = new Hashtable JavaDoc();
32     /** Used to avoid multiple error messages for the same language if a pattern file is missing. */
33     private Set JavaDoc missingHyphenationTrees;
34     
35     /**
36      * Looks in the cache if a hyphenation tree is available and returns it if it is found.
37      * @param lang the language
38      * @param country the country (may be null or "none")
39      * @return the HyhenationTree instance or null if it's not in the cache
40      */

41     public HyphenationTree getHyphenationTree(String JavaDoc lang, String JavaDoc country) {
42         String JavaDoc key = constructKey(lang, country);
43         
44         // first try to find it in the cache
45
if (hyphenTrees.containsKey(key)) {
46             return (HyphenationTree)hyphenTrees.get(key);
47         } else if (hyphenTrees.containsKey(lang)) {
48             return (HyphenationTree)hyphenTrees.get(lang);
49         } else {
50             return null;
51         }
52     }
53     
54     /**
55      * Constructs the key for the hyphenation pattern file.
56      * @param lang the language
57      * @param country the country (may be null or "none")
58      * @return the resulting key
59      */

60     public static String JavaDoc constructKey(String JavaDoc lang, String JavaDoc country) {
61         String JavaDoc key = lang;
62         // check whether the country code has been used
63
if (country != null && !country.equals("none")) {
64             key += "_" + country;
65         }
66         return key;
67     }
68     
69     /**
70      * Cache a hyphenation tree under its key.
71      * @param key the key (ex. "de_CH" or "en")
72      * @param hTree the hyphenation tree
73      */

74     public void cache(String JavaDoc key, HyphenationTree hTree) {
75         hyphenTrees.put(key, hTree);
76     }
77     
78     /**
79      * Notes a key to a hyphenation tree as missing.
80      * This is to avoid searching a second time for a hyphneation pattern file which is not
81      * available.
82      * @param key the key (ex. "de_CH" or "en")
83      */

84     public void noteMissing(String JavaDoc key) {
85         if (missingHyphenationTrees == null) {
86             missingHyphenationTrees = new java.util.HashSet JavaDoc();
87         }
88         missingHyphenationTrees.add(key);
89     }
90     
91     /**
92      * Indicates whether a hyphenation file has been requested before but it wasn't available.
93      * This is to avoid searching a second time for a hyphneation pattern file which is not
94      * available.
95      * @param key the key (ex. "de_CH" or "en")
96      * @return true if the hyphenation tree is unavailable
97      */

98     public boolean isMissing(String JavaDoc key) {
99         return (missingHyphenationTrees != null && missingHyphenationTrees.contains(key));
100     }
101     
102 }
103
Popular Tags