From: Simon Pepping Date: Sat, 2 Oct 2010 11:09:13 +0000 (+0000) Subject: Enable configuration of hyphenation pattern file names X-Git-Tag: fop-1_1rc1old~434 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=6d4f48908482cc16e20f90cfb239433517dd2def;p=xmlgraphics-fop.git Enable configuration of hyphenation pattern file names git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@1003774 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/src/documentation/content/xdocs/trunk/configuration.xml b/src/documentation/content/xdocs/trunk/configuration.xml index aae7de7f0..3ef8795b7 100644 --- a/src/documentation/content/xdocs/trunk/configuration.xml +++ b/src/documentation/content/xdocs/trunk/configuration.xml @@ -89,6 +89,12 @@ disabled + + hyphenation-pattern + String, attribute lang, attribute country (optional) + Register a file name for the hyphenation pattern for the mentioned language and country. Language ll and country CC must both consist of two letters. + ll_CC + source-resolution Integer, dpi @@ -215,6 +221,9 @@ + + + nl_Bel ]]> diff --git a/src/java/org/apache/fop/apps/FopFactory.java b/src/java/org/apache/fop/apps/FopFactory.java index 907895c99..7bcdd2703 100644 --- a/src/java/org/apache/fop/apps/FopFactory.java +++ b/src/java/org/apache/fop/apps/FopFactory.java @@ -26,6 +26,8 @@ import java.io.OutputStream; import java.net.MalformedURLException; import java.util.Collection; import java.util.Collections; +import java.util.HashMap; +import java.util.Map; import java.util.Set; import javax.xml.transform.Source; @@ -108,6 +110,11 @@ public class FopFactory implements ImageContext { /** The base URL for all hyphen URL resolutions. */ private String hyphenBase = null; + /** + * Map of configured names of hyphenation pattern file names: ll_CC => name + */ + private Map/**/ hyphPatNames = null; + /** * FOP has the ability, for some FO's, to continue processing even if the * input XSL violates that FO's content model. This is the default @@ -406,6 +413,23 @@ public class FopFactory implements ImageContext { this.hyphenBase = foURIResolver.checkBaseURL(hyphenBase); } + /** + * @return the hyphPatNames + */ + public Map getHyphPatNames() { + return hyphPatNames; + } + + /** + * @param hyphPatNames the hyphPatNames to set + */ + public void setHyphPatNames(Map hyphPatNames) { + if (hyphPatNames == null) { + hyphPatNames = new HashMap(); + } + this.hyphPatNames = hyphPatNames; + } + /** * Sets the URI Resolver. It is used for resolving factory-level URIs like hyphenation * patterns and as backup for URI resolution performed during a rendering run. diff --git a/src/java/org/apache/fop/apps/FopFactoryConfigurator.java b/src/java/org/apache/fop/apps/FopFactoryConfigurator.java index e46530177..093b8fb53 100644 --- a/src/java/org/apache/fop/apps/FopFactoryConfigurator.java +++ b/src/java/org/apache/fop/apps/FopFactoryConfigurator.java @@ -22,6 +22,8 @@ package org.apache.fop.apps; import java.io.File; import java.io.IOException; import java.net.MalformedURLException; +import java.util.HashMap; +import java.util.Map; import org.xml.sax.SAXException; @@ -35,6 +37,7 @@ import org.apache.xmlgraphics.image.loader.spi.ImageImplRegistry; import org.apache.xmlgraphics.image.loader.util.Penalty; import org.apache.fop.fonts.FontManagerConfigurator; +import org.apache.fop.hyphenation.HyphenationTreeCache; import org.apache.fop.util.LogUtil; /** @@ -142,6 +145,62 @@ public class FopFactoryConfigurator { } } + /** + * Read configuration elements hyphenation-pattern, + * construct a map ll_CC => filename, and set it on the factory + */ + Configuration[] hyphPatConfig = cfg.getChildren("hyphenation-pattern"); + if (hyphPatConfig.length != 0) { + Map/**/ hyphPatNames = new HashMap/**/(); + for (int i = 0; i < hyphPatConfig.length; ++i) { + String lang, country, filename; + StringBuffer error = new StringBuffer(); + String location = hyphPatConfig[i].getLocation(); + + lang = hyphPatConfig[i].getAttribute("lang", null); + if (lang == null) { + addError("The lang attribute of a hyphenation-pattern configuration" + + " element must exist (" + location + ")", error); + } else if (!lang.matches("[a-zA-Z]{2}")) { + addError("The lang attribute of a hyphenation-pattern configuration" + + " element must consist of exactly two letters (" + location + ")", error); + } + lang = lang.toLowerCase(); + + country = hyphPatConfig[i].getAttribute("country", null); + if ("".equals(country)) { + country = null; + } + if (country != null) { + if (!country.matches("[a-zA-Z]{2}")) { + addError("The country attribute of a hyphenation-pattern configuration" + + " element must consist of exactly two letters (" + location + ")", error); + } + country = country.toUpperCase(); + } + + filename = hyphPatConfig[i].getValue(null); + if (filename == null) { + addError("The value of a hyphenation-pattern configuration" + + " element may not be empty (" + location + ")", error); + } + + if (error.length() != 0) { + LogUtil.handleError(log, error.toString(), strict); + continue; + } + + String llccKey = HyphenationTreeCache.constructLlccKey(lang, country); + hyphPatNames.put(llccKey, filename); + if (log.isDebugEnabled()) { + log.debug("Using hyphenation pattern filename " + filename + + " for lang=\"" + lang + "\"" + + (country != null?", country=\"" + country + "\"":"")); + } + } + factory.setHyphPatNames(hyphPatNames); + } + // renderer options if (cfg.getChild("source-resolution", false) != null) { factory.setSourceResolution( @@ -203,6 +262,13 @@ public class FopFactoryConfigurator { configureImageLoading(cfg.getChild("image-loading", false), strict); } + private static void addError(String message, StringBuffer error) { + if (error.length() != 0) { + error.append(". "); + } + error.append(message); + } + private void configureImageLoading(Configuration parent, boolean strict) throws FOPException { if (parent == null) { return; diff --git a/src/java/org/apache/fop/hyphenation/HyphenationTreeCache.java b/src/java/org/apache/fop/hyphenation/HyphenationTreeCache.java index 5831e2b98..40a44a942 100644 --- a/src/java/org/apache/fop/hyphenation/HyphenationTreeCache.java +++ b/src/java/org/apache/fop/hyphenation/HyphenationTreeCache.java @@ -20,6 +20,7 @@ package org.apache.fop.hyphenation; import java.util.Hashtable; +import java.util.Map; import java.util.Set; /** @@ -39,7 +40,7 @@ public class HyphenationTreeCache { * @return the HyhenationTree instance or null if it's not in the cache */ public HyphenationTree getHyphenationTree(String lang, String country) { - String key = constructKey(lang, country); + String key = constructLlccKey(lang, country); // first try to find it in the cache if (hyphenTrees.containsKey(key)) { @@ -57,7 +58,7 @@ public class HyphenationTreeCache { * @param country the country (may be null or "none") * @return the resulting key */ - public static String constructKey(String lang, String country) { + public static String constructLlccKey(String lang, String country) { String key = lang; // check whether the country code has been used if (country != null && !country.equals("none")) { @@ -66,6 +67,24 @@ public class HyphenationTreeCache { return key; } + /** + * If the user configured a hyphenation pattern file name + * for this (lang,country) value, return it. If not, return null. + * @param lang the language + * @param country the country (may be null or "none") + * @param hyphPatNames the map of user-configured hyphenation pattern file names + * @return the hyphenation pattern file name or null + */ + public static String constructUserKey(String lang, String country, Map hyphPatNames) { + String userKey = null; + if (hyphPatNames != null) { + String key = constructLlccKey(lang, country); + key.replace('_', '-'); + userKey = (String) hyphPatNames.get(key); + } + return userKey; + } + /** * Cache a hyphenation tree under its key. * @param key the key (ex. "de_CH" or "en") diff --git a/src/java/org/apache/fop/hyphenation/Hyphenator.java b/src/java/org/apache/fop/hyphenation/Hyphenator.java index 230f2ae20..3c7c0a43c 100644 --- a/src/java/org/apache/fop/hyphenation/Hyphenator.java +++ b/src/java/org/apache/fop/hyphenation/Hyphenator.java @@ -24,6 +24,7 @@ import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.ObjectInputStream; +import java.util.Map; import javax.xml.transform.Source; import javax.xml.transform.stream.StreamSource; @@ -83,7 +84,7 @@ public class Hyphenator { */ public static HyphenationTree getHyphenationTree(String lang, String country) { - return getHyphenationTree(lang, country, null); + return getHyphenationTree(lang, country, null, null); } /** @@ -95,12 +96,12 @@ public class Hyphenator { * @return the hyphenation tree */ public static HyphenationTree getHyphenationTree(String lang, - String country, HyphenationTreeResolver resolver) { - String key = HyphenationTreeCache.constructKey(lang, country); + String country, HyphenationTreeResolver resolver, Map hyphPatNames) { + String llccKey = HyphenationTreeCache.constructLlccKey(lang, country); HyphenationTreeCache cache = getHyphenationTreeCache(); // See if there was an error finding this hyphenation tree before - if (cache.isMissing(key)) { + if (cache.isMissing(llccKey)) { return null; } @@ -111,6 +112,10 @@ public class Hyphenator { return hTree; } + String key = HyphenationTreeCache.constructUserKey(lang, country, hyphPatNames); + if (key == null) { + key = llccKey; + } if (resolver != null) { hTree = getUserHyphenationTree(key, resolver); } @@ -120,10 +125,10 @@ public class Hyphenator { // put it into the pattern cache if (hTree != null) { - cache.cache(key, hTree); + cache.cache(llccKey, hTree); } else { - log.error("Couldn't find hyphenation pattern " + key); - cache.noteMissing(key); + log.error("Couldn't find hyphenation pattern " + llccKey); + cache.noteMissing(llccKey); } return hTree; } @@ -342,9 +347,10 @@ public class Hyphenator { */ public static Hyphenation hyphenate(String lang, String country, HyphenationTreeResolver resolver, + Map hyphPatNames, String word, int leftMin, int rightMin) { - HyphenationTree hTree = getHyphenationTree(lang, country, resolver); + HyphenationTree hTree = getHyphenationTree(lang, country, resolver, hyphPatNames); if (hTree == null) { return null; } @@ -363,7 +369,7 @@ public class Hyphenator { public static Hyphenation hyphenate(String lang, String country, String word, int leftMin, int rightMin) { - return hyphenate(lang, country, null, word, leftMin, rightMin); + return hyphenate(lang, country, null, null, word, leftMin, rightMin); } /** @@ -381,9 +387,10 @@ public class Hyphenator { public static Hyphenation hyphenate(String lang, // CSOK: ParameterNumber String country, HyphenationTreeResolver resolver, + Map hyphPatNames, char[] word, int offset, int len, int leftMin, int rightMin) { - HyphenationTree hTree = getHyphenationTree(lang, country, resolver); + HyphenationTree hTree = getHyphenationTree(lang, country, resolver, hyphPatNames); if (hTree == null) { return null; } @@ -404,7 +411,7 @@ public class Hyphenator { public static Hyphenation hyphenate(String lang, String country, char[] word, int offset, int len, int leftMin, int rightMin) { - return hyphenate(lang, country, null, word, offset, len, leftMin, rightMin); + return hyphenate(lang, country, null, null, word, offset, len, leftMin, rightMin); } /** diff --git a/src/java/org/apache/fop/layoutmgr/inline/LineLayoutManager.java b/src/java/org/apache/fop/layoutmgr/inline/LineLayoutManager.java index 83d286c15..cbae691b1 100644 --- a/src/java/org/apache/fop/layoutmgr/inline/LineLayoutManager.java +++ b/src/java/org/apache/fop/layoutmgr/inline/LineLayoutManager.java @@ -1415,6 +1415,7 @@ public class LineLayoutManager extends InlineStackingLayoutManager = Hyphenator.hyphenate(hyphenationProperties.language.getString(), hyphenationProperties.country.getString(), getFObj().getUserAgent().getFactory().getHyphenationTreeResolver(), + getFObj().getUserAgent().getFactory().getHyphPatNames(), sbChars.toString(), hyphenationProperties.hyphenationRemainCharacterCount.getValue(), hyphenationProperties.hyphenationPushCharacterCount.getValue());