]> matita.cs.unibo.it Git - helm.git/blob - helm/uwobo/src/ocaml/uwobo_styles.ml
snapshot Wed, 27 Nov 2002 18:10:57 +0100
[helm.git] / helm / uwobo / src / ocaml / uwobo_styles.ml
1
2 (* Copyright (C) 2002, HELM Team.
3  * 
4  * This file is part of HELM, an Hypertextual, Electronic
5  * Library of Mathematics, developed at the Computer Science
6  * Department, University of Bologna, Italy.
7  * 
8  * HELM is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  * 
13  * HELM is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with HELM; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
21  * MA  02111-1307, USA.
22  * 
23  * For details, see the HELM World-Wide-Web page,
24  * http://cs.unibo.it/helm/.
25  *)
26
27 open Printf;;
28
29 exception Stylesheet_not_found of string;;
30 exception Stylesheet_already_in of string;;
31
32 class styles =
33   object (self)
34     (* INVARIANT: 'stylesheets' and 'uris' are in sync *)
35
36     inherit Uwobo_common.threadSafe
37
38     val mutable stylesheets = []
39     val mutable uris = []
40     val domImpl = Gdome.domImplementation ()
41
42       (** process an XSLT stylesheet *)
43     method private process uri =
44       Gdome_xslt.processStylesheet (domImpl#createDocumentFromURI ~uri ())
45
46     method get keys =
47       self#doReader (lazy (
48         List.fold_left
49           (fun collected_styles key ->
50             (List.find (fun (k, _) -> k = key) stylesheets)::collected_styles)
51           []
52           (List.rev keys)
53       ))
54
55     method add key uri =
56       self#doWriter (lazy (
57         if (List.mem_assoc key uris) then
58           raise (Stylesheet_already_in key)
59         else begin
60           uris <- (key, uri) :: uris;
61           stylesheets <- (key, self#process uri) :: stylesheets
62         end
63       ))
64
65     method remove key =
66       self#doWriter (lazy (
67         if not (List.mem_assoc key uris) then
68           raise (Stylesheet_not_found key)
69         else begin
70           uris <- List.remove_assoc key uris;
71           stylesheets <- List.remove_assoc key stylesheets
72         end
73       ))
74
75     method removeAll =
76       self#doWriter (lazy (
77         uris <- [];
78         stylesheets <- []
79       ))
80
81     method list =
82       let uris = self#doReader (lazy (
83         uris
84       ))
85       in
86       List.map
87         (fun (key, uri) -> sprintf "key = %s, uri = %s" key (List.assoc key uris))
88         uris
89
90     method reload key =
91       self#doWriter (lazy (
92         (try
93           let uri = List.assoc key uris in
94           stylesheets <-
95             (key, self#process uri) :: (List.remove_assoc key stylesheets)
96         with Not_found ->
97           raise (Stylesheet_not_found key))
98       ))
99
100     method reloadAll =
101       self#doWriter (lazy (
102         stylesheets <- List.map (fun (key, uri) -> (key, self#process uri)) uris
103       ))
104
105   end
106