Example usage for java.net URL getRef

List of usage examples for java.net URL getRef

Introduction

In this page you can find the example usage for java.net URL getRef.

Prototype

public String getRef() 

Source Link

Document

Gets the anchor (also known as the "reference") of this URL .

Usage

From source file:com.cladonia.xngreditor.URLUtilities.java

/**
 * Returns a String representation without the Username and Password.
 * //from w w w.  j a  va 2s  .  c o  m
 * @param url the url to convert to a String...
 * 
 * @return the string representation.
 */
public static String toString(URL url) {
    if (url != null) {
        StringBuffer result = new StringBuffer(url.getProtocol());
        result.append(":");

        if (url.getHost() != null && url.getHost().length() > 0) {
            result.append("//");
            result.append(url.getHost());
        }

        if (url.getPort() > 0) {
            result.append(":");
            result.append(url.getPort());
        }

        if (url.getPath() != null) {
            result.append(url.getPath());
        }

        if (url.getQuery() != null) {
            result.append('?');
            result.append(url.getQuery());
        }

        if (url.getRef() != null) {
            result.append("#");
            result.append(url.getRef());
        }

        return result.toString();
    }

    return null;
}

From source file:com.piusvelte.webcaster.MainActivity.java

@Override
public void openMedium(int parent, int child) {
    Medium m = getMediaAt(parent).get(child);
    final ContentMetadata contentMetadata = new ContentMetadata();
    contentMetadata.setTitle(m.getFile().substring(m.getFile().lastIndexOf(File.separator) + 1));

    if (mediaProtocolMessageStream != null) {
        String urlStr = String.format("http://%s/%s", mediaHost, m.getFile());

        try {/*from  w  w  w  .j  ava 2s .c o  m*/
            URL url = new URL(urlStr);
            URI uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(),
                    url.getQuery(), url.getRef());
            url = uri.toURL();
            MediaProtocolCommand cmd = mediaProtocolMessageStream.loadMedia(url.toString(), contentMetadata,
                    true);
            cmd.setListener(new MediaProtocolCommand.Listener() {
                @Override
                public void onCompleted(MediaProtocolCommand mPCommand) {
                    btnPlay.setText(getString(R.string.pause) + " " + contentMetadata.getTitle());
                    onSetVolume(0.5);
                }

                @Override
                public void onCancelled(MediaProtocolCommand mPCommand) {
                    btnPlay.setText(getString(R.string.play) + " " + contentMetadata.getTitle());
                }
            });
        } catch (IllegalStateException e) {
            Log.e(TAG, "Problem occurred with MediaProtocolCommand during loading", e);
        } catch (IOException e) {
            Log.e(TAG, "Problem opening MediaProtocolCommand during loading", e);
        } catch (URISyntaxException e) {
            Log.e(TAG, "Problem encoding URI: " + urlStr, e);
        }
    } else {
        Toast.makeText(this, "No message stream", Toast.LENGTH_SHORT).show();
    }
}

From source file:net.refractions.udig.catalog.internal.CatalogImpl.java

/**
 * Check if the provided query is a child of identifier.
 *
 * @param query/*  ww  w.j a v  a2s  .  c  o m*/
 * @param identifier
 * @return true if query may be a child of identifier
 */
private boolean matchedService(URL query, URL identifier) {
    return query.getRef() == null && URLUtils.urlEquals(query, identifier, false);
}

From source file:org.xwiki.contrib.confluence.filter.internal.input.ConfluenceConverterListener.java

private void fixInternalLinks(ResourceReference reference, boolean freestanding, Map<String, String> parameters,
        boolean begin) {
    ResourceReference fixedReference = reference;
    Map<String, String> fixedParameters = parameters;

    if (CollectionUtils.isNotEmpty(this.properties.getBaseURLs())
            && Objects.equals(reference.getType(), ResourceType.URL)) {
        for (URL baseURL : this.properties.getBaseURLs()) {
            String baseURLString = baseURL.toExternalForm();

            if (reference.getReference().startsWith(baseURLString)) {
                // Fix the URL if the format is known

                String urlString = reference.getReference();

                URL url;
                try {
                    url = new URL(urlString);
                } catch (MalformedURLException e) {
                    // Should never happen
                    this.logger.error("Wrong URL resource reference [{}]", reference, e);

                    continue;
                }//from  ww w .j  av  a2  s .c om

                String pattern = urlString.substring(baseURLString.length());
                if (pattern.isEmpty() || pattern.charAt(0) != '/') {
                    pattern = "/" + pattern;
                }

                List<String[]> urlParameters = parseURLParameters(url.getQuery());
                String urlAnchor = url.getRef();

                ResourceReference ref = fixReference(pattern, urlParameters, urlAnchor);

                if (ref != null) {
                    fixedReference = ref;
                    break;
                }
            }
        }
    }

    if (begin) {
        super.beginLink(fixedReference, freestanding, fixedParameters);
    } else {
        super.endLink(fixedReference, freestanding, fixedParameters);
    }
}

From source file:com.amytech.android.library.utils.asynchttp.AsyncHttpClient.java

/**
 * Will encode url, if not disabled, and adds params on the end of it
 *
 * @param url//  ww w .  j av a2 s.c  om
 *            String with URL, should be valid URL without params
 * @param params
 *            RequestParams to be appended on the end of URL
 * @param shouldEncodeUrl
 *            whether url should be encoded (replaces spaces with %20)
 * @return encoded url if requested with params appended if any available
 */
