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.

HyphenationTreeCache.java 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. /* $Id$ */
  18. package org.apache.fop.hyphenation;
  19. import java.util.Hashtable;
  20. import java.util.Map;
  21. import java.util.Set;
  22. /**
  23. * <p>This is a cache for HyphenationTree instances.</p>
  24. */
  25. public class HyphenationTreeCache {
  26. /** Contains the cached hyphenation trees */
  27. private Hashtable hyphenTrees = new Hashtable();
  28. /** Used to avoid multiple error messages for the same language if a pattern file is missing. */
  29. private Set missingHyphenationTrees;
  30. /**
  31. * Looks in the cache if a hyphenation tree is available and returns it if it is found.
  32. * @param lang the language
  33. * @param country the country (may be null or "none")
  34. * @return the HyhenationTree instance or null if it's not in the cache
  35. */
  36. public HyphenationTree getHyphenationTree(String lang, String country) {
  37. String key = constructLlccKey(lang, country);
  38. // first try to find it in the cache
  39. if (hyphenTrees.containsKey(key)) {
  40. return (HyphenationTree)hyphenTrees.get(key);
  41. } else if (hyphenTrees.containsKey(lang)) {
  42. return (HyphenationTree)hyphenTrees.get(lang);
  43. } else {
  44. return null;
  45. }
  46. }
  47. /**
  48. * Constructs the key for the hyphenation pattern file.
  49. * @param lang the language
  50. * @param country the country (may be null or "none")
  51. * @return the resulting key
  52. */
  53. public static String constructLlccKey(String lang, String country) {
  54. String key = lang;
  55. // check whether the country code has been used
  56. if (country != null && !country.equals("none")) {
  57. key += "_" + country;
  58. }
  59. return key;
  60. }
  61. /**
  62. * If the user configured a hyphenation pattern file name
  63. * for this (lang,country) value, return it. If not, return null.
  64. * @param lang the language
  65. * @param country the country (may be null or "none")
  66. * @param hyphPatNames the map of user-configured hyphenation pattern file names
  67. * @return the hyphenation pattern file name or null
  68. */
  69. public static String constructUserKey(String lang, String country, Map hyphPatNames) {
  70. String userKey = null;
  71. if (hyphPatNames != null) {
  72. String key = constructLlccKey(lang, country);
  73. key = key.replace('_', '-');
  74. userKey = (String) hyphPatNames.get(key);
  75. }
  76. return userKey;
  77. }
  78. /**
  79. * Cache a hyphenation tree under its key.
  80. * @param key the key (ex. "de_CH" or "en")
  81. * @param hTree the hyphenation tree
  82. */
  83. public void cache(String key, HyphenationTree hTree) {
  84. hyphenTrees.put(key, hTree);
  85. }
  86. /**
  87. * Notes a key to a hyphenation tree as missing.
  88. * This is to avoid searching a second time for a hyphneation pattern file which is not
  89. * available.
  90. * @param key the key (ex. "de_CH" or "en")
  91. */
  92. public void noteMissing(String key) {
  93. if (missingHyphenationTrees == null) {
  94. missingHyphenationTrees = new java.util.HashSet();
  95. }
  96. missingHyphenationTrees.add(key);
  97. }
  98. /**
  99. * Indicates whether a hyphenation file has been requested before but it wasn't available.
  100. * This is to avoid searching a second time for a hyphneation pattern file which is not
  101. * available.
  102. * @param key the key (ex. "de_CH" or "en")
  103. * @return true if the hyphenation tree is unavailable
  104. */
  105. public boolean isMissing(String key) {
  106. return (missingHyphenationTrees != null && missingHyphenationTrees.contains(key));
  107. }
  108. }