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

The following examples show how to use java.net.URL#toExternalForm() . 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: RelativePath.java    From sldeditor with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Convert URL to absolute/relative path.
 *
 * @param externalFileURL the external file URL
 * @param useRelativePaths the use relative paths
 * @return the string
 */
public static String convert(URL externalFileURL, boolean useRelativePaths) {
    String path = "";
    if (externalFileURL != null) {
        if (isLocalFile(externalFileURL)) {
            if (useRelativePaths) {
                path = convertRelativePaths(externalFileURL);
            } else {
                path = externalFileURL.toExternalForm();
            }
        } else {
            path = externalFileURL.toExternalForm();
        }
    }
    return path;
}
 
Example 2
Source File: HtmlCatalog.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@Override
public String getSystemID(String publicId) {
    if (publicId == null){
        return null;
    }
    for(ReaderProvider provider : providers) {
        FileObject systemIdFile = provider.getSystemId(publicId);
        if(systemIdFile != null) {
            URL url = URLMapper.findURL(systemIdFile, URLMapper.INTERNAL);
            if(url != null) {
                return url.toExternalForm();
            }
        }
    }

    return null;
}
 
Example 3
Source File: NYTEventSearchExtractor.java    From wandora with GNU General Public License v3.0 6 votes vote down vote up
@Override
public boolean _extractTopicsFrom(URL u, TopicMap tm) throws Exception {
    try {
        currentURL = u.toExternalForm();

        log("Event search extraction with " + currentURL);

        String in = IObox.doUrl(u);

        System.out.println("New York Times API returned-------------------------\n" + in
                + "\n----------------------------------------------------");
        
        JSONObject json = new JSONObject(in);
        if (json.get("num_results").toString().equals("0")){
            log("No results returned.");
        } else {
            parse(json, tm); 
        }

    } catch (Exception e) {
        e.printStackTrace();
    }
    return true;
}
 
Example 4
Source File: TestDocument.java    From gate-core with GNU Lesser General Public License v3.0 6 votes vote down vote up
/** Get the name of the test server */
public static String getTestServerName() {
  if(testServer != null)
    return testServer;
  else {
    try {
      URL url =
          Gate.getClassLoader().getResource("gate/resources/gate.ac.uk/");

      testServer = url.toExternalForm();
    }

    catch(Exception e) {
    }
    return testServer;
  }
}
 
Example 5
Source File: PhotobucketRipper.java    From ripme with MIT License 5 votes vote down vote up
@Override
public URL sanitizeURL(URL url) throws MalformedURLException {
    LOGGER.info(url);
    String u = url.toExternalForm();
    if (u.contains("?")) {
        // strip options from URL
        u = u.substring(0, u.indexOf("?"));
    }
    if (!u.endsWith("/")) {
        // append trailing slash
        u = u + "/";
    }
    return new URL(u);
}
 
Example 6
Source File: RedditRipper.java    From ripme with MIT License 5 votes vote down vote up
@Override
public URL sanitizeURL(URL url) throws MalformedURLException {
    String u = url.toExternalForm();
    // Strip '/u/' from URL
    u = u.replaceAll("reddit\\.com/u/", "reddit.com/user/");
    return new URL(u);
}
 
Example 7
Source File: DefaultGhidraProtocolHandler.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Override
public GhidraProtocolConnector getConnector(URL ghidraUrl) throws MalformedURLException {
	String protocol = ghidraUrl.getProtocol();
	if (protocol != null) {
		if (ghidraUrl.getAuthority() == null) {
			return new DefaultLocalGhidraProtocolConnector(ghidraUrl);
		}
		return new DefaultGhidraProtocolConnector(ghidraUrl);
	}
	throw new MalformedURLException(
		"Unsupported URL form for ghidra protocol: " + ghidraUrl.toExternalForm());
}
 
