1 (* Copyright (C) 2004, 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://helm.cs.unibo.it/
28 exception Malformed_key of string
29 exception Key_not_found of string
30 exception Type_error of string * string * string (* expected type, value, msg *)
31 exception Parse_error of string * int * int * string (* file, line, col, msg *)
32 exception Invalid_value of (string * string) * string (* key, value, descr *)
34 type validator_id = int
36 let get_next_validator_id =
37 let next_id = ref 0 in
43 let validators = Hashtbl.create magic_size
44 let registry = Hashtbl.create magic_size
46 let backup_registry () = Hashtbl.copy registry
47 let restore_registry backup =
48 Hashtbl.clear registry;
49 Hashtbl.iter (fun key value -> Hashtbl.replace registry key value) backup
52 * - no sequences of '_' longer than 1 are permitted
53 * - no uppercase letter are permitted
55 let valid_step_rex_raw = "[a-z0-9]+(_[a-z0-9]+)*"
56 let valid_key_rex_raw =
57 sprintf "%s(\\.%s)*" valid_step_rex_raw valid_step_rex_raw
58 let valid_key_rex = Pcre.regexp ("^" ^ valid_key_rex_raw ^ "$")
60 (* escapes for xml configuration file *)
61 let (escape, unescape) =
62 let (in_enc, out_enc) = (`Enc_utf8, `Enc_utf8) in
63 (Netencoding.Html.encode ~in_enc ~out_enc (),
64 Netencoding.Html.decode ~in_enc ~out_enc ~entity_base:`Xml ())
66 let key_is_valid key =
67 if not (Pcre.pmatch ~rex:valid_key_rex key) then
68 raise (Malformed_key key)
70 let value_is_valid ~key ~value =
72 (fun (validator, descr) ->
73 if not (validator value) then
74 raise (Invalid_value ((key, value), descr)))
75 (Hashtbl.find_all validators key)
77 let set' registry ~key ~value =
79 value_is_valid ~key ~value;
80 Hashtbl.replace registry key value
83 let dot_RE = Pcre.regexp "\\." in
85 Pcre.replace ~rex:dot_RE ~templ:"__" (String.uppercase key)
91 Some (Hashtbl.find registry key)
92 with Not_found -> None
96 Some (Sys.getenv (env_var_of_key key))
97 with Not_found -> None
99 match (registry_value, env_value) with
100 | Some reg, Some env -> env
101 | Some reg, None -> reg
102 | None, Some env -> env
103 | None, None -> raise (Key_not_found key)
105 let set = set' registry
107 let string_list_of_string s =
108 (* trailing blanks are removed per default by Pcre.split *)
109 Pcre.split ~pat:"\\s+" (Pcre.replace ~pat:"^\\s+" s)
110 let string_of_string_list l = String.concat " " l
112 let mk_get_set type_name (from_string: string -> 'a) (to_string: 'a -> string) =
114 let value = get key in
118 raise (Type_error (type_name, value, Printexc.to_string exn))
120 let setter ~key ~value = set ~key ~value:(to_string value) in
123 let (get_string, set_string) = (get, set)
124 let (get_int, set_int) = mk_get_set "int" int_of_string string_of_int
125 let (get_float, set_float) = mk_get_set "float" float_of_string string_of_float
126 let (get_string_list, set_string_list) =
127 mk_get_set "string list" string_list_of_string string_of_string_list
130 let oc = open_out fname in
131 output_string oc "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
132 output_string oc "<helm_registry>\n";
136 fprintf oc " <value key=\"%s\">%s</value>\n" key (escape value))
138 output_string oc "</helm_registry>";
144 let add_validator ~key ~validator ~descr =
145 let id = get_next_validator_id () in
146 Hashtbl.add validators key (validator, descr);
154 let config = default_config in
155 let entry = `Entry_document [ `Extend_dtd_fully; `Parse_xml_decl ] in
158 parse_wfdocument_entity config (from_file fname) default_spec
160 let fill_registry () =
161 document#root#iter_nodes (fun n ->
163 (match n#node_type with
164 | T_element "value" ->
165 let key = n#required_string_attribute "key" in
166 let value = n#data in
170 let (fname, line, pos) = n#position in
171 raise (Parse_error (fname, line, pos,
172 "Uncaught exception: " ^ Printexc.to_string exn)))
174 let backup = backup_registry () in
175 Hashtbl.clear registry;
179 restore_registry backup;
184 let dump () = Hashtbl.iter (fun k v -> printf "%s = %s\n" k v) registry