| author | wenzelm |
| Tue, 02 Jan 2018 19:52:17 +0100 | |
| changeset 67323 | d02208cefbdb |
| parent 67245 | caa4c9001009 |
| child 69394 | f3240f3aa698 |
| 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 |
|
54 |
||
55 |
/* file URIs */ |
|
56 |
||
| 66234 | 57 |
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
|
58 |
def print_file_name(name: String): String = print_file(new JFile(name)) |
| 64775 | 59 |
|
60 |
def parse_file(uri: String): JFile = Paths.get(new URI(uri)).toFile |
|
| 64729 | 61 |
|
62 |
def is_wellformed_file(uri: String): Boolean = |
|
| 64775 | 63 |
try { parse_file(uri); true }
|
| 65188 | 64 |
catch {
|
65 |
case _: URISyntaxException | _: IllegalArgumentException | _: FileSystemNotFoundException => |
|
66 |
false |
|
67 |
} |
|
| 64730 | 68 |
|
|
66235
d4fa51e7c4ff
retain symlinks in file names from VSCode: relevant for proper file locations in decorations etc.;
wenzelm
parents:
66234
diff
changeset
|
69 |
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
|
70 |
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
|
71 |
|
| 66234 | 72 |
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
|
73 |
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
|
74 |
} |