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;;
40 let string_of_uri uri =
41 match uri.(Array.length uri - 1) with
43 uri.(Array.length uri - 3)
46 [ uri.(Array.length uri - 3); uri.(Array.length uri - 1) ]
49 let name_of_uri uri = uri.(Array.length uri - 2);;
50 let buri_of_uri uri = uri.(Array.length uri - 4);;
51 let depth_of_uri uri = Array.length uri - 3;;
53 module OrderedStrings =
56 let compare (s1 : t) (s2 : t) = compare s1 s2
60 module SetOfStrings = Map.Make(OrderedStrings);;
62 (*CSC: commento obsoleto ed errato *)
63 (* Invariant: the map is the identity function, *)
64 (* i.e. (SetOfStrings.find str !set_of_uri) == str *)
65 let set_of_uri = ref SetOfStrings.empty;;
66 let set_of_prefixes = ref SetOfStrings.empty;;
68 (* similar to uri_of_string, but used for prefixes of uris *)
69 let normalize prefix =
71 SetOfStrings.find prefix !set_of_prefixes
74 set_of_prefixes := SetOfStrings.add prefix prefix !set_of_prefixes ;
78 exception IllFormedUri of string;;
80 let mk_prefixes str xpointer =
84 let prefix_uri = curi ^ "/" ^ he
85 and name = List.hd (Str.split (Str.regexp "\\.") he) in
86 [ normalize prefix_uri ; name ]
88 let prefix_uri = curi ^ "/" ^ he in
89 (normalize prefix_uri)::(aux prefix_uri tl)
90 | _ -> raise (IllFormedUri str)
92 let tokens = (Str.split (Str.regexp "/") str) in
95 (try (List.hd tokens, List.tl tokens)
96 with Failure "hd" | Failure "tl" ->
97 raise (IllFormedUri str))
99 (aux ty sp) @ [xpointer]
107 let uri_of_string str =
109 match Str.split sharp_rex str with
111 | [base; xpointer] -> base,xpointer
112 | _ -> raise (IllFormedUri str)
115 SetOfStrings.find str !set_of_uri
118 let uri = Array.of_list (mk_prefixes base xpointer) in
119 set_of_uri := SetOfStrings.add str uri !set_of_uri ;
123 let strip_xpointer uri =
124 let stripped_uri = Array.copy uri in
125 stripped_uri.(Array.length uri - 1) <- ""; (* reset xpointer field *)
126 let stripped_uri_str = string_of_uri stripped_uri in
127 set_of_uri := SetOfStrings.add stripped_uri_str stripped_uri !set_of_uri;
130 let cicuri_of_uri uri =
131 let completeuri = string_of_uri uri in
133 (Str.replace_first (Str.regexp "\\.types$") ""
134 (Str.replace_first (Str.regexp "\\.ann$") "" completeuri))
136 if completeuri = newcompleteuri then
139 let newuri = Array.copy uri in
140 newuri.(Array.length uri - 3) <- newcompleteuri ;
144 let annuri_of_uri uri =
145 let completeuri = string_of_uri uri in
146 if Str.string_match (Str.regexp ".*\\.ann$") completeuri 0 then
149 let newuri = Array.copy uri in
150 newuri.(Array.length uri - 3) <- completeuri ^ ".ann" ;
154 let uri_is_annuri uri =
155 Str.string_match (Str.regexp ".*\\.ann$") (string_of_uri uri) 0
158 let bodyuri_of_uri uri =
159 let struri = string_of_uri uri in
160 if Str.string_match (Str.regexp ".*\\.con$") (string_of_uri uri) 0 then
161 let newuri = Array.copy uri in
162 newuri.(Array.length uri - 3) <- struri ^ ".body" ;
168 let innertypesuri_of_uri uri =
169 let cicuri = cicuri_of_uri uri in
170 let newuri = Array.copy cicuri in
171 newuri.(Array.length cicuri - 3) <- (string_of_uri cicuri) ^ ".types" ;
175 type uriref = uri * (int list)
177 let string_of_uriref (uri, fi) =
178 let str = string_of_uri uri in
179 let xp t = "#xpointer(1/" ^ string_of_int (t + 1) in
182 | [t] -> str ^ xp t ^ ")"
183 | t :: c :: _ -> str ^ xp t ^ "/" ^ string_of_int c ^ ")"
186 let su1 = string_of_uri u1 in
187 let su2 = string_of_uri u2 in
188 Pervasives.compare su1 su2
193 let compare = compare (* the one above, not Pervasives.compare *)
196 module UriSet = Set.Make (OrderedUri)