org.apache.tools.ant.types.Resource.as()方法的使用及代码示例

x33g5p2x  于2022-01-29 转载在 其他  
字(7.6k)|赞(0)|评价(0)|浏览(80)

本文整理了Java中org.apache.tools.ant.types.Resource.as方法的一些代码示例,展示了Resource.as的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Resource.as方法的具体详情如下:
包路径:org.apache.tools.ant.types.Resource
类名称:Resource
方法名:as

Resource.as介绍

[英]Returns a view of this resource that implements the interface given as the argument or null if there is no such view.

This allows extension interfaces to be added to resources without growing the number of permutations of interfaces decorators/adapters need to implement.

This implementation of the method will return the current instance itself if it can be assigned to the given class.
[中]返回此资源的视图,该视图实现作为参数给定的接口,如果没有此类视图,则返回null。
这允许将扩展接口添加到资源中,而无需增加需要实现的接口装饰器/适配器的排列数量。
如果可以将当前实例分配给给定的类,则该方法的实现将返回当前实例本身。

代码示例

代码示例来源:origin: org.apache.ant/ant

/**
 * Return {@link #as(Class)} as an {@link Optional}.
 * @param <T> desired type
 * @param clazz a class
 * @return {@link Optional} resource of a desired type
 * @since Ant 1.10.2
 */
public <T> Optional<T> asOptional(Class<T> clazz) {
  return Optional.ofNullable(as(clazz));
}

代码示例来源:origin: org.apache.ant/ant

/**
 * {@inheritDoc}
 */
@Override
public <T> T as(Class<T> clazz) {
  return getResource().as(clazz);
}

代码示例来源:origin: org.apache.ant/ant

/**
 * Suppress FileProvider
 * @param clazz the type to implement
 */
@Override
public <T> T as(Class<T> clazz) {
  return FileProvider.class.isAssignableFrom(clazz)
      ? null : getResource().as(clazz);
}

代码示例来源:origin: org.apache.ant/ant

private Stream<File> directories(ResourceCollection rc) {
    return rc.stream().map(r -> r.as(FileProvider.class))
      .filter(Objects::nonNull).map(FileProvider::getFile)
      .filter(File::isDirectory);
  }
}

代码示例来源:origin: org.apache.ant/ant

/**
 * @return the file attribute.
 */
protected File getFile() {
  FileProvider fp = src.as(FileProvider.class);
  return fp != null ? fp.getFile() : null;
}

代码示例来源:origin: org.apache.ant/ant

/**
 * Resource to write to.
 * @param output the Resource to write to.
 * @since Ant 1.8
 */
public void setOutput(Resource output) {
  if (this.output != null) {
    throw new BuildException("Cannot set > 1 output target");
  }
  this.output = output;
  FileProvider fp = output.as(FileProvider.class);
  this.file = fp != null ? fp.getFile() : null;
}

代码示例来源:origin: org.apache.ant/ant

/**
 * Sets the src for scanning. This is the jar or zip file that
 * is scanned for matching entries.
 *
 * @param src the (non-null) archive resource
 */
public void setSrc(Resource src) {
  this.src = src;
  FileProvider fp = src.as(FileProvider.class);
  if (fp != null) {
    srcFile = fp.getFile();
  }
}

代码示例来源:origin: org.apache.ant/ant

/**
 * The resource to pack; required.
 * @param src resource to expand
 */
public void setSrcResource(Resource src) {
  if (src.isDirectory()) {
    throw new BuildException("the source can't be a directory");
  }
  if (src.as(FileProvider.class) != null || supportsNonFileResources()) {
    this.src = src;
  } else {
    throw new BuildException("Only FileSystem resources are supported.");
  }
}

代码示例来源:origin: org.apache.ant/ant

private String resourceToURI(final Resource resource) {
  final FileProvider fp = resource.as(FileProvider.class);
  if (fp != null) {
    return FILE_UTILS.toURI(fp.getFile().getAbsolutePath());
  }
  final URLProvider up = resource.as(URLProvider.class);
  if (up != null) {
    final URL u = up.getURL();
    return String.valueOf(u);
  } else {
    return resource.getName();
  }
}

代码示例来源:origin: org.apache.ant/ant

private static boolean areSame(final Resource resource1, final Resource resource2) throws IOException {
  if (resource1 == null || resource2 == null) {
    return false;
  }
  final FileProvider fileResource1 = resource1.as(FileProvider.class);
  final FileProvider fileResource2 = resource2.as(FileProvider.class);
  return fileResource1 != null && fileResource2 != null
      && FileUtils.getFileUtils().areSame(fileResource1.getFile(), fileResource2.getFile());
}

代码示例来源:origin: org.apache.ant/ant

public void execute() {
    for (Resource r : resources) {
      URLProvider up = r.as(URLProvider.class);
      if (up != null) {
        URL u = up.getURL();
        try {
          FileUtils.close(u.openConnection());
        } catch (IOException ex) {
          // ignore
        }
      }
    }
  }
}

