+let
+ save_object_to_disk uri obj
+=
+ let ensure_path_exists path =
+ let dir = Filename.dirname path in
+ try
+ let stats = Unix.stat dir in
+ if stats.Unix.st_kind <> Unix.S_DIR then
+ raise (Failure (dir ^ " already exists and is not a directory"))
+ else
+ ()
+ with
+ Unix.Unix_error (_,_,_) ->
+ let pstatus = Unix.system ("mkdir -p " ^ dir) in
+ match pstatus with
+ | Unix.WEXITED n when n = 0 -> ()
+ | _ -> raise (Failure ("Unable to create " ^ dir))
+ in
+ (* generate annobj, ids_to_inner_sorts and ids_to_inner_types *)
+ let annobj,_,_,ids_to_inner_sorts,ids_to_inner_types,_,_ =
+ Cic2acic.acic_object_of_cic_object ~eta_fix:false obj
+ in
+
+ (* prepare XML *)
+ let xml, bodyxml =
+ Cic2Xml.print_object uri ~ids_to_inner_sorts ~ask_dtd_to_the_getter:false
+ annobj
+ in
+ let xmlinnertypes =
+ Cic2Xml.print_inner_types uri ~ids_to_inner_sorts ~ids_to_inner_types
+ ~ask_dtd_to_the_getter:false
+ in
+
+ (* prepare URIs and paths *)
+ let innertypesuri = UriManager.innertypesuri_of_uri uri in
+ let bodyuri = UriManager.bodyuri_of_uri uri in
+ let innertypesfilename = Str.replace_first (Str.regexp "^cic:") ""
+ (UriManager.string_of_uri innertypesuri) ^ ".xml" in
+ let innertypespath = !basedir ^ "/" ^ innertypesfilename in
+ let xmlfilename = Str.replace_first (Str.regexp "^cic:") ""
+ (UriManager.string_of_uri uri) ^ ".xml" in
+ let xmlpath = !basedir ^ "/" ^ xmlfilename in
+ let xmlbodyfilename = Str.replace_first (Str.regexp "^cic:") ""
+ (UriManager.string_of_uri uri) ^ ".body.xml" in
+ let xmlbodypath = !basedir ^ "/" ^ xmlbodyfilename in
+ let path_scheme_of path = "file:/" ^ path in
+
+ (* now write to disk *)
+ ensure_path_exists innertypespath;
+ Xml.pp ~quiet:true xmlinnertypes (Some innertypespath) ;
+ ensure_path_exists xmlpath;
+ Xml.pp ~quiet:true xml (Some xmlpath) ;
+
+ (* now register to the getter *)
+ Http_getter.register' innertypesuri (path_scheme_of innertypespath);
+ Http_getter.register' uri (path_scheme_of xmlpath);
+
+ (* now the optional body, both write and register *)
+ (match bodyxml,bodyuri with
+ None,None -> ()
+ | Some bodyxml,Some bodyuri->
+ ensure_path_exists xmlbodypath;
+ Xml.pp ~quiet:true bodyxml (Some xmlbodypath) ;
+ Http_getter.register' bodyuri (path_scheme_of xmlbodypath)
+ | _-> assert false)
+;;
+
+
+