Exemple #1
1
  public void setObject(String location) {

    if (location != null) {
      File dataDirectory = canonicalize(rootDir);
      File file = canonicalize(new File(location));
      if (isSubfile(dataDirectory, file)) {
        File curr = file;
        String path = null;
        // paranoid check to avoid infinite loops
        while (curr != null && !curr.equals(dataDirectory)) {
          if (path == null) {
            path = curr.getName();
          } else {
            path = curr.getName() + "/" + path;
          }
          curr = curr.getParentFile();
        }
        location = "file:" + path;
      } else {
        File dataFile = Files.url(rootDir, location);
        if (dataFile != null && !dataFile.equals(file)) {
          // relative to the data directory, does not need fixing
        } else {
          location = "file://" + file.getAbsolutePath();
        }
      }
    }
    delegate.setObject(location);
  }
  private String sourceURL(String url) {
    File baseDirectory = dataDir().getResourceLoader().getBaseDirectory();

    File file = Files.url(baseDirectory, url);
    if (file != null) {
      return Paths.convert(baseDirectory, file);
    }
    return url;
  }
  private String source(URL url) {
    File baseDirectory = dataDir().getResourceLoader().getBaseDirectory();

    if (url.getProtocol().equals("file")) {
      File file = Files.url(baseDirectory, url.toExternalForm());
      if (file != null && !file.isAbsolute()) {
        return Paths.convert(baseDirectory, file);
      }
    }
    return url.toExternalForm();
  }
  /**
   * Check for the existence of the file, if the file exists do nothing.
   *
   * <p>If the file does not exist, check for a template file contained in the jar with the same
   * name, if found, use it.
   *
   * <p>If no template was found, use the default template
   *
   * @param fileName target location
   * @param namedRoot parent dir if fileName is relative
   * @param defaultResource the standard template
   * @throws IOException
   * @return the file to use
   */
  protected Resource checkORCreateJDBCPropertyFile(
      String fileName, Resource namedRoot, String defaultResource) throws IOException {

    Resource resource;
    fileName = fileName != null ? fileName : defaultResource;
    File file = new File(fileName);
    if (file.isAbsolute()) {
      resource = Files.asResource(file);
    } else {
      resource = namedRoot.get(fileName);
    }

    if (Resources.exists(resource)) {
      return resource; // we are happy
    }

    // try to find a template with the same name
    InputStream is = this.getClass().getResourceAsStream(fileName);
    if (is != null) IOUtils.copy(is, resource.out());
    else // use the default template
    FileUtils.copyURLToFile(getClass().getResource(defaultResource), file);

    return resource;
  }
 /**
  * Create from file system
  *
  * @param file the spatial file * @Depecrated Use Resource instead of File
  */
 @Deprecated
 public SpatialFile(File file) {
   this(Files.asResource(file));
 }