View Javadoc

1   package org.lcsim.util.cache;
2   
3   import java.io.File;
4   import java.io.FileInputStream;
5   import java.io.IOException;
6   import java.net.URL;
7   import org.xml.sax.EntityResolver;
8   import org.xml.sax.InputSource;
9   import org.xml.sax.SAXException;
10  
11  /**
12   * An entity resolver which caches schemas locally. This makes it possible to
13   * run the tests without a network connection, provided the cache has been
14   * previously seeded.
15   * @author tonyj
16   * @version $Id: CachingEntityResolver.java,v 1.1.1.1 2010/01/25 22:23:07 jeremy
17   *          Exp $
18   */
19  public class CachingEntityResolver implements EntityResolver {
20      private FileCache fileCache;
21  
22      public CachingEntityResolver() throws IOException {
23          fileCache = new FileCache();
24      }
25  
26      public CachingEntityResolver(File cacheDirectory) throws IOException {
27          fileCache = new FileCache(cacheDirectory);
28      }
29  
30      public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
31          File file = fileCache.getCachedFile(new URL(systemId));
32          InputSource result = new InputSource(new FileInputStream(file));
33          result.setSystemId(systemId);
34          return result;
35      }
36  }