123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495open!Coretypexml=Xml_light.Xml.xmlmoduletypeXmlable=sigtypetvalxsd:xmllistvalto_xml:t->xmllistvalof_xml:xml->tendletto_stringxml=Xml_light.Xml.to_stringxmlletto_string_fmtxml=Xml_light.Xml.to_string_fmtxmlletto_human_stringxml=Xml_light.Xml.to_human_stringxmlmoduleParser_state=structtypet=Xml_light.XmlParser.tletmake=Xml_light.XmlParser.makeendletstateful_of_string=Xml_light.Xml.parse_string_withletof_filefile=Xml_light.Xml.parse_filefileletcreate_node~tag~body=Xml_light.Xml.Element(tag,[],body)letcreate_databody=Xml_light.Xml.PCDatabodylettagxml=matchxmlwith|Xml_light.Xml.PCData_->None|Xml_light.Xml.Element(tag,_,_)->Sometagletattributesxml=matchxmlwith|Xml_light.Xml.PCData_->[]|Xml_light.Xml.Element(_,attrs,_)->attrsletchildrenxml=matchxmlwith|Xml_light.Xml.PCData_->[]|Xml_light.Xml.Element(_,_,children)->childrenletchildxmlmy_tag=List.find~f:(funxml->(Somemy_tag)=(tagxml))(childrenxml)letreccontentsxml=matchxmlwith|Xml_light.Xml.PCDatastr->Somestr|Xml_light.Xml.Element(_,_,[element])->contentselement|Xml_light.Xml.Element_->Noneletkindxml=matchxmlwith|Xml_light.Xml.PCData_|Xml_light.Xml.Element(_,_,[Xml_light.Xml.PCData_])->`Leaf|Xml_light.Xml.Element_->`Internalletxml_datastring=Xml_light.Xml.PCDatastringexceptionUnexpected_xmlof(xml*string)exceptionIllegal_atomofxmlletcheck_extra_fieldsxmlfields=letfailgot=letfields=String.concat~sep:";"fieldsinletmsg=Printf.sprintf"record expected with fields [%s] but got %S"fieldsgotinraise(Unexpected_xml(xml,msg))inmatchxmlwith|Xml_light.Xml.PCData_->fail"PCData"|Xml_light.Xml.Element(_,_,children)->letiterchild=matchchildwith|Xml_light.Xml.PCData_->fail"PCData"|Xml_light.Xml.Element(tag,_,_)->ifnot(List.memfieldstag~equal:String.equal)thenfailtaginList.iter~f:iterchildrenmoduleRestriction=structmoduleFormat=structtypet=[`string|`decimal|`date|`time|`datetime|`integer]exceptionIllegal_formatletto_string=function|`time->"xs:time"|`string->"xs:string"|`decimal->"xs:decimal"|`date->"xs:date"|`datetime->"xs:dateTime"|`integer->"xs:integer"letof_string=function|"xs:time"->`time|"xs:string"->`string|"xs:decimal"->`decimal|"xs:date"->`date|"xs:dateTime"->`datetime|"xs:integer"->`integer|_->raiseIllegal_formatendtypet=xmlletsimple_type~restrictions~format=letrestriction=Xml_light.Xml.Element("xs:restriction",["base",Format.to_stringformat],restrictions)in[Xml_light.Xml.Element("xs:simpleType",[],[restriction])]letrestrictionkindvalue=Xml_light.Xml.Element("xs:"^kind,["value",value],[])letenumerationstring=restriction"enumeration"stringexceptionIllegal_restrictionletnot_negativestrn=ifn<0thenraiseIllegal_restrictionelserestrictionstr(string_of_intn)letfraction_digitsn=not_negative"fractionDigits"nletlengthn=not_negative"length"nletmax_exclusiven=restriction"maxExclusive"(string_of_intn)letmin_exclusiven=restriction"minExclusive"(string_of_intn)letmax_inclusiven=restriction"maxInclusive"(string_of_intn)letmin_inclusiven=restriction"minInclusive"(string_of_intn)letmax_lengthn=not_negative"maxLength"nletmin_lengthn=not_negative"minLength"nletpatternstr=restriction"pattern"strlettotal_digitsn=not_negative"totalDigits"nendmoduletypeAtom=sigtypetvalof_string:string->tvalto_string:t->stringvalxsd_format:Restriction.Format.tvalxsd_restrictions:Restriction.tlistendletto_xml~to_stringv=[Xml_light.Xml.PCData(to_stringv)]letof_xml~of_stringxml=matchxmlwith|Xml_light.Xml.Element(_,[],[])->of_string""|Xml_light.Xml.Element(_,[],[Xml_light.Xml.PCDatastr])|Xml_light.Xml.PCDatastr->of_stringstr|Xml_light.Xml.Element_->raise(Unexpected_xml(xml,"noname"))moduleMake(Atom:Atom):Xmlablewithtypet=Atom.t=structtypet=Atom.tletxsd=Restriction.simple_type~restrictions:Atom.xsd_restrictions~format:Atom.xsd_formatletto_xml=to_xml~to_string:Atom.to_stringletof_xml=of_xml~of_string:Atom.of_stringendletwrapxsd=Xml_light.Xml.Element("xs:schema",["xmlns:xs","http://www.w3.org/2001/XMLSchema"],[xsd])(** All the conversion functions *)(*let atom_conversion f xml =
match xml with
| Xml_light.Xml.Element (_, [], [Xml_light.Xml.PCData string])
| Xml_light.Xml.PCData string -> f string
| Xml_light.Xml.Element _ -> raise (Illegal_atom xml)*)letget_childnamexml=tryletchildren=Xml_light.Xml.childrenxmlinmatchList.find~f:(funt->name=Xml_light.Xml.tagt)childrenwith|Someres->res|None->letmsg=Printf.sprintf"Expected to find an entry %S but it was not present"nameinraise(Unexpected_xml(xml,msg))with|Xml_light.Xml.Not_elementxml->raise(Unexpected_xml(xml,name))type'aof_xml=xml->'aletof_xml_conversionof_strxml=matchxmlwith|Xml_light.Xml.Element(_,[],[])->of_str""|Xml_light.Xml.Element(_,[],[Xml_light.Xml.PCDatastr])|Xml_light.Xml.PCDatastr->of_strstr|Xml_light.Xml.Element_->raise(Unexpected_xml(xml,""))letunit_of_xmlxml=of_xml_conversion(fun_->())xmlletbool_of_xmlxml=of_xml_conversion(funstr->letstr=String.uppercasestrinifnot(str="TRUE"||str="FALSE")thenfailwith(Printf.sprintf"Xml conversion: Illegal boolean %s (should be TRUE or FALSE)."str)elsestr="TRUE")xmlletstring_of_xmlxml=of_xml_conversion(funx->x)xmlletchar_of_xmlxml=letfstr=String.getstr0inof_xml_conversionfxmlletint_of_xmlxml=of_xml_conversionint_of_stringxmlletfloat_of_xmlxml=of_xml_conversionfloat_of_stringxmlletint32_of_xmlxml=of_xml_conversionInt32.of_stringxmlletint64_of_xmlxml=of_xml_conversionInt64.of_stringxmlletnativeint_of_xmlxml=of_xml_conversionNativeint.of_stringxmlletbig_int_of_xmlxml=of_xml_conversionBig_int.big_int_of_stringxmlletnat_of_xmlxml=of_xml_conversionNat.nat_of_stringxmlletnum_of_xmlxml=of_xml_conversionNum.num_of_stringxmlletratio_of_xmlxml=of_xml_conversionRatio.ratio_of_stringxmlletrecursive_of_xmlnamea__of_xmlxml=letme=get_childnamexmlina__of_xmlmeletlist_of_xml?taga__of_xmlxml=matchtagwith|None->(matchxmlwith|Xml_light.Xml.Element(_,_,contents)->List.map~f:a__of_xmlcontents|Xml_light.Xml.PCData_->raise(Unexpected_xml(xml,"")))|Sometag->matchchildxmltagwith|None->[]|Somechild->matchchildwith|Xml_light.Xml.Element(_,_,contents)->List.map~f:a__of_xmlcontents|Xml_light.Xml.PCData_->raise(Unexpected_xml(xml,""))letoption_of_xml~taga__of_xmlxml=matchchildxmltagwith|None->None|Somechild->Some(a__of_xmlchild)letlazy_t_of_xmla__of_xmlxml=Lazy.from_val(a__of_xmlxml)letref_of_xmla__of_xmlxml=ref(a__of_xmlxml)letarray_of_xml~taga__of_xmlxml=letlst=list_of_xml~taga__of_xmlxmlinArray.of_listlstletconversionto_stringt=[Xml_light.Xml.PCData(to_stringt)]letconversion_sexpsexp_of_tt=letto_stringt=Sexplib.Sexp.to_string(sexp_of_tt)inconversionto_stringttype'ato_xml='a->xmllistletxml_of_unit()=conversion(fun()->"")()letxml_of_boolt=conversion(functiontrue->"TRUE"|false->"FALSE")tletxml_of_stringt=conversion(funx->x)tletxml_of_chart=letfchar=String.make1charinconversionftletxml_of_intt=conversionstring_of_inttletxml_of_floatt=conversion_sexpSexplib.Conv.sexp_of_floattletxml_of_int32t=conversionInt32.to_stringtletxml_of_int64t=conversionInt64.to_stringtletxml_of_nativeintt=conversionNativeint.to_stringtletxml_of_big_intt=conversionBig_int.string_of_big_inttletxml_of_natt=conversionNat.string_of_nattletxml_of_numt=conversionNum.string_of_numtletxml_of_ratiot=conversionRatio.string_of_ratiotletxml_of_list~tagxml_of__at=[create_node~tag~body:(List.map~f:(funt->create_node~tag~body:(xml_of__at))t)]letxml_of_option~tagxml_of__at=matchtwith|None->[]|Somet->[create_node~tag~body:(xml_of__at)]letxml_of_lazy_txml_of__at=xml_of__a(Lazy.force_valt)letxml_of_refxml_of__at=xml_of__a(!t)letxml_of_array~tagxml_of__at=xml_of_list~tagxml_of__a(Array.to_listt)letxsd_conversionbase=[Xml_light.Xml.Element("xs:simpleType",[],[Xml_light.Xml.Element("xs:restriction",["base","xs:"^base],[])])]typeto_xsd=xmllistletxsd_of_unit=xsd_conversion"string"letxsd_of_string=xsd_conversion"string"letxsd_of_char=xsd_conversion"string"letxsd_of_int=xsd_conversion"integer"letxsd_of_float=xsd_conversion"decimal"letxsd_of_int32=xsd_conversion"integer"letxsd_of_bool=letrestriction=letlst=["TRUE";"FALSE";"true";"false";"True";"False"]inXml_light.Xml.Element("xs:restriction",["base",Restriction.Format.to_string`string],(List.map~f:Restriction.enumerationlst))in[Xml_light.Xml.Element("xs:simpleType",[],[restriction])]letxsd_of_int64=xsd_conversion"integer"letxsd_of_nativeint=xsd_conversion"integer"letxsd_of_big_int=xsd_conversion"integer"letcomplex_typecontents=Xml_light.Xml.Element("xs:complexType",[],[Xml_light.Xml.Element("xs:sequence",[],contents)])letdecomplexifyxml=matchxmlwith|[Xml_light.Xml.Element("xs:complexType",[],[Xml_light.Xml.Element("xs:sequence",[],[contents])])]->contents|_->failwith"Not a complex_type"letdecomplexify_optxml=trySome(decomplexifyxml)with_->Noneletdecomplexify_listxml=matchxmlwith|[Xml_light.Xml.Element("xs:complexType",[],[Xml_light.Xml.Element("xs:sequence",[],contents)])]->Somecontents|_->Nonelettype_of_simplexml=matchxmlwith|[Xml_light.Xml.Element("xs:simpleType",[],[Xml_light.Xml.Element("xs:restriction",["base",t],children)])]->(matchchildrenwith|[]->t|_->(* enumeration values *)if"xs:string"=tthen"xs:bool"elsefailwith"Enumeration isn't representing a bool, Bad type")|_->failwith"Not a simple type"letxsd_element?(attr=[])~namecontents=Xml_light.Xml.Element("xs:element",("name",name)::attr,contents)letxml_element?(attr=[])~namecontents=Xml_light.Xml.Element(name,attr,contents)letxsd_of_list'~attrfield_namexsd_of__a=letelements=[complex_type[xsd_element~attr~name:field_namexsd_of__a]]in[complex_type[xsd_element~attr:["minOccurs","0";"maxOccurs","1"]~name:field_nameelements]]letxsd_of_listfield_namexsd_of__a=letattr=["minOccurs","0";"maxOccurs","unbounded"]inxsd_of_list'~attrfield_namexsd_of__aletxsd_of_array=xsd_of_listletxsd_of_nat=xsd_conversion"integer"letxsd_of_num=xsd_conversion"decimal"letxsd_of_ratio=xsd_conversion"decimal"letxsd_of_refxsd_of__a=xsd_of__aletxsd_of_lazy_txsd_of__a=xsd_of__aletxsd_of_optionfield_namexsd_of__a=letattr=["minOccurs","0";"maxOccurs","1"]in[complex_type[xsd_element~attr~name:field_namexsd_of__a]]moduletypeX=sigtypetvaladd_string:t->string->unitendmoduleWrite(X:X)=structmoduleY=structincludeXletstring_of_char=letmemo=Array.init256~f:(funcode->String.make1(Char.of_int_exncode))in(funchar->memo.(Char.to_intchar))letadd_chartchar=add_stringt(string_of_charchar)endincludeXml_light.Xml.Make(Y)end