From 3d2b970d8e559003aea495c0e2a941d0c7d81160 Mon Sep 17 00:00:00 2001 From: Mehdi Houshmand Date: Fri, 18 Nov 2011 16:23:43 +0000 Subject: [PATCH] Bugzilla #52177: Changed the AFP double-byte characterset builder to a singleton, this now matches the behaviour of the single byte characterset builder git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@1203719 13f79535-47bb-0310-9956-ffa450edef68 --- .../fop/afp/fonts/CharacterSetBuilder.java | 58 ++++++++++++------- .../render/afp/AFPRendererConfigurator.java | 8 +-- 2 files changed, 41 insertions(+), 25 deletions(-) diff --git a/src/java/org/apache/fop/afp/fonts/CharacterSetBuilder.java b/src/java/org/apache/fop/afp/fonts/CharacterSetBuilder.java index 16893a152..c8f604584 100644 --- a/src/java/org/apache/fop/afp/fonts/CharacterSetBuilder.java +++ b/src/java/org/apache/fop/afp/fonts/CharacterSetBuilder.java @@ -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.

* */ -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/*>*/ codePagesCache - = new WeakHashMap/*>*/(); + private final Map> codePagesCache + = Collections.synchronizedMap(new WeakHashMap>()); /** * 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 codePage; + synchronized (codePagesCache) { + codePage = codePagesCache.get(codePageName); - Map/**/ codePage - = (Map/**/)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/**/ loadCodePage(String codePage, String encoding, ResourceAccessor accessor) throws IOException { diff --git a/src/java/org/apache/fop/render/afp/AFPRendererConfigurator.java b/src/java/org/apache/fop/render/afp/AFPRendererConfigurator.java index 25d684e42..54eb6c87d 100644 --- a/src/java/org/apache/fop/render/afp/AFPRendererConfigurator.java +++ b/src/java/org/apache/fop/render/afp/AFPRendererConfigurator.java @@ -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); -- 2.39.5