1 (* Copyright (C) 2000, HELM Team.
3 * This file is part of HELM, an Hypertextual, Electronic
4 * Library of Mathematics, developed at the Computer Science
5 * Department, University of Bologna, Italy.
7 * HELM is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * HELM is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with HELM; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
22 * For details, see the HELM World-Wide-Web page,
23 * http://cs.unibo.it/helm/.
26 (******************************************************************************)
30 (* Claudio Sacerdoti Coen <sacerdot@cs.unibo.it> *)
34 (******************************************************************************)
37 let starts_with s s' =
38 if String.length s < String.length s' then
41 (String.sub s 0 (String.length s')) = s'
43 if starts_with s "http:" then
44 ClientHTTP.get_and_save_to_tmp s
53 `Not_recognized -> Neturl.Url_part_not_recognized
54 | `Allowed -> Neturl.Url_part_allowed
55 | `Required -> Neturl.Url_part_required
57 { Neturl.null_url_syntax with
58 Neturl.url_enable_scheme = enable_if `Allowed;
59 Neturl.url_enable_host = enable_if `Allowed;
60 Neturl.url_enable_path = Neturl.Url_part_required;
61 Neturl.url_accepts_8bits = true;
65 exception Unexpected;; (* Added when porting the file to PXP 1.1 *)
67 let file_url_of_id xid =
68 let file_url_of_sysname sysname =
69 (* By convention, we can assume that sysname is a URL conforming
70 * to RFC 1738 with the exception that it may contain non-ASCII
74 Neturl.url_of_string url_syntax sysname
75 (* may raise Malformed_URL *)
77 Neturl.Malformed_URL -> raise Pxp_reader.Not_competent
81 Pxp_types.Anonymous -> raise Pxp_reader.Not_competent
82 | Pxp_types.Public (_,sysname) ->
83 let sysname = resolve sysname in
84 if sysname <> "" then file_url_of_sysname sysname
85 else raise Pxp_reader.Not_competent
86 | Pxp_types.System sysname ->
87 let sysname = resolve sysname in
88 file_url_of_sysname sysname
89 | Pxp_types.Private pid -> raise Unexpected
92 try Neturl.url_scheme url with Not_found -> "file" in
94 try Neturl.url_host url with Not_found -> "" in
96 if scheme <> "file" then raise Pxp_reader.Not_competent;
97 if host <> "" && host <> "localhost" then raise Pxp_reader.Not_competent;
102 let from_file ?system_encoding utf8_filename =
105 new Pxp_reader.resolve_as_file
106 ?system_encoding:system_encoding
107 ~url_of_id:file_url_of_id
111 let utf8_abs_filename =
112 if utf8_filename <> "" && utf8_filename.[0] = '/' then
115 Sys.getcwd() ^ "/" ^ utf8_filename
118 let syntax = { Neturl.ip_url_syntax with Neturl.url_accepts_8bits = true } in
119 let url = Neturl.make_url
122 ~path:(Neturl.split_path utf8_abs_filename)
126 let xid = Pxp_types.System (Neturl.string_of_url url) in
129 Pxp_yacc.ExtID(xid, r)
134 (* csc_pxp_reader.ml is an exact copy of PXP pxp_reader.ml *)
135 (* The only reason is to loosen the interface *)
137 class resolve_as_file
138 ?(file_prefix = (`Allowed :> Csc_pxp_reader.spec))
139 ?(host_prefix = (`Allowed :> Csc_pxp_reader.spec))
140 ?(system_encoding = `Enc_utf8)
141 ?(map_private_id = (fun _ -> raise Csc_pxp_reader.Not_competent))
142 ?(open_private_id = (fun _ -> raise Csc_pxp_reader.Not_competent))
149 `Not_recognized -> Neturl.Url_part_not_recognized
150 | `Allowed -> Neturl.Url_part_allowed
151 | `Required -> Neturl.Url_part_required
153 { Neturl.null_url_syntax with
154 Neturl.url_enable_scheme = enable_if file_prefix;
155 Neturl.url_enable_host = enable_if host_prefix;
156 Neturl.url_enable_path = Neturl.Url_part_required;
157 Neturl.url_accepts_8bits = true;
161 let base_url_syntax =
162 { Neturl.null_url_syntax with
163 Neturl.url_enable_scheme = Neturl.Url_part_required;
164 Neturl.url_enable_host = Neturl.Url_part_allowed;
165 Neturl.url_enable_path = Neturl.Url_part_required;
166 Neturl.url_accepts_8bits = true;
170 let default_base_url =
174 ~path: (Neturl.split_path (Sys.getcwd() ^ "/"))
178 let file_url_of_id xid =
179 let module P = Csc_pxp_reader in
180 let module T = Pxp_types in
181 let file_url_of_sysname sysname =
182 (* By convention, we can assume that sysname is a URL conforming
183 * to RFC 1738 with the exception that it may contain non-ASCII
187 Neturl.url_of_string url_syntax sysname
188 (* may raise Malformed_URL *)
190 Neturl.Malformed_URL -> raise P.Not_competent
194 T.Anonymous -> raise P.Not_competent
195 | T.Public (_,sysname) -> let sysname = resolve sysname in
196 if sysname <> "" then file_url_of_sysname sysname
197 else raise P.Not_competent
198 | T.System sysname -> let sysname = resolve sysname in
199 file_url_of_sysname sysname
200 | T.Private pid -> map_private_id pid
203 try Neturl.url_scheme url with Not_found -> "file" in
205 try Neturl.url_host url with Not_found -> "" in
207 if scheme <> "file" then raise P.Not_competent;
208 if host <> "" && host <> "localhost" then raise P.Not_competent;
213 let channel_of_file_url xid url =
214 let module P = Csc_pxp_reader in
215 let module T = Pxp_types in
217 T.Private pid -> open_private_id pid
221 try Neturl.join_path (Neturl.url_path ~encoded:false url)
222 with Not_found -> raise P.Not_competent
226 Netconversion.recode_string
228 ~out_enc: system_encoding
230 (* May raise Malformed_code *)
232 open_in_bin path, None
233 (* May raise Sys_error *)
236 | Netconversion.Malformed_code -> assert false
237 (* should not happen *)
238 | Sys_error _ as e ->
239 raise (P.Not_resolvable e)
243 Csc_pxp_reader.resolve_read_url_channel
244 ~base_url: default_base_url
245 ~url_of_id: file_url_of_id
246 ~channel_of_url: channel_of_file_url
250 let from_file ?(alt = []) ?system_encoding ?enc utf8_filename =
253 ?system_encoding:system_encoding
257 let url = Csc_pxp_reader.make_file_url
262 let xid = Pxp_types.System (Neturl.string_of_url url) in
264 Pxp_yacc.ExtID(xid, new Csc_pxp_reader.combine (r :: alt))