]> matita.cs.unibo.it Git - helm.git/blob - helm/uwobo/uwobo_styles.ml
- redesigned error and warning handling for libxslt
[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 open Uwobo_common;;
31
32 exception Stylesheet_not_found of string ;;
33 exception Stylesheet_already_in of string ;;
34
35 class styles =
36   object (self)
37     (* INVARIANT: 'stylesheets' and 'uris' are in sync *)
38
39     val mutable stylesheets = []  (** association list: key -> Gdome.document *)
40     val mutable uris = []         (** association list: key -> uri *)
41
42     val domImpl = Gdome.domImplementation ()
43
44       (** process an XSLT stylesheet *)
45     method private process uri =
46       let dom = domImpl#createDocumentFromURI ~uri () in
47       ignore (Gdome_xslt.processStylesheet dom);  (* produce libXSLT messages in
48                                                   case of errors *)
49       dom
50
51     (* stylesheets management *)
52
53     method add key uri =
54       if (List.mem_assoc key uris) then
55         raise (Stylesheet_already_in key)
56       else begin
57         uris <- (key, uri) :: uris;
58         stylesheets <- (key, self#process uri) :: stylesheets
59       end
60
61     method remove key =
62       if not (List.mem_assoc key uris) then
63         raise (Stylesheet_not_found key)
64       else begin
65         uris <- List.remove_assoc key uris;
66         stylesheets <- List.remove_assoc key stylesheets
67       end
68
69     method removeAll =
70       uris <- [];
71       stylesheets <- []
72
73     method reload key =
74       (try
75         let uri = List.assoc key uris in
76         stylesheets <-
77           (key, self#process uri) :: (List.remove_assoc key stylesheets)
78       with Not_found ->
79         raise (Stylesheet_not_found key))
80
81     method reloadAll =
82       stylesheets <- List.map (fun (key, uri) -> (key, self#process uri)) uris
83
84     (* stylesheets usage *)
85
86     method keys = List.map fst uris
87
88     method list =
89       List.map
90         (fun (key, uri) ->
91           sprintf "key = %s, uri = %s" key (List.assoc key uris))
92         uris
93
94     method get keys =
95       let rev_keys = List.rev keys in
96       let last_key = List.hd rev_keys in
97       let p_stylesheets =
98         List.fold_left
99           (fun collected_styles key ->
100             let (key, stylesheet) =
101               try
102                 List.find (fun (k, _) -> k = key) stylesheets
103               with Not_found -> raise (Stylesheet_not_found key)
104             in
105             (key, Gdome_xslt.processStylesheet stylesheet)::collected_styles)
106           []
107           rev_keys
108       in
109       let last_stylesheet =
110         snd (List.find (fun (k, _) -> k = last_key) stylesheets)
111       in
112       (p_stylesheets, last_stylesheet)
113
114   end
115