author | wenzelm |
Mon, 03 Dec 2018 15:15:54 +0100 | |
changeset 69394 | f3240f3aa698 |
parent 67245 | caa4c9001009 |
child 69901 | 20b32ade0024 |
permissions | -rw-r--r-- |
56501
5fda9e5c5874
basic URL operations (with Isabelle/Scala error handling);
wenzelm
parents:
diff
changeset
|
1 |
/* Title: Pure/General/url.scala |
5fda9e5c5874
basic URL operations (with Isabelle/Scala error handling);
wenzelm
parents:
diff
changeset
|
2 |
Author: Makarius |
5fda9e5c5874
basic URL operations (with Isabelle/Scala error handling);
wenzelm
parents:
diff
changeset
|
3 |
|
5fda9e5c5874
basic URL operations (with Isabelle/Scala error handling);
wenzelm
parents:
diff
changeset
|
4 |
Basic URL operations. |
5fda9e5c5874
basic URL operations (with Isabelle/Scala error handling);
wenzelm
parents:
diff
changeset
|
5 |
*/ |
5fda9e5c5874
basic URL operations (with Isabelle/Scala error handling);
wenzelm
parents:
diff
changeset
|
6 |
|
5fda9e5c5874
basic URL operations (with Isabelle/Scala error handling);
wenzelm
parents:
diff
changeset
|
7 |
package isabelle |
5fda9e5c5874
basic URL operations (with Isabelle/Scala error handling);
wenzelm
parents:
diff
changeset
|
8 |
|
5fda9e5c5874
basic URL operations (with Isabelle/Scala error handling);
wenzelm
parents:
diff
changeset
|
9 |
|
64729 | 10 |
import java.io.{File => JFile} |
65188 | 11 |
import java.nio.file.{Paths, FileSystemNotFoundException} |
67245 | 12 |
import java.net.{URI, URISyntaxException, URL, MalformedURLException, URLDecoder, URLEncoder} |
63642 | 13 |
import java.util.zip.GZIPInputStream |
56501
5fda9e5c5874
basic URL operations (with Isabelle/Scala error handling);
wenzelm
parents:
diff
changeset
|
14 |
|
5fda9e5c5874
basic URL operations (with Isabelle/Scala error handling);
wenzelm
parents:
diff
changeset
|
15 |
|
5fda9e5c5874
basic URL operations (with Isabelle/Scala error handling);
wenzelm
parents:
diff
changeset
|
16 |
object Url |
5fda9e5c5874
basic URL operations (with Isabelle/Scala error handling);
wenzelm
parents:
diff
changeset
|
17 |
{ |
62248 | 18 |
def escape(name: String): String = |
19 |
(for (c <- name.iterator) yield if (c == '\'') "%27" else new String(Array(c))).mkString |
|
20 |
||
56501
5fda9e5c5874
basic URL operations (with Isabelle/Scala error handling);
wenzelm
parents:
diff
changeset
|
21 |
def apply(name: String): URL = |
5fda9e5c5874
basic URL operations (with Isabelle/Scala error handling);
wenzelm
parents:
diff
changeset
|
22 |
{ |
5fda9e5c5874
basic URL operations (with Isabelle/Scala error handling);
wenzelm
parents:
diff
changeset
|
23 |
try { new URL(name) } |
5fda9e5c5874
basic URL operations (with Isabelle/Scala error handling);
wenzelm
parents:
diff
changeset
|
24 |
catch { case _: MalformedURLException => error("Malformed URL " + quote(name)) } |
5fda9e5c5874
basic URL operations (with Isabelle/Scala error handling);
wenzelm
parents:
diff
changeset
|
25 |
} |
5fda9e5c5874
basic URL operations (with Isabelle/Scala error handling);
wenzelm
parents:
diff
changeset
|
26 |
|
5fda9e5c5874
basic URL operations (with Isabelle/Scala error handling);
wenzelm
parents:
diff
changeset
|
27 |
def is_wellformed(name: String): Boolean = |
5fda9e5c5874
basic URL operations (with Isabelle/Scala error handling);
wenzelm
parents:
diff
changeset
|
28 |
try { Url(name); true } |
5fda9e5c5874
basic URL operations (with Isabelle/Scala error handling);
wenzelm
parents:
diff
changeset
|
29 |
catch { case ERROR(_) => false } |
5fda9e5c5874
basic URL operations (with Isabelle/Scala error handling);
wenzelm
parents:
diff
changeset
|
30 |
|
5fda9e5c5874
basic URL operations (with Isabelle/Scala error handling);
wenzelm
parents:
diff
changeset
|
31 |
def is_readable(name: String): Boolean = |
5fda9e5c5874
basic URL operations (with Isabelle/Scala error handling);
wenzelm
parents:
diff
changeset
|
32 |
try { Url(name).openStream.close; true } |
5fda9e5c5874
basic URL operations (with Isabelle/Scala error handling);
wenzelm
parents:
diff
changeset
|
33 |
catch { case ERROR(_) => false } |
5fda9e5c5874
basic URL operations (with Isabelle/Scala error handling);
wenzelm
parents:
diff
changeset
|
34 |
|
63642 | 35 |
|
67245 | 36 |
/* strings */ |
37 |
||
38 |
def decode(s: String): String = URLDecoder.decode(s, UTF8.charset_name) |
|
39 |
def encode(s: String): String = URLEncoder.encode(s, UTF8.charset_name) |
|
40 |
||
41 |
||
63642 | 42 |
/* read */ |
43 |
||
63645 | 44 |
private def read(url: URL, gzip: Boolean): String = |
65069 | 45 |
using(url.openStream)(stream => |
46 |
File.read_stream(if (gzip) new GZIPInputStream(stream) else stream)) |
|
63642 | 47 |
|
63645 | 48 |
def read(url: URL): String = read(url, false) |
49 |
def read_gzip(url: URL): String = read(url, true) |
|
50 |
||
51 |
def read(name: String): String = read(Url(name), false) |
|
52 |
def read_gzip(name: String): String = read(Url(name), true) |
|
64729 | 53 |
|
69394 | 54 |
def read_bytes(url: URL): Bytes = |
55 |
{ |
|
56 |
val connection = url.openConnection |
|
57 |
val length = connection.getContentLength |
|
58 |
using(connection.getInputStream)(Bytes.read_stream(_, hint = length)) |
|
59 |
} |
|
60 |
||
64729 | 61 |
|
62 |
/* file URIs */ |
|
63 |
||
66234 | 64 |
def print_file(file: JFile): String = File.absolute(file).toPath.toUri.toString |
64777
ca09695eb43c
clarified Document.Node.Name (again): canonical platform file;
wenzelm
parents:
64775
diff
changeset
|
65 |
def print_file_name(name: String): String = print_file(new JFile(name)) |
64775 | 66 |
|
67 |
def parse_file(uri: String): JFile = Paths.get(new URI(uri)).toFile |
|
64729 | 68 |
|
69 |
def is_wellformed_file(uri: String): Boolean = |
|
64775 | 70 |
try { parse_file(uri); true } |
65188 | 71 |
catch { |
72 |
case _: URISyntaxException | _: IllegalArgumentException | _: FileSystemNotFoundException => |
|
73 |
false |
|
74 |
} |
|
64730 | 75 |
|
66235
d4fa51e7c4ff
retain symlinks in file names from VSCode: relevant for proper file locations in decorations etc.;
wenzelm
parents:
66234
diff
changeset
|
76 |
def absolute_file(uri: String): JFile = File.absolute(parse_file(uri)) |
d4fa51e7c4ff
retain symlinks in file names from VSCode: relevant for proper file locations in decorations etc.;
wenzelm
parents:
66234
diff
changeset
|
77 |
def absolute_file_name(uri: String): String = absolute_file(uri).getPath |
d4fa51e7c4ff
retain symlinks in file names from VSCode: relevant for proper file locations in decorations etc.;
wenzelm
parents:
66234
diff
changeset
|
78 |
|
66234 | 79 |
def canonical_file(uri: String): JFile = File.canonical(parse_file(uri)) |
64777
ca09695eb43c
clarified Document.Node.Name (again): canonical platform file;
wenzelm
parents:
64775
diff
changeset
|
80 |
def canonical_file_name(uri: String): String = canonical_file(uri).getPath |
56501
5fda9e5c5874
basic URL operations (with Isabelle/Scala error handling);
wenzelm
parents:
diff
changeset
|
81 |
} |