You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

FontCacheManagerFactory.java 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /* $Id$ */
  18. package org.apache.fop.fonts;
  19. import java.io.File;
  20. import java.net.URI;
  21. import org.apache.fop.apps.FOPException;
  22. /**
  23. * A factory that provides the font caching manager mechanism.
  24. *
  25. */
  26. public final class FontCacheManagerFactory {
  27. private FontCacheManagerFactory() {
  28. }
  29. /**
  30. * Create the default font caching mechanism.
  31. * @return the font cache manager
  32. */
  33. public static FontCacheManager createDefault() {
  34. return new FontCacheManagerImpl();
  35. }
  36. /**
  37. * Create a disabled font caching mechanism which by definition does nothing to cache fonts.
  38. * @return a completely restricted font cache manager
  39. */
  40. public static FontCacheManager createDisabled() {
  41. return new DisabledFontCacheManager();
  42. }
  43. private static final class FontCacheManagerImpl implements FontCacheManager {
  44. /** Provides a font cache file path **/
  45. private File cacheFile;
  46. private FontCache fontCache;
  47. public FontCache load() {
  48. if (fontCache == null) {
  49. fontCache = FontCache.loadFrom(getCacheFile(false));
  50. if (fontCache == null) {
  51. fontCache = new FontCache();
  52. }
  53. }
  54. return fontCache;
  55. }
  56. public void save() throws FOPException {
  57. if (fontCache != null && fontCache.hasChanged()) {
  58. fontCache.saveTo(getCacheFile(true));
  59. }
  60. }
  61. public void delete() throws FOPException {
  62. if (!getCacheFile(true).delete()) {
  63. throw new FOPException("Failed to flush the font cache file '" + cacheFile + "'.");
  64. }
  65. }
  66. private File getCacheFile(boolean forWriting) {
  67. if (cacheFile != null) {
  68. return cacheFile;
  69. }
  70. return FontCache.getDefaultCacheFile(forWriting);
  71. }
  72. public void setCacheFile(URI fontCacheURI) {
  73. cacheFile = new File(fontCacheURI);
  74. }
  75. }
  76. private static final class DisabledFontCacheManager implements FontCacheManager {
  77. public FontCache load() {
  78. return null;
  79. }
  80. public void save() throws FOPException {
  81. // nop
  82. }
  83. public void delete() throws FOPException {
  84. throw new FOPException("Font Cache disabled");
  85. }
  86. public void setCacheFile(URI fontCacheURI) {
  87. // nop
  88. }
  89. }
  90. }