]> source.dussan.org Git - xmlgraphics-fop.git/commitdiff
Enable configuration of hyphenation pattern file names
authorSimon Pepping <spepping@apache.org>
Sat, 2 Oct 2010 11:09:13 +0000 (11:09 +0000)
committerSimon Pepping <spepping@apache.org>
Sat, 2 Oct 2010 11:09:13 +0000 (11:09 +0000)
git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@1003774 13f79535-47bb-0310-9956-ffa450edef68

src/documentation/content/xdocs/trunk/configuration.xml
src/java/org/apache/fop/apps/FopFactory.java
src/java/org/apache/fop/apps/FopFactoryConfigurator.java
src/java/org/apache/fop/hyphenation/HyphenationTreeCache.java
src/java/org/apache/fop/hyphenation/Hyphenator.java
src/java/org/apache/fop/layoutmgr/inline/LineLayoutManager.java

index aae7de7f0d48e3bb069ee9476808123e5d75c2d3..3ef8795b77afaf2458512eae20d215e323d950b8 100644 (file)
         </td>
         <td>disabled</td>
       </tr>
+         <tr>
+               <td>hyphenation-pattern</td>
+               <td>String, attribute lang, attribute country (optional)</td>
+               <td>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.</td>
+               <td>ll_CC</td>
+         </tr>
       <tr>
         <td>source-resolution</td>
         <td>Integer, dpi</td>
   <!-- default page-height and page-width, in case
        value is specified as auto -->
   <default-page-settings height="11in" width="8.26in"/>
+
+  <!-- Use file name nl_Bel instead of the default nl_BE -->
+  <hyphenation-pattern lang="nl" country="BE">nl_Bel</hyphenation-pattern>
   
   <!-- etc. etc..... -->
 </fop>]]></source>
index 907895c9964bcff65d714beaed5c7d09b71dff06..7bcdd27035a8336f3307094f9e1a1dff5702fb9c 100644 (file)
@@ -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/*<String,String>*/ 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.
index e465301774fae5656d65a5bcabe583c136a41028..093b8fb5370b8ce6dcfaa405055263748c4f710c 100644 (file)
@@ -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/*<String,String>*/ hyphPatNames = new HashMap/*<String,String>*/();
+            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;
index 5831e2b984fa6faa83189a95e848c6c228aad98f..40a44a942cf199812c92762c64ec131cbe24cb5b 100644 (file)
@@ -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")
index 230f2ae20ed45a487ef5ce7680f641ee263fb7d1..3c7c0a43c37703d2ccc208fc02385a7c3632f0c4 100644 (file)
@@ -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);
     }
 
     /**
index 83d286c15990b4aae80758189a8d943a79a59577..cbae691b1986fa0a59cea0c16568b3eee2974222 100644 (file)
@@ -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());