[ https://issues.apache.org/jira/browse/FOP-2857?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16815140#comment-16815140 ]
simon steiner commented on FOP-2857: ------------------------------------ Bug description should avoid lots of code since the patch file should be used > FontCache.toDirectory() and FontCache.getDefaultCacheFile() work not correct > ---------------------------------------------------------------------------- > > Key: FOP-2857 > URL: https://issues.apache.org/jira/browse/FOP-2857 > Project: FOP > Issue Type: Bug > Components: font/unqualified > Affects Versions: 2.3 > Reporter: Vera Straube > Priority: Critical > Attachments: FontCache.java, FontCache.java.patch > > > {code:java} > // wrong code > /** FOP's user directory name */ > private static final String FOP_USER_DIR = ".fop"; > /** font cache file path */ > private static final String DEFAULT_CACHE_FILENAME = "fop-fonts.cache"; > > private static File getUserHome() { > return toDirectory(System.getProperty("user.home")); > } > private static File getTempDirectory() { > return toDirectory(System.getProperty("java.io.tmpdir")); > } > private static File toDirectory(String path) { > if (path != null) { > File dir = new File(path); > if (dir.exists()) { > return dir; > } > } > return null; > } > > /** > * Returns the default font cache file. > * > * @param forWriting > * true if the user directory should be created > * @return the default font cache file > */ > public static File getDefaultCacheFile(boolean forWriting) { > File userHome = getUserHome(); > if (userHome != null) { > File fopUserDir = new File(userHome, FOP_USER_DIR); > if (forWriting) { > boolean writable = fopUserDir.canWrite(); > if (!fopUserDir.exists()) { > writable = fopUserDir.mkdir(); > } > if (!writable) { > userHome = getTempDirectory(); > fopUserDir = new File(userHome, FOP_USER_DIR); > fopUserDir.mkdir(); > } > } > return new File(fopUserDir, DEFAULT_CACHE_FILENAME); > } > return new File(FOP_USER_DIR); > } > {code} > {code:java} > //correct code > /** FOP's user directory name */ > private static final String FOP_USER_DIR = ".fop"; > > /** font cache file path */ > private static final String DEFAULT_CACHE_FILENAME = "fop-fonts.cache"; > > /** complete path to user directory*/ > public static String getUserDir(){ > return System.getProperty("user.home"); > } > > /** complete path to FOP's user directory*/ > private static String getFopUserDir(){ > return getUserDir() + System.getProperty("file.separator") + > FOP_USER_DIR; > } > > /** complete path to temp directory*/ > private static String getTempDir(){ > return System.getProperty("java.io.tmpdir"); > } > > /** complete path to FOP's temp directory*/ > private static String getFopTempDir(){ > return getTempDir() + System.getProperty("file.separator") + > FOP_USER_DIR; > } > > /** > * returns the file object of the existing directory or null > * > * @param path > * complete path to the directory > * @param forWriting > * true if the directory should be created if it not exists > * @return the file object of the existing directory or null > */ > private static File toDirectory(String path, boolean forWriting) > { > File result = null; > if (path != null && !path.trim().isEmpty()) { > File tmp = new File(path); > if( tmp.exists() ){ > result = tmp; > }else{ > if(forWriting){ > if( tmp.mkdirs() ){ > result = tmp; > } > } > } > } > return result; > } > > /** > * Returns the default font cache file. > * > * @param forWriting > * true if the user directory should be created if it not exists > * @return the default font cache file > */ > public static File getDefaultCacheFile(boolean forWriting) { > File result = new File(DEFAULT_CACHE_FILENAME); > File userHome = toDirectory(getUserDir(),false); > if(userHome!=null){ > File fopUserDir = toDirectory(getFopUserDir(),forWriting); > if(fopUserDir!=null){ > result = new File(fopUserDir,DEFAULT_CACHE_FILENAME); > }else{ > File tempDir = toDirectory(getTempDir(),forWriting); > if(tempDir!=null){ > File fopTempDir = toDirectory(getFopTempDir(),forWriting); > if(fopTempDir!=null){ > result = new File(fopTempDir,DEFAULT_CACHE_FILENAME); > } > } > } > } > return result; > } > {code} > {code:java} > // corrected code V2: > // if FopUserDir not exists, try FopTempDir > // if FopTempDir not exists, try CurrDir > public static File getDefaultCacheFile(boolean forWriting) { > File result = new File(DEFAULT_CACHE_FILENAME); > File fopUserDir = toDirectory(getFopUserDir(),forWriting); > if(fopUserDir!=null){ > result = new File(fopUserDir,DEFAULT_CACHE_FILENAME); > }else{ > File fopTempDir = toDirectory(getFopTempDir(),forWriting); > if(fopTempDir!=null){ > result = new File(fopTempDir,DEFAULT_CACHE_FILENAME); > } > } > return result; > } > {code} -- This message was sent by Atlassian JIRA (v7.6.3#76005)