URL parser: Difference between revisions
Added Kotlin
(Added Kotlin) |
|||
Line 1,143:
query: a=1&b=2+2&c=3&c=4&d=%65%6e%63%6F%64%65%64
specifies_authority: true
</pre>
=={{header|Kotlin}}==
Although the java.net.URL class can parse urls just fine, unfortunately (as far as this task is concerned) the constructor throws an exception if it does not recognize the scheme (or 'protocol' as it calls it). To deal with unrecognized protocols such as 'foo', we therefore need to replace them with a valid protocol such as 'http' to trick the URL class into parsing them properly:
<lang scala>// version 1.1.2
import java.net.URL
import java.net.MalformedURLException
fun parseUrl(url: String) {
var u: URL
var scheme: String
try {
u = URL(url)
scheme = u.protocol
}
catch (ex: MalformedURLException) {
val index = url.indexOf(':')
scheme = url.take(index)
u = URL("http" + url.drop(index))
}
println("Parsing $url")
println(" scheme = $scheme")
with(u) {
if (userInfo != null) println(" userinfo = $userInfo")
if (!host.isEmpty()) println(" domain = $host")
if (port != -1) println(" port = $port")
if (!path.isEmpty()) println(" path = $path")
if (query != null) println(" query = $query")
if (ref != null) println(" fragment = $ref")
}
println()
}
fun main(args: Array<String>){
val urls = arrayOf(
"foo://example.com:8042/over/there?name=ferret#nose",
"urn:example:animal:ferret:nose",
"jdbc:mysql://test_user:ouupppssss@localhost:3306/sakila?profileSQL=true",
"ftp://ftp.is.co.za/rfc/rfc1808.txt",
"http://www.ietf.org/rfc/rfc2396.txt#header1",
"ldap://[2001:db8::7]/c=GB?objectClass=one&objectClass=two",
"mailto:John.Doe@example.com",
"news:comp.infosystems.www.servers.unix",
"tel:+1-816-555-1212",
"telnet://192.0.2.16:80/",
"urn:oasis:names:specification:docbook:dtd:xml:4.1.2",
"ssh://alice@example.com",
"https://bob:pass@example.com/place",
"http://example.com/?a=1&b=2+2&c=3&c=4&d=%65%6e%63%6F%64%65%64"
)
for (url in urls) parseUrl(url)
}</lang>
{{out}}
<pre>
Parsing foo://example.com:8042/over/there?name=ferret#nose
scheme = foo
domain = example.com
port = 8042
path = /over/there
query = name=ferret
fragment = nose
Parsing urn:example:animal:ferret:nose
scheme = urn
path = example:animal:ferret:nose
Parsing jdbc:mysql://test_user:ouupppssss@localhost:3306/sakila?profileSQL=true
scheme = jdbc
path = mysql://test_user:ouupppssss@localhost:3306/sakila
query = profileSQL=true
Parsing ftp://ftp.is.co.za/rfc/rfc1808.txt
scheme = ftp
domain = ftp.is.co.za
path = /rfc/rfc1808.txt
Parsing http://www.ietf.org/rfc/rfc2396.txt#header1
scheme = http
domain = www.ietf.org
path = /rfc/rfc2396.txt
fragment = header1
Parsing ldap://[2001:db8::7]/c=GB?objectClass=one&objectClass=two
scheme = ldap
domain = [2001:db8::7]
path = /c=GB
query = objectClass=one&objectClass=two
Parsing mailto:John.Doe@example.com
scheme = mailto
path = John.Doe@example.com
Parsing news:comp.infosystems.www.servers.unix
scheme = news
path = comp.infosystems.www.servers.unix
Parsing tel:+1-816-555-1212
scheme = tel
path = +1-816-555-1212
Parsing telnet://192.0.2.16:80/
scheme = telnet
domain = 192.0.2.16
port = 80
path = /
Parsing urn:oasis:names:specification:docbook:dtd:xml:4.1.2
scheme = urn
path = oasis:names:specification:docbook:dtd:xml:4.1.2
Parsing ssh://alice@example.com
scheme = ssh
userinfo = alice
domain = example.com
Parsing https://bob:pass@example.com/place
scheme = https
userinfo = bob:pass
domain = example.com
path = /place
Parsing http://example.com/?a=1&b=2+2&c=3&c=4&d=%65%6e%63%6F%64%65%64
scheme = http
domain = example.com
path = /
query = a=1&b=2+2&c=3&c=4&d=%65%6e%63%6F%64%65%64
</pre>
|