| 35 |
import java.io.File; |
import java.io.File; |
| 36 |
import java.io.InputStream; |
import java.io.InputStream; |
| 37 |
import java.net.URL; |
import java.net.URL; |
| 38 |
|
import java.security.AccessControlException; |
| 39 |
import java.util.Collection; |
import java.util.Collection; |
| 40 |
import java.util.Collections; |
import java.util.Collections; |
| 41 |
|
import java.util.Comparator; |
| 42 |
import java.util.List; |
import java.util.List; |
| 43 |
import java.util.Map; |
import java.util.Map; |
| 44 |
import java.util.Properties; |
import java.util.Properties; |
| 45 |
|
import java.util.Set; |
| 46 |
import java.util.TreeMap; |
import java.util.TreeMap; |
| 47 |
|
import java.util.TreeSet; |
| 48 |
import java.util.concurrent.ExecutionException; |
import java.util.concurrent.ExecutionException; |
| 49 |
import java.util.concurrent.Future; |
import java.util.concurrent.Future; |
| 50 |
import java.util.logging.Logger; |
import java.util.logging.Logger; |
| 95 |
private final CharacterCodeMapper characterCodeMapper |
private final CharacterCodeMapper characterCodeMapper |
| 96 |
= new SimpleJisToUnicodeMapper(); |
= new SimpleJisToUnicodeMapper(); |
| 97 |
|
|
| 98 |
|
private boolean recordResources = false; |
| 99 |
|
private final Set<URL> resources = new TreeSet<URL>(new Comparator<URL>() { |
| 100 |
|
public int compare(URL arg0, URL arg1) { |
| 101 |
|
return arg0.toString().compareTo(arg1.toString()); |
| 102 |
|
} |
| 103 |
|
}); |
| 104 |
|
|
| 105 |
private final ProgressRecorder recorder = new ProgressRecorder(this) { |
private final ProgressRecorder recorder = new ProgressRecorder(this) { |
| 106 |
@Override |
@Override |
| 107 |
protected boolean removeEldestElement(ManagedProgressItem item) |
protected boolean removeEldestElement(ManagedProgressItem item) |
| 136 |
throws DviException |
throws DviException |
| 137 |
{ |
{ |
| 138 |
File cacheDir = getCacheDirectory(); |
File cacheDir = getCacheDirectory(); |
| 139 |
if (!cacheDir.exists()) { |
if (cacheDir != null && !cacheDir.exists()) { |
| 140 |
if (!cacheDir.mkdirs()) { |
if (!cacheDir.mkdirs()) { |
| 141 |
throw new DviException("Failed to create cache directory: " + cacheDir); |
throw new DviException("Failed to create cache directory: " + cacheDir); |
| 142 |
} |
} |
| 143 |
} |
} |
| 144 |
File tmpDir = getTemporaryDirectory(); |
File tmpDir = getTemporaryDirectory(); |
| 145 |
if (!tmpDir.exists()) { |
if (tmpDir != null && !tmpDir.exists()) { |
| 146 |
if (!tmpDir.mkdirs()) { |
if (!tmpDir.mkdirs()) { |
| 147 |
throw new DviException("Failed to create temporary directory: " + tmpDir); |
throw new DviException("Failed to create temporary directory: " + tmpDir); |
| 148 |
} |
} |
| 155 |
try { |
try { |
| 156 |
URL url = DefaultDviContext.class.getResource("default-context.properties"); |
URL url = DefaultDviContext.class.getResource("default-context.properties"); |
| 157 |
Properties prop = new Properties(); |
Properties prop = new Properties(); |
| 158 |
prop.load(url.openStream()); |
if (null != url) { |
| 159 |
|
prop.load(url.openStream()); |
| 160 |
|
} else { |
| 161 |
|
LOGGER.warning("Failed to load default-context.properties"); |
| 162 |
|
} |
| 163 |
return prop; |
return prop; |
| 164 |
} catch (Exception e) { |
} catch (Exception e) { |
| 165 |
throw new DviException(e); |
throw new DviException(e); |
| 334 |
private static final CachedComputer<String, Collection<URL>> dviResourceComputer |
private static final CachedComputer<String, Collection<URL>> dviResourceComputer |
| 335 |
= new CachedComputer<String, Collection<URL>> |
= new CachedComputer<String, Collection<URL>> |
| 336 |
(new ThreadedComputer<String, Collection<URL>>(1)); |
(new ThreadedComputer<String, Collection<URL>>(1)); |
| 337 |
|
|
| 338 |
public URL getDviResource(String filename) throws DviException |
public URL getDviResource(String filename) throws DviException |
| 339 |
{ |
{ |
| 340 |
if (filename.startsWith(DVICORE_INTERNAL_FILENAME_PREFIX)) { |
if (filename.startsWith(DVICORE_INTERNAL_FILENAME_PREFIX)) { |
| 345 |
} |
} |
| 346 |
|
|
| 347 |
Computation<String, Collection<URL>> c |
Computation<String, Collection<URL>> c |
| 348 |
= new FileLocationResolver(this, "dvi/builtin", filename); |
= new FileLocationResolver(this, "/dvi/builtin", filename); |
| 349 |
final Future<Collection<URL>> future = dviResourceComputer.compute(c); |
final Future<Collection<URL>> future = dviResourceComputer.compute(c); |
| 350 |
try { |
try { |
| 351 |
final Collection<URL> list = future.get(); |
final Collection<URL> list = future.get(); |
| 352 |
for (URL url : list) { |
for (URL url : list) { |
| 353 |
LOGGER.finest("resolved resource: filename=" + filename + " url=" + url); |
if (recordResources) { |
| 354 |
|
LOGGER.info("resolved resource: filename=" + filename + " url=" + url); |
| 355 |
|
resources.add(url); |
| 356 |
|
} else { |
| 357 |
|
LOGGER.finest("resolved resource: filename=" + filename + " url=" + url); |
| 358 |
|
} |
| 359 |
return url; |
return url; |
| 360 |
} |
} |
| 361 |
return null; |
return null; |
| 371 |
public LogicalFont mapLogicalFont(LogicalFont logicalFont) |
public LogicalFont mapLogicalFont(LogicalFont logicalFont) |
| 372 |
throws DviException |
throws DviException |
| 373 |
{ |
{ |
| 374 |
if (logicalFont == null) return null; |
LogicalFont mapped = logicalFont; |
| 375 |
String prefix = getClass().getName(); |
if (logicalFont != null) { |
| 376 |
String face = getProperties().getProperty(prefix + ".fontMap." + logicalFont.fontSpec().name()); |
String prefix = getClass().getName(); |
| 377 |
if (face == null) return logicalFont; |
String faceKey = prefix + ".fontMap." + logicalFont.fontSpec().name(); |
| 378 |
LogicalFont newLogicalFont = logicalFont.renameTo(face); |
LOGGER.info("Face key: " + faceKey); |
| 379 |
LOGGER.fine("Map logical font: " + logicalFont + " => " + newLogicalFont); |
String face = getProperties().getProperty(faceKey); |
| 380 |
return newLogicalFont; |
LOGGER.info("Properties: " + getProperties()); |
| 381 |
|
if (face != null) { |
| 382 |
|
mapped = logicalFont.renameTo(face); |
| 383 |
|
LOGGER.info("Rename logical font: " + logicalFont + " => " + mapped); |
| 384 |
|
} |
| 385 |
|
} |
| 386 |
|
LOGGER.info("Map logical font: " + logicalFont + " => " + mapped); |
| 387 |
|
return mapped; |
| 388 |
} |
} |
| 389 |
|
|
| 390 |
|
// N.B. System.getProperty() throws an exception when invoked |
| 391 |
|
// from inside an applet. |
| 392 |
private static String getSystemProperty(String key) { |
private static String getSystemProperty(String key) { |
| 393 |
return System.getProperty(key); |
try { |
| 394 |
|
return System.getProperty(key); |
| 395 |
|
} catch (AccessControlException ex) { |
| 396 |
|
return null; |
| 397 |
|
} |
| 398 |
} |
} |
| 399 |
|
|
| 400 |
private static final String userDir = getSystemProperty("user.dir"); |
private static final String userDir = getSystemProperty("user.dir"); |
| 404 |
public File getApplicationHomeDir() |
public File getApplicationHomeDir() |
| 405 |
throws DviException |
throws DviException |
| 406 |
{ |
{ |
| 407 |
File home = new File(userDir); |
if (userDir != null) { |
| 408 |
File markFile = new File(home, "dvibrowser.jar"); |
File home = new File(userDir); |
| 409 |
if (markFile.exists()) { |
File markFile = new File(home, "dvibrowser.jar"); |
| 410 |
// It seems that we are using DviContext from within dvibrowser. |
if (markFile.exists()) { |
| 411 |
return home; |
// It seems that we are using DviContext from within dvibrowser. |
| 412 |
} |
return home; |
| 413 |
|
} |
| 414 |
|
} |
| 415 |
return null; |
return null; |
| 416 |
} |
} |
| 417 |
|
|
| 420 |
File appHome = getApplicationHomeDir(); |
File appHome = getApplicationHomeDir(); |
| 421 |
if (appHome == null) { |
if (appHome == null) { |
| 422 |
File tmpDir = getTemporaryDirectory(); |
File tmpDir = getTemporaryDirectory(); |
| 423 |
return new File(tmpDir, "cache"); |
if (tmpDir != null) { |
| 424 |
|
return new File(tmpDir, "cache"); |
| 425 |
|
} |
| 426 |
} else { |
} else { |
| 427 |
File var = new File(userDir, "var"); |
if (userDir != null) { |
| 428 |
return new File(var, "cache"); |
File var = new File(userDir, "var"); |
| 429 |
|
return new File(var, "cache"); |
| 430 |
|
} |
| 431 |
} |
} |
| 432 |
|
return null; |
| 433 |
} |
} |
| 434 |
|
|
| 435 |
public File getTemporaryDirectory() throws DviException |
public File getTemporaryDirectory() throws DviException |
| 436 |
{ |
{ |
| 437 |
File appHome = getApplicationHomeDir(); |
File appHome = getApplicationHomeDir(); |
| 438 |
if (appHome == null) { |
if (appHome == null) { |
| 439 |
return new File(ioTmpDir, "dvicontext"); |
if (ioTmpDir != null) { |
| 440 |
|
return new File(ioTmpDir, "dvicontext"); |
| 441 |
|
} |
| 442 |
} else { |
} else { |
| 443 |
return new File(appHome, "tmp"); |
return new File(appHome, "tmp"); |
| 444 |
} |
} |
| 445 |
|
return null; |
| 446 |
} |
} |
| 447 |
|
|
| 448 |
private volatile String [] ghostscriptExecutables = null; |
private volatile String [] ghostscriptExecutables = null; |
| 479 |
public MetafontMode getDefaultMetafontMode() throws DviException { |
public MetafontMode getDefaultMetafontMode() throws DviException { |
| 480 |
return libraryDefaultMetafontMode; |
return libraryDefaultMetafontMode; |
| 481 |
} |
} |
| 482 |
|
|
| 483 |
|
public void setRecordResources(boolean recordResources) { |
| 484 |
|
this.recordResources = recordResources; |
| 485 |
|
} |
| 486 |
|
|
| 487 |
|
public boolean wantRecordResources() { |
| 488 |
|
return recordResources; |
| 489 |
|
} |
| 490 |
|
|
| 491 |
|
public Set<URL> getRecordedResources() { |
| 492 |
|
return resources; |
| 493 |
|
} |
| 494 |
} |
} |