]> matita.cs.unibo.it Git - helm.git/blob - helm/uwobo/uwobo_styles.ml
added keys method that list currently loaded stylesheets' keys
[helm.git] / helm / uwobo / uwobo_styles.ml
1 (*
2  * Copyright (C) 2003:
3  *    Stefano Zacchiroli <zack@cs.unibo.it>
4  *    for the HELM Team http://helm.cs.unibo.it/
5  *
6  *  This file is part of HELM, an Hypertextual, Electronic
7  *  Library of Mathematics, developed at the Computer Science
8  *  Department, University of Bologna, Italy.
9  *
10  *  HELM is free software; you can redistribute it and/or
11  *  modify it under the terms of the GNU General Public License
12  *  as published by the Free Software Foundation; either version 2
13  *  of the License, or (at your option) any later version.
14  *
15  *  HELM is distributed in the hope that it will be useful,
16  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *  GNU General Public License for more details.
19  *
20  *  You should have received a copy of the GNU General Public License
21  *  along with HELM; if not, write to the Free Software
22  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston,
23  *  MA  02111-1307, USA.
24  *
25  *  For details, see the HELM World-Wide-Web page,
26  *  http://helm.cs.unibo.it/
27  *)
28
29 open Printf;;
30
31 exception Stylesheet_not_found of string ;;
32 exception Stylesheet_already_in of string ;;
33
34 type xslt_msg =
35   | LibXsltErrorMsg of string
36   | LibXsltDebugMsg of string
37 ;;
38 type log = xslt_msg list ;;
39
40 class styles =
41   object (self)
42     (* INVARIANT: 'stylesheets' and 'uris' are in sync *)
43
44     initializer
45       Gdome_xslt.setErrorCallback
46         (Some (fun msg -> self#appendMsg (LibXsltErrorMsg msg)));
47       Gdome_xslt.setDebugCallback
48         (Some (fun msg -> self#appendMsg (LibXsltDebugMsg msg)))
49
50       
51     val mutable stylesheets = []  (** association list: key -> Gdome.document *)
52     val mutable uris = []         (** association list: key -> uri *)
53     val mutable libXsltMsgs = []  (** libxslt's error and debugging messages *)
54
55     val domImpl = Gdome.domImplementation ()
56
57       (** process an XSLT stylesheet *)
58     method private process uri =
59       let dom = domImpl#createDocumentFromURI ~uri () in
60       ignore (Gdome_xslt.processStylesheet dom);  (* fills libXsltMsgs in case
61                                                   of errors *)
62       dom
63
64       (* libxslt's error and debugging messages handling *)
65
66     method private appendMsg msg = libXsltMsgs <- msg :: libXsltMsgs
67     method private clearMsgs = libXsltMsgs <- []
68
69     (* stylesheets management *)
70
71     method add key uri =
72       if (List.mem_assoc key uris) then
73         raise (Stylesheet_already_in key)
74       else begin
75         self#clearMsgs;
76         uris <- (key, uri) :: uris;
77         stylesheets <- (key, self#process uri) :: stylesheets;
78         libXsltMsgs
79       end
80
81     method remove key : log =
82       if not (List.mem_assoc key uris) then
83         raise (Stylesheet_not_found key)
84       else begin
85         uris <- List.remove_assoc key uris;
86         stylesheets <- List.remove_assoc key stylesheets;
87         []  (* no XSLT action -> no logs *)
88       end
89
90     method removeAll : log =
91       uris <- [];
92       stylesheets <- [];
93       []  (* no XSLT action -> no logs *)
94
95     method reload key =
96       (try
97         self#clearMsgs;
98         let uri = List.assoc key uris in
99         stylesheets <-
100           (key, self#process uri) :: (List.remove_assoc key stylesheets);
101         libXsltMsgs
102       with Not_found ->
103         raise (Stylesheet_not_found key))
104
105     method reloadAll =
106       self#clearMsgs;
107       stylesheets <- List.map (fun (key, uri) -> (key, self#process uri)) uris;
108       libXsltMsgs
109
110     (* stylesheets usage *)
111
112     method keys = List.map fst uris
113
114     method list =
115       List.map
116         (fun (key, uri) ->
117           sprintf "key = %s, uri = %s" key (List.assoc key uris))
118         uris
119
120     method get keys =
121       let rev_keys = List.rev keys in
122       let last_key = List.hd rev_keys in
123       let p_stylesheets =
124         List.fold_left
125           (fun collected_styles key ->
126             let (key, stylesheet) =
127               try
128                 List.find (fun (k, _) -> k = key) stylesheets
129               with Not_found -> raise (Stylesheet_not_found key)
130             in
131             (key, Gdome_xslt.processStylesheet stylesheet)::collected_styles)
132           []
133           rev_keys
134       in
135       let last_stylesheet =
136         snd (List.find (fun (k, _) -> k = last_key) stylesheets)
137       in
138       (p_stylesheets, last_stylesheet)
139
140   end
141