]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/urimanager/uriManager.ml
merged changes from the svn fork by me and Enrico
[helm.git] / helm / ocaml / urimanager / uriManager.ml
1 (* Copyright (C) 2000, HELM Team.
2  * 
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.
6  * 
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.
11  * 
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.
16  *
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,
20  * MA  02111-1307, USA.
21  * 
22  * For details, see the HELM World-Wide-Web page,
23  * http://cs.unibo.it/helm/.
24  *)
25
26 (*
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)" |]
32 *)
33 type uri = string array;;
34
35 let eq uri1 uri2 =
36  uri1 == uri2
37 ;;
38
39
40 let string_of_uri uri =
41   match  uri.(Array.length uri - 1) with
42   | "" -> 
43       uri.(Array.length uri - 3)
44   | _ -> 
45       String.concat "#" 
46         [ uri.(Array.length uri - 3); uri.(Array.length uri - 1) ]
47
48
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;;
52
53 module OrderedStrings =
54  struct
55   type t = string
56   let compare (s1 : t) (s2 : t) = compare s1 s2
57  end
58 ;;
59
60 module SetOfStrings = Map.Make(OrderedStrings);;
61
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;;
67
68 (* similar to uri_of_string, but used for prefixes of uris *)
69 let normalize prefix =
70  try
71   SetOfStrings.find prefix !set_of_prefixes
72  with
73   Not_found ->
74    set_of_prefixes := SetOfStrings.add prefix prefix !set_of_prefixes ;
75    prefix
76 ;;
77
78 exception IllFormedUri of string;;
79
80 let mk_prefixes str xpointer =
81  let rec aux curi =
82   function
83      [he] ->
84       let prefix_uri = curi ^ "/" ^ he
85       and name = List.hd (Str.split (Str.regexp "\\.") he) in
86        [ normalize prefix_uri ; name ]
87    | he::tl ->
88       let prefix_uri = curi ^ "/" ^ he in
89        (normalize prefix_uri)::(aux prefix_uri tl)
90    | _ -> raise (IllFormedUri str)
91  in
92   let tokens = (Str.split (Str.regexp "/") str) in
93    (* ty = "cic:" *)
94    let (ty, sp) =
95     (try (List.hd tokens, List.tl tokens)
96      with Failure "hd" | Failure "tl" ->
97       raise (IllFormedUri str))
98     in
99     (aux ty sp) @ [xpointer]
100 ;;
101
102
103 let sharp_rex = 
104   Str.regexp "#"
105 ;;
106
107 let uri_of_string str =
108   let base, xpointer =
109     match Str.split sharp_rex str with
110     | [base] -> base,""
111     | [base; xpointer] -> base,xpointer 
112     | _ -> raise (IllFormedUri str)
113   in
114    try
115     SetOfStrings.find str !set_of_uri
116    with
117     Not_found ->
118       let uri = Array.of_list (mk_prefixes base xpointer) in
119       set_of_uri := SetOfStrings.add str uri !set_of_uri ;
120       uri
121 ;;
122
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;
128   stripped_uri
129
130 let cicuri_of_uri uri =
131  let completeuri = string_of_uri uri in
132   let newcompleteuri = 
133    (Str.replace_first (Str.regexp "\\.types$") ""
134     (Str.replace_first (Str.regexp "\\.ann$") "" completeuri))
135   in
136    if completeuri = newcompleteuri then
137     uri
138    else
139     let newuri = Array.copy uri in
140      newuri.(Array.length uri - 3) <- newcompleteuri ;
141      newuri
142 ;;
143
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
147    uri
148   else
149    let newuri = Array.copy uri in
150     newuri.(Array.length uri - 3) <- completeuri ^ ".ann" ;
151     newuri
152 ;;
153
154 let uri_is_annuri uri =
155  Str.string_match (Str.regexp ".*\\.ann$") (string_of_uri uri) 0
156 ;;
157
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" ;
163     Some newuri
164   else
165    None
166 ;;
167
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" ;
172    newuri
173 ;;
174
175 type uriref = uri * (int list)
176
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
180    match fi with
181       | []          -> str 
182       | [t]         -> str ^ xp t ^ ")" 
183       | t :: c :: _ -> str ^ xp t ^ "/" ^ string_of_int c ^ ")" 
184
185 let compare u1 u2 =
186   let su1 = string_of_uri u1 in
187   let su2 = string_of_uri u2 in
188   Pervasives.compare su1 su2
189
190 module OrderedUri =
191 struct
192   type t = uri
193   let compare = compare (* the one above, not Pervasives.compare *)
194 end
195
196 module UriSet = Set.Make (OrderedUri)
197