]> matita.cs.unibo.it Git - helm.git/blob - helm/software/helena/src/xml/xmlLibrary.ml
new semantics of the -g option completed
[helm.git] / helm / software / helena / src / xml / xmlLibrary.ml
1 (*
2     ||M||  This file is part of HELM, an Hypertextual, Electronic        
3     ||A||  Library of Mathematics, developed at the Computer Science     
4     ||T||  Department, University of Bologna, Italy.                     
5     ||I||                                                                
6     ||T||  HELM is free software; you can redistribute it and/or         
7     ||A||  modify it under the terms of the GNU General Public License   
8     \   /  version 2 or (at your option) any later version.              
9      \ /   This software is distributed as is, NO WARRANTY.              
10       V_______________________________________________________________ *)
11
12 module KF = Filename
13
14 module U  = NUri
15 module C  = Cps
16 module G  = Options
17 module H  = Hierarchy
18 module N  = Layer
19 module E  = Entity
20
21 (* internal functions *******************************************************)
22
23 let base = "xml"
24
25 let ext = ".xml"
26
27 let obj_root = "CONSTANT"
28
29 let home = "http://lambdadelta.info/"
30
31 let system = KF.concat (KF.concat home base) "ld.dtd"
32
33 let xmlns = "xmlns", home
34
35 let path_of_uri xdir uri =
36    let base = KF.concat xdir base in 
37    KF.concat base (Str.string_after (U.string_of_uri uri) 3)
38
39 (* interface functions ******************************************************)
40
41 type och = string -> unit
42
43 type attr = string * string
44
45 type pp = och -> int -> unit
46
47 let attribute out (name, contents) =
48    if contents <> "" then begin
49       out " "; out name; out "=\""; out contents; out "\""
50    end
51
52 let xml out version encoding =
53    out "<?xml";
54       attribute out ("version", version);
55       attribute out ("encoding", encoding);
56    out "?>\n\n"
57
58 let doctype out root system =
59    out "<!DOCTYPE "; out root; out " SYSTEM \""; out system; out "\">\n\n"
60
61 let tag tag attrs ?contents out indent =
62    let spc = String.make indent ' ' in
63    out spc; out "<"; out tag; List.iter (attribute out) attrs;
64    match contents with
65       | None      -> out "/>\n"
66       | Some cont -> 
67          out ">\n"; cont out (indent + 3); out spc; 
68          out "</"; out tag; out ">\n"
69
70 let sort = "Sort"
71
72 let lref = "LRef"
73
74 let gref = "GRef"
75
76 let cast = "Cast"
77
78 let appl x = if x then "Appx" else "Appr"
79
80 let proj = "Proj"
81
82 let abst = "Abst"
83
84 let abbr = "Abbr"
85
86 let void = "Void"
87
88 let position i =
89    "position", string_of_int i
90
91 let depth i =
92    "depth", string_of_int i
93
94 let uri u =
95    "uri", U.string_of_uri u
96
97 let name a =
98    let err () = "name", "" in
99    let f n r = "name", if r then n else "-" ^ n in 
100    E.name err f a
101
102 let layer st n =
103    "layer", N.to_string st n
104
105 let main a =
106    let sort, degr = a.E.n_main in
107    ["main-position", string_of_int sort;
108     "main-degree", string_of_int degr;
109    ]
110
111 let side a =
112    let sort, degr = a.E.n_side in
113    ["side-position", string_of_int sort;
114     "side-degree", string_of_int degr;
115    ]
116
117 let apix a =
118    "level", string_of_int a.E.n_apix
119
120 let meta a =
121    let map = function
122       | E.Main     -> "Main"
123       | E.InProp   -> "InProp"
124       | E.Progress -> "Progress"
125       | E.Private  -> "Private"
126    in
127    "meta", String.concat " " (List.rev_map map a.E.r_meta)
128
129 (* TODO: the string tx must be quoted *)
130 let info a =
131    let err () = ["lang", ""; "info", ""] in
132    let f lg tx = ["lang", lg; "info", tx] in
133    E.info err f a
134
135 let export_entity pp_term (ra, na, u, b) = 
136    let path = path_of_uri !G.xdir u in
137    let _ = Sys.command (Printf.sprintf "mkdir -p %s" (KF.dirname path)) in
138    let och = open_out (path ^ ext) in
139    let out = output_string och in
140    xml out "1.0" "UTF-8"; doctype out obj_root system;
141    let na = {na with E.n_name = Some (U.name_of_uri u, true)} in
142    let attrs = uri u :: name na :: apix na :: meta ra :: info ra in 
143    let contents = match b with
144       | E.Abst w -> tag "GDec" attrs ~contents:(pp_term w) 
145       | E.Abbr v -> tag "GDef" attrs ~contents:(pp_term v)
146       | E.Void   -> assert false
147    in
148    let opts = if !G.si then "si" else "" in
149    let shp = H.string_of_graph () in
150    let attrs = [xmlns; "hierarchy", shp; "options", opts] in
151    tag obj_root attrs ~contents out 0;
152    close_out och