/** * Set an URL to get. * * @param u URL for the file. */ public void setSrc(final URL u) { add(new URLResource(u)); }
/** * Convenience constructor. * @param u the URL to expose. */ public URLResource(URL u) { setURL(u); }
/** * Set the URL for this URLResource. * @param u the URL to expose. */ public synchronized void setURL(URL u) { checkAttributesAllowed(); url = u; }
/** * Get the hash code for this Resource. * @return hash code as int. */ public synchronized int hashCode() { if (isReference()) { return getCheckedRef().hashCode(); } return MAGIC * ((getURL() == null) ? NULL_URL : getURL().hashCode()); }
/** * Find out whether the URL exists . * @return true if this resource exists. */ public synchronized boolean isExists() { if (isReference()) { return getCheckedRef().isExists(); } return isExists(false); }
/** * Get the size of this Resource. * @return the size, as a long, 0 if the Resource does not exist (for * compatibility with java.io.File), or UNKNOWN_SIZE if not known. */ public synchronized long getSize() { if (isReference()) { return getCheckedRef().getSize(); } if (!isExists(false)) { return 0L; } return withConnection(c -> conn.getContentLength(), UNKNOWN_SIZE); }
/** * Access the stylesheet to be used as a resource. * * @return stylesheet as a resource */ @Override protected Resource getStylesheet() { String xslname = "junit-frames.xsl"; if (NOFRAMES.equals(format)) xslname = "junit-noframes.xsl"; if (styleDir == null) { URLResource stylesheet = new URLResource(); URL stylesheetURL = getClass().getClassLoader().getResource(concat(XSL_FILE_PATH, xslname)); stylesheet.setURL(stylesheetURL); return stylesheet; } FileResource stylesheet = new FileResource(); File stylesheetFile = new File(styleDir, xslname); stylesheet.setFile(stylesheetFile); return stylesheet; }
/** * Get an OutputStream for the Resource. * @return an OutputStream to which content can be written. * @throws IOException if unable to provide the content of this * Resource as a stream. * @throws UnsupportedOperationException if OutputStreams are not * supported for this Resource type. * @throws IOException if the URL cannot be opened. */ public synchronized OutputStream getOutputStream() throws IOException { if (isReference()) { return getCheckedRef().getOutputStream(); } connect(); try { return conn.getOutputStream(); } finally { conn = null; } }
/** * Tells if the resource is a directory. * @return boolean whether the resource is a directory. */ public synchronized boolean isDirectory() { return isReference() ? getCheckedRef().isDirectory() : getName().endsWith("/"); }
/** * Ensure that we have a connection. * @throws IOException if the connection cannot be established. */ protected void connect() throws IOException { connect(Project.MSG_ERR); }
/** * Return this URLResource formatted as a String. * @return a String representation of this URLResource. */ public synchronized String toString() { return isReference() ? getCheckedRef().toString() : String.valueOf(getURL()); }
/** * Access the stylesheet to be used as a resource. * * @return stylesheet as a resource */ @Override protected Resource getStylesheet() { String xslname = "junit-frames.xsl"; if (NOFRAMES.equals(format)) xslname = "junit-noframes.xsl"; if (styleDir == null) { URLResource stylesheet = new URLResource(); URL stylesheetURL = getClass().getClassLoader().getResource(concat(XSL_FILE_PATH, xslname)); stylesheet.setURL(stylesheetURL); return stylesheet; } FileResource stylesheet = new FileResource(); File stylesheetFile = new File(styleDir, xslname); stylesheet.setFile(stylesheetFile); return stylesheet; }
/** * Tells the modification time in milliseconds since 01.01.1970 . * * @return 0 if the resource does not exist to mirror the behavior * of {@link java.io.File File}. */ public synchronized long getLastModified() { if (isReference()) { return getCheckedRef().getLastModified(); } if (!isExists(false)) { return UNKNOWN_DATETIME; } return withConnection(c -> conn.getLastModified(), UNKNOWN_DATETIME); }
/** * Get an InputStream for the Resource. * @return an InputStream containing this Resource's content. * @throws IOException if unable to provide the content of this * Resource as a stream. * @throws UnsupportedOperationException if InputStreams are not * supported for this Resource type. */ public synchronized InputStream getInputStream() throws IOException { if (isReference()) { return getCheckedRef().getInputStream(); } connect(); try { return conn.getInputStream(); } finally { conn = null; } }
private Resource getFileAttributeResource() { // Paths are relative to the build file they're imported from, // *not* the current directory (same as entity includes). if (file != null) { if (isExistingAbsoluteFile(file)) { return new FileResource(FILE_UTILS.normalize(file)); } File buildFile = new File(getLocation().getFileName()).getAbsoluteFile(); if (buildFile.exists()) { File buildFileParent = new File(buildFile.getParent()); File importedFile = FILE_UTILS.resolveFile(buildFileParent, file); return new FileResource(importedFile); } // maybe this import tasks is inside an imported URL? try { URL buildFileURL = new URL(getLocation().getFileName()); URL importedFile = new URL(buildFileURL, file); return new URLResource(importedFile); } catch (MalformedURLException ex) { log(ex.toString(), Project.MSG_VERBOSE); } throw new BuildException("failed to resolve %s relative to %s", file, getLocation().getFileName()); } return null; }
/** * Get the name of this URLResource * (its file component minus the leading separator). * @return the name of this resource. */ public synchronized String getName() { if (isReference()) { return getCheckedRef().getName(); } String name = getURL().getFile(); return name.isEmpty() ? name : name.substring(1); }
/** * Convenience constructor. * @param u holds the URL to expose. */ public URLResource(URLProvider u) { setURL(u.getURL()); }
/** * Base URL which combined with the relativePath attribute defines * the URL. * @param base URL * @since Ant 1.8.0 */ public synchronized void setBaseURL(URL base) { checkAttributesAllowed(); if (url != null) { throw new BuildException("can't define URL and baseURL attribute"); } baseURL = base; }