Java Code Examples for java.net.URL#sameFile()

The following examples show how to use java.net.URL#sameFile() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AbstractHtmlEngine.java    From ats-framework with Apache License 2.0 6 votes vote down vote up
private boolean isAlreadyLoadedByAncestor(
                                           final URL url,
                                           HtmlPage page ) {

    WebWindow window = page.getEnclosingWindow();
    while (window != null) {
        if (url.sameFile(window.getEnclosedPage().getWebResponse().getWebRequest().getUrl())) {
            return true;
        }
        if (window == window.getParentWindow()) {
            window = null;
        } else {
            window = window.getParentWindow();
        }
    }
    return false;
}
 
Example 2
Source File: FileUtilities.java    From opt4j with MIT License 5 votes vote down vote up
/**
 * Copy sourceURL to destinationFile without doing any byte conversion.
 * 
 * @param sourceURL
 *            The source URL
 * @param destinationFile
 *            The destination File.
 * @return true if the file was copied, false if the file was not copied
 *         because the sourceURL and the destinationFile refer to the same
 *         file.
 * @exception IOException
 *                If the source file does not exist.
 */
public static boolean binaryCopyURLToFile(URL sourceURL, File destinationFile) throws IOException {
	URL destinationURL = destinationFile.getCanonicalFile().toURI().toURL();

	if (sourceURL.sameFile(destinationURL)) {
		return false;
	}

	// If sourceURL is of the form file:./foo, then we need to try again.
	File sourceFile = new File(sourceURL.getFile());

	// If the sourceURL is not a jar URL, then check to see if we
	// have the same file.
	// FIXME: should we check for !/ and !\ everywhere?
	if ((sourceFile.getPath().indexOf("!/") == -1) && (sourceFile.getPath().indexOf("!\\") == -1)) {
		try {
			if (sourceFile.getCanonicalFile().toURI().toURL().sameFile(destinationURL)) {
				return false;
			}
		} catch (IOException ex) {
			// JNLP Jar urls sometimes throw an exception here.
			// IOException constructor does not take a cause
			IOException ioException = new IOException("Cannot find canonical file name of '" + sourceFile + "'");
			ioException.initCause(ex);
			throw ioException;
		}
	}

	_binaryCopyStream(sourceURL.openStream(), destinationFile);

	return true;
}
 
Example 3
Source File: JarFileResource.java    From IoTgo_Android_App with MIT License 5 votes vote down vote up
/**
 * Check if this jar:file: resource is contained in the
 * named resource. Eg <code>jar:file:///a/b/c/foo.jar!/x.html</code> isContainedIn <code>file:///a/b/c/foo.jar</code>
 * @param resource
 * @return true if resource is contained in the named resource
 * @throws MalformedURLException
 */
@Override
public boolean isContainedIn (Resource resource) 
throws MalformedURLException
{
    String string = _urlString;
    int index = string.indexOf("!/");
    if (index > 0)
        string = string.substring(0,index);
    if (string.startsWith("jar:"))
        string = string.substring(4);
    URL url = new URL(string);
    return url.sameFile(resource.getURL());     
}
 
Example 4
Source File: JarFileResource.java    From IoTgo_Android_App with MIT License 5 votes vote down vote up
/**
 * Check if this jar:file: resource is contained in the
 * named resource. Eg <code>jar:file:///a/b/c/foo.jar!/x.html</code> isContainedIn <code>file:///a/b/c/foo.jar</code>
 * @param resource
 * @return true if resource is contained in the named resource
 * @throws MalformedURLException
 */
@Override
public boolean isContainedIn (Resource resource) 
throws MalformedURLException
{
    String string = _urlString;
    int index = string.indexOf("!/");
    if (index > 0)
        string = string.substring(0,index);
    if (string.startsWith("jar:"))
        string = string.substring(4);
    URL url = new URL(string);
    return url.sameFile(resource.getURL());     
}
 
Example 5
Source File: BrowserPane.java    From SwingBox with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
public void setPage(final URL newPage) throws IOException
{
    fireGeneralEvent(new GeneralEvent(this, EventType.page_loading_begin,
            newPage, null));

    if (newPage == null)
    {
        // TODO fire general event here
        throw new IOException("invalid url");
    }
    final URL oldPage = getPage();
    Object postData = getPostData();

    if ((oldPage == null) || !oldPage.sameFile(newPage) || (postData != null))
    {
        // different url or POST method, load the new content

        final InputStream in = getStream(newPage);
        // editor kit is set according to content type
        EditorKit kit = getEditorKit();

        if (kit == null)
        {
            UIManager.getLookAndFeel().provideErrorFeedback(this);
        }
        else
        {
            document = createDocument(kit, newPage);

            int p = getAsynchronousLoadPriority(document);

            if (p < 0)
            {
                // load synchro
                loadPage(newPage, oldPage, in, document);
            }
            else
            {
                // load asynchro
                Thread t = new Thread(new Runnable()
                {
                    @Override
                    public void run()
                    {
                        loadPage(newPage, oldPage, in, document);
                    }
                });
                t.setDaemon(true);
                t.start();
            }
        }
    }
    else if (oldPage.sameFile(newPage))
    {
       if (newPage.getRef() != null)
       {
           final String reference = newPage.getRef();
           SwingUtilities.invokeLater(new Runnable()
           {
               @Override
               public void run()
               {
                   scrollToReference(reference);
               }
           });
       }
    }


}