]> matita.cs.unibo.it Git - helm.git/blob - helm/uwobo/src/ocaml/uwobo_styles.ml
b402987800bb6c51940197f1aa713c70ec4af015
[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 = domImpl#createDocumentFromURI ~uri ()
44
45     method get keys =
46       self#doReader (lazy (
47         let rev_keys = List.rev keys in
48         let last_key = List.hd rev_keys in
49         let p_stylesheets =
50           List.fold_left
51             (fun collected_styles key ->
52               let (key, stylesheet) =
53                 List.find (fun (k, _) -> k = key) stylesheets
54               in
55               (key, Gdome_xslt.processStylesheet stylesheet)::collected_styles)
56             []
57             rev_keys
58         in
59         let last_stylesheet =
60           snd (List.find (fun (k, _) -> k = last_key) stylesheets)
61         in
62         (p_stylesheets, last_stylesheet)
63       ))
64
65     method add key uri =
66       self#doWriter (lazy (
67         if (List.mem_assoc key uris) then
68           raise (Stylesheet_already_in key)
69         else begin
70           uris <- (key, uri) :: uris;
71           stylesheets <- (key, self#process uri) :: stylesheets
72         end
73       ))
74
75     method remove key =
76       self#doWriter (lazy (
77         if not (List.mem_assoc key uris) then
78           raise (Stylesheet_not_found key)
79         else begin
80           uris <- List.remove_assoc key uris;
81           stylesheets <- List.remove_assoc key stylesheets
82         end
83       ))
84
85     method removeAll =
86       self#doWriter (lazy (
87         uris <- [];
88         stylesheets <- []
89       ))
90
91     method list =
92       let uris = self#doReader (lazy (
93         uris
94       ))
95       in
96       List.map
97         (fun (key, uri) -> sprintf "key = %s, uri = %s" key (List.assoc key uris))
98         uris
99
100     method reload key =
101       self#doWriter (lazy (
102         (try
103           let uri = List.assoc key uris in
104           stylesheets <-
105             (key, self#process uri) :: (List.remove_assoc key stylesheets)
106         with Not_found ->
107           raise (Stylesheet_not_found key))
108       ))
109
110     method reloadAll =
111       self#doWriter (lazy (
112         stylesheets <- List.map (fun (key, uri) -> (key, self#process uri)) uris
113       ))
114
115   end
116