author | wenzelm |
Fri, 12 Mar 2021 19:46:37 +0100 | |
changeset 73416 | 08aa4c1ed579 |
parent 73367 | 77ef8bef0593 |
child 73417 | 1dcc2b228b8b |
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} |
69901 | 13 |
import java.util.Locale |
63642 | 14 |
import java.util.zip.GZIPInputStream |
56501
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 |
|
5fda9e5c5874
basic URL operations (with Isabelle/Scala error handling);
wenzelm
parents:
diff
changeset
|
17 |
object Url |
5fda9e5c5874
basic URL operations (with Isabelle/Scala error handling);
wenzelm
parents:
diff
changeset
|
18 |
{ |
69901 | 19 |
/* special characters */ |
20 |
||
21 |
def escape_special(c: Char): String = |
|
71163 | 22 |
if ("!#$&'()*+,/:;=?@[]".contains(c)) { |
23 |
String.format(Locale.ROOT, "%%%02X", Integer.valueOf(c.toInt)) |
|
24 |
} |
|
69901 | 25 |
else c.toString |
26 |
||
71601 | 27 |
def escape_special(s: String): String = s.iterator.map(escape_special).mkString |
69901 | 28 |
|
29 |
def escape_name(name: String): String = |
|
30 |
name.iterator.map({ case '\'' => "%27" case c => c.toString }).mkString |
|
31 |
||
32 |
||
33 |
/* make and check URLs */ |
|
62248 | 34 |
|
56501
5fda9e5c5874
basic URL operations (with Isabelle/Scala error handling);
wenzelm
parents:
diff
changeset
|
35 |
def apply(name: String): URL = |
5fda9e5c5874
basic URL operations (with Isabelle/Scala error handling);
wenzelm
parents:
diff
changeset
|
36 |
{ |
5fda9e5c5874
basic URL operations (with Isabelle/Scala error handling);
wenzelm
parents:
diff
changeset
|
37 |
try { new URL(name) } |
5fda9e5c5874
basic URL operations (with Isabelle/Scala error handling);
wenzelm
parents:
diff
changeset
|
38 |
catch { case _: MalformedURLException => error("Malformed URL " + quote(name)) } |
5fda9e5c5874
basic URL operations (with Isabelle/Scala error handling);
wenzelm
parents:
diff
changeset
|
39 |
} |
5fda9e5c5874
basic URL operations (with Isabelle/Scala error handling);
wenzelm
parents:
diff
changeset
|
40 |
|
5fda9e5c5874
basic URL operations (with Isabelle/Scala error handling);
wenzelm
parents:
diff
changeset
|
41 |
def is_wellformed(name: String): Boolean = |
5fda9e5c5874
basic URL operations (with Isabelle/Scala error handling);
wenzelm
parents:
diff
changeset
|
42 |
try { Url(name); true } |
5fda9e5c5874
basic URL operations (with Isabelle/Scala error handling);
wenzelm
parents:
diff
changeset
|
43 |
catch { case ERROR(_) => false } |
5fda9e5c5874
basic URL operations (with Isabelle/Scala error handling);
wenzelm
parents:
diff
changeset
|
44 |
|
5fda9e5c5874
basic URL operations (with Isabelle/Scala error handling);
wenzelm
parents:
diff
changeset
|
45 |
def is_readable(name: String): Boolean = |
73367 | 46 |
try { Url(name).openStream.close(); true } |
56501
5fda9e5c5874
basic URL operations (with Isabelle/Scala error handling);
wenzelm
parents:
diff
changeset
|
47 |
catch { case ERROR(_) => false } |
5fda9e5c5874
basic URL operations (with Isabelle/Scala error handling);
wenzelm
parents:
diff
changeset
|
48 |
|
63642 | 49 |
|
72558 | 50 |
/* trim index */ |
51 |
||
52 |
def trim_index(url: URL): URL = |
|
53 |
{ |
|
54 |
Library.try_unprefix("/index.html", url.toString) match { |
|
55 |
case Some(u) => Url(u) |
|
56 |
case None => |
|
57 |
Library.try_unprefix("/index.php", url.toString) match { |
|
58 |
case Some(u) => Url(u) |
|
59 |
case None => url |
|
60 |
} |
|
61 |
} |
|
62 |
} |
|
63 |
||
64 |
||
67245 | 65 |
/* strings */ |
66 |
||
67 |
def decode(s: String): String = URLDecoder.decode(s, UTF8.charset_name) |
|
68 |
def encode(s: String): String = URLEncoder.encode(s, UTF8.charset_name) |
|
69 |
||
70 |
||
63642 | 71 |
/* read */ |
72 |
||
63645 | 73 |
private def read(url: URL, gzip: Boolean): String = |
65069 | 74 |
using(url.openStream)(stream => |
75 |
File.read_stream(if (gzip) new GZIPInputStream(stream) else stream)) |
|
63642 | 76 |
|
63645 | 77 |
def read(url: URL): String = read(url, false) |
78 |
def read_gzip(url: URL): String = read(url, true) |
|
79 |
||
80 |
def read(name: String): String = read(Url(name), false) |
|
81 |
def read_gzip(name: String): String = read(Url(name), true) |
|
64729 | 82 |
|
83 |
||
84 |
/* file URIs */ |
|
85 |
||
66234 | 86 |
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
|
87 |
def print_file_name(name: String): String = print_file(new JFile(name)) |
64775 | 88 |
|
89 |
def parse_file(uri: String): JFile = Paths.get(new URI(uri)).toFile |
|
64729 | 90 |
|
91 |
def is_wellformed_file(uri: String): Boolean = |
|
64775 | 92 |
try { parse_file(uri); true } |
65188 | 93 |
catch { |
94 |
case _: URISyntaxException | _: IllegalArgumentException | _: FileSystemNotFoundException => |
|
95 |
false |
|
96 |
} |
|
64730 | 97 |
|
66235
d4fa51e7c4ff
retain symlinks in file names from VSCode: relevant for proper file locations in decorations etc.;
wenzelm
parents:
66234
diff
changeset
|
98 |
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
|
99 |
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
|
100 |
|
66234 | 101 |
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
|
102 |
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
|
103 |
} |