public static String getUrlWithQueryString(boolean shouldEncodeUrl, String url, RequestParams params) {
    if (url == null)
        return null;

    if (shouldEncodeUrl) {
        try {
            String decodedURL = URLDecoder.decode(url, "UTF-8");
            URL _url = new URL(decodedURL);
            URI _uri = new URI(_url.getProtocol(), _url.getUserInfo(), _url.getHost(), _url.getPort(),
                    _url.getPath(), _url.getQuery(), _url.getRef());
            url = _uri.toASCIIString();
        } catch (Exception ex) {
            // Should not really happen, added just for sake of validity
            Log.e(LOG_TAG, "getUrlWithQueryString encoding URL", ex);
        }
    }

    if (params != null) {
        // Construct the query string and trim it, in case it
        // includes any excessive white spaces.
        String paramString = params.getParamString().trim();

        // Only add the query string if it isn't empty and it
        // isn't equal to '?'.
        if (!paramString.equals("") && !paramString.equals("?")) {
            url += url.contains("?") ? "&" : "?";
            url += paramString;
        }
    }

    return url;
}

From source file:com.silverwrist.venice.std.TrackbackManager.java

/**
 * Given the URL of a trackback item, return the associated
 * {@link com.silverwrist.venice.std.TrackbackItem TrackbackItem} object, if it can be found.
 *
 * @param url The {@link java.net.URL URL} of the trackback item to look for.
 * @return The associated <code>TrackbackItem</code>, or <code>null</code> if it could not be found.
 * @exception com.silverwrist.venice.except.TrackbackException If there was an error looking for trackback items.
 *///w w w  .  ja va  2  s  . c  om
public TrackbackItem getItem(URL url) throws TrackbackException {
    URL normurl = url;
    if (url.getRef() != null) { // normalize the URL
        try { // we normalize it by chopping at the hashmark
            String s = url.toString();
            int n = s.lastIndexOf('#');
            normurl = new URL(s.substring(0, n));

        } // end try
        catch (MalformedURLException e) { // forget it
            normurl = url;

        } // end catch

    } // end if

    MyTrackbackItem rc = getItem(url, normurl);
    if ((rc != null) && rc.expire())
        rc = getItem(url, normurl); // expired - re-get
    if (rc != null) { // see if we need to reload the item
        if (load(url, rc.getAttributes()))
            rc = getItem(url, normurl);

    } // end if
    else { // try loading the URL directly
        load(url, null);
        rc = getItem(url, normurl);

    } // end else

    return rc;

}

From source file:saintybalboa.nutch.net.AdvancedURLNormalizer.java

public String normalize(String urlString, String scope) throws MalformedURLException {
    if ("".equals(urlString)) // permit empty
        return urlString;

    urlString = urlString.trim(); // remove extra spaces
    String ourlString = urlString;
    URL url = new URL(urlString);

    String protocol = url.getProtocol();
    String host = url.getHost().toLowerCase();
    int port = url.getPort();
    String path = url.getPath().toLowerCase();
    String queryStr = url.getQuery();

    boolean changed = false;

    if (!urlString.startsWith(protocol)) // protocol was lowercased
        changed = true;//  w w  w.  jav  a 2 s .  c  o m

    if ("http".equals(protocol) || "https".equals(protocol) || "ftp".equals(protocol)) {

        if (host != null) {
            String newHost = host.toLowerCase(); // lowercase host
            if (!host.equals(newHost)) {
                host = newHost;
                changed = true;
            }
        }

        if (port == url.getDefaultPort()) { // uses default port
            port = -1; // so don't specify it
            changed = true;
        }

        if (url.getRef() != null) { // remove the ref
            changed = true;
        }

        if (queryStr != null) {
            if (!queryStr.isEmpty() && queryStr != "?") {

                changed = true;

                //convert query param names values to lowercase. Dependent on arguments
                queryStr = formatQueryString(queryStr);
            }
        }

    }

    urlString = (queryStr != null && queryStr != "")
            ? new URL(protocol, host, port, path).toString() + "?" + queryStr
            : new URL(protocol, host, port, path).toString();

    //the url should be the same as the url passed in
    if (ourlString.length() > urlString.length())
        urlString = urlString + ourlString.substring(urlString.length(), ourlString.length());

    return urlString;
}

From source file:org.apache.pig.backend.hadoop.executionengine.mapReduceLayer.JobControlCompiler.java

private static Path getExistingDistCacheFilePath(Configuration conf, URL url) throws IOException {
    URI[] cacheFileUris = DistributedCache.getCacheFiles(conf);
    if (cacheFileUris != null) {
        String fileName = url.getRef() == null ? FilenameUtils.getName(url.getPath()) : url.getRef();
        for (URI cacheFileUri : cacheFileUris) {
            Path path = new Path(cacheFileUri);
            String cacheFileName = cacheFileUri.getFragment() == null ? path.getName()
                    : cacheFileUri.getFragment();
            // Match
            //     - if both filenames are same and no symlinks (or)
            //     - if both symlinks are same (or)
            //     - symlink of existing cache file is same as the name of the new file to be added.
            //         That would be the case when hbase-0.98.4.jar#hbase.jar is configured via Oozie
            // and register hbase.jar is done in the pig script.
            // If two different files are symlinked to the same name, then there is a conflict
            // and hadoop itself does not guarantee which file will be symlinked to that name.
            // So we are good.
            if (fileName.equals(cacheFileName)) {
                return path;
            }/*  w  w  w .  ja  va 2s  .com*/
        }
    }
    return null;
}

From source file:com.machinepublishers.jbrowserdriver.StreamConnection.java

StreamConnection(URL url) throws MalformedURLException {
    super(url);//from  ww w  . java2  s . c  om
    this.url = new URL(url.getProtocol(), url.getHost(), url.getPort(), url.getFile());
    this.urlString = url.toExternalForm();
    this.urlFragment = url.getRef();
}