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/.
27 * "cic:/a/b/c.con" => [| "cic:/a" ; "cic:/a/b" ; "cic:/a/b/c.con" ; "c"; "" |]
28 * "cic:/a/b/c.ind#xpointer(1/1)" =>
29 * [| "cic:/a" ; "cic:/a/b" ; "cic:/a/b/c.con" ; "c"; "#xpointer(1/1)" |]
30 * "cic:/a/b/c.ind#xpointer(1/1/1)" =>
31 * [| "cic:/a" ; "cic:/a/b" ; "cic:/a/b/c.con" ; "c"; "#xpointer(1/1/1)" |]
33 type uri = string array;;
39 let string_of_uri uri =
40 match uri.(Array.length uri - 1) with
42 uri.(Array.length uri - 3)
45 [ uri.(Array.length uri - 3); uri.(Array.length uri - 1) ]
46 let name_of_uri uri = uri.(Array.length uri - 2);;
47 let buri_of_uri uri = uri.(Array.length uri - 4);;
48 let depth_of_uri uri = Array.length uri - 3;;
50 module OrderedStrings =
53 let compare (s1 : t) (s2 : t) = compare s1 s2
57 module SetOfStrings = Map.Make(OrderedStrings);;
59 (*CSC: commento obsoleto ed errato *)
60 (* Invariant: the map is the identity function, *)
61 (* i.e. (SetOfStrings.find str !set_of_uri) == str *)
62 let set_of_uri = ref SetOfStrings.empty;;
63 let set_of_prefixes = ref SetOfStrings.empty;;
65 (* similar to uri_of_string, but used for prefixes of uris *)
66 let normalize prefix =
68 SetOfStrings.find prefix !set_of_prefixes
71 set_of_prefixes := SetOfStrings.add prefix prefix !set_of_prefixes ;
75 exception IllFormedUri of string;;
77 let mk_prefixes str xpointer =
81 let prefix_uri = curi ^ "/" ^ he
82 and name = List.hd (Str.split (Str.regexp "\\.") he) in
83 [ normalize prefix_uri ; name ]
85 let prefix_uri = curi ^ "/" ^ he in
86 (normalize prefix_uri)::(aux prefix_uri tl)
87 | _ -> raise (IllFormedUri str)
89 let tokens = (Str.split (Str.regexp "/") str) in
92 (try (List.hd tokens, List.tl tokens)
93 with Failure "hd" | Failure "tl" ->
94 raise (IllFormedUri str))
96 (aux ty sp) @ [xpointer]
104 let uri_of_string str =
106 match Str.split sharp_rex str with
108 | [base; xpointer] -> base,xpointer
109 | _ -> raise (IllFormedUri str)
112 SetOfStrings.find str !set_of_uri
115 let uri = Array.of_list (mk_prefixes base xpointer) in
116 set_of_uri := SetOfStrings.add str uri !set_of_uri ;
120 let strip_xpointer uri =
121 let stripped_uri = Array.copy uri in
122 stripped_uri.(Array.length uri - 1) <- ""; (* reset xpointer field *)
123 let stripped_uri_str = string_of_uri stripped_uri in
124 set_of_uri := SetOfStrings.add stripped_uri_str stripped_uri !set_of_uri;
127 let cicuri_of_uri uri =
128 let completeuri = string_of_uri uri in
130 (Str.replace_first (Str.regexp "\\.types$") ""
131 (Str.replace_first (Str.regexp "\\.ann$") "" completeuri))
133 if completeuri = newcompleteuri then
136 let newuri = Array.copy uri in
137 newuri.(Array.length uri - 3) <- newcompleteuri ;
141 let annuri_of_uri uri =
142 let completeuri = string_of_uri uri in
143 if Str.string_match (Str.regexp ".*\\.ann$") completeuri 0 then
146 let newuri = Array.copy uri in
147 newuri.(Array.length uri - 3) <- completeuri ^ ".ann" ;
151 let uri_is_annuri uri =
152 Str.string_match (Str.regexp ".*\\.ann$") (string_of_uri uri) 0
155 let bodyuri_of_uri uri =
156 let struri = string_of_uri uri in
157 if Str.string_match (Str.regexp ".*\\.con$") (string_of_uri uri) 0 then
158 let newuri = Array.copy uri in
159 newuri.(Array.length uri - 3) <- struri ^ ".body" ;
165 let innertypesuri_of_uri uri =
166 let cicuri = cicuri_of_uri uri in
167 let newuri = Array.copy cicuri in
168 newuri.(Array.length cicuri - 3) <- (string_of_uri cicuri) ^ ".types" ;
172 type uriref = uri * (int list)
174 let string_of_uriref (uri, fi) =
175 let str = string_of_uri uri in
176 let xp t = "#xpointer(1/" ^ string_of_int (t + 1) in
179 | [t] -> str ^ xp t ^ ")"
180 | t :: c :: _ -> str ^ xp t ^ "/" ^ string_of_int c ^ ")"