You are viewing a plain text version of this content. The canonical link for it is here.
Posted to fop-commits@xmlgraphics.apache.org by sp...@apache.org on 2010/10/02 13:09:13 UTC

svn commit: r1003774 - in /xmlgraphics/fop/trunk/src: documentation/content/xdocs/trunk/ java/org/apache/fop/apps/ java/org/apache/fop/hyphenation/ java/org/apache/fop/layoutmgr/inline/

Author: spepping
Date: Sat Oct  2 11:09:13 2010
New Revision: 1003774

URL: http://svn.apache.org/viewvc?rev=1003774&view=rev
Log:
Enable configuration of hyphenation pattern file names

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

Modified: xmlgraphics/fop/trunk/src/documentation/content/xdocs/trunk/configuration.xml
URL: http://svn.apache.org/viewvc/xmlgraphics/fop/trunk/src/documentation/content/xdocs/trunk/configuration.xml?rev=1003774&r1=1003773&r2=1003774&view=diff
==============================================================================
--- xmlgraphics/fop/trunk/src/documentation/content/xdocs/trunk/configuration.xml (original)
+++ xmlgraphics/fop/trunk/src/documentation/content/xdocs/trunk/configuration.xml Sat Oct  2 11:09:13 2010
@@ -89,6 +89,12 @@
         </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>
@@ -215,6 +221,9 @@
   <!-- 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>

Modified: xmlgraphics/fop/trunk/src/java/org/apache/fop/apps/FopFactory.java
URL: http://svn.apache.org/viewvc/xmlgraphics/fop/trunk/src/java/org/apache/fop/apps/FopFactory.java?rev=1003774&r1=1003773&r2=1003774&view=diff
==============================================================================
--- xmlgraphics/fop/trunk/src/java/org/apache/fop/apps/FopFactory.java (original)
+++ xmlgraphics/fop/trunk/src/java/org/apache/fop/apps/FopFactory.java Sat Oct  2 11:09:13 2010
@@ -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;
@@ -109,6 +111,11 @@ public class FopFactory implements Image
     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
      * behavior for FOP.  However, this flag, if set, provides the user the
@@ -407,6 +414,23 @@ public class FopFactory implements Image
     }
 
     /**
+     * @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.
      * @param uriResolver the new URI resolver

Modified: xmlgraphics/fop/trunk/src/java/org/apache/fop/apps/FopFactoryConfigurator.java
URL: http://svn.apache.org/viewvc/xmlgraphics/fop/trunk/src/java/org/apache/fop/apps/FopFactoryConfigurator.java?rev=1003774&r1=1003773&r2=1003774&view=diff
==============================================================================
--- xmlgraphics/fop/trunk/src/java/org/apache/fop/apps/FopFactoryConfigurator.java (original)
+++ xmlgraphics/fop/trunk/src/java/org/apache/fop/apps/FopFactoryConfigurator.java Sat Oct  2 11:09:13 2010
@@ -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.load
 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;

Modified: xmlgraphics/fop/trunk/src/java/org/apache/fop/hyphenation/HyphenationTreeCache.java
URL: http://svn.apache.org/viewvc/xmlgraphics/fop/trunk/src/java/org/apache/fop/hyphenation/HyphenationTreeCache.java?rev=1003774&r1=1003773&r2=1003774&view=diff
==============================================================================
--- xmlgraphics/fop/trunk/src/java/org/apache/fop/hyphenation/HyphenationTreeCache.java (original)
+++ xmlgraphics/fop/trunk/src/java/org/apache/fop/hyphenation/HyphenationTreeCache.java Sat Oct  2 11:09:13 2010
@@ -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")) {
@@ -67,6 +68,24 @@ public class HyphenationTreeCache {
     }
 
     /**
+     * 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")
      * @param hTree the hyphenation tree

Modified: xmlgraphics/fop/trunk/src/java/org/apache/fop/hyphenation/Hyphenator.java
URL: http://svn.apache.org/viewvc/xmlgraphics/fop/trunk/src/java/org/apache/fop/hyphenation/Hyphenator.java?rev=1003774&r1=1003773&r2=1003774&view=diff
==============================================================================
--- xmlgraphics/fop/trunk/src/java/org/apache/fop/hyphenation/Hyphenator.java (original)
+++ xmlgraphics/fop/trunk/src/java/org/apache/fop/hyphenation/Hyphenator.java Sat Oct  2 11:09:13 2010
@@ -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);
     }
 
     /**

Modified: xmlgraphics/fop/trunk/src/java/org/apache/fop/layoutmgr/inline/LineLayoutManager.java
URL: http://svn.apache.org/viewvc/xmlgraphics/fop/trunk/src/java/org/apache/fop/layoutmgr/inline/LineLayoutManager.java?rev=1003774&r1=1003773&r2=1003774&view=diff
==============================================================================
--- xmlgraphics/fop/trunk/src/java/org/apache/fop/layoutmgr/inline/LineLayoutManager.java (original)
+++ xmlgraphics/fop/trunk/src/java/org/apache/fop/layoutmgr/inline/LineLayoutManager.java Sat Oct  2 11:09:13 2010
@@ -1415,6 +1415,7 @@ public class LineLayoutManager extends I
             = Hyphenator.hyphenate(hyphenationProperties.language.getString(),
                                hyphenationProperties.country.getString(),
                                getFObj().getUserAgent().getFactory().getHyphenationTreeResolver(),
+                               getFObj().getUserAgent().getFactory().getHyphPatNames(),
                                sbChars.toString(),
                                hyphenationProperties.hyphenationRemainCharacterCount.getValue(),
                                hyphenationProperties.hyphenationPushCharacterCount.getValue());



---------------------------------------------------------------------
To unsubscribe, e-mail: fop-commits-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: fop-commits-help@xmlgraphics.apache.org