Example 8
Source File: FileUtils.java    From gama with GNU General Public License v3.0 5 votes vote down vote up
@SuppressWarnings ("deprecation")
public static String fetchToTempFile(final IScope scope, final URL url) {
	String pathName = constructRelativeTempFilePath(scope, url);
	final String urlPath = url.toExternalForm();
	final String status = "Downloading file " + urlPath.substring(urlPath.lastIndexOf(SEPARATOR));
	scope.getGui().getStatus(scope).beginSubStatus(status);
	final Webb web = WEB.get();
	try {
		try (InputStream in = web.get(urlPath).ensureSuccess()
				.connectTimeout(GamaPreferences.External.CORE_HTTP_CONNECT_TIMEOUT.getValue())
				.readTimeout(GamaPreferences.External.CORE_HTTP_READ_TIMEOUT.getValue())
				.retry(GamaPreferences.External.CORE_HTTP_RETRY_NUMBER.getValue(), false).asStream().getBody();) {
			// final java.net.URI uri = URIUtil.toURI(pathName);
			pathName = ROOT.getPathVariableManager().resolvePath(new Path(pathName)).toOSString();
			// pathName = ROOT.getPathVariableManager().resolveURI(uri).getPath();
			final java.nio.file.Path p = new File(pathName).toPath();
			if (Files.exists(p)) {
				Files.delete(p);
			}
			Files.copy(in, p);
		}
	} catch (final IOException | WebbException e) {
		throw GamaRuntimeException.create(e, scope);
	} finally {
		scope.getGui().getStatus(scope).endSubStatus(status);
	}
	return pathName;
}
 
Example 9
Source File: ParallelWorldClassLoader.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Given the URL inside jar, returns the URL to the jar itself.
 */
public static URL toJarUrl(URL res) throws ClassNotFoundException, MalformedURLException {
    String url = res.toExternalForm();
    if(!url.startsWith("jar:"))
        throw new ClassNotFoundException("Loaded outside a jar "+url);
    url = url.substring(4); // cut off jar:
    url = url.substring(0,url.lastIndexOf('!'));    // cut off everything after '!'
    url = url.replaceAll(" ", "%20"); // support white spaces in path
    return new URL(url);
}
 
Example 10
Source File: XmlUtils.java    From TranskribusCore with GNU General Public License v3.0 5 votes vote down vote up
public static boolean isValid(File xmlFile, URL schemaUrl) throws IOException {
	if(xmlFile == null || schemaUrl == null) {
		throw new IllegalArgumentException("An argument is null.");
	}
	SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
	Schema schema;
	try {
		schema = schemaFactory.newSchema(schemaUrl);
	} catch (SAXException e) {
		throw new IllegalArgumentException("Schema could not be parsed! " + schemaUrl.toExternalForm());
	}
	return isValid(xmlFile, schema);
}
 
Example 11
Source File: SimpleFetcherBolt.java    From storm-crawler with Apache License 2.0 5 votes vote down vote up
private String getPolitenessKey(URL u) {
    String key;
    if (QUEUE_MODE_IP.equalsIgnoreCase(queueMode)) {
        try {
            final InetAddress addr = InetAddress.getByName(u.getHost());
            key = addr.getHostAddress();
        } catch (final UnknownHostException e) {
            // unable to resolve it, so don't fall back to host name
            LOG.warn("Unable to resolve: {}, skipping.", u.getHost());
            return null;
        }
    } else if (QUEUE_MODE_DOMAIN.equalsIgnoreCase(queueMode)) {
        key = PaidLevelDomain.getPLD(u.getHost());
        if (key == null) {
            LOG.warn("Unknown domain for url: {}, using hostname as key",
                    u.toExternalForm());
            key = u.getHost();
        }
    } else {
        key = u.getHost();
        if (key == null) {
            LOG.warn("Unknown host for url: {}, using URL string as key",
                    u.toExternalForm());
            key = u.toExternalForm();
        }
    }
    return key.toLowerCase(Locale.ROOT);
}
 
Example 12
Source File: ServiceDiscoveryImpl.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
public void registerConfs( ClassLoader classLoader, URL url ) {
    log.debug("Loading kie.conf from  " + url + " in classloader " + classLoader);

    try ( BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream())) ) {
        for (String line = br.readLine(); line != null; line = br.readLine()) {
            // DROOLS-2122: parsing with Properties.load a Drools version 6 kie.conf, hence skipping this entry
            if (line.contains( "=" ) && !line.contains( "[" )) {
                String[] entry = line.split( "=" );
                processKieService( classLoader, entry[0].trim(), entry[1].trim() );
            }
        }
    } catch (Exception exc) {
        throw new RuntimeException( "Unable to build kie service url = " + url.toExternalForm(), exc );
    }
}
 
