2 (* Copyright (C) 2002, HELM Team.
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.
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.
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.
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,
23 * For details, see the HELM World-Wide-Web page,
24 * http://cs.unibo.it/helm/.
30 exception Unsupported_property of string;;
32 let supported_properties = [
33 "cdata-section-elements";
40 "omit-xml-declaration";
45 let dump_args keys params props =
52 (String.concat ", " keys)
63 (fun (key,value) -> sprintf "%s:%s" key value)
68 (List.map (fun (key,value) -> sprintf "%s:%s" key value) props)))
70 let xslNS = Gdome.domString "http://www.w3.org/1999/XSL/Transform"
71 let outputS = Gdome.domString "output"
72 let q_outputS = Gdome.domString "xsl:output"
73 let is_supported_property name = List.mem name supported_properties
75 (** set a list of output properties in an xslt stylesheet, return a copy of
76 the given stylesheet modified as needed, given stylesheet wont be changed by
78 let apply_properties logger last_stylesheet props =
80 new Gdome.document_of_node (last_stylesheet#cloneNode ~deep:true)
83 let node_list = last_stylesheet#getElementsByTagNameNS xslNS outputS in
84 (match node_list#item 0 with
85 | None -> (* no xsl:output element, create it from scratch *)
86 logger#log `Debug "Creating xsl:output node ...";
87 let elt = last_stylesheet#createElementNS (Some xslNS) q_outputS in
88 let root = last_stylesheet#get_documentElement in
89 ignore (root#appendChild (elt :> Gdome.node));
91 | Some node -> new Gdome.element_of_node node)
93 let apply_property (name, value) =
94 if is_supported_property name then begin
95 logger#log `Debug (sprintf "Setting property: %s = %s" name value);
96 output_element#setAttribute
97 (Gdome.domString name)
98 (Gdome.domString value)
101 raise (Unsupported_property name)
103 List.iter apply_property props;
106 (** given a Gdome.document representing an XSLT stylesheet and an output
107 property return 'Some value' where 'value' is the property value, or None if
109 let get_property name (document: Gdome.document) =
110 let node_list = document#getElementsByTagNameNS xslNS outputS in
111 match node_list#item 0 with
114 let element = new Gdome.element_of_node node in
115 let domName = Gdome.domString name in
116 if element#hasAttribute domName then
117 Some (element#getAttribute domName)#to_string
122 ~(logger: Uwobo_logger.sysLogger)
123 ~(styles: Uwobo_styles.styles)
124 ~keys ~params ~props ~input =
125 (* "p_" prefix means "processed" *)
126 let (p_stylesheets, last_stylesheet) = styles#get keys in
127 logger#log `Debug (dump_args keys params props);
128 logger#log `Debug "Creating input document ...";
129 let result = (* Gdome.document *)
131 (fun source (key, stylesheet) ->
132 logger#log `Debug (sprintf "Applying stylesheet %s ..." key);
135 List.map (fun (key,value) -> (key, "'" ^ value ^ "'")) (params key)
140 "Gdome_xslt.applyStylesheet params=%s"
141 (String.concat ", " (List.map (fun (k,v) -> k^": "^v) params)));
142 let res = Gdome_xslt.applyStylesheet ~source ~stylesheet ~params in
144 with e -> raise (Uwobo_failure (Printexc.to_string e)))
148 (* used to retrieve serialization options *)
149 let last_stylesheet =
151 apply_properties logger last_stylesheet props
152 with Unsupported_property prop ->
153 raise (Uwobo_failure (sprintf "Unsupported property: %s" prop))
155 let p_last_stylesheet = Gdome_xslt.processStylesheet last_stylesheet in
156 ((fun outchan -> (* serialization function *)
157 Gdome_xslt.saveResultToChannel
160 ~stylesheet:p_last_stylesheet),
161 (get_property "media-type" last_stylesheet), (* media-type *)
162 (get_property "encoding" last_stylesheet)) (* encoding *)