author | wenzelm |
Fri, 21 Oct 2022 19:05:48 +0200 | |
changeset 76354 | 908433a347d1 |
parent 75393 | 87ebf5a50283 |
child 76617 | d5adc9126ae8 |
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 |
|
75393 | 17 |
object Url { |
69901 | 18 |
/* special characters */ |
19 |
||
20 |
def escape_special(c: Char): String = |
|
71163 | 21 |
if ("!#$&'()*+,/:;=?@[]".contains(c)) { |
22 |
String.format(Locale.ROOT, "%%%02X", Integer.valueOf(c.toInt)) |
|
23 |
} |
|
69901 | 24 |
else c.toString |
25 |
||
71601 | 26 |
def escape_special(s: String): String = s.iterator.map(escape_special).mkString |
69901 | 27 |
|
28 |
def escape_name(name: String): String = |
|
29 |
name.iterator.map({ case '\'' => "%27" case c => c.toString }).mkString |
|
30 |
||
31 |
||
32 |
/* make and check URLs */ |
|
62248 | 33 |
|
75393 | 34 |
def apply(name: String): URL = { |
56501
5fda9e5c5874
basic URL operations (with Isabelle/Scala error handling);
wenzelm
parents:
diff
changeset
|
35 |
try { new URL(name) } |
5fda9e5c5874
basic URL operations (with Isabelle/Scala error handling);
wenzelm
parents:
diff
changeset
|
36 |
catch { case _: MalformedURLException => error("Malformed URL " + quote(name)) } |
5fda9e5c5874
basic URL operations (with Isabelle/Scala error handling);
wenzelm
parents:
diff
changeset
|
37 |
} |
5fda9e5c5874
basic URL operations (with Isabelle/Scala error handling);
wenzelm
parents:
diff
changeset
|
38 |
|
5fda9e5c5874
basic URL operations (with Isabelle/Scala error handling);
wenzelm
parents:
diff
changeset
|
39 |
def is_wellformed(name: String): Boolean = |
5fda9e5c5874
basic URL operations (with Isabelle/Scala error handling);
wenzelm
parents:
diff
changeset
|
40 |
try { Url(name); true } |
5fda9e5c5874
basic URL operations (with Isabelle/Scala error handling);
wenzelm
parents:
diff
changeset
|
41 |
catch { case ERROR(_) => false } |
5fda9e5c5874
basic URL operations (with Isabelle/Scala error handling);
wenzelm
parents:
diff
changeset
|
42 |
|
5fda9e5c5874
basic URL operations (with Isabelle/Scala error handling);
wenzelm
parents:
diff
changeset
|
43 |
def is_readable(name: String): Boolean = |
73367 | 44 |
try { Url(name).openStream.close(); true } |
56501
5fda9e5c5874
basic URL operations (with Isabelle/Scala error handling);
wenzelm
parents:
diff
changeset
|
45 |
catch { case ERROR(_) => false } |
5fda9e5c5874
basic URL operations (with Isabelle/Scala error handling);
wenzelm
parents:
diff
changeset
|
46 |
|
63642 | 47 |
|
73417 | 48 |
/* file name */ |
49 |
||
50 |
def file_name(url: URL): String = |
|
51 |
Library.take_suffix[Char](c => c != '/' && c != '\\', url.getFile.toString.toList)._2.mkString |
|
72558 | 52 |
|
75393 | 53 |
def trim_index(url: URL): URL = { |
72558 | 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 |
||
76354 | 67 |
def decode(s: String): String = URLDecoder.decode(s, UTF8.charset) |
68 |
def encode(s: String): String = URLEncoder.encode(s, UTF8.charset) |
|
67245 | 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 |
} |