| author | wenzelm |
| Sat, 08 Apr 2023 20:26:32 +0200 | |
| changeset 77796 | f5aca3ed1adb |
| parent 76841 | b8e1c3158012 |
| child 79044 | 8cc1ae43e12e |
| 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 |
| 76617 | 103 |
|
104 |
||
| 77796 | 105 |
/* generic path notation: standard, platform, ssh, rsync, ftp, http, https */ |
| 76617 | 106 |
|
| 76828 | 107 |
private val separators1 = "/\\" |
108 |
private val separators2 = ":/\\" |
|
109 |
||
110 |
def is_base_name(s: String, suffix: String = ""): Boolean = |
|
111 |
s.nonEmpty && !s.exists(separators2.contains) && s.endsWith(suffix) |
|
112 |
||
113 |
def get_base_name(s: String, suffix: String = ""): Option[String] = {
|
|
114 |
val i = s.lastIndexWhere(separators2.contains) |
|
115 |
if (i + 1 >= s.length) None else Library.try_unsuffix(suffix, s.substring(i + 1)) |
|
116 |
} |
|
117 |
||
118 |
def strip_base_name(s: String, suffix: String = ""): Option[String] = {
|
|
119 |
val i = s.lastIndexWhere(separators2.contains) |
|
120 |
val j = s.lastIndexWhere(c => !separators1.contains(c), end = i) |
|
121 |
if (i + 1 >= s.length || !s.endsWith(suffix)) None |
|
122 |
else if (j < 0) Some(s.substring(0, i + 1)) |
|
123 |
else Some(s.substring(0, j + 1)) |
|
124 |
} |
|
125 |
||
| 76617 | 126 |
def append_path(prefix: String, suffix: String): String = |
| 76827 | 127 |
if (prefix.endsWith(":") || prefix.endsWith("/") || prefix.endsWith("\\") || prefix.isEmpty) {
|
128 |
prefix + suffix |
|
129 |
} |
|
130 |
else if (prefix.endsWith(":.") || prefix.endsWith("/.") || prefix.endsWith("\\.") || prefix == ".") {
|
|
| 76618 | 131 |
prefix.substring(0, prefix.length - 1) + suffix |
132 |
} |
|
| 76827 | 133 |
else if (prefix.contains('\\') || suffix.contains('\\')) {
|
134 |
prefix + "\\" + suffix |
|
135 |
} |
|
| 76617 | 136 |
else prefix + "/" + suffix |
| 76622 | 137 |
|
138 |
def direct_path(prefix: String): String = append_path(prefix, ".") |
|
|
56501
5fda9e5c5874
basic URL operations (with Isabelle/Scala error handling);
wenzelm
parents:
diff
changeset
|
139 |
} |