代码示例来源:origin: org.apache.ant/ant

/**
 * Get the zipfile that holds this ZipResource.
 * @return the zipfile as a File.
 */
public File getZipfile() {
  FileProvider fp = getArchive().as(FileProvider.class);
  return fp.getFile();
}

代码示例来源:origin: org.apache.ant/ant

/**
 * Parse the given URL as an antlib descriptor and return the
 * content as something that can be turned into an Antlib task.
 *
 * <p>simply delegates to {@link #parseUnknownElement
 * parseUnknownElement} if the resource provides an URL and throws
 * an exception otherwise.</p>
 *
 * @since Ant 1.8.0
 */
@Override
public UnknownElement parseAntlibDescriptor(Project containingProject,
                      Resource resource) {
  URLProvider up = resource.as(URLProvider.class);
  if (up == null) {
    throw new BuildException("Unsupported resource type: " + resource);
  }
  return parseUnknownElement(containingProject, up.getURL());
}

代码示例来源:origin: org.apache.ant/ant

/**
 * @return the resource.
 */
protected Resource getResource() {
  // delegate this way around to support subclasses that
  // overwrite getFile
  File f = getFile();
  FileProvider fp = src.as(FileProvider.class);
  return f == null ? src : fp != null
      && fp.getFile().equals(f) ? src : new FileResource(f);
}

代码示例来源:origin: org.apache.ant/ant

/**
 * The resource to pack; required.
 * @param src resource to expand
 */
public void setSrcResource(Resource src) {
  if (src.isDirectory()) {
    throw new BuildException("the source can't be a directory");
  }
  FileProvider fp = src.as(FileProvider.class);
  if (fp != null) {
    source = fp.getFile();
  } else if (!supportsNonFileResources()) {
    throw new BuildException("Only FileSystem resources are supported.");
  }
  this.src = src;
}

代码示例来源:origin: org.apache.ant/ant

/**
 * Suppress FileProvider, re-implement Appendable
 */
@Override
public <T> T as(final Class<T> clazz) {
  if (Appendable.class.isAssignableFrom(clazz)) {
    if (isAppendSupported()) {
      final Appendable a = getResource().as(Appendable.class);
      if (a != null) {
        return clazz.cast((Appendable) () -> {
          OutputStream out = a.getAppendOutputStream();
          return out == null ? null : wrapStream(out);
        });
      }
    }
    return null;
  }
  return FileProvider.class.isAssignableFrom(clazz)
    ? null : getResource().as(clazz);
}

代码示例来源:origin: org.apache.ant/ant

/** {@inheritDoc}. */
public void execute() {
  if (length != null && adjust != null) {
    throw new BuildException(
        "length and adjust are mutually exclusive options");
  }
  if (length == null && adjust == null) {
    length = ZERO;
  }
  if (path == null) {
    throw new BuildException(NO_CHILD);
  }
  for (Resource r : path) {
    File f = r.as(FileProvider.class).getFile();
    if (shouldProcess(f)) {
      process(f);
    }
  }
}

代码示例来源:origin: org.apache.ant/ant

private static OutputStream getOutputStream(final Resource resource, final boolean append, final Project project)
    throws IOException {
  if (append) {
    final Appendable a = resource.as(Appendable.class);
    if (a != null) {
      return a.getAppendOutputStream();
    }
    String msg = "Appendable OutputStream not available for non-appendable resource "
      + resource + "; using plain OutputStream";
    if (project != null) {
      project.log(msg, Project.MSG_VERBOSE);
    } else {
      System.out.println(msg);
    }
  }
  return resource.getOutputStream();
}

代码示例来源:origin: org.apache.ant/ant

/**
 * Fulfill the ResourceCollection contract.
 * @return whether this Resource is a FileProvider.
 * @since Ant 1.7
 */
@Override
public boolean isFilesystemOnly() {
  return (isReference() && getCheckedRef().isFilesystemOnly())
    || this.as(FileProvider.class) != null;
}

代码示例来源:origin: org.apache.ant/ant

/**
 * make a get request, with the supplied progress and logging info.
 * All the other config parameters are set at the task level,
 * source, dest, ignoreErrors, etc.
 * @param logLevel level to log at, see {@link Project#log(String, int)}
 * @param progress progress callback; null for no-callbacks
 * @return true for a successful download, false otherwise.
 * The return value is only relevant when {@link #ignoreErrors} is true, as
 * when false all failures raise BuildExceptions.
 * @throws IOException for network trouble
 * @throws BuildException for argument errors, or other trouble when ignoreErrors
 * is false.
 * @deprecated only gets the first configured resource
 */
@Deprecated
public boolean doGet(final int logLevel, final DownloadProgress progress)
    throws IOException {
  checkAttributes();
  return doGet(sources.iterator().next().as(URLProvider.class).getURL(),
      destination, logLevel, progress);
}

相关文章