]> matita.cs.unibo.it Git - helm.git/blob - helm/software/lambda-delta/common/library.ml
Additional contribs.
[helm.git] / helm / software / lambda-delta / common / library.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 Y = Entity
17
18 (* internal functions *******************************************************)
19
20 let base = "xml"
21
22 let obj_ext = ".xml"
23
24 let root = "ENTITY"
25
26 let system = "http://helm.cs.unibo.it/lambda-delta/" ^ base ^ "/ld.dtd"
27
28 let path_of_uri uri =
29    F.concat base (Str.string_after (U.string_of_uri uri) 3)
30
31 (* interface functions ******************************************************)
32
33 type och = string -> unit
34
35 type attr = string * string
36
37 type pp = och -> int -> unit
38
39 let attribute out (name, contents) =
40    if contents <> "" then begin
41       out " "; out name; out "=\""; out contents; out "\""
42    end
43
44 let xml out version encoding =
45    out "<?xml";
46       attribute out ("version", version);
47       attribute out ("encoding", encoding);
48    out "?>\n\n"
49
50 let doctype out root system =
51    out "<!DOCTYPE "; out root; out " SYSTEM \""; out system; out "\">\n\n"
52
53 let tag tag attrs ?contents out indent =
54    let spc = String.make indent ' ' in
55    out spc; out "<"; out tag; List.iter (attribute out) attrs;
56    match contents with
57       | None      -> out "/>\n"
58       | Some cont -> 
59          out ">\n"; cont out (indent + 3); out spc; 
60          out "</"; out tag; out ">\n"
61
62 let sort = "Sort"
63
64 let lref = "LRef"
65
66 let gref = "GRef"
67
68 let cast = "Cast"
69
70 let appl = "Appl"
71
72 let proj = "Proj"
73
74 let abst = "Abst"
75
76 let abbr = "Abbr"
77
78 let void = "Void"
79
80 let position i =
81    "position", string_of_int i
82
83 let offset j = 
84    let contents = if j > 0 then string_of_int j else "" in
85    "offset", contents
86
87 let uri u =
88    "uri", U.string_of_uri u
89
90 let arity n =
91    let contents = if n > 1 then string_of_int n else "" in
92    "arity", contents
93
94 let name a =
95    let map f i n r s =
96       let n = if r then n else "^" ^ n in 
97       let spc = if i then "" else " " in
98       f (s ^ n ^ spc)
99    in
100    let f s = "name", s in
101    Y.names f map a ""
102
103 let mark a =
104    let err () = "mark", "" in
105    let f i = "mark", string_of_int i in
106    Y.mark err f a
107
108 let export_entity pp_term si g (a, u, b) =
109    let path = path_of_uri u in
110    let _ = Sys.command (Printf.sprintf "mkdir -p %s" (F.dirname path)) in
111    let och = open_out (path ^ obj_ext) in
112    let out = output_string och in
113    xml out "1.0" "UTF-8"; doctype out root system;
114    let a = Y.Name (U.name_of_uri u, true) :: a in
115    let attrs = [uri u; name a; mark a] in 
116    let contents = match b with
117       | Y.Abst w -> tag "ABST" attrs ~contents:(pp_term w) 
118       | Y.Abbr v -> tag "ABBR" attrs ~contents:(pp_term v)
119       | Y.Void   -> assert false
120    in
121    let opts = if si then "si" else "" in
122    let shp = H.string_of_graph g in
123    let attrs = ["hierarchy", shp; "options", opts] in
124    tag root attrs ~contents out 0;
125    close_out och