URI resolveURI(String uri);
}
- private final class NullBaseURIResolver implements URIResolver {
+ private static final class NullBaseURIResolver implements URIResolver {
public URI resolveURI(URI uri) {
return uri;
throw new FOPException("if you use '-cache', you must specify "
+ "the name of the font cache file");
} else {
- factory.getFontManager().setCacheFile(new File(args[i + 1]));
+ factory.getFontManager().setCacheFile(URI.create(args[i + 1]));
return 1;
}
}
package org.apache.fop.fonts;
-import java.io.File;
+import java.net.URI;
import org.apache.fop.apps.FOPException;
*/
public interface FontCacheManager {
+ /**
+ * Sets the font cache file given the URI pointing to the file.
+ * @param fontCacheURI the font cache URI
+ */
+ void setCacheFile(URI fontCacheURI);
+
/**
* Loads the font cache into memory from the given file.
- * @param file the serialized font cache
* @return the de-serialized font cache
*/
- FontCache load(File file);
+ FontCache load();
/**
* Serializes the font cache to file.
- * @param file the file to serialize the font cache to
* @throws FOPException if an error occurs serializing the font cache
*/
- void save(File file) throws FOPException;
+ void save() throws FOPException;
/**
* Deletes the font cache from the file-system.
- * @param file delete the serialized font cache
* @throws FOPException if an error occurs deleting the font cache
*/
- void delete(File file) throws FOPException;
-
+ void delete() throws FOPException;
}
package org.apache.fop.fonts;
import java.io.File;
+import java.net.URI;
import org.apache.fop.apps.FOPException;
private static final class FontCacheManagerImpl implements FontCacheManager {
+ /** Provides a font cache file path **/
+ private File cacheFile;
+
private FontCache fontCache;
- public FontCache load(File cacheFile) {
+ public FontCache load() {
if (fontCache == null) {
- fontCache = FontCache.loadFrom(cacheFile);
+ fontCache = FontCache.loadFrom(getCacheFile(false));
if (fontCache == null) {
fontCache = new FontCache();
}
return fontCache;
}
- public void save(File cacheFile) throws FOPException {
+ public void save() throws FOPException {
if (fontCache != null && fontCache.hasChanged()) {
- fontCache.saveTo(cacheFile);
+ fontCache.saveTo(getCacheFile(true));
}
}
- public void delete(File cacheFile) throws FOPException {
- if (!cacheFile.delete()) {
+ public void delete() throws FOPException {
+ if (!getCacheFile(true).delete()) {
throw new FOPException("Failed to flush the font cache file '" + cacheFile + "'.");
}
}
+
+ private File getCacheFile(boolean forWriting) {
+ if (cacheFile != null) {
+ return cacheFile;
+ }
+ return FontCache.getDefaultCacheFile(forWriting);
+ }
+
+ public void setCacheFile(URI fontCacheURI) {
+ cacheFile = new File(fontCacheURI);
+ }
}
private static final class DisabledFontCacheManager implements FontCacheManager {
- public FontCache load(File cacheFile) {
+ public FontCache load() {
return null;
}
- public void save(File cacheFile) throws FOPException {
+ public void save() throws FOPException {
// nop
}
- public void delete(File cacheFile) throws FOPException {
+ public void delete() throws FOPException {
throw new FOPException("Font Cache disabled");
}
+
+ public void setCacheFile(URI fontCacheURI) {
+ // nop
+ }
}
}
package org.apache.fop.fonts;
-import java.io.File;
+import java.net.URI;
import java.util.List;
import org.apache.fop.apps.FOPException;
/** FontTriplet matcher for fonts that shall be referenced rather than embedded. */
private FontTriplet.Matcher referencedFontsMatcher;
- /** Provides a font cache file path **/
- private File cacheFile;
-
/**
* Main constructor
*
/**
* Sets the font cache file
- * @param cacheFile the font cache file
- */
- public void setCacheFile(File cacheFile) {
- this.cacheFile = cacheFile;
- }
-
- /**
- * Returns the font cache file
- * @return the font cache file
+ * @param cacheFileURI the URI of the font cache file
*/
- public File getCacheFile() {
- return getCacheFile(false);
- }
-
- private File getCacheFile(boolean writable) {
- if (cacheFile != null) {
- return cacheFile;
- }
- return FontCache.getDefaultCacheFile(writable);
+ public void setCacheFile(URI cacheFileURI) {
+ fontCacheManager.setCacheFile(resourceResolver.resolveFromBase(cacheFileURI));
}
/**
* @return the font cache
*/
public FontCache getFontCache() {
- return fontCacheManager.load(getCacheFile());
+ return fontCacheManager.load();
}
/**
* @throws FOPException fop exception
*/
public void saveCache() throws FOPException {
- fontCacheManager.save(getCacheFile());
+ fontCacheManager.save();
}
/**
* @throws FOPException if an error was thrown while deleting the cache
*/
public void deleteCache() throws FOPException {
- fontCacheManager.delete(getCacheFile(true));
+ fontCacheManager.delete();
}
/**
package org.apache.fop.fonts;
-import java.io.File;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
* @throws FOPException if an exception occurs while processing the configuration
*/
public void configure(FontManager fontManager, boolean strict) throws FOPException {
+ if (cfg.getChild("font-base", false) != null) {
+ try {
+ URI fontBase = InternalResourceResolver.getBaseURI(cfg.getChild("font-base")
+ .getValue(null));
+ fontManager.setResourceResolver(ResourceResolverFactory.createInternalResourceResolver(
+ defaultBaseUri.resolve(fontBase), resourceResolver));
+ } catch (URISyntaxException use) {
+ LogUtil.handleException(log, use, true);
+ }
+ } else {
+ fontManager.setResourceResolver(ResourceResolverFactory.createInternalResourceResolver(
+ defaultBaseUri, resourceResolver));
+ }
// caching (fonts)
if (cfg.getChild("use-cache", false) != null) {
try {
fontManager.disableFontCache();
} else {
if (cfg.getChild("cache-file", false) != null) {
- fontManager.setCacheFile(new File(cfg.getChild("cache-file").getValue()));
+
+ fontManager.setCacheFile(URI.create(cfg.getChild("cache-file").getValue()));
}
}
} catch (ConfigurationException mfue) {
LogUtil.handleException(log, mfue, true);
}
}
- if (cfg.getChild("font-base", false) != null) {
- try {
- URI fontBase = InternalResourceResolver.getBaseURI(cfg.getChild("font-base").getValue(
- null));
- fontManager.setResourceResolver(ResourceResolverFactory.createInternalResourceResolver(
- defaultBaseUri.resolve(fontBase), resourceResolver));
- } catch (URISyntaxException use) {
- LogUtil.handleException(log, use, true);
- }
- } else {
- fontManager.setResourceResolver(ResourceResolverFactory.createInternalResourceResolver(
- defaultBaseUri, resourceResolver));
- }
-
// [GA] permit configuration control over base14 kerning; without this,
// there is no way for a user to enable base14 kerning other than by
// programmatic API;
--- /dev/null
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.fop.fonts;
+
+import java.net.URI;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.InOrder;
+
+import static org.mockito.Matchers.any;
+import static org.mockito.Mockito.inOrder;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+
+import org.apache.fop.apps.FOPException;
+import org.apache.fop.apps.io.InternalResourceResolver;
+
+public class FontManagerTestCase {
+
+ private FontManager sut;
+ private FontCacheManager fontCacheManager;
+ private FontDetector fontDetector;
+ private InternalResourceResolver resolver;
+
+ @Before
+ public void setUp() {
+ resolver = mock(InternalResourceResolver.class);
+ fontCacheManager = mock(FontCacheManager.class);
+ fontDetector = mock(FontDetector.class);
+
+ sut = new FontManager(resolver, fontDetector, fontCacheManager);
+ }
+
+ @Test
+ public void testSetCacheFile() {
+ URI testURI = URI.create("test/uri");
+ sut.setCacheFile(testURI);
+
+ InOrder inOrder = inOrder(resolver, fontCacheManager);
+ inOrder.verify(resolver).resolveFromBase(testURI);
+ inOrder.verify(fontCacheManager).setCacheFile(any(URI.class));
+ }
+
+ @Test
+ public void testGetFontCache() {
+ sut.getFontCache();
+ verify(fontCacheManager).load();
+ }
+
+ @Test
+ public void testSaveCache() throws FOPException {
+ sut.saveCache();
+ verify(fontCacheManager).save();
+ }
+
+ @Test
+ public void testDeleteCache() throws FOPException {
+ sut.deleteCache();
+ verify(fontCacheManager).delete();
+ }
+}