]> source.dussan.org Git - xmlgraphics-fop.git/commitdiff
Bugzilla #52177: Changed the AFP double-byte characterset builder to a singleton...
authorMehdi Houshmand <mehdi@apache.org>
Fri, 18 Nov 2011 16:23:43 +0000 (16:23 +0000)
committerMehdi Houshmand <mehdi@apache.org>
Fri, 18 Nov 2011 16:23:43 +0000 (16:23 +0000)
git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@1203719 13f79535-47bb-0310-9956-ffa450edef68

src/java/org/apache/fop/afp/fonts/CharacterSetBuilder.java
src/java/org/apache/fop/render/afp/AFPRendererConfigurator.java

index 16893a152050ec24aa77a66d455a9a9ddec3b8f2..c8f604584a54dbd2db1a1a3227f208d1a189ce90 100644 (file)
@@ -24,6 +24,7 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.net.URI;
 import java.net.URISyntaxException;
+import java.util.Collections;
 import java.util.List;
 import java.util.Map;
 import java.util.WeakHashMap;
@@ -56,18 +57,13 @@ import org.apache.xmlgraphics.image.loader.util.SoftMapCache;
  * formatted object. <p/>
  *
  */
-public class CharacterSetBuilder {
+public abstract class CharacterSetBuilder {
 
     /**
      * Static logging instance
      */
     protected static final Log LOG = LogFactory.getLog(CharacterSetBuilder.class);
 
-    /**
-     * Singleton reference
-     */
-    private static CharacterSetBuilder instance;
-
     /**
      * Template used to convert lists to arrays.
      */
@@ -104,8 +100,8 @@ public class CharacterSetBuilder {
     /**
      * The collection of code pages
      */
-    private final Map/*<String, Map<String, String>>*/ codePagesCache
-            = new WeakHashMap/*<String, Map<String, String>>*/();
+    private final Map<String, Map<String, String>> codePagesCache
+            = Collections.synchronizedMap(new WeakHashMap<String, Map<String, String>>());
 
     /**
      * Cache of charactersets
@@ -120,11 +116,8 @@ public class CharacterSetBuilder {
      * Factory method for the single-byte implementation of AFPFontReader.
      * @return AFPFontReader
      */
-    public static CharacterSetBuilder getInstance() {
-        if (instance == null) {
-            instance = new CharacterSetBuilder();
-        }
-        return instance;
+    public static CharacterSetBuilder getSingleByteInstance() {
+        return SingleByteLoader.getInstance();
     }
 
     /**
@@ -132,7 +125,7 @@ public class CharacterSetBuilder {
      * @return AFPFontReader
      */
     public static CharacterSetBuilder getDoubleByteInstance() {
-        return new DoubleByteLoader();
+        return DoubleByteLoader.getInstance();
     }
 
 
@@ -251,13 +244,14 @@ public class CharacterSetBuilder {
              * information to map the unicode character id to the graphic
              * chracter global identifier.
              */
+            Map<String, String> codePage;
+            synchronized (codePagesCache) {
+                codePage = codePagesCache.get(codePageName);
 
-            Map/*<String,String>*/ codePage
-                = (Map/*<String,String>*/)codePagesCache.get(codePageName);
-
-            if (codePage == null) {
-                codePage = loadCodePage(codePageName, encoding, accessor);
-                codePagesCache.put(codePageName, codePage);
+                if (codePage == null) {
+                    codePage = loadCodePage(codePageName, encoding, accessor);
+                    codePagesCache.put(codePageName, codePage);
+                }
             }
 
             inputStream = openInputStream(accessor, characterSetName);
@@ -671,10 +665,32 @@ public class CharacterSetBuilder {
         }
     }
 
+    private static final class SingleByteLoader extends CharacterSetBuilder {
+
+        private static final SingleByteLoader INSTANCE = new SingleByteLoader();
+
+        private SingleByteLoader() {
+            super();
+        }
+
+        private static SingleByteLoader getInstance() {
+            return INSTANCE;
+        }
+    }
+
     /**
      * Double-byte (CID Keyed font (Type 0)) implementation of AFPFontReader.
      */
-    private static class DoubleByteLoader extends CharacterSetBuilder {
+    private static final class DoubleByteLoader extends CharacterSetBuilder {
+
+        private static final DoubleByteLoader INSTANCE = new DoubleByteLoader();
+
+        private DoubleByteLoader() {
+        }
+
+        static DoubleByteLoader getInstance() {
+            return INSTANCE;
+        }
 
         protected Map/*<String,String>*/ loadCodePage(String codePage, String encoding,
                 ResourceAccessor accessor) throws IOException {
index 25d684e42dd8bd686573d163913fc638a35509b1..54eb6c87d9fdd593f0ea4ba537f6ef11dbb043e9 100644 (file)
@@ -190,7 +190,7 @@ public class AFPRendererConfigurator extends PrintRendererConfigurator
                         try {
                             Typeface tf = clazz.newInstance();
                             font.addCharacterSet(sizeMpt,
-                                    CharacterSetBuilder.getInstance()
+                                    CharacterSetBuilder.getSingleByteInstance()
                                         .build(characterset, codepage, encoding, tf));
                         } catch (Exception ie) {
                             String msg = "The base 14 font class " + clazz.getName()
@@ -204,7 +204,7 @@ public class AFPRendererConfigurator extends PrintRendererConfigurator
                     }
                 } else {
                     try {
-                        font.addCharacterSet(sizeMpt, CharacterSetBuilder.getInstance()
+                        font.addCharacterSet(sizeMpt, CharacterSetBuilder.getSingleByteInstance()
                                 .build(characterset, codepage, encoding, accessor));
                     } catch (IOException ioe) {
                         toConfigurationException(codepage, characterset, ioe);
@@ -228,7 +228,7 @@ public class AFPRendererConfigurator extends PrintRendererConfigurator
                             + base14).asSubclass(Typeface.class);
                     try {
                         Typeface tf = clazz.newInstance();
-                        characterSet = CharacterSetBuilder.getInstance()
+                        characterSet = CharacterSetBuilder.getSingleByteInstance()
                                         .build(characterset, codepage, encoding, tf);
                     } catch (Exception ie) {
                         String msg = "The base 14 font class " + clazz.getName()
@@ -242,7 +242,7 @@ public class AFPRendererConfigurator extends PrintRendererConfigurator
                 }
             } else {
                 try {
-                    characterSet = CharacterSetBuilder.getInstance().build(
+                    characterSet = CharacterSetBuilder.getSingleByteInstance().build(
                             characterset, codepage, encoding, accessor);
                 } catch (IOException ioe) {
                     toConfigurationException(codepage, characterset, ioe);