thorxmlwrite.cpp 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256
  1. /*##############################################################################
  2. HPCC SYSTEMS software Copyright (C) 2012 HPCC Systems.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ############################################################################## */
  13. #include "platform.h"
  14. #include "jlib.hpp"
  15. #include "thorxmlwrite.hpp"
  16. #include "eclrtl.hpp"
  17. #include "rtlkey.hpp"
  18. #include "eclhelper.hpp"
  19. #include "deftype.hpp"
  20. #include "rtlbcd.hpp"
  21. CommonXmlWriter::CommonXmlWriter(unsigned _flags, unsigned initialIndent, IXmlStreamFlusher *_flusher)
  22. {
  23. flusher = _flusher;
  24. flags = _flags;
  25. indent = initialIndent;
  26. nestLimit = flags & XWFnoindent ? (unsigned) -1 : 0;
  27. tagClosed = true;
  28. }
  29. CommonXmlWriter::~CommonXmlWriter()
  30. {
  31. flush(true);
  32. }
  33. CommonXmlWriter & CommonXmlWriter::clear()
  34. {
  35. out.clear();
  36. indent = 0;
  37. nestLimit = flags & XWFnoindent ? (unsigned) -1 : 0;
  38. tagClosed = true;
  39. return *this;
  40. }
  41. bool CommonXmlWriter::checkForAttribute(const char * fieldname)
  42. {
  43. if (!tagClosed)
  44. {
  45. if (fieldname && (fieldname[0] == '@'))
  46. return true;
  47. closeTag();
  48. }
  49. return false;
  50. }
  51. void CommonXmlWriter::closeTag()
  52. {
  53. if (!tagClosed)
  54. {
  55. out.append(">");
  56. if (!nestLimit)
  57. out.newline();
  58. tagClosed = true;
  59. }
  60. flush(false);
  61. }
  62. void CommonXmlWriter::outputQuoted(const char *text)
  63. {
  64. out.append(text);
  65. }
  66. void CommonXmlWriter::outputString(unsigned len, const char *field, const char *fieldname)
  67. {
  68. if (flags & XWFtrim)
  69. len = rtlTrimStrLen(len, field);
  70. if ((flags & XWFopt) && (rtlTrimStrLen(len, field) == 0))
  71. return;
  72. if (checkForAttribute(fieldname))
  73. outputXmlAttrString(len, field, fieldname+1, out);
  74. else
  75. {
  76. if (!nestLimit)
  77. out.pad(indent);
  78. outputXmlString(len, field, fieldname, out);
  79. if (!nestLimit)
  80. out.newline();
  81. }
  82. }
  83. void CommonXmlWriter::outputQString(unsigned len, const char *field, const char *fieldname)
  84. {
  85. MemoryAttr tempBuffer;
  86. char * temp;
  87. if (len <= 100)
  88. temp = (char *)alloca(len);
  89. else
  90. temp = (char *)tempBuffer.allocate(len);
  91. rtlQStrToStr(len, temp, len, field);
  92. outputString(len, temp, fieldname);
  93. }
  94. void CommonXmlWriter::outputBool(bool field, const char *fieldname)
  95. {
  96. if (checkForAttribute(fieldname))
  97. outputXmlAttrBool(field, fieldname+1, out);
  98. else
  99. {
  100. if (!nestLimit)
  101. out.pad(indent);
  102. outputXmlBool(field, fieldname, out);
  103. if (!nestLimit)
  104. out.newline();
  105. }
  106. }
  107. void CommonXmlWriter::outputData(unsigned len, const void *field, const char *fieldname)
  108. {
  109. if (checkForAttribute(fieldname))
  110. outputXmlAttrData(len, field, fieldname+1, out);
  111. else
  112. {
  113. if (!nestLimit)
  114. out.pad(indent);
  115. outputXmlData(len, field, fieldname, out);
  116. if (!nestLimit)
  117. out.newline();
  118. }
  119. }
  120. void CommonXmlWriter::outputInt(__int64 field, const char *fieldname)
  121. {
  122. if (checkForAttribute(fieldname))
  123. outputXmlAttrInt(field, fieldname+1, out);
  124. else
  125. {
  126. if (!nestLimit)
  127. out.pad(indent);
  128. outputXmlInt(field, fieldname, out);
  129. if (!nestLimit)
  130. out.newline();
  131. }
  132. }
  133. void CommonXmlWriter::outputUInt(unsigned __int64 field, const char *fieldname)
  134. {
  135. if (checkForAttribute(fieldname))
  136. outputXmlAttrUInt(field, fieldname+1, out);
  137. else
  138. {
  139. if (!nestLimit)
  140. out.pad(indent);
  141. outputXmlUInt(field, fieldname, out);
  142. if (!nestLimit)
  143. out.newline();
  144. }
  145. }
  146. void CommonXmlWriter::outputReal(double field, const char *fieldname)
  147. {
  148. if (checkForAttribute(fieldname))
  149. outputXmlAttrReal(field, fieldname+1, out);
  150. else
  151. {
  152. if (!nestLimit)
  153. out.pad(indent);
  154. outputXmlReal(field, fieldname, out);
  155. if (!nestLimit)
  156. out.newline();
  157. }
  158. }
  159. void CommonXmlWriter::outputDecimal(const void *field, unsigned size, unsigned precision, const char *fieldname)
  160. {
  161. if (checkForAttribute(fieldname))
  162. outputXmlAttrDecimal(field, size, precision, fieldname+1, out);
  163. else
  164. {
  165. if (!nestLimit)
  166. out.pad(indent);
  167. outputXmlDecimal(field, size, precision, fieldname, out);
  168. if (!nestLimit)
  169. out.newline();
  170. }
  171. }
  172. void CommonXmlWriter::outputUDecimal(const void *field, unsigned size, unsigned precision, const char *fieldname)
  173. {
  174. if (checkForAttribute(fieldname))
  175. outputXmlAttrUDecimal(field, size, precision, fieldname+1, out);
  176. else
  177. {
  178. if (!nestLimit)
  179. out.pad(indent);
  180. outputXmlUDecimal(field, size, precision, fieldname, out);
  181. if (!nestLimit)
  182. out.newline();
  183. }
  184. }
  185. void CommonXmlWriter::outputUnicode(unsigned len, const UChar *field, const char *fieldname)
  186. {
  187. if (flags & XWFtrim)
  188. len = rtlTrimUnicodeStrLen(len, field);
  189. if ((flags & XWFopt) && (rtlTrimUnicodeStrLen(len, field) == 0))
  190. return;
  191. if (checkForAttribute(fieldname))
  192. outputXmlAttrUnicode(len, field, fieldname+1, out);
  193. else
  194. {
  195. if (!nestLimit)
  196. out.pad(indent);
  197. outputXmlUnicode(len, field, fieldname, out);
  198. if (!nestLimit)
  199. out.newline();
  200. }
  201. }
  202. void CommonXmlWriter::outputUtf8(unsigned len, const char *field, const char *fieldname)
  203. {
  204. if (flags & XWFtrim)
  205. len = rtlTrimUtf8StrLen(len, field);
  206. if ((flags & XWFopt) && (rtlTrimUtf8StrLen(len, field) == 0))
  207. return;
  208. if (checkForAttribute(fieldname))
  209. outputXmlAttrUtf8(len, field, fieldname+1, out);
  210. else
  211. {
  212. if (!nestLimit)
  213. out.pad(indent);
  214. outputXmlUtf8(len, field, fieldname, out);
  215. if (!nestLimit)
  216. out.newline();
  217. }
  218. }
  219. void CommonXmlWriter::outputBeginNested(const char *fieldname, bool nestChildren)
  220. {
  221. const char * sep = strchr(fieldname, '/');
  222. if (sep)
  223. {
  224. StringAttr leading(fieldname, sep-fieldname);
  225. outputBeginNested(leading, nestChildren);
  226. outputBeginNested(sep+1, nestChildren);
  227. return;
  228. }
  229. closeTag();
  230. if (!nestLimit)
  231. out.pad(indent);
  232. out.append('<').append(fieldname);
  233. indent += 1;
  234. if (!nestChildren && !nestLimit)
  235. nestLimit = indent;
  236. tagClosed = false;
  237. }
  238. void CommonXmlWriter::outputEndNested(const char *fieldname)
  239. {
  240. const char * sep = strchr(fieldname, '/');
  241. if (sep)
  242. {
  243. StringAttr leading(fieldname, sep-fieldname);
  244. outputEndNested(sep+1);
  245. outputEndNested(leading);
  246. return;
  247. }
  248. if (flags & XWFexpandempty)
  249. closeTag();
  250. if (!tagClosed)
  251. {
  252. out.append("/>");
  253. tagClosed = true;
  254. }
  255. else
  256. {
  257. if (!nestLimit)
  258. out.pad(indent-1);
  259. out.append("</").append(fieldname).append('>');
  260. }
  261. if (indent==nestLimit)
  262. nestLimit = 0;
  263. indent -= 1;
  264. if (!nestLimit)
  265. out.newline();
  266. }
  267. void CommonXmlWriter::outputSetAll()
  268. {
  269. closeTag();
  270. if (!nestLimit)
  271. out.pad(indent);
  272. outputXmlSetAll(out);
  273. if (!nestLimit)
  274. out.newline();
  275. }
  276. //=====================================================================================
  277. CommonJsonWriter::CommonJsonWriter(unsigned _flags, unsigned initialIndent, IXmlStreamFlusher *_flusher)
  278. {
  279. flusher = _flusher;
  280. flags = _flags;
  281. indent = initialIndent;
  282. nestLimit = flags & XWFnoindent ? (unsigned) -1 : 0;
  283. needDelimiter = false;
  284. }
  285. CommonJsonWriter::~CommonJsonWriter()
  286. {
  287. flush(true);
  288. }
  289. CommonJsonWriter & CommonJsonWriter::clear()
  290. {
  291. out.clear();
  292. indent = 0;
  293. nestLimit = flags & XWFnoindent ? (unsigned) -1 : 0;
  294. return *this;
  295. }
  296. void CommonJsonWriter::checkFormat(bool doDelimit, bool delimitNext, int inc)
  297. {
  298. if (doDelimit)
  299. {
  300. if (needDelimiter)
  301. {
  302. if (!out.length()) //new block
  303. out.append(',');
  304. else
  305. delimitJSON(out);
  306. }
  307. if (!nestLimit)
  308. out.append('\n').pad(indent);
  309. }
  310. indent+=inc;
  311. needDelimiter = delimitNext;
  312. }
  313. void CommonJsonWriter::checkDelimit(int inc)
  314. {
  315. checkFormat(true, true, inc);
  316. }
  317. const char *CommonJsonWriter::checkItemName(CJsonWriterItem *item, const char *name)
  318. {
  319. if (item && item->depth==0 && strieq(item->name, name))
  320. return NULL;
  321. return name;
  322. }
  323. const char *CommonJsonWriter::checkItemName(const char *name)
  324. {
  325. CJsonWriterItem *item = (arrays.length()) ? &arrays.tos() : NULL;
  326. return checkItemName(item, name);
  327. }
  328. const char *CommonJsonWriter::checkItemNameInc(const char *name)
  329. {
  330. CJsonWriterItem *item = (arrays.length()) ? &arrays.tos() : NULL;
  331. name = checkItemName(item, name);
  332. if (item)
  333. item->depth++;
  334. return name;
  335. }
  336. const char *CommonJsonWriter::checkItemNameDec(const char *name)
  337. {
  338. CJsonWriterItem *item = (arrays.length()) ? &arrays.tos() : NULL;
  339. if (item)
  340. item->depth--;
  341. return checkItemName(item, name);
  342. }
  343. void CommonJsonWriter::outputQuoted(const char *text)
  344. {
  345. checkDelimit();
  346. appendJSONValue(out, NULL, text);
  347. }
  348. void CommonJsonWriter::outputString(unsigned len, const char *field, const char *fieldname)
  349. {
  350. if (flags & XWFtrim)
  351. len = rtlTrimStrLen(len, field);
  352. if ((flags & XWFopt) && (rtlTrimStrLen(len, field) == 0))
  353. return;
  354. checkDelimit();
  355. appendJSONValue(out, checkItemName(fieldname), len, field);
  356. }
  357. void CommonJsonWriter::outputQString(unsigned len, const char *field, const char *fieldname)
  358. {
  359. MemoryAttr tempBuffer;
  360. char * temp;
  361. if (len <= 100)
  362. temp = (char *)alloca(len);
  363. else
  364. temp = (char *)tempBuffer.allocate(len);
  365. rtlQStrToStr(len, temp, len, field);
  366. outputString(len, temp, fieldname);
  367. }
  368. void CommonJsonWriter::outputBool(bool field, const char *fieldname)
  369. {
  370. checkDelimit();
  371. appendJSONValue(out, checkItemName(fieldname), field);
  372. }
  373. void CommonJsonWriter::outputData(unsigned len, const void *field, const char *fieldname)
  374. {
  375. checkDelimit();
  376. appendJSONValue(out, checkItemName(fieldname), len, field);
  377. }
  378. void CommonJsonWriter::outputInt(__int64 field, const char *fieldname)
  379. {
  380. checkDelimit();
  381. appendJSONValue(out, checkItemName(fieldname), field);
  382. }
  383. void CommonJsonWriter::outputUInt(unsigned __int64 field, const char *fieldname)
  384. {
  385. checkDelimit();
  386. appendJSONValue(out, checkItemName(fieldname), field);
  387. }
  388. void CommonJsonWriter::outputReal(double field, const char *fieldname)
  389. {
  390. checkDelimit();
  391. appendJSONValue(out, checkItemName(fieldname), field);
  392. }
  393. void CommonJsonWriter::outputDecimal(const void *field, unsigned size, unsigned precision, const char *fieldname)
  394. {
  395. checkDelimit();
  396. outputJsonDecimal(field, size, precision, checkItemName(fieldname), out);
  397. }
  398. void CommonJsonWriter::outputUDecimal(const void *field, unsigned size, unsigned precision, const char *fieldname)
  399. {
  400. checkDelimit();
  401. outputJsonUDecimal(field, size, precision, checkItemName(fieldname), out);
  402. }
  403. void CommonJsonWriter::outputUnicode(unsigned len, const UChar *field, const char *fieldname)
  404. {
  405. if (flags & XWFtrim)
  406. len = rtlTrimUnicodeStrLen(len, field);
  407. if ((flags & XWFopt) && (rtlTrimUnicodeStrLen(len, field) == 0))
  408. return;
  409. checkDelimit();
  410. outputJsonUnicode(len, field, checkItemName(fieldname), out);
  411. }
  412. void CommonJsonWriter::outputUtf8(unsigned len, const char *field, const char *fieldname)
  413. {
  414. if (flags & XWFtrim)
  415. len = rtlTrimUtf8StrLen(len, field);
  416. if ((flags & XWFopt) && (rtlTrimUtf8StrLen(len, field) == 0))
  417. return;
  418. checkDelimit();
  419. appendJSONValue(out, checkItemName(fieldname), len, field);
  420. }
  421. void CommonJsonWriter::outputBeginArray(const char *fieldname)
  422. {
  423. arrays.append(*new CJsonWriterItem(fieldname));
  424. const char * sep = strchr(fieldname, '/');
  425. while (sep)
  426. {
  427. StringAttr leading(fieldname, sep-fieldname);
  428. appendJSONName(out, leading).append(" {");
  429. fieldname = sep+1;
  430. sep = strchr(fieldname, '/');
  431. }
  432. checkFormat(false, false, 1);
  433. appendJSONName(out, fieldname).append('[');
  434. }
  435. void CommonJsonWriter::outputEndArray(const char *fieldname)
  436. {
  437. arrays.pop();
  438. checkFormat(false, true, -1);
  439. out.append(']');
  440. const char * sep = (fieldname) ? strchr(fieldname, '/') : NULL;
  441. while (sep)
  442. {
  443. out.append('}');
  444. sep = strchr(sep+1, '/');
  445. }
  446. }
  447. void CommonJsonWriter::outputBeginNested(const char *fieldname, bool nestChildren)
  448. {
  449. flush(false);
  450. checkFormat(true, false, 1);
  451. fieldname = checkItemNameInc(fieldname);
  452. if (fieldname)
  453. {
  454. const char * sep = (fieldname) ? strchr(fieldname, '/') : NULL;
  455. while (sep)
  456. {
  457. StringAttr leading(fieldname, sep-fieldname);
  458. appendJSONName(out, leading).append("{");
  459. fieldname = sep+1;
  460. sep = strchr(fieldname, '/');
  461. }
  462. appendJSONName(out, fieldname);
  463. }
  464. out.append("{");
  465. if (!nestChildren && !nestLimit)
  466. nestLimit = indent;
  467. }
  468. void CommonJsonWriter::outputEndNested(const char *fieldname)
  469. {
  470. flush(false);
  471. checkFormat(false, true, -1);
  472. fieldname = checkItemNameDec(fieldname);
  473. if (fieldname)
  474. {
  475. const char * sep = (fieldname) ? strchr(fieldname, '/') : NULL;
  476. while (sep)
  477. {
  478. out.append('}');
  479. sep = strchr(sep+1, '/');
  480. }
  481. }
  482. out.append("}");
  483. if (indent==nestLimit)
  484. nestLimit = 0;
  485. }
  486. void CommonJsonWriter::outputSetAll()
  487. {
  488. flush(false);
  489. checkDelimit();
  490. appendJSONValue(out, NULL, "All");
  491. }
  492. //=====================================================================================
  493. inline void outputEncodedXmlString(unsigned len, const char *field, const char *fieldname, StringBuffer &out)
  494. {
  495. if (fieldname)
  496. out.append('<').append(fieldname).append(" xsi:type=\"xsd:string\">");
  497. encodeXML(field, out, 0, len);
  498. if (fieldname)
  499. out.append("</").append(fieldname).append('>');
  500. }
  501. inline void outputEncodedXmlBool(bool field, const char *fieldname, StringBuffer &out)
  502. {
  503. const char * text = field ? "true" : "false";
  504. if (fieldname)
  505. out.append('<').append(fieldname).append(" xsi:type=\"xsd:boolean\">").append(text).append("</").append(fieldname).append('>');
  506. else
  507. out.append(text);
  508. }
  509. static char thorHelperhexchar[] = "0123456789ABCDEF";
  510. inline void outputEncodedXmlData(unsigned len, const void *_field, const char *fieldname, StringBuffer &out)
  511. {
  512. const unsigned char *field = (const unsigned char *) _field;
  513. if (fieldname)
  514. out.append('<').append(fieldname).append(" xsi:type=\"xsd:hexBinary\">");
  515. for (unsigned int i = 0; i < len; i++)
  516. {
  517. out.append(thorHelperhexchar[field[i] >> 4]).append(thorHelperhexchar[field[i] & 0x0f]);
  518. }
  519. if (fieldname)
  520. out.append("</").append(fieldname).append('>');
  521. }
  522. inline void outputEncoded64XmlData(unsigned len, const void *_field, const char *fieldname, StringBuffer &out)
  523. {
  524. if (fieldname)
  525. out.append('<').append(fieldname).append(" xsi:type=\"xsd:base64Binary\">");
  526. JBASE64_Encode(_field, len, out, false);
  527. if (fieldname)
  528. out.append("</").append(fieldname).append('>');
  529. }
  530. inline void outputEncodedXmlInt(__int64 field, const char *fieldname, StringBuffer &out)
  531. {
  532. if (fieldname)
  533. out.append('<').append(fieldname).append(" xsi:type=\"xsd:integer\">").append(field).append("</").append(fieldname).append('>');
  534. else
  535. out.append(field);
  536. }
  537. inline void outputEncodedXmlUInt(unsigned __int64 field, const char *fieldname, StringBuffer &out)
  538. {
  539. if (fieldname)
  540. out.append('<').append(fieldname).append(" xsi:type=\"xsd:nonNegativeInteger\">").append(field).append("</").append(fieldname).append('>');
  541. else
  542. out.append(field);
  543. }
  544. inline void outputEncodedXmlReal(double field, const char *fieldname, StringBuffer &out)
  545. {
  546. if (fieldname)
  547. out.append('<').append(fieldname).append(" xsi:type=\"xsd:double\">").append(field).append("</").append(fieldname).append('>');
  548. else
  549. out.append(field);
  550. }
  551. inline void outputEncodedXmlDecimal(const void *field, unsigned size, unsigned precision, const char *fieldname, StringBuffer &out)
  552. {
  553. char dec[50];
  554. if (fieldname)
  555. out.append('<').append(fieldname).append(" xsi:type=\"xsd:decimal\">");
  556. DecLock();
  557. if (DecValid(true, size*2-1, field))
  558. {
  559. DecPushDecimal(field, size, precision);
  560. DecPopCString(sizeof(dec), dec);
  561. const char *finger = dec;
  562. while(isspace(*finger)) finger++;
  563. out.append(finger);
  564. }
  565. else
  566. out.append("####");
  567. DecUnlock();
  568. if (fieldname)
  569. out.append("</").append(fieldname).append('>');
  570. }
  571. inline void outputEncodedXmlUDecimal(const void *field, unsigned size, unsigned precision, const char *fieldname, StringBuffer &out)
  572. {
  573. char dec[50];
  574. if (fieldname)
  575. out.append('<').append(fieldname).append(" xsi:type=\"xsd:decimal\">");
  576. DecLock();
  577. if (DecValid(false, size*2, field))
  578. {
  579. DecPushUDecimal(field, size, precision);
  580. DecPopCString(sizeof(dec), dec);
  581. const char *finger = dec;
  582. while(isspace(*finger)) finger++;
  583. out.append(finger);
  584. }
  585. else
  586. out.append("####");
  587. DecUnlock();
  588. if (fieldname)
  589. out.append("</").append(fieldname).append('>');
  590. }
  591. inline void outputEncodedXmlUnicode(unsigned len, const UChar *field, const char *fieldname, StringBuffer &out)
  592. {
  593. char * buff = 0;
  594. unsigned bufflen = 0;
  595. rtlUnicodeToCodepageX(bufflen, buff, len, field, "utf-8");
  596. if (fieldname)
  597. out.append('<').append(fieldname).append(" xsi:type=\"xsd:string\">");
  598. encodeXML(buff, out, 0, bufflen, true); // output as UTF-8
  599. if (fieldname)
  600. out.append("</").append(fieldname).append('>');
  601. rtlFree(buff);
  602. }
  603. inline void outputEncodedXmlUtf8(unsigned len, const char *field, const char *fieldname, StringBuffer &out)
  604. {
  605. if (fieldname)
  606. out.append('<').append(fieldname).append(" xsi:type=\"xsd:string\">");
  607. encodeXML(field, out, 0, rtlUtf8Size(len, field), true); // output as UTF-8
  608. if (fieldname)
  609. out.append("</").append(fieldname).append('>');
  610. }
  611. //=====================================================================================
  612. CommonEncodedXmlWriter::CommonEncodedXmlWriter(unsigned _flags, unsigned initialIndent, IXmlStreamFlusher *_flusher)
  613. : CommonXmlWriter(_flags, initialIndent, _flusher)
  614. {
  615. }
  616. void CommonEncodedXmlWriter::outputString(unsigned len, const char *field, const char *fieldname)
  617. {
  618. if (flags & XWFtrim)
  619. len = rtlTrimStrLen(len, field);
  620. if ((flags & XWFopt) && (rtlTrimStrLen(len, field) == 0))
  621. return;
  622. if (checkForAttribute(fieldname))
  623. outputXmlAttrString(len, field, fieldname+1, out);
  624. else
  625. {
  626. if (!nestLimit)
  627. out.pad(indent);
  628. outputEncodedXmlString(len, field, fieldname, out);
  629. if (!nestLimit)
  630. out.newline();
  631. }
  632. }
  633. void CommonEncodedXmlWriter::outputBool(bool field, const char *fieldname)
  634. {
  635. if (checkForAttribute(fieldname))
  636. outputXmlAttrBool(field, fieldname+1, out);
  637. else
  638. {
  639. if (!nestLimit)
  640. out.pad(indent);
  641. outputEncodedXmlBool(field, fieldname, out);
  642. if (!nestLimit)
  643. out.newline();
  644. }
  645. }
  646. void CommonEncodedXmlWriter::outputData(unsigned len, const void *field, const char *fieldname)
  647. {
  648. if (checkForAttribute(fieldname))
  649. outputXmlAttrData(len, field, fieldname+1, out);
  650. else
  651. {
  652. if (!nestLimit)
  653. out.pad(indent);
  654. outputEncodedXmlData(len, field, fieldname, out);
  655. if (!nestLimit)
  656. out.newline();
  657. }
  658. }
  659. void CommonEncodedXmlWriter::outputInt(__int64 field, const char *fieldname)
  660. {
  661. if (checkForAttribute(fieldname))
  662. outputXmlAttrInt(field, fieldname+1, out);
  663. else
  664. {
  665. if (!nestLimit)
  666. out.pad(indent);
  667. outputEncodedXmlInt(field, fieldname, out);
  668. if (!nestLimit)
  669. out.newline();
  670. }
  671. }
  672. void CommonEncodedXmlWriter::outputUInt(unsigned __int64 field, const char *fieldname)
  673. {
  674. if (checkForAttribute(fieldname))
  675. outputXmlAttrUInt(field, fieldname+1, out);
  676. else
  677. {
  678. if (!nestLimit)
  679. out.pad(indent);
  680. outputEncodedXmlUInt(field, fieldname, out);
  681. if (!nestLimit)
  682. out.newline();
  683. }
  684. }
  685. void CommonEncodedXmlWriter::outputReal(double field, const char *fieldname)
  686. {
  687. if (checkForAttribute(fieldname))
  688. outputXmlAttrReal(field, fieldname+1, out);
  689. else
  690. {
  691. if (!nestLimit)
  692. out.pad(indent);
  693. outputEncodedXmlReal(field, fieldname, out);
  694. if (!nestLimit)
  695. out.newline();
  696. }
  697. }
  698. void CommonEncodedXmlWriter::outputDecimal(const void *field, unsigned size, unsigned precision, const char *fieldname)
  699. {
  700. if (checkForAttribute(fieldname))
  701. outputXmlAttrDecimal(field, size, precision, fieldname+1, out);
  702. else
  703. {
  704. if (!nestLimit)
  705. out.pad(indent);
  706. outputEncodedXmlDecimal(field, size, precision, fieldname, out);
  707. if (!nestLimit)
  708. out.newline();
  709. }
  710. }
  711. void CommonEncodedXmlWriter::outputUDecimal(const void *field, unsigned size, unsigned precision, const char *fieldname)
  712. {
  713. if (checkForAttribute(fieldname))
  714. outputXmlAttrUDecimal(field, size, precision, fieldname+1, out);
  715. else
  716. {
  717. if (!nestLimit)
  718. out.pad(indent);
  719. outputEncodedXmlUDecimal(field, size, precision, fieldname, out);
  720. if (!nestLimit)
  721. out.newline();
  722. }
  723. }
  724. void CommonEncodedXmlWriter::outputUnicode(unsigned len, const UChar *field, const char *fieldname)
  725. {
  726. if (flags & XWFtrim)
  727. len = rtlTrimUnicodeStrLen(len, field);
  728. if ((flags & XWFopt) && (rtlTrimUnicodeStrLen(len, field) == 0))
  729. return;
  730. if (checkForAttribute(fieldname))
  731. outputXmlAttrUnicode(len, field, fieldname+1, out);
  732. else
  733. {
  734. if (!nestLimit)
  735. out.pad(indent);
  736. outputEncodedXmlUnicode(len, field, fieldname, out);
  737. if (!nestLimit)
  738. out.newline();
  739. }
  740. }
  741. void CommonEncodedXmlWriter::outputUtf8(unsigned len, const char *field, const char *fieldname)
  742. {
  743. if (flags & XWFtrim)
  744. len = rtlTrimUtf8StrLen(len, field);
  745. if ((flags & XWFopt) && (rtlTrimUtf8StrLen(len, field) == 0))
  746. return;
  747. if (checkForAttribute(fieldname))
  748. outputXmlAttrUtf8(len, field, fieldname+1, out);
  749. else
  750. {
  751. if (!nestLimit)
  752. out.pad(indent);
  753. outputEncodedXmlUtf8(len, field, fieldname, out);
  754. if (!nestLimit)
  755. out.newline();
  756. }
  757. }
  758. //=====================================================================================
  759. CommonEncoded64XmlWriter::CommonEncoded64XmlWriter(unsigned _flags, unsigned initialIndent, IXmlStreamFlusher *_flusher)
  760. : CommonEncodedXmlWriter(_flags, initialIndent, _flusher)
  761. {
  762. }
  763. void CommonEncoded64XmlWriter::outputData(unsigned len, const void *field, const char *fieldname)
  764. {
  765. if (checkForAttribute(fieldname))
  766. outputXmlAttrData(len, field, fieldname+1, out);
  767. else
  768. {
  769. if (!nestLimit)
  770. out.pad(indent);
  771. outputEncoded64XmlData(len, field, fieldname, out);
  772. if (!nestLimit)
  773. out.newline();
  774. }
  775. }
  776. //=====================================================================================
  777. CommonXmlWriter * CreateCommonXmlWriter(unsigned _flags, unsigned _initialIndent, IXmlStreamFlusher *_flusher, XMLWriterType xmlType)
  778. {
  779. switch (xmlType)
  780. {
  781. case WTStandard:
  782. return new CommonXmlWriter(_flags, _initialIndent, _flusher);//standard XML writer
  783. case WTEncodingData64:
  784. return new CommonEncoded64XmlWriter(_flags, _initialIndent, _flusher);//writes xsd type attributes, and all data as base64binary
  785. case WTEncoding:
  786. return new CommonEncodedXmlWriter(_flags, _initialIndent, _flusher);//writes xsd type attributes, and all data as hexBinary
  787. default:
  788. assertex(false);
  789. return NULL;
  790. }
  791. }
  792. //=====================================================================================
  793. IXmlWriter * createIXmlWriter(unsigned _flags, unsigned _initialIndent, IXmlStreamFlusher *_flusher, XMLWriterType xmlType)
  794. {
  795. if (xmlType==WTJSON)
  796. return new CommonJsonWriter(_flags, _initialIndent, _flusher);
  797. return CreateCommonXmlWriter(_flags, _initialIndent, _flusher, xmlType);
  798. }
  799. //=====================================================================================
  800. SimpleOutputWriter::SimpleOutputWriter()
  801. {
  802. separatorNeeded = false;
  803. }
  804. void SimpleOutputWriter::outputFieldSeparator()
  805. {
  806. if (separatorNeeded)
  807. out.append(',');
  808. separatorNeeded = true;
  809. }
  810. SimpleOutputWriter & SimpleOutputWriter::clear()
  811. {
  812. out.clear();
  813. separatorNeeded = false;
  814. return *this;
  815. }
  816. void SimpleOutputWriter::outputQuoted(const char *text)
  817. {
  818. out.append(text);
  819. }
  820. void SimpleOutputWriter::outputString(unsigned len, const char *field, const char *)
  821. {
  822. outputFieldSeparator();
  823. out.append(len, field);
  824. }
  825. void SimpleOutputWriter::outputQString(unsigned len, const char *field, const char *fieldname)
  826. {
  827. MemoryAttr tempBuffer;
  828. char * temp;
  829. if (len <= 100)
  830. temp = (char *)alloca(len);
  831. else
  832. temp = (char *)tempBuffer.allocate(len);
  833. rtlQStrToStr(len, temp, len, field);
  834. outputString(len, temp, fieldname);
  835. }
  836. void SimpleOutputWriter::outputBool(bool field, const char *)
  837. {
  838. outputFieldSeparator();
  839. outputXmlBool(field, NULL, out);
  840. }
  841. void SimpleOutputWriter::outputData(unsigned len, const void *field, const char *)
  842. {
  843. outputFieldSeparator();
  844. outputXmlData(len, field, NULL, out);
  845. }
  846. void SimpleOutputWriter::outputInt(__int64 field, const char *)
  847. {
  848. outputFieldSeparator();
  849. outputXmlInt(field, NULL, out);
  850. }
  851. void SimpleOutputWriter::outputUInt(unsigned __int64 field, const char *)
  852. {
  853. outputFieldSeparator();
  854. outputXmlUInt(field, NULL, out);
  855. }
  856. void SimpleOutputWriter::outputReal(double field, const char *)
  857. {
  858. outputFieldSeparator();
  859. outputXmlReal(field, NULL, out);
  860. }
  861. void SimpleOutputWriter::outputDecimal(const void *field, unsigned size, unsigned precision, const char *)
  862. {
  863. outputFieldSeparator();
  864. outputXmlDecimal(field, size, precision, NULL, out);
  865. }
  866. void SimpleOutputWriter::outputUDecimal(const void *field, unsigned size, unsigned precision, const char *)
  867. {
  868. outputFieldSeparator();
  869. outputXmlUDecimal(field, size, precision, NULL, out);
  870. }
  871. void SimpleOutputWriter::outputUnicode(unsigned len, const UChar *field, const char *)
  872. {
  873. outputFieldSeparator();
  874. outputXmlUnicode(len, field, NULL, out);
  875. }
  876. void SimpleOutputWriter::outputUtf8(unsigned len, const char *field, const char *)
  877. {
  878. outputFieldSeparator();
  879. outputXmlUtf8(len, field, NULL, out);
  880. }
  881. void SimpleOutputWriter::outputBeginNested(const char *s, bool)
  882. {
  883. if (!s || !*s)
  884. return;
  885. outputFieldSeparator();
  886. out.append('[');
  887. separatorNeeded = false;
  888. }
  889. void SimpleOutputWriter::outputEndNested(const char *s)
  890. {
  891. if (!s || !*s)
  892. return;
  893. out.append(']');
  894. separatorNeeded = true;
  895. }
  896. void SimpleOutputWriter::outputSetAll()
  897. {
  898. out.append('*');
  899. }
  900. void SimpleOutputWriter::newline()
  901. {
  902. out.append('\n');
  903. }
  904. //=====================================================================================
  905. CommonFieldProcessor::CommonFieldProcessor(StringBuffer &_result, bool _trim) : result(_result), trim(_trim)
  906. {
  907. }
  908. void CommonFieldProcessor::processString(unsigned len, const char *value, const RtlFieldInfo * field)
  909. {
  910. if (trim)
  911. len = rtlTrimStrLen(len, value);
  912. result.append("'");
  913. outputXmlString(len, value, NULL, result);
  914. result.append("'");
  915. }
  916. void CommonFieldProcessor::processBool(bool value, const RtlFieldInfo * field)
  917. {
  918. outputXmlBool(value, NULL, result);
  919. }
  920. void CommonFieldProcessor::processData(unsigned len, const void *value, const RtlFieldInfo * field)
  921. {
  922. outputXmlData(len, value, NULL, result);
  923. }
  924. void CommonFieldProcessor::processInt(__int64 value, const RtlFieldInfo * field)
  925. {
  926. outputXmlInt(value, NULL, result);
  927. }
  928. void CommonFieldProcessor::processUInt(unsigned __int64 value, const RtlFieldInfo * field)
  929. {
  930. outputXmlUInt(value, NULL, result);
  931. }
  932. void CommonFieldProcessor::processReal(double value, const RtlFieldInfo * field)
  933. {
  934. outputXmlReal(value, NULL, result);
  935. }
  936. void CommonFieldProcessor::processDecimal(const void *value, unsigned digits, unsigned precision, const RtlFieldInfo * field)
  937. {
  938. outputXmlDecimal(value, digits, precision, NULL, result);
  939. }
  940. void CommonFieldProcessor::processUDecimal(const void *value, unsigned digits, unsigned precision, const RtlFieldInfo * field)
  941. {
  942. outputXmlUDecimal(value, digits, precision, NULL, result);
  943. }
  944. void CommonFieldProcessor::processUnicode(unsigned len, const UChar *value, const RtlFieldInfo * field)
  945. {
  946. if (trim)
  947. len = rtlTrimUnicodeStrLen(len, value);
  948. outputXmlUnicode(len, value, NULL, result);
  949. }
  950. void CommonFieldProcessor::processQString(unsigned len, const char *value, const RtlFieldInfo * field)
  951. {
  952. MemoryAttr tempBuffer;
  953. char * temp;
  954. if (len <= 100)
  955. temp = (char *)alloca(len);
  956. else
  957. temp = (char *)tempBuffer.allocate(len);
  958. rtlQStrToStr(len, temp, len, value);
  959. processString(len, temp, field);
  960. }
  961. void CommonFieldProcessor::processSetAll(const RtlFieldInfo * field)
  962. {
  963. result.append("ALL");
  964. }
  965. void CommonFieldProcessor::processUtf8(unsigned len, const char *value, const RtlFieldInfo * field)
  966. {
  967. if (trim)
  968. len = rtlTrimUtf8StrLen(len, value);
  969. outputXmlUtf8(len, value, NULL, result);
  970. }
  971. bool CommonFieldProcessor::processBeginSet(const RtlFieldInfo * field)
  972. {
  973. result.append('[');
  974. return true;
  975. }
  976. bool CommonFieldProcessor::processBeginDataset(const RtlFieldInfo * field)
  977. {
  978. result.append('[');
  979. return true;
  980. }
  981. bool CommonFieldProcessor::processBeginRow(const RtlFieldInfo * field)
  982. {
  983. result.append('{');
  984. return true;
  985. }
  986. void CommonFieldProcessor::processEndSet(const RtlFieldInfo * field)
  987. {
  988. result.append(']');
  989. }
  990. void CommonFieldProcessor::processEndDataset(const RtlFieldInfo * field)
  991. {
  992. result.append(']');
  993. }
  994. void CommonFieldProcessor::processEndRow(const RtlFieldInfo * field)
  995. {
  996. result.append('}');
  997. }
  998. //=============================================================================================
  999. void printKeyedValues(StringBuffer &out, IIndexReadContext *segs, IOutputMetaData *rowMeta)
  1000. {
  1001. unsigned totalKeyedSize = 0;
  1002. unsigned numSegs = segs->ordinality();
  1003. while (numSegs)
  1004. {
  1005. IKeySegmentMonitor &seg = *segs->item(numSegs-1);
  1006. if (!seg.isWild())
  1007. {
  1008. totalKeyedSize = seg.getOffset() + seg.getSize();
  1009. break;
  1010. }
  1011. numSegs--;
  1012. }
  1013. if (numSegs)
  1014. {
  1015. byte *tempRow = (byte *) alloca(totalKeyedSize);
  1016. byte *savedRow = (byte *) alloca(totalKeyedSize);
  1017. const RtlFieldInfo * const *fields = rowMeta->queryTypeInfo()->queryFields();
  1018. unsigned fieldOffset = 0;
  1019. bool inKeyed = false;
  1020. bool inWild = false;
  1021. for (unsigned segNo = 0; segNo < numSegs; segNo++)
  1022. {
  1023. IKeySegmentMonitor &seg = *segs->item(segNo);
  1024. unsigned segOffset = seg.getOffset();
  1025. unsigned segSize = seg.getSize();
  1026. while (fieldOffset < segOffset + segSize) // This is trying to cope with the combined case but not sure it completely does
  1027. {
  1028. assertex(fields[0]->type->isFixedSize());
  1029. unsigned curFieldSize = fields[0]->type->size(NULL, NULL);
  1030. if (seg.isWild())
  1031. {
  1032. if (!inWild)
  1033. {
  1034. if (inKeyed)
  1035. {
  1036. out.append("),");
  1037. inKeyed = false;
  1038. }
  1039. out.append("WILD(");
  1040. inWild = true;
  1041. }
  1042. else
  1043. out.append(',');
  1044. out.append(fields[0]->name);
  1045. }
  1046. else
  1047. {
  1048. StringBuffer setValues;
  1049. CommonFieldProcessor setProcessor(setValues, true);
  1050. unsigned numValues = 0;
  1051. unsigned subStringLength = 0;
  1052. if (!seg.isEmpty())
  1053. {
  1054. seg.setLow(tempRow);
  1055. loop
  1056. {
  1057. if (numValues)
  1058. setValues.append(",");
  1059. memcpy(savedRow+segOffset, tempRow+segOffset, segSize);
  1060. seg.endRange(tempRow);
  1061. if (memcmp(savedRow+segOffset, tempRow+segOffset, segSize) != 0)
  1062. {
  1063. // Special case - if they differ only in trailing values that are 0 vs 0xff, then it's a substring match...
  1064. if (numValues==0 && (fields[0]->type->fieldType & (RFTMkind | RFTMebcdic)) == type_string)
  1065. {
  1066. unsigned pos;
  1067. for (pos = 0; pos < segSize; pos++)
  1068. {
  1069. if (savedRow[segOffset+pos] != tempRow[segOffset+pos])
  1070. break;
  1071. }
  1072. subStringLength = pos;
  1073. for (; pos < segSize; pos++)
  1074. {
  1075. if (savedRow[segOffset+pos] != 0 || tempRow[segOffset+pos] != 0xff)
  1076. {
  1077. subStringLength = 0;
  1078. break;
  1079. }
  1080. }
  1081. }
  1082. fields[0]->process(savedRow + fieldOffset, tempRow, setProcessor);
  1083. setValues.append("..");
  1084. fields[0]->process(tempRow + fieldOffset, tempRow, setProcessor);
  1085. numValues+=2;
  1086. }
  1087. else
  1088. {
  1089. fields[0]->process(tempRow + fieldOffset, tempRow, setProcessor);
  1090. numValues++;
  1091. }
  1092. if (!seg.increment(tempRow))
  1093. break;
  1094. }
  1095. }
  1096. if (!inKeyed)
  1097. {
  1098. if (inWild)
  1099. {
  1100. out.append("),");
  1101. inWild = false;
  1102. }
  1103. out.append("KEYED(");
  1104. inKeyed = true;
  1105. }
  1106. else
  1107. out.append(',');
  1108. out.append(fields[0]->name);
  1109. if (numValues==1)
  1110. out.append("=").append(setValues);
  1111. else if (subStringLength)
  1112. out.appendf("[1..%d]='", subStringLength).append(subStringLength, (char *) savedRow+fieldOffset).append("'");
  1113. else
  1114. out.append(" IN [").append(setValues).append("]");
  1115. }
  1116. fieldOffset += curFieldSize;
  1117. fields++;
  1118. if (!fields[0])
  1119. break;
  1120. }
  1121. }
  1122. if (inKeyed || inWild)
  1123. out.append(")");
  1124. }
  1125. else
  1126. out.append("UNKEYED");
  1127. }
  1128. extern thorhelper_decl void convertRowToXML(size32_t & lenResult, char * & result, IOutputMetaData & info, const void * row, unsigned flags)
  1129. {
  1130. const byte * self = (const byte *)row;
  1131. if (flags == (unsigned)-1)
  1132. flags = XWFtrim|XWFopt|XWFnoindent;
  1133. CommonXmlWriter writer(flags);
  1134. info.toXML(self, writer);
  1135. //could use detach...
  1136. unsigned sizeResult;
  1137. rtlStrToStrX(sizeResult, result, writer.length(), writer.str());
  1138. lenResult = rtlUtf8Length(sizeResult, result);
  1139. }