Example 13
Source File: HTMLEditorKit.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
private void activateLink(String href, HTMLDocument doc,
                          JEditorPane editor, int offset) {
    try {
        URL page =
            (URL)doc.getProperty(Document.StreamDescriptionProperty);
        URL url = new URL(page, href);
        HyperlinkEvent linkEvent = new HyperlinkEvent
            (editor, HyperlinkEvent.EventType.
             ACTIVATED, url, url.toExternalForm(),
             doc.getCharacterElement(offset));
        editor.fireHyperlinkUpdate(linkEvent);
    } catch (MalformedURLException m) {
    }
}
 
Example 14
Source File: FetcherBolt.java    From storm-crawler with Apache License 2.0 4 votes vote down vote up
/**
 * Create an item. Queue id will be created based on
 * <code>queueMode</code> argument, either as a protocol + hostname
 * pair, protocol + IP address pair or protocol+domain pair.
 */

public static FetchItem create(URL u, String url, Tuple t,
        String queueMode) {

    String queueID;

    String key = null;
    // reuse any key that might have been given
    // be it the hostname, domain or IP
    if (t.contains("key")) {
        key = t.getStringByField("key");
    }
    if (StringUtils.isNotBlank(key)) {
        queueID = key.toLowerCase(Locale.ROOT);
        return new FetchItem(url, t, queueID);
    }

    if (FetchItemQueues.QUEUE_MODE_IP.equalsIgnoreCase(queueMode)) {
        try {
            final InetAddress addr = InetAddress.getByName(u.getHost());
            key = addr.getHostAddress();
        } catch (final UnknownHostException e) {
            LOG.warn(
                    "Unable to resolve IP for {}, using hostname as key.",
                    u.getHost());
            key = u.getHost();
        }
    } else if (FetchItemQueues.QUEUE_MODE_DOMAIN
            .equalsIgnoreCase(queueMode)) {
        key = PaidLevelDomain.getPLD(u.getHost());
        if (key == null) {
            LOG.warn(
                    "Unknown domain for url: {}, using hostname as key",
                    url);
            key = u.getHost();
        }
    } else {
        key = u.getHost();
    }

    if (key == null) {
        LOG.warn("Unknown host for url: {}, using URL string as key",
                url);
        key = u.toExternalForm();
    }

    queueID = key.toLowerCase(Locale.ROOT);
    return new FetchItem(url, t, queueID);
}
 
Example 15
Source File: XbaseDeclarativeHoverSignatureProvider.java    From xtext-eclipse with Eclipse Public License 2.0 4 votes vote down vote up
protected String getImageTagLink(ImageDescriptor imageDescriptor) {
	URL url = getURL(imageDescriptor);
	if (url != null)
		return "<image src='" + url.toExternalForm() + "'/>";
	return "";
}
 
Example 16
Source File: JAXPFinder.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
/**
 * Return the string form of the URL for the jar file that contains
 * whichever JAXP parser implementation is picked up from the user's
 * classpath.  If the JAXP parser is not in the user's classpath,
 * then it must be embedded within the JVM (either implicitly or else
 * through use of "endorsed standards" jars), in which case we return
 * null.
 *
 * NOTE: Assumption is that we only get here if we know there is in
 * fact a JAXP parser available to the JVM.  I.e. if a call to
 * the "classpathHasXalanAndJAXP()" method of junit/XML.java returns
 * true.
 */
