| 1 |
/* |
| 2 |
* Copyright (c) 2009, Takeyuki Nagao |
| 3 |
* All rights reserved. |
| 4 |
* |
| 5 |
* Redistribution and use in source and binary forms, with or |
| 6 |
* without modification, are permitted provided that the |
| 7 |
* following conditions are met: |
| 8 |
* |
| 9 |
* * Redistributions of source code must retain the above |
| 10 |
* copyright notice, this list of conditions and the |
| 11 |
* following disclaimer. |
| 12 |
* * Redistributions in binary form must reproduce the above |
| 13 |
* copyright notice, this list of conditions and the |
| 14 |
* following disclaimer in the documentation and/or other |
| 15 |
* materials provided with the distribution. |
| 16 |
* |
| 17 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND |
| 18 |
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, |
| 19 |
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 20 |
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 21 |
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR |
| 22 |
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 24 |
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 25 |
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 26 |
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 27 |
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 28 |
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE |
| 29 |
* USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY |
| 30 |
* OF SUCH DAMAGE. |
| 31 |
*/ |
| 32 |
|
| 33 |
package jp.sourceforge.dvibrowser.dvicore.ctx; |
| 34 |
import java.io.File; |
| 35 |
import java.net.URL; |
| 36 |
import java.security.AccessControlException; |
| 37 |
import java.util.ArrayList; |
| 38 |
import java.util.Collection; |
| 39 |
import java.util.List; |
| 40 |
import java.util.logging.Logger; |
| 41 |
|
| 42 |
import jp.sourceforge.dvibrowser.dvicore.DviObject; |
| 43 |
import jp.sourceforge.dvibrowser.dvicore.api.DviContextSupport; |
| 44 |
import jp.sourceforge.dvibrowser.dvicore.util.concurrent.Computation; |
| 45 |
import jp.sourceforge.dvibrowser.dvicore.util.progress.ProgressItem; |
| 46 |
|
| 47 |
|
| 48 |
public class FileLocationResolver |
| 49 |
extends DviObject |
| 50 |
implements Computation<String, Collection<URL>> { |
| 51 |
private static final Logger LOGGER = Logger.getLogger(FileLocationResolver.class |
| 52 |
.getName()); |
| 53 |
private final String systemResourcePath; |
| 54 |
private final String filename; |
| 55 |
|
| 56 |
public FileLocationResolver(DviContextSupport dcs, String systemResourcePath, String filename) |
| 57 |
{ |
| 58 |
super(dcs); |
| 59 |
this.filename = filename; |
| 60 |
this.systemResourcePath = systemResourcePath; |
| 61 |
} |
| 62 |
|
| 63 |
public Collection<URL> call() throws Exception |
| 64 |
{ |
| 65 |
ProgressItem progress = getDviContext().getProgressRecorder().open("preparing " + filename); |
| 66 |
List<URL> list = new ArrayList<URL>(); |
| 67 |
try { |
| 68 |
try { |
| 69 |
if ("true".equals(System.getProperty("dvi.ctx.DefaultDviContext.usePackageShareDir"))) { |
| 70 |
File f = new File("share", filename); |
| 71 |
if (f.exists() && f.canRead()) { |
| 72 |
LOGGER.fine("Using resource from share: " + f); |
| 73 |
list.add(f.toURL()); |
| 74 |
return list; |
| 75 |
} |
| 76 |
} |
| 77 |
} catch (AccessControlException ex) { |
| 78 |
LOGGER.warning(ex.toString()); |
| 79 |
} |
| 80 |
|
| 81 |
LOGGER.finer("running kpsewhich: " + filename); |
| 82 |
try { |
| 83 |
URL url = null; |
| 84 |
KpseWhich kpseWhich = new KpseWhich(this); |
| 85 |
url = kpseWhich.findURL(filename, true); |
| 86 |
LOGGER.finer("kpsewhich result: " + filename + " => " + url); |
| 87 |
if (url != null) |
| 88 |
list.add(url); |
| 89 |
} catch (RuntimeException ex) { |
| 90 |
LOGGER.warning(ex.toString()); |
| 91 |
} |
| 92 |
|
| 93 |
try { |
| 94 |
URL u = ClassLoader.getSystemResource(systemResourcePath + "/" |
| 95 |
+ filename); |
| 96 |
LOGGER.fine("system resource by classloader: " + filename + " => " + u); |
| 97 |
if (u != null) |
| 98 |
list.add(u); |
| 99 |
} catch (RuntimeException ex) { |
| 100 |
LOGGER.warning(ex.toString()); |
| 101 |
} |
| 102 |
|
| 103 |
try { |
| 104 |
URL u = getClass().getResource(systemResourcePath + "/" |
| 105 |
+ filename); |
| 106 |
LOGGER.fine("resource by classloader: " + filename + " => " + u); |
| 107 |
if (u != null) |
| 108 |
list.add(u); |
| 109 |
} catch (RuntimeException ex) { |
| 110 |
LOGGER.warning(ex.toString()); |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
if (list.size() == 0) { |
| 115 |
LOGGER.warning("Failed to resolve file: " + filename); |
| 116 |
} |
| 117 |
} finally { |
| 118 |
progress.close(); |
| 119 |
} |
| 120 |
|
| 121 |
return list; |
| 122 |
} |
| 123 |
|
| 124 |
public String getFilename() |
| 125 |
{ |
| 126 |
return filename; |
| 127 |
} |
| 128 |
|
| 129 |
public String getCacheKey() |
| 130 |
{ |
| 131 |
return filename; |
| 132 |
} |
| 133 |
|
| 134 |
public String getSystemResourcePath() |
| 135 |
{ |
| 136 |
return systemResourcePath; |
| 137 |
} |
| 138 |
} |