]> matita.cs.unibo.it Git - helm.git/blob - helm/software/helena/src/xml/xmlLibrary.ml
- the disambiguation of unified binders continues
[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 F = Filename
13 module U = NUri
14 module C = Cps
15 module H = Hierarchy
16 module G = Options
17 module E = Entity
18 module N = Level
19
20 (* internal functions *******************************************************)
21
22 let base = "xml"
23
24 let ext = ".xml"
25
26 let obj_root = "ENTITY"
27
28 let ccs_name = "ccs.ldc"
29
30 let ccs_root = "CCS"
31
32 let home = "http://lambdadelta.info/"
33
34 let system = F.concat (F.concat home base) "ld.dtd"
35
36 let xmlns = "xmlns", home
37
38 let path_of_uri xdir uri =
39    let base = F.concat xdir base in 
40    F.concat base (Str.string_after (U.string_of_uri uri) 3)
41
42 (* interface functions ******************************************************)
43
44 type och = string -> unit
45
46 type attr = string * string
47
48 type pp = och -> int -> unit
49
50 let attribute out (name, contents) =
51    if contents <> "" then begin
52       out " "; out name; out "=\""; out contents; out "\""
53    end
54
55 let xml out version encoding =
56    out "<?xml";
57       attribute out ("version", version);
58       attribute out ("encoding", encoding);
59    out "?>\n\n"
60
61 let doctype out root system =
62    out "<!DOCTYPE "; out root; out " SYSTEM \""; out system; out "\">\n\n"
63
64 let tag tag attrs ?contents out indent =
65    let spc = String.make indent ' ' in
66    out spc; out "<"; out tag; List.iter (attribute out) attrs;
67    match contents with
68       | None      -> out "/>\n"
69       | Some cont -> 
70          out ">\n"; cont out (indent + 3); out spc; 
71          out "</"; out tag; out ">\n"
72
73 let sort = "Sort"
74
75 let lref = "LRef"
76
77 let gref = "GRef"
78
79 let cast = "Cast"
80
81 let appl = "Appl"
82
83 let proj = "Proj"
84
85 let abst = "Abst"
86
87 let abbr = "Abbr"
88
89 let void = "Void"
90
91 let position i =
92    "position", 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 apix a =
103    let err () = "age", "" in
104    let f i = "age", string_of_int i in
105    E.apix err f a
106
107 let level st n =
108    "level", N.to_string st n
109
110 let meta a =
111    let map = function
112       | E.Main     -> "Main"
113       | E.InProp   -> "InProp"
114       | E.Progress -> "Progress"
115       | E.Private  -> "Private"
116    in
117    "meta", String.concat " " (List.rev_map map a.E.r_meta)
118
119 (* TODO: the string tx must be quoted *)
120 let info a =
121    let err () = ["lang", ""; "info", ""] in
122    let f lg tx = ["lang", lg; "info", tx] in
123    E.info err f a
124
125 let export_entity pp_term (ra, na, u, b) = 
126    let path = path_of_uri !G.xdir u in
127    let _ = Sys.command (Printf.sprintf "mkdir -p %s" (F.dirname path)) in
128    let och = open_out (path ^ ext) in
129    let out = output_string och in
130    xml out "1.0" "UTF-8"; doctype out obj_root system;
131    let na = {na with E.n_name = Some (U.name_of_uri u, true)} in
132    let attrs = uri u :: name na :: apix na :: meta ra :: info ra in 
133    let contents = match b with
134       | E.Abst w -> tag "GDec" attrs ~contents:(pp_term w) 
135       | E.Abbr v -> tag "GDef" attrs ~contents:(pp_term v)
136       | E.Void   -> assert false
137    in
138    let opts = if !G.si then "si" else "" in
139    let shp = H.string_of_graph () in
140    let attrs = [xmlns; "hierarchy", shp; "options", opts] in
141    tag obj_root attrs ~contents out 0;
142    close_out och