]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/urimanager/uriManager.ml
added strip_xpointer: remove trailing #xpointer from a UriManager.uri value
[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 let string_of_uri uri =
40   match  uri.(Array.length uri - 1) with
41   | "" -> 
42       uri.(Array.length uri - 3)
43   | _ -> 
44       String.concat "#" 
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;;
49
50 module OrderedStrings =
51  struct
52   type t = string
53   let compare (s1 : t) (s2 : t) = compare s1 s2
54  end
55 ;;
56
57 module SetOfStrings = Map.Make(OrderedStrings);;
58
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;;
64
65 (* similar to uri_of_string, but used for prefixes of uris *)
66 let normalize prefix =
67  try
68   SetOfStrings.find prefix !set_of_prefixes
69  with
70   Not_found ->
71    set_of_prefixes := SetOfStrings.add prefix prefix !set_of_prefixes ;
72    prefix
73 ;;
74
75 exception IllFormedUri of string;;
76
77 let mk_prefixes str xpointer =
78  let rec aux curi =
79   function
80      [he] ->
81       let prefix_uri = curi ^ "/" ^ he
82       and name = List.hd (Str.split (Str.regexp "\\.") he) in
83        [ normalize prefix_uri ; name ]
84    | he::tl ->
85       let prefix_uri = curi ^ "/" ^ he in
86        (normalize prefix_uri)::(aux prefix_uri tl)
87    | _ -> raise (IllFormedUri str)
88  in
89   let tokens = (Str.split (Str.regexp "/") str) in
90    (* ty = "cic:" *)
91    let (ty, sp) =
92     (try (List.hd tokens, List.tl tokens)
93      with Failure "hd" | Failure "tl" ->
94       raise (IllFormedUri str))
95     in
96     (aux ty sp) @ [xpointer]
97 ;;
98
99
100 let sharp_rex = 
101   Str.regexp "#"
102 ;;
103
104 let uri_of_string str =
105   let base, xpointer =
106     match Str.split sharp_rex str with
107     | [base] -> base,""
108     | [base; xpointer] -> base,xpointer 
109     | _ -> raise (IllFormedUri str)
110   in
111    try
112     SetOfStrings.find str !set_of_uri
113    with
114     Not_found ->
115       let uri = Array.of_list (mk_prefixes base xpointer) in
116       set_of_uri := SetOfStrings.add str uri !set_of_uri ;
117       uri
118 ;;
119
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;
125   stripped_uri
126
127 let cicuri_of_uri uri =
128  let completeuri = string_of_uri uri in
129   let newcompleteuri = 
130    (Str.replace_first (Str.regexp "\\.types$") ""
131     (Str.replace_first (Str.regexp "\\.ann$") "" completeuri))
132   in
133    if completeuri = newcompleteuri then
134     uri
135    else
136     let newuri = Array.copy uri in
137      newuri.(Array.length uri - 3) <- newcompleteuri ;
138      newuri
139 ;;
140
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
144    uri
145   else
146    let newuri = Array.copy uri in
147     newuri.(Array.length uri - 3) <- completeuri ^ ".ann" ;
148     newuri
149 ;;
150
151 let uri_is_annuri uri =
152  Str.string_match (Str.regexp ".*\\.ann$") (string_of_uri uri) 0
153 ;;
154
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" ;
160     Some newuri
161   else
162    None
163 ;;
164
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" ;
169    newuri
170 ;;
171
172 type uriref = uri * (int list)
173
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
177    match fi with
178       | []          -> str 
179       | [t]         -> str ^ xp t ^ ")" 
180       | t :: c :: _ -> str ^ xp t ^ "/" ^ string_of_int c ^ ")" 
181