kuriouscoder
kuriouscoder

Reputation: 5582

Add scheme to URL if not present

I have existing code that uses java.net.URL instead of java.net.URI all over the codebase.

Also, the code has URL parser that parses URLs appearing in some text body. All URLs that do not have a protocol prefix, such as www.google.com, are considered malformed when converting to URL object.

Is there a clean way to handle such cases in Java?

Upvotes: -1

Views: 1348

Answers (1)

user207421
user207421

Reputation: 310913

Create a URI and see if it has a scheme. Set the scheme, or reconstruct the URI with a scheme argument, if not present. Convert to URL.

Upvotes: -1

Related Questions