protected static String getJAXPParserLocation()
{
    // Only get the URL if we have not already done it.
    if (jaxpURLString == null)
    {
        /* Figure out which JAXP implementation we have by
         * instantiating a DocumentBuilderFactory and then getting
         * the implementation-specific class for that object.
         * Note that we cannot just use:
         *
         *   SecurityManagerSetup.getURL(DocumentBuilderFactory.class)
         *
         * because the 1.4, 1.5, and 1.6 JVMs (at least, Sun and IBM)
         * all embed the JAXP API classes, so any attempts to look
         * up the URL for DocumentBuilderFactory.class will return
         * null for those JVMs. But in the case of, say, Sun 1.5, the
         * JAXP *implementation* classes are not embedded. So if we're
         * running with Sun 1.5 and we have an external JAXP
         * implementation (say Xerces) in the classpath, we need to
         * find the URL for that external jar file. By instantiating
         * DocumentBuilderFactory and then using the implementation-
         * specific class name we ensure that, for external (w.r.t the
         * JVM) JAXP implementations, we can find the implementation
         * jar file and thus we can assign the correct permissions.
         */
        URL jaxpURL = SecurityManagerSetup.getURL(
            DocumentBuilderFactory.newInstance().getClass());

        /* If we found a URL then the JAXP parser is in the classpath
         * in some jar external to the JVM; in that case we have the
         * the jar's location so we use/return that.  Otherwise we
         * assume that the JAXP parser is either embedded within the
         * JVM or else "endorsed" by it. In those cases we set our
         * URL string to be the empty string, which is non-null and
         * thus we will only execute this try-catch once.
         */
        jaxpURLString =
            (jaxpURL == null) ? "" : jaxpURL.toExternalForm();
    }

    // If we didn't find the JAXP parser URL, then return null.
    return ((jaxpURLString.length() == 0) ? null : jaxpURLString);
}
 
Example 17
Source File: ChanRipper.java    From ripme with MIT License 4 votes vote down vote up
/**
 * For example the archives are all known. (Check 4chan-x)
 * Should be based on the software the specific chan uses.
 * FoolFuuka uses the same (url) layout as 4chan
 *
 * @param url
 * @return
 *      The thread id in string form
 * @throws java.net.MalformedURLException */
@Override
public String getGID(URL url) throws MalformedURLException {
    Pattern p;
    Matcher m;

    String u = url.toExternalForm();
    if (u.contains("/thread/") || u.contains("/res/") || u.contains("yuki.la") || u.contains("55chan.org")) {
        p = Pattern.compile("^.*\\.[a-z]{1,4}/[a-zA-Z0-9]+/(thread|res)/([0-9]+)(\\.html|\\.php)?.*$");
        m = p.matcher(u);
        if (m.matches()) {
            return m.group(2);
        }

        // Drawchan is weird, has drawchan.net/dc/dw/res/####.html
        p = Pattern.compile("^.*\\.[a-z]{1,3}/[a-zA-Z0-9]+/[a-zA-Z0-9]+/res/([0-9]+)(\\.html|\\.php)?.*$");
        m = p.matcher(u);
        if (m.matches()) {
            return m.group(1);
        }
        // xchan
        p = Pattern.compile("^.*\\.[a-z]{1,3}/board/[a-zA-Z0-9]+/thread/([0-9]+)/?.*$");
        m = p.matcher(u);
        if (m.matches()) {
            return m.group(1);
        }

        // yuki.la
        p = Pattern.compile("https?://yuki.la/[a-zA-Z0-9]+/([0-9]+)");
        m = p.matcher(u);
        if (m.matches()) {
            return m.group(1);
        }

        //55chan.org
        p = Pattern.compile("https?://55chan.org/[a-z0-9]+/(res|thread)/[0-9]+.html");
        m = p.matcher(u);
        if (m.matches()) {
            return m.group(1);
        }
    }

    throw new MalformedURLException(
            "Expected *chan URL formats: "
                    + ".*/@/(res|thread)/####.html"
                    + " Got: " + u);
}
 
Example 18
Source File: SimpleConfigOrigin.java    From PlayerVaults with GNU General Public License v3.0 4 votes vote down vote up
SimpleConfigOrigin addURL(URL url) {
    return new SimpleConfigOrigin(this.description, this.lineNumber, this.endLineNumber, this.originType,
            url != null ? url.toExternalForm() : null, this.resourceOrNull, this.commentsOrNull);
}
 
Example 19
Source File: WSEndpointReference.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * @see #WSEndpointReference(String, AddressingVersion)
 */
public WSEndpointReference(URL address, AddressingVersion version) {
    this(address.toExternalForm(), version);
}
 
Example 20
Source File: EmbeddedHttpGitServer.java    From smart-testing with Apache License 2.0 2 votes vote down vote up
/**
 * Creates {@link EmbeddedHttpGitServer} serving repository imported from remote repository
 * the file system.
 *
 * @param name under which the repository will be served
 * @param url url of remote repository
 */
public static EmbeddedHttpGitServerBuilder fromUrl(String name, URL url) {
    return new EmbeddedHttpGitServerBuilder(name, url.toExternalForm());
}