xmlxsd.pas 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. unit xmlxsd;
  2. {$mode objfpc}
  3. {$H+}
  4. interface
  5. uses
  6. libxml2,
  7. SysUtils;
  8. { Format functions }
  9. function xsdFormatBoolean(Value: Boolean): String;
  10. function xsdFormatDate(Year, Month, Day: Longword): String;
  11. function xsdFormatTime(Daytime: Longword): String;
  12. function xsdFormatDateTime(Year, Month, Day, Daytime: Longword): String;
  13. function xsdFormatDecimal(Value: Extended; Precision: Integer = 4; Digits: Integer = 1): String;
  14. function xsdFormatDouble(Value: Double): String;
  15. function xsdFormatFloat(Value: Single): String;
  16. function xsdFormatByte(Value: Shortint): String;
  17. function xsdFormatShort(Value: Smallint): String;
  18. function xsdFormatInt(Value: Longint): String;
  19. function xsdFormatLong(Value: Int64): String;
  20. function xsdFormatUnsignedByte(Value: Byte): String;
  21. function xsdFormatUnsignedShort(Value: Word): String;
  22. function xsdFormatUnsignedInt(Value: Longword): String;
  23. function xsdFormatUnsignedLong(Value: QWord): String;
  24. { Node creation functions }
  25. function xsdNewChildBoolean(parent: xmlNodePtr; ns: xmlNsPtr; name: xmlCharPtr; Value: Boolean): xmlNodePtr;
  26. function xsdNewChildDate(parent: xmlNodePtr; ns: xmlNsPtr; name: xmlCharPtr; Year, Month, Day: Longword): xmlNodePtr;
  27. function xsdNewChildTime(parent: xmlNodePtr; ns: xmlNsPtr; name: xmlCharPtr; Daytime: Longword): xmlNodePtr;
  28. function xsdNewChildDateTime(parent: xmlNodePtr; ns: xmlNsPtr; name: xmlCharPtr; Year, Month, Day, Daytime: Longword): xmlNodePtr;
  29. function xsdNewChildDecimal(parent: xmlNodePtr; ns: xmlNsPtr; name: xmlCharPtr; Value: Extended; Precision: Integer = 4; Digits: Integer = 1): xmlNodePtr;
  30. function xsdNewChildDouble(parent: xmlNodePtr; ns: xmlNsPtr; name: xmlCharPtr; Value: Double): xmlNodePtr;
  31. function xsdNewChildFloat(parent: xmlNodePtr; ns: xmlNsPtr; name: xmlCharPtr; Value: Single): xmlNodePtr;
  32. function xsdNewChildByte(parent: xmlNodePtr; ns: xmlNsPtr; name: xmlCharPtr; Value: Shortint): xmlNodePtr;
  33. function xsdNewChildShort(parent: xmlNodePtr; ns: xmlNsPtr; name: xmlCharPtr; Value: Smallint): xmlNodePtr;
  34. function xsdNewChildInt(parent: xmlNodePtr; ns: xmlNsPtr; name: xmlCharPtr; Value: Longint): xmlNodePtr;
  35. function xsdNewChildLong(parent: xmlNodePtr; ns: xmlNsPtr; name: xmlCharPtr; Value: Int64): xmlNodePtr;
  36. function xsdNewChildUnsignedByte(parent: xmlNodePtr; ns: xmlNsPtr; name: xmlCharPtr; Value: Byte): xmlNodePtr;
  37. function xsdNewChildUnsignedShort(parent: xmlNodePtr; ns: xmlNsPtr; name: xmlCharPtr; Value: Word): xmlNodePtr;
  38. function xsdNewChildUnsignedInt(parent: xmlNodePtr; ns: xmlNsPtr; name: xmlCharPtr; Value: Longword): xmlNodePtr;
  39. function xsdNewChildUnsignedLong(parent: xmlNodePtr; ns: xmlNsPtr; name: xmlCharPtr; Value: QWord): xmlNodePtr;
  40. { Property creation functions }
  41. function xsdNewPropBoolean(node: xmlNodePtr; ns: xmlNsPtr; name: xmlCharPtr; Value: Boolean): xmlAttrPtr;
  42. function xsdNewPropDate(node: xmlNodePtr; ns: xmlNsPtr; name: xmlCharPtr; Year, Month, Day: Longword): xmlAttrPtr;
  43. function xsdNewPropTime(node: xmlNodePtr; ns: xmlNsPtr; name: xmlCharPtr; Daytime: Longword): xmlAttrPtr;
  44. function xsdNewPropDateTime(node: xmlNodePtr; ns: xmlNsPtr; name: xmlCharPtr; Year, Month, Day, Daytime: Longword): xmlAttrPtr;
  45. function xsdNewPropDecimal(node: xmlNodePtr; ns: xmlNsPtr; name: xmlCharPtr; Value: Extended; Precision: Integer = 4; Digits: Integer = 1): xmlAttrPtr;
  46. function xsdNewPropDouble(node: xmlNodePtr; ns: xmlNsPtr; name: xmlCharPtr; Value: Double): xmlAttrPtr;
  47. function xsdNewPropFloat(node: xmlNodePtr; ns: xmlNsPtr; name: xmlCharPtr; Value: Single): xmlAttrPtr;
  48. function xsdNewPropByte(node: xmlNodePtr; ns: xmlNsPtr; name: xmlCharPtr; Value: Shortint): xmlAttrPtr;
  49. function xsdNewPropShort(node: xmlNodePtr; ns: xmlNsPtr; name: xmlCharPtr; Value: Smallint): xmlAttrPtr;
  50. function xsdNewPropInt(node: xmlNodePtr; ns: xmlNsPtr; name: xmlCharPtr; Value: Longint): xmlAttrPtr;
  51. function xsdNewPropLong(node: xmlNodePtr; ns: xmlNsPtr; name: xmlCharPtr; Value: Int64): xmlAttrPtr;
  52. function xsdNewPropUnsignedByte(node: xmlNodePtr; ns: xmlNsPtr; name: xmlCharPtr; Value: Byte): xmlAttrPtr;
  53. function xsdNewPropUnsignedShort(node: xmlNodePtr; ns: xmlNsPtr; name: xmlCharPtr; Value: Word): xmlAttrPtr;
  54. function xsdNewPropUnsignedInt(node: xmlNodePtr; ns: xmlNsPtr; name: xmlCharPtr; Value: Longword): xmlAttrPtr;
  55. function xsdNewPropUnsignedLong(node: xmlNodePtr; ns: xmlNsPtr; name: xmlCharPtr; Value: QWord): xmlAttrPtr;
  56. implementation
  57. function xsdFormatBoolean(Value: Boolean): String;
  58. begin
  59. if Value then
  60. Result := 'true'
  61. else
  62. Result := 'false';
  63. end;
  64. function xsdFormatDate(Year, Month, Day: Longword): String;
  65. begin
  66. Result := Format('%4.4d-%2.2d-%2.2dZ', [Year, Month, Day]);
  67. end;
  68. function xsdFormatTime(Daytime: Longword): String;
  69. var
  70. Hour, Minute, Second: Longword;
  71. begin
  72. Daytime := Daytime div 1000; // ms to sec
  73. Second := Daytime mod 60; // extract sec
  74. Daytime := Daytime div 60; // sec to min
  75. Minute := Daytime mod 60; // extract min
  76. Daytime := Daytime div 60; // min to hour
  77. Hour := Daytime mod 60; // extract hour
  78. Result := Format('%2.2d:%2.2d:%2.2dZ', [Hour, Minute, Second]);
  79. end;
  80. function xsdFormatDateTime(Year, Month, Day, Daytime: Longword): String;
  81. var
  82. Hour, Minute, Second: Longword;
  83. begin
  84. Daytime := Daytime div 1000; // ms to sec
  85. Second := Daytime mod 60; // extract sec
  86. Daytime := Daytime div 60; // sec to min
  87. Minute := Daytime mod 60; // extract min
  88. Daytime := Daytime div 60; // min to hour
  89. Hour := Daytime mod 60; // extract hour
  90. Result := Format('%4.4d-%2.2d-%2.2dT%2.2d:%2.2d:%2.2dZ', [Year, Month, Day, Hour, Minute, Second]);
  91. end;
  92. function xsdFormatDecimal(Value: Extended; Precision: Integer; Digits: Integer): String;
  93. begin
  94. Result := FloatToStrF(Value, ffFixed, Precision, Digits);
  95. end;
  96. function xsdFormatDouble(Value: Double): String;
  97. begin
  98. Result := FloatToStr(Value);
  99. end;
  100. function xsdFormatFloat(Value: Single): String;
  101. begin
  102. Result := FloatToStr(Value);
  103. end;
  104. function xsdFormatByte(Value: Shortint): String;
  105. begin
  106. Result := IntToStr(Value);
  107. end;
  108. function xsdFormatShort(Value: Smallint): String;
  109. begin
  110. Result := IntToStr(Value);
  111. end;
  112. function xsdFormatInt(Value: Integer): String;
  113. begin
  114. Result := IntToStr(Value);
  115. end;
  116. function xsdFormatLong(Value: Int64): String;
  117. begin
  118. Result := IntToStr(Value);
  119. end;
  120. function xsdFormatUnsignedByte(Value: Byte): String;
  121. begin
  122. Result := IntToStr(Value);
  123. end;
  124. function xsdFormatUnsignedShort(Value: Word): String;
  125. begin
  126. Result := IntToStr(Value);
  127. end;
  128. function xsdFormatUnsignedInt(Value: Longword): String;
  129. begin
  130. Result := IntToStr(Value);
  131. end;
  132. function xsdFormatUnsignedLong(Value: QWord): String;
  133. begin
  134. Result := IntToStr(Value);
  135. end;
  136. function xsdNewChildBoolean(parent: xmlNodePtr; ns: xmlNsPtr; name: xmlCharPtr; Value: Boolean): xmlNodePtr;
  137. var
  138. Tmp: String;
  139. begin
  140. Tmp := xsdFormatBoolean(Value);
  141. Result := xmlNewChild(parent, ns, name, PChar(Tmp));
  142. end;
  143. function xsdNewChildTime(parent: xmlNodePtr; ns: xmlNsPtr; name: xmlCharPtr; Daytime: Longword): xmlNodePtr;
  144. var
  145. Tmp: String;
  146. begin
  147. Tmp := xsdFormatTime(Daytime);
  148. Result := xmlNewChild(parent, ns, name, PChar(Tmp));
  149. end;
  150. function xsdNewChildDate(parent: xmlNodePtr; ns: xmlNsPtr; name: xmlCharPtr; Year, Month, Day: Longword): xmlNodePtr;
  151. var
  152. Tmp: String;
  153. begin
  154. Tmp := xsdFormatDate(Year, Month, Day);
  155. Result := xmlNewChild(parent, ns, name, PChar(Tmp));
  156. end;
  157. function xsdNewChildDateTime(parent: xmlNodePtr; ns: xmlNsPtr; name: xmlCharPtr; Year, Month, Day, Daytime: Longword): xmlNodePtr;
  158. var
  159. Tmp: String;
  160. begin
  161. Tmp := xsdFormatDateTime(Year, Month, Day, Daytime);
  162. Result := xmlNewChild(parent, ns, name, PChar(Tmp));
  163. end;
  164. function xsdNewChildDecimal(parent: xmlNodePtr; ns: xmlNsPtr; name: xmlCharPtr; Value: Extended; Precision: Integer; Digits: Integer): xmlNodePtr;
  165. var
  166. Tmp: String;
  167. begin
  168. Tmp := xsdFormatDecimal(Value, Precision, Digits);
  169. Result := xmlNewChild(parent, ns, name, PChar(Tmp));
  170. end;
  171. function xsdNewChildDouble(parent: xmlNodePtr; ns: xmlNsPtr; name: xmlCharPtr; Value: Double): xmlNodePtr;
  172. var
  173. Tmp: String;
  174. begin
  175. Tmp := xsdFormatDouble(Value);
  176. Result := xmlNewChild(parent, ns, name, PChar(Tmp));
  177. end;
  178. function xsdNewChildFloat(parent: xmlNodePtr; ns: xmlNsPtr; name: xmlCharPtr; Value: Single): xmlNodePtr;
  179. var
  180. Tmp: String;
  181. begin
  182. Tmp := xsdFormatFloat(Value);
  183. Result := xmlNewChild(parent, ns, name, PChar(Tmp));
  184. end;
  185. function xsdNewChildByte(parent: xmlNodePtr; ns: xmlNsPtr; name: xmlCharPtr; Value: Shortint): xmlNodePtr;
  186. var
  187. Tmp: String;
  188. begin
  189. Tmp := xsdFormatByte(Value);
  190. Result := xmlNewChild(parent, ns, name, PChar(Tmp));
  191. end;
  192. function xsdNewChildShort(parent: xmlNodePtr; ns: xmlNsPtr; name: xmlCharPtr; Value: Smallint): xmlNodePtr;
  193. var
  194. Tmp: String;
  195. begin
  196. Tmp := xsdFormatShort(Value);
  197. Result := xmlNewChild(parent, ns, name, PChar(Tmp));
  198. end;
  199. function xsdNewChildInt(parent: xmlNodePtr; ns: xmlNsPtr; name: xmlCharPtr; Value: Longint): xmlNodePtr;
  200. var
  201. Tmp: String;
  202. begin
  203. Tmp := xsdFormatInt(Value);
  204. Result := xmlNewChild(parent, ns, name, PChar(Tmp));
  205. end;
  206. function xsdNewChildLong(parent: xmlNodePtr; ns: xmlNsPtr; name: xmlCharPtr; Value: Int64): xmlNodePtr;
  207. var
  208. Tmp: String;
  209. begin
  210. Tmp := xsdFormatLong(Value);
  211. Result := xmlNewChild(parent, ns, name, PChar(Tmp));
  212. end;
  213. function xsdNewChildUnsignedByte(parent: xmlNodePtr; ns: xmlNsPtr; name: xmlCharPtr; Value: Byte): xmlNodePtr;
  214. var
  215. Tmp: String;
  216. begin
  217. Tmp := xsdFormatUnsignedByte(Value);
  218. Result := xmlNewChild(parent, ns, name, PChar(Tmp));
  219. end;
  220. function xsdNewChildUnsignedShort(parent: xmlNodePtr; ns: xmlNsPtr; name: xmlCharPtr; Value: Word): xmlNodePtr;
  221. var
  222. Tmp: String;
  223. begin
  224. Tmp := xsdFormatUnsignedShort(Value);
  225. Result := xmlNewChild(parent, ns, name, PChar(Tmp));
  226. end;
  227. function xsdNewChildUnsignedInt(parent: xmlNodePtr; ns: xmlNsPtr; name: xmlCharPtr; Value: Longword): xmlNodePtr;
  228. var
  229. Tmp: String;
  230. begin
  231. Tmp := xsdFormatUnsignedInt(Value);
  232. Result := xmlNewChild(parent, ns, name, PChar(Tmp));
  233. end;
  234. function xsdNewChildUnsignedLong(parent: xmlNodePtr; ns: xmlNsPtr; name: xmlCharPtr; Value: QWord): xmlNodePtr;
  235. var
  236. Tmp: String;
  237. begin
  238. Tmp := xsdFormatUnsignedLong(Value);
  239. Result := xmlNewChild(parent, ns, name, PChar(Tmp));
  240. end;
  241. function xsdNewPropBoolean(node: xmlNodePtr; ns: xmlNsPtr; name: xmlCharPtr; Value: Boolean): xmlAttrPtr;
  242. var
  243. Tmp: String;
  244. begin
  245. Tmp := xsdFormatBoolean(Value);
  246. Result := xmlNewNsProp(node, ns, name, PChar(Tmp));
  247. end;
  248. function xsdNewPropTime(node: xmlNodePtr; ns: xmlNsPtr; name: xmlCharPtr; Daytime: Longword): xmlAttrPtr;
  249. var
  250. Tmp: String;
  251. begin
  252. Tmp := xsdFormatTime(Daytime);
  253. Result := xmlNewNsProp(node, ns, name, PChar(Tmp));
  254. end;
  255. function xsdNewPropDate(node: xmlNodePtr; ns: xmlNsPtr; name: xmlCharPtr; Year, Month, Day: Longword): xmlAttrPtr;
  256. var
  257. Tmp: String;
  258. begin
  259. Tmp := xsdFormatDate(Year, Month, Day);
  260. Result := xmlNewNsProp(node, ns, name, PChar(Tmp));
  261. end;
  262. function xsdNewPropDateTime(node: xmlNodePtr; ns: xmlNsPtr; name: xmlCharPtr; Year, Month, Day, Daytime: Longword): xmlAttrPtr;
  263. var
  264. Tmp: String;
  265. begin
  266. Tmp := xsdFormatDateTime(Year, Month, Day, Daytime);
  267. Result := xmlNewNsProp(node, ns, name, PChar(Tmp));
  268. end;
  269. function xsdNewPropDecimal(node: xmlNodePtr; ns: xmlNsPtr; name: xmlCharPtr; Value: Extended; Precision: Integer; Digits: Integer): xmlAttrPtr;
  270. var
  271. Tmp: String;
  272. begin
  273. Tmp := xsdFormatDecimal(Value, Precision, Digits);
  274. Result := xmlNewNsProp(node, ns, name, PChar(Tmp));
  275. end;
  276. function xsdNewPropDouble(node: xmlNodePtr; ns: xmlNsPtr; name: xmlCharPtr; Value: Double): xmlAttrPtr;
  277. var
  278. Tmp: String;
  279. begin
  280. Tmp := xsdFormatDouble(Value);
  281. Result := xmlNewNsProp(node, ns, name, PChar(Tmp));
  282. end;
  283. function xsdNewPropFloat(node: xmlNodePtr; ns: xmlNsPtr; name: xmlCharPtr; Value: Single): xmlAttrPtr;
  284. var
  285. Tmp: String;
  286. begin
  287. Tmp := xsdFormatFloat(Value);
  288. Result := xmlNewNsProp(node, ns, name, PChar(Tmp));
  289. end;
  290. function xsdNewPropByte(node: xmlNodePtr; ns: xmlNsPtr; name: xmlCharPtr; Value: Shortint): xmlAttrPtr;
  291. var
  292. Tmp: String;
  293. begin
  294. Tmp := xsdFormatByte(Value);
  295. Result := xmlNewNsProp(node, ns, name, PChar(Tmp));
  296. end;
  297. function xsdNewPropShort(node: xmlNodePtr; ns: xmlNsPtr; name: xmlCharPtr; Value: Smallint): xmlAttrPtr;
  298. var
  299. Tmp: String;
  300. begin
  301. Tmp := xsdFormatShort(Value);
  302. Result := xmlNewNsProp(node, ns, name, PChar(Tmp));
  303. end;
  304. function xsdNewPropInt(node: xmlNodePtr; ns: xmlNsPtr; name: xmlCharPtr; Value: Longint): xmlAttrPtr;
  305. var
  306. Tmp: String;
  307. begin
  308. Tmp := xsdFormatInt(Value);
  309. Result := xmlNewNsProp(node, ns, name, PChar(Tmp));
  310. end;
  311. function xsdNewPropLong(node: xmlNodePtr; ns: xmlNsPtr; name: xmlCharPtr; Value: Int64): xmlAttrPtr;
  312. var
  313. Tmp: String;
  314. begin
  315. Tmp := xsdFormatLong(Value);
  316. Result := xmlNewNsProp(node, ns, name, PChar(Tmp));
  317. end;
  318. function xsdNewPropUnsignedByte(node: xmlNodePtr; ns: xmlNsPtr; name: xmlCharPtr; Value: Byte): xmlAttrPtr;
  319. var
  320. Tmp: String;
  321. begin
  322. Tmp := xsdFormatUnsignedByte(Value);
  323. Result := xmlNewNsProp(node, ns, name, PChar(Tmp));
  324. end;
  325. function xsdNewPropUnsignedShort(node: xmlNodePtr; ns: xmlNsPtr; name: xmlCharPtr; Value: Word): xmlAttrPtr;
  326. var
  327. Tmp: String;
  328. begin
  329. Tmp := xsdFormatUnsignedShort(Value);
  330. Result := xmlNewNsProp(node, ns, name, PChar(Tmp));
  331. end;
  332. function xsdNewPropUnsignedInt(node: xmlNodePtr; ns: xmlNsPtr; name: xmlCharPtr; Value: Longword): xmlAttrPtr;
  333. var
  334. Tmp: String;
  335. begin
  336. Tmp := xsdFormatUnsignedInt(Value);
  337. Result := xmlNewNsProp(node, ns, name, PChar(Tmp));
  338. end;
  339. function xsdNewPropUnsignedLong(node: xmlNodePtr; ns: xmlNsPtr; name: xmlCharPtr; Value: QWord): xmlAttrPtr;
  340. var
  341. Tmp: String;
  342. begin
  343. Tmp := xsdFormatUnsignedLong(Value);
  344. Result := xmlNewNsProp(node, ns, name, PChar(Tmp));
  345. end;
  346. end.