rtlformat.cpp 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042
  1. #include "jiface.hpp"
  2. #include "jbuff.hpp"
  3. #include "jptree.hpp"
  4. #include "jstring.hpp"
  5. #include "junicode.hpp"
  6. #include "rtlbcd.hpp"
  7. #include "rtlformat.hpp"
  8. SimpleOutputWriter::SimpleOutputWriter()
  9. {
  10. separatorNeeded = false;
  11. }
  12. void SimpleOutputWriter::outputFieldSeparator()
  13. {
  14. if (separatorNeeded)
  15. out.append(',');
  16. separatorNeeded = true;
  17. }
  18. SimpleOutputWriter & SimpleOutputWriter::clear()
  19. {
  20. out.clear();
  21. separatorNeeded = false;
  22. return *this;
  23. }
  24. void SimpleOutputWriter::outputQuoted(const char *text)
  25. {
  26. out.append(text);
  27. }
  28. void SimpleOutputWriter::outputString(unsigned len, const char *field, const char *)
  29. {
  30. outputFieldSeparator();
  31. out.append(len, field);
  32. }
  33. void SimpleOutputWriter::outputQString(unsigned len, const char *field, const char *fieldname)
  34. {
  35. MemoryAttr tempBuffer;
  36. char * temp;
  37. if (len <= 100)
  38. temp = (char *)alloca(len);
  39. else
  40. temp = (char *)tempBuffer.allocate(len);
  41. rtlQStrToStr(len, temp, len, field);
  42. outputString(len, temp, fieldname);
  43. }
  44. void SimpleOutputWriter::outputBool(bool field, const char *)
  45. {
  46. outputFieldSeparator();
  47. outputXmlBool(field, NULL, out);
  48. }
  49. void SimpleOutputWriter::outputData(unsigned len, const void *field, const char *)
  50. {
  51. outputFieldSeparator();
  52. outputXmlData(len, field, NULL, out);
  53. }
  54. void SimpleOutputWriter::outputInt(__int64 field, unsigned size, const char *)
  55. {
  56. outputFieldSeparator();
  57. outputXmlInt(field, NULL, out);
  58. }
  59. void SimpleOutputWriter::outputUInt(unsigned __int64 field, unsigned size, const char *)
  60. {
  61. outputFieldSeparator();
  62. outputXmlUInt(field, NULL, out);
  63. }
  64. void SimpleOutputWriter::outputReal(double field, const char *)
  65. {
  66. outputFieldSeparator();
  67. outputXmlReal(field, NULL, out);
  68. }
  69. void SimpleOutputWriter::outputDecimal(const void *field, unsigned size, unsigned precision, const char *)
  70. {
  71. outputFieldSeparator();
  72. outputXmlDecimal(field, size, precision, NULL, out);
  73. }
  74. void SimpleOutputWriter::outputUDecimal(const void *field, unsigned size, unsigned precision, const char *)
  75. {
  76. outputFieldSeparator();
  77. outputXmlUDecimal(field, size, precision, NULL, out);
  78. }
  79. void SimpleOutputWriter::outputUnicode(unsigned len, const UChar *field, const char *)
  80. {
  81. outputFieldSeparator();
  82. outputXmlUnicode(len, field, NULL, out);
  83. }
  84. void SimpleOutputWriter::outputUtf8(unsigned len, const char *field, const char *)
  85. {
  86. outputFieldSeparator();
  87. outputXmlUtf8(len, field, NULL, out);
  88. }
  89. void SimpleOutputWriter::outputBeginNested(const char *s, bool)
  90. {
  91. if (!s || !*s)
  92. return;
  93. outputFieldSeparator();
  94. out.append('[');
  95. separatorNeeded = false;
  96. }
  97. void SimpleOutputWriter::outputEndNested(const char *s)
  98. {
  99. if (!s || !*s)
  100. return;
  101. out.append(']');
  102. separatorNeeded = true;
  103. }
  104. void SimpleOutputWriter::outputSetAll()
  105. {
  106. out.append('*');
  107. }
  108. void SimpleOutputWriter::newline()
  109. {
  110. out.append('\n');
  111. }
  112. //=====================================================================================
  113. CommonXmlWriter::CommonXmlWriter(unsigned _flags, unsigned initialIndent, IXmlStreamFlusher *_flusher)
  114. {
  115. flusher = _flusher;
  116. flags = _flags;
  117. indent = initialIndent;
  118. nestLimit = flags & XWFnoindent ? (unsigned) -1 : 0;
  119. tagClosed = true;
  120. }
  121. CommonXmlWriter::~CommonXmlWriter()
  122. {
  123. flush(true);
  124. }
  125. IXmlWriterExt & CommonXmlWriter::clear()
  126. {
  127. out.clear();
  128. indent = 0;
  129. nestLimit = flags & XWFnoindent ? (unsigned) -1 : 0;
  130. tagClosed = true;
  131. return *this;
  132. }
  133. bool CommonXmlWriter::checkForAttribute(const char * fieldname)
  134. {
  135. if (!tagClosed)
  136. {
  137. if (fieldname && (fieldname[0] == '@'))
  138. return true;
  139. closeTag();
  140. }
  141. return false;
  142. }
  143. void CommonXmlWriter::closeTag()
  144. {
  145. if (!tagClosed)
  146. {
  147. out.append(">");
  148. if (!nestLimit)
  149. out.newline();
  150. tagClosed = true;
  151. }
  152. flush(false);
  153. }
  154. void CommonXmlWriter::cutFrom(IInterface *location, StringBuffer& databuf)
  155. {
  156. if (flusher)
  157. throwUnexpected();
  158. CommonXmlPosition *position = dynamic_cast<CommonXmlPosition *>(location);
  159. if (!position)
  160. return;
  161. if (position->pos < out.length())
  162. {
  163. size32_t startInd = position->pos;
  164. if (!position->tagClosed && out.charAt(startInd) == '>')
  165. startInd += 1;
  166. if (!position->nestLimit && startInd < out.length() && out.charAt(startInd) == '\n')
  167. startInd += 1;
  168. if (startInd < out.length())
  169. databuf.append(out.length() - startInd, out.str() + startInd);
  170. out.setLength(position->pos);
  171. tagClosed = position->tagClosed;
  172. indent = position->indent;
  173. nestLimit = position->nestLimit;
  174. }
  175. }
  176. void CommonXmlWriter::outputQuoted(const char *text)
  177. {
  178. out.append(text);
  179. }
  180. void CommonXmlWriter::outputString(unsigned len, const char *field, const char *fieldname)
  181. {
  182. if (flags & XWFtrim)
  183. len = rtlTrimStrLen(len, field);
  184. if ((flags & XWFopt) && (rtlTrimStrLen(len, field) == 0))
  185. return;
  186. if (checkForAttribute(fieldname))
  187. outputXmlAttrString(len, field, fieldname+1, out);
  188. else
  189. {
  190. if (!nestLimit)
  191. out.pad(indent);
  192. outputXmlString(len, field, fieldname, out);
  193. if (!nestLimit)
  194. out.newline();
  195. }
  196. }
  197. void CommonXmlWriter::outputQString(unsigned len, const char *field, const char *fieldname)
  198. {
  199. MemoryAttr tempBuffer;
  200. char * temp;
  201. if (len <= 100)
  202. temp = (char *)alloca(len);
  203. else
  204. temp = (char *)tempBuffer.allocate(len);
  205. rtlQStrToStr(len, temp, len, field);
  206. // outputString(len, temp, fieldname, isnumeric);
  207. outputString(len, temp, fieldname);
  208. }
  209. void CommonXmlWriter::outputBool(bool field, const char *fieldname)
  210. {
  211. if (checkForAttribute(fieldname))
  212. outputXmlAttrBool(field, fieldname+1, out);
  213. else
  214. {
  215. if (!nestLimit)
  216. out.pad(indent);
  217. outputXmlBool(field, fieldname, out);
  218. if (!nestLimit)
  219. out.newline();
  220. }
  221. }
  222. void CommonXmlWriter::outputData(unsigned len, const void *field, const char *fieldname)
  223. {
  224. if (checkForAttribute(fieldname))
  225. outputXmlAttrData(len, field, fieldname+1, out);
  226. else
  227. {
  228. if (!nestLimit)
  229. out.pad(indent);
  230. outputXmlData(len, field, fieldname, out);
  231. if (!nestLimit)
  232. out.newline();
  233. }
  234. }
  235. void CommonXmlWriter::outputInt(__int64 field, unsigned size, const char *fieldname)
  236. {
  237. if (checkForAttribute(fieldname))
  238. outputXmlAttrInt(field, fieldname+1, out);
  239. else
  240. {
  241. if (!nestLimit)
  242. out.pad(indent);
  243. outputXmlInt(field, fieldname, out);
  244. if (!nestLimit)
  245. out.newline();
  246. }
  247. }
  248. void CommonXmlWriter::outputUInt(unsigned __int64 field, unsigned size, const char *fieldname)
  249. {
  250. if (checkForAttribute(fieldname))
  251. outputXmlAttrUInt(field, fieldname+1, out);
  252. else
  253. {
  254. if (!nestLimit)
  255. out.pad(indent);
  256. outputXmlUInt(field, fieldname, out);
  257. if (!nestLimit)
  258. out.newline();
  259. }
  260. }
  261. void CommonXmlWriter::outputReal(double field, const char *fieldname)
  262. {
  263. if (checkForAttribute(fieldname))
  264. outputXmlAttrReal(field, fieldname+1, out);
  265. else
  266. {
  267. if (!nestLimit)
  268. out.pad(indent);
  269. outputXmlReal(field, fieldname, out);
  270. if (!nestLimit)
  271. out.newline();
  272. }
  273. }
  274. void CommonXmlWriter::outputDecimal(const void *field, unsigned size, unsigned precision, const char *fieldname)
  275. {
  276. if (checkForAttribute(fieldname))
  277. outputXmlAttrDecimal(field, size, precision, fieldname+1, out);
  278. else
  279. {
  280. if (!nestLimit)
  281. out.pad(indent);
  282. outputXmlDecimal(field, size, precision, fieldname, out);
  283. if (!nestLimit)
  284. out.newline();
  285. }
  286. }
  287. void CommonXmlWriter::outputUDecimal(const void *field, unsigned size, unsigned precision, const char *fieldname)
  288. {
  289. if (checkForAttribute(fieldname))
  290. outputXmlAttrUDecimal(field, size, precision, fieldname+1, out);
  291. else
  292. {
  293. if (!nestLimit)
  294. out.pad(indent);
  295. outputXmlUDecimal(field, size, precision, fieldname, out);
  296. if (!nestLimit)
  297. out.newline();
  298. }
  299. }
  300. void CommonXmlWriter::outputUnicode(unsigned len, const UChar *field, const char *fieldname)
  301. {
  302. if (flags & XWFtrim)
  303. len = rtlTrimUnicodeStrLen(len, field);
  304. if ((flags & XWFopt) && (rtlTrimUnicodeStrLen(len, field) == 0))
  305. return;
  306. if (checkForAttribute(fieldname))
  307. outputXmlAttrUnicode(len, field, fieldname+1, out);
  308. else
  309. {
  310. if (!nestLimit)
  311. out.pad(indent);
  312. outputXmlUnicode(len, field, fieldname, out);
  313. if (!nestLimit)
  314. out.newline();
  315. }
  316. }
  317. void CommonXmlWriter::outputUtf8(unsigned len, const char *field, const char *fieldname)
  318. {
  319. if (flags & XWFtrim)
  320. len = rtlTrimUtf8StrLen(len, field);
  321. if ((flags & XWFopt) && (rtlTrimUtf8StrLen(len, field) == 0))
  322. return;
  323. if (checkForAttribute(fieldname))
  324. outputXmlAttrUtf8(len, field, fieldname+1, out);
  325. else
  326. {
  327. if (!nestLimit)
  328. out.pad(indent);
  329. outputXmlUtf8(len, field, fieldname, out);
  330. if (!nestLimit)
  331. out.newline();
  332. }
  333. }
  334. void CommonXmlWriter::outputXmlns(const char *name, const char *uri)
  335. {
  336. StringBuffer fieldname;
  337. if (!streq(name, "xmlns"))
  338. fieldname.append("xmlns:");
  339. outputXmlAttrUtf8(rtlUtf8Length(strlen(uri), uri), uri, fieldname.append(name), out);
  340. }
  341. void CommonXmlWriter::outputBeginDataset(const char *dsname, bool nestChildren)
  342. {
  343. outputBeginNested("Dataset", nestChildren, false); //indent row, not dataset for backward compatibility
  344. if (nestChildren && indent==0)
  345. indent++;
  346. if (!dsname || !*dsname)
  347. return;
  348. out.append(" name='"); //single quote for backward compatibility
  349. outputXmlUtf8(rtlUtf8Length(strlen(dsname), dsname), dsname, NULL, out);
  350. out.append("'");
  351. }
  352. void CommonXmlWriter::outputEndDataset(const char *dsname)
  353. {
  354. outputEndNested("Dataset", false);
  355. }
  356. void CommonXmlWriter::outputBeginNested(const char *fieldname, bool nestChildren, bool doIndent)
  357. {
  358. if (!fieldname || !*fieldname)
  359. return;
  360. const char * sep = strchr(fieldname, '/');
  361. if (sep)
  362. {
  363. StringAttr leading(fieldname, sep-fieldname);
  364. outputBeginNested(leading, nestChildren, doIndent);
  365. outputBeginNested(sep+1, nestChildren, doIndent);
  366. return;
  367. }
  368. closeTag();
  369. if (!nestLimit && doIndent)
  370. out.pad(indent);
  371. out.append('<').append(fieldname);
  372. if (doIndent)
  373. indent += 1;
  374. if (!nestChildren && !nestLimit)
  375. nestLimit = indent;
  376. tagClosed = false;
  377. }
  378. void CommonXmlWriter::outputBeginNested(const char *fieldname, bool nestChildren)
  379. {
  380. outputBeginNested(fieldname, nestChildren, true);
  381. }
  382. void CommonXmlWriter::outputEndNested(const char *fieldname, bool doIndent)
  383. {
  384. if (!fieldname || !*fieldname)
  385. return;
  386. const char * sep = strchr(fieldname, '/');
  387. if (sep)
  388. {
  389. StringAttr leading(fieldname, sep-fieldname);
  390. outputEndNested(sep+1, doIndent);
  391. outputEndNested(leading, doIndent);
  392. return;
  393. }
  394. if (flags & XWFexpandempty)
  395. closeTag();
  396. if (!tagClosed)
  397. {
  398. out.append("/>");
  399. tagClosed = true;
  400. }
  401. else
  402. {
  403. if (!nestLimit && doIndent)
  404. out.pad(indent-1);
  405. out.append("</").append(fieldname).append('>');
  406. }
  407. if (indent==nestLimit)
  408. nestLimit = 0;
  409. if (doIndent)
  410. indent -= 1;
  411. if (!nestLimit)
  412. out.newline();
  413. }
  414. void CommonXmlWriter::outputEndNested(const char *fieldname)
  415. {
  416. outputEndNested(fieldname, true);
  417. }
  418. void CommonXmlWriter::outputSetAll()
  419. {
  420. closeTag();
  421. if (!nestLimit)
  422. out.pad(indent);
  423. outputXmlSetAll(out);
  424. if (!nestLimit)
  425. out.newline();
  426. }
  427. //=====================================================================================
  428. CommonJsonWriter::CommonJsonWriter(unsigned _flags, unsigned initialIndent, IXmlStreamFlusher *_flusher)
  429. {
  430. flusher = _flusher;
  431. flags = _flags;
  432. indent = initialIndent;
  433. nestLimit = flags & XWFnoindent ? (unsigned) -1 : 0;
  434. needDelimiter = false;
  435. }
  436. CommonJsonWriter::~CommonJsonWriter()
  437. {
  438. flush(true);
  439. }
  440. IXmlWriterExt & CommonJsonWriter::clear()
  441. {
  442. out.clear();
  443. indent = 0;
  444. nestLimit = flags & XWFnoindent ? (unsigned) -1 : 0;
  445. return *this;
  446. }
  447. void CommonJsonWriter::checkFormat(bool doDelimit, bool delimitNext, int inc)
  448. {
  449. if (doDelimit)
  450. {
  451. if (needDelimiter)
  452. {
  453. if (!out.length()) //new block
  454. out.append(',');
  455. else
  456. delimitJSON(out);
  457. }
  458. if (!nestLimit)
  459. out.append('\n').pad(indent);
  460. }
  461. indent+=inc;
  462. needDelimiter = delimitNext;
  463. }
  464. void CommonJsonWriter::checkDelimit(int inc)
  465. {
  466. checkFormat(true, true, inc);
  467. }
  468. const char *CommonJsonWriter::checkItemName(CJsonWriterItem *item, const char *name, bool simpleType)
  469. {
  470. if (simpleType && (!name || !*name))
  471. name = "#value"; //xml mixed content
  472. if (item && item->depth==0 && strieq(item->name, name))
  473. return NULL;
  474. return name;
  475. }
  476. const char *CommonJsonWriter::checkItemName(const char *name, bool simpleType)
  477. {
  478. CJsonWriterItem *item = (arrays.length()) ? &arrays.tos() : NULL;
  479. return checkItemName(item, name, simpleType);
  480. }
  481. const char *CommonJsonWriter::checkItemNameBeginNested(const char *name)
  482. {
  483. CJsonWriterItem *item = (arrays.length()) ? &arrays.tos() : NULL;
  484. name = checkItemName(item, name, false);
  485. if (item)
  486. item->depth++;
  487. return name;
  488. }
  489. bool CommonJsonWriter::checkUnamedArrayItem(bool begin)
  490. {
  491. CJsonWriterItem *item = (arrays.length()) ? &arrays.tos() : NULL;
  492. if (item && item->depth==(begin ? 0 : 1) && item->name.isEmpty())
  493. return true;
  494. return false;
  495. }
  496. const char *CommonJsonWriter::checkItemNameEndNested(const char *name)
  497. {
  498. CJsonWriterItem *item = (arrays.length()) ? &arrays.tos() : NULL;
  499. if (item)
  500. item->depth--;
  501. return checkItemName(item, name, false);
  502. }
  503. void CommonJsonWriter::outputQuoted(const char *text)
  504. {
  505. checkDelimit();
  506. appendJSONValue(out, NULL, text);
  507. }
  508. void CommonJsonWriter::outputNumericString(const char *field, const char *fieldname)
  509. {
  510. unsigned len = (size32_t)strlen(field);
  511. if (flags & XWFtrim)
  512. len = rtlTrimStrLen(len, field);
  513. if ((flags & XWFopt) && (rtlTrimStrLen(len, field) == 0))
  514. return;
  515. checkDelimit();
  516. appendJSONStringValue(out, checkItemName(fieldname), len, field, true, false);
  517. }
  518. void CommonJsonWriter::outputString(unsigned len, const char *field, const char *fieldname)
  519. {
  520. if (flags & XWFtrim)
  521. len = rtlTrimStrLen(len, field);
  522. if ((flags & XWFopt) && (rtlTrimStrLen(len, field) == 0))
  523. return;
  524. checkDelimit();
  525. appendJSONStringValue(out, checkItemName(fieldname), len, field, true);
  526. }
  527. void CommonJsonWriter::outputQString(unsigned len, const char *field, const char *fieldname)
  528. {
  529. MemoryAttr tempBuffer;
  530. char * temp;
  531. if (len <= 100)
  532. temp = (char *)alloca(len);
  533. else
  534. temp = (char *)tempBuffer.allocate(len);
  535. rtlQStrToStr(len, temp, len, field);
  536. outputString(len, temp, fieldname);
  537. }
  538. void CommonJsonWriter::outputBool(bool field, const char *fieldname)
  539. {
  540. checkDelimit();
  541. appendJSONValue(out, checkItemName(fieldname), field);
  542. }
  543. void CommonJsonWriter::outputData(unsigned len, const void *field, const char *fieldname)
  544. {
  545. checkDelimit();
  546. appendJSONDataValue(out, checkItemName(fieldname), len, field);
  547. }
  548. void CommonJsonWriter::outputInt(__int64 field, unsigned size, const char *fieldname)
  549. {
  550. checkDelimit();
  551. if (size < 7) //JavaScript only supports 53 significant bits
  552. appendJSONValue(out, checkItemName(fieldname), field);
  553. else
  554. {
  555. appendJSONNameOrDelimit(out, checkItemName(fieldname));
  556. out.append('"').append(field).append('"');
  557. }
  558. }
  559. void CommonJsonWriter::outputUInt(unsigned __int64 field, unsigned size, const char *fieldname)
  560. {
  561. checkDelimit();
  562. if (size < 7) //JavaScript doesn't support unsigned, and only supports 53 significant bits
  563. appendJSONValue(out, checkItemName(fieldname), field);
  564. else
  565. {
  566. appendJSONNameOrDelimit(out, checkItemName(fieldname));
  567. out.append('"').append(field).append('"');
  568. }
  569. }
  570. void CommonJsonWriter::outputReal(double field, const char *fieldname)
  571. {
  572. checkDelimit();
  573. appendJSONValue(out, checkItemName(fieldname), field);
  574. }
  575. void CommonJsonWriter::outputDecimal(const void *field, unsigned size, unsigned precision, const char *fieldname)
  576. {
  577. checkDelimit();
  578. outputJsonDecimal(field, size, precision, checkItemName(fieldname), out);
  579. }
  580. void CommonJsonWriter::outputUDecimal(const void *field, unsigned size, unsigned precision, const char *fieldname)
  581. {
  582. checkDelimit();
  583. outputJsonUDecimal(field, size, precision, checkItemName(fieldname), out);
  584. }
  585. void CommonJsonWriter::outputUnicode(unsigned len, const UChar *field, const char *fieldname)
  586. {
  587. if (flags & XWFtrim)
  588. len = rtlTrimUnicodeStrLen(len, field);
  589. if ((flags & XWFopt) && (rtlTrimUnicodeStrLen(len, field) == 0))
  590. return;
  591. checkDelimit();
  592. outputJsonUnicode(len, field, checkItemName(fieldname), out);
  593. }
  594. void CommonJsonWriter::outputUtf8(unsigned len, const char *field, const char *fieldname)
  595. {
  596. if (flags & XWFtrim)
  597. len = rtlTrimUtf8StrLen(len, field);
  598. if ((flags & XWFopt) && (rtlTrimUtf8StrLen(len, field) == 0))
  599. return;
  600. checkDelimit();
  601. appendJSONStringValue(out, checkItemName(fieldname), rtlUtf8Size(len, field), field, true);
  602. }
  603. void CommonJsonWriter::prepareBeginArray(const char *fieldname)
  604. {
  605. arrays.append(*new CJsonWriterItem(fieldname));
  606. const char * sep = (fieldname) ? strchr(fieldname, '/') : NULL;
  607. while (sep)
  608. {
  609. StringAttr leading(fieldname, sep-fieldname);
  610. appendJSONName(out, leading).append(" {");
  611. fieldname = sep+1;
  612. sep = strchr(fieldname, '/');
  613. }
  614. checkFormat(false, false, 1);
  615. }
  616. void CommonJsonWriter::outputBeginArray(const char *fieldname)
  617. {
  618. prepareBeginArray(fieldname);
  619. appendJSONName(out, fieldname).append('[');
  620. }
  621. void CommonJsonWriter::outputEndArray(const char *fieldname)
  622. {
  623. arrays.pop();
  624. checkFormat(false, true, -1);
  625. out.append(']');
  626. const char * sep = (fieldname) ? strchr(fieldname, '/') : NULL;
  627. while (sep)
  628. {
  629. out.append('}');
  630. sep = strchr(sep+1, '/');
  631. }
  632. }
  633. void CommonJsonWriter::outputBeginDataset(const char *dsname, bool nestChildren)
  634. {
  635. if (dsname && *dsname)
  636. outputBeginNested(dsname, nestChildren);
  637. }
  638. void CommonJsonWriter::outputEndDataset(const char *dsname)
  639. {
  640. if (dsname && *dsname)
  641. outputEndNested(dsname);
  642. }
  643. void CommonJsonWriter::cutFrom(IInterface *location, StringBuffer& databuf)
  644. {
  645. if (flusher)
  646. throwUnexpected();
  647. CommonXmlPosition *position = dynamic_cast<CommonXmlPosition *>(location);
  648. if (!position)
  649. return;
  650. if (position->pos < out.length())
  651. {
  652. databuf.append(out.length() - position->pos, out.str() + position->pos);
  653. out.setLength(position->pos);
  654. needDelimiter = position->needDelimiter;
  655. indent = position->indent;
  656. nestLimit = position->nestLimit;
  657. }
  658. }
  659. void CommonJsonWriter::outputBeginNested(const char *fieldname, bool nestChildren)
  660. {
  661. if (!fieldname)
  662. return;
  663. if (!*fieldname && !checkUnamedArrayItem(true))
  664. return;
  665. checkFormat(true, false, 1);
  666. fieldname = checkItemNameBeginNested(fieldname);
  667. if (fieldname && *fieldname)
  668. {
  669. const char * sep = strchr(fieldname, '/');
  670. while (sep)
  671. {
  672. StringAttr leading(fieldname, sep-fieldname);
  673. appendJSONName(out, leading).append("{");
  674. fieldname = sep+1;
  675. sep = strchr(fieldname, '/');
  676. }
  677. appendJSONName(out, fieldname);
  678. }
  679. out.append("{");
  680. if (!nestChildren && !nestLimit)
  681. nestLimit = indent;
  682. }
  683. void CommonJsonWriter::outputEndNested(const char *fieldname)
  684. {
  685. if (!fieldname)
  686. return;
  687. if (!*fieldname && !checkUnamedArrayItem(false))
  688. return;
  689. checkFormat(false, true, -1);
  690. fieldname = checkItemNameEndNested(fieldname);
  691. if (fieldname && *fieldname)
  692. {
  693. const char * sep = strchr(fieldname, '/');
  694. while (sep)
  695. {
  696. out.append('}');
  697. sep = strchr(sep+1, '/');
  698. }
  699. }
  700. out.append("}");
  701. if (indent==nestLimit)
  702. nestLimit = 0;
  703. }
  704. void CommonJsonWriter::outputSetAll()
  705. {
  706. checkDelimit();
  707. appendJSONValue(out, "All", true);
  708. }
  709. //=====================================================================================
  710. CPropertyTreeWriter::CPropertyTreeWriter(IPropertyTree *_root, unsigned _flags) : root(_root), flags(_flags)
  711. {
  712. target = root;
  713. }
  714. void CPropertyTreeWriter::setRoot(IPropertyTree &_root)
  715. {
  716. nestedLevels.clear();
  717. root.set(&_root);
  718. target = root;
  719. }
  720. void CPropertyTreeWriter::outputInlineXml(const char *text)
  721. {
  722. Owned<IPropertyTree> xmlTree = createPTreeFromXMLString(text);
  723. target->addPropTree(xmlTree->queryName(), xmlTree.getLink());
  724. }
  725. void CPropertyTreeWriter::outputQuoted(const char *text)
  726. {
  727. target->setProp(nullptr, text);
  728. }
  729. void CPropertyTreeWriter::outputString(unsigned len, const char *field, const char *fieldname)
  730. {
  731. if (flags & XWFtrim)
  732. len = rtlTrimStrLen(len, field);
  733. if ((flags & XWFopt) && (rtlTrimStrLen(len, field) == 0))
  734. return;
  735. StringBuffer tmp;
  736. appendStringAsUtf8(tmp, len, field);
  737. target->addProp(fieldname, tmp.str());
  738. }
  739. void CPropertyTreeWriter::outputQString(unsigned len, const char *field, const char *fieldname)
  740. {
  741. MemoryAttr tempBuffer;
  742. char * temp;
  743. if (len <= 100)
  744. temp = (char *)alloca(len);
  745. else
  746. temp = (char *)tempBuffer.allocate(len);
  747. rtlQStrToStr(len, temp, len, field);
  748. // outputString(len, temp, fieldname, isnumeric);
  749. outputString(len, temp, fieldname);
  750. }
  751. void CPropertyTreeWriter::outputBool(bool field, const char *fieldname)
  752. {
  753. target->addPropBool(fieldname, field);
  754. }
  755. void CPropertyTreeWriter::outputData(unsigned len, const void *field, const char *fieldname)
  756. {
  757. target->addPropBin(fieldname, len, field);
  758. }
  759. void CPropertyTreeWriter::outputInt(__int64 field, unsigned size, const char *fieldname)
  760. {
  761. target->addPropInt64(fieldname, field);
  762. }
  763. void CPropertyTreeWriter::outputUInt(unsigned __int64 field, unsigned size, const char *fieldname)
  764. {
  765. target->addPropInt64(fieldname, field);
  766. }
  767. void CPropertyTreeWriter::outputReal(double field, const char *fieldname)
  768. {
  769. StringBuffer fieldStr;
  770. target->setProp(fieldname, fieldStr.append(field).str());
  771. }
  772. void CPropertyTreeWriter::outputDecimal(const void *field, unsigned size, unsigned precision, const char *fieldname)
  773. {
  774. StringBuffer fieldStr;
  775. outputXmlDecimal(field, size, precision, nullptr, fieldStr);
  776. target->setProp(fieldname, fieldStr);
  777. }
  778. void CPropertyTreeWriter::outputUDecimal(const void *field, unsigned size, unsigned precision, const char *fieldname)
  779. {
  780. StringBuffer fieldStr;
  781. outputXmlUDecimal(field, size, precision, nullptr, fieldStr);
  782. target->setProp(fieldname, fieldStr);
  783. }
  784. void CPropertyTreeWriter::outputUnicode(unsigned len, const UChar *field, const char *fieldname)
  785. {
  786. if (flags & XWFtrim)
  787. len = rtlTrimUnicodeStrLen(len, field);
  788. if ((flags & XWFopt) && (rtlTrimUnicodeStrLen(len, field) == 0))
  789. return;
  790. StringBuffer fieldStr;
  791. char * buff = 0;
  792. unsigned bufflen = 0;
  793. rtlUnicodeToCodepageX(bufflen, buff, len, field, "utf-8");
  794. fieldStr.append(bufflen, buff);
  795. rtlFree(buff);
  796. target->setProp(fieldname, fieldStr);
  797. }
  798. void CPropertyTreeWriter::outputUtf8(unsigned len, const char *field, const char *fieldname)
  799. {
  800. if (flags & XWFtrim)
  801. len = rtlTrimUtf8StrLen(len, field);
  802. if ((flags & XWFopt) && (rtlTrimUtf8StrLen(len, field) == 0))
  803. return;
  804. StringBuffer fieldStr;
  805. fieldStr.append(rtlUtf8Size(len, field), field);
  806. target->setProp(fieldname, fieldStr); //MORE: addProp - discuss in code review!
  807. }
  808. void CPropertyTreeWriter::outputXmlns(const char *name, const char *uri)
  809. {
  810. StringBuffer fieldname;
  811. if (!streq(name, "xmlns"))
  812. fieldname.append("xmlns:");
  813. StringBuffer fieldStr;
  814. outputXmlUtf8(rtlUtf8Length(strlen(uri), uri), uri, nullptr, fieldStr);
  815. target->setProp(fieldname, fieldStr);
  816. }
  817. void CPropertyTreeWriter::outputBeginDataset(const char *dsname, bool nestChildren)
  818. {
  819. outputBeginNested("Dataset", nestChildren);
  820. if (!dsname || !*dsname)
  821. return;
  822. StringBuffer dsNameUtf8;
  823. outputXmlUtf8(rtlUtf8Length(strlen(dsname), dsname), dsname, nullptr, dsNameUtf8);
  824. target->addProp("@name", dsNameUtf8);
  825. }
  826. void CPropertyTreeWriter::outputEndDataset(const char *dsname)
  827. {
  828. outputEndNested("Dataset");
  829. }
  830. void CPropertyTreeWriter::outputBeginNested(const char *fieldname, bool nestChildren)
  831. {
  832. if (!fieldname || !*fieldname)
  833. return;
  834. const char * sep = strchr(fieldname, '/');
  835. if (sep)
  836. {
  837. StringAttr leading(fieldname, sep-fieldname);
  838. outputBeginNested(leading, nestChildren);
  839. outputBeginNested(sep+1, nestChildren);
  840. return;
  841. }
  842. nestedLevels.append(*target);
  843. target = target->addPropTree(fieldname);
  844. }
  845. void CPropertyTreeWriter::outputEndNested(const char *fieldname)
  846. {
  847. if (!fieldname || !*fieldname)
  848. return;
  849. const char * sep = strchr(fieldname, '/');
  850. if (sep)
  851. {
  852. StringAttr leading(fieldname, sep-fieldname);
  853. outputEndNested(sep+1);
  854. outputEndNested(leading);
  855. return;
  856. }
  857. target = &nestedLevels.popGet();
  858. }
  859. void CPropertyTreeWriter::outputSetAll()
  860. {
  861. // JCS->GH what's this for?
  862. target->addPropTree("All");
  863. }
  864. //=====================================================================================
  865. StringBuffer &buildJsonHeader(StringBuffer &header, const char *suppliedHeader, const char *rowTag)
  866. {
  867. if (suppliedHeader)
  868. {
  869. header.append(suppliedHeader);
  870. if (rowTag && *rowTag)
  871. appendJSONName(header, rowTag).append('[');
  872. return header;
  873. }
  874. if (rowTag && *rowTag)
  875. {
  876. header.append('{');
  877. appendJSONName(header, rowTag);
  878. }
  879. return header.append('[');
  880. }
  881. StringBuffer &buildJsonFooter(StringBuffer &footer, const char *suppliedFooter, const char *rowTag)
  882. {
  883. if (suppliedFooter)
  884. {
  885. if (rowTag && *rowTag)
  886. footer.append(']');
  887. footer.append(suppliedFooter);
  888. return footer;
  889. }
  890. return footer.append((rowTag && *rowTag) ? "]}" : "]");
  891. }
  892. static char thorHelperhexchar[] = "0123456789ABCDEF";
  893. //=====================================================================================
  894. static char csvQuote = '\"';
  895. CommonCSVWriter::CommonCSVWriter(unsigned _flags, CSVOptions& _options, IXmlStreamFlusher* _flusher)
  896. {
  897. flusher = _flusher;
  898. flags = _flags;
  899. options.terminator.set(_options.terminator.get());
  900. options.delimiter.set(_options.delimiter.get());
  901. options.includeHeader = _options.includeHeader; //output CSV headers
  902. recordCount = headerColumnID = 0;
  903. nestedHeaderLayerID = 0;
  904. readingCSVHeader = true;
  905. addingSimpleNestedContent = false; //Set by CommonCSVWriter::checkHeaderName()
  906. }
  907. CommonCSVWriter::~CommonCSVWriter()
  908. {
  909. flush(true);
  910. }
  911. void CommonCSVWriter::outputString(unsigned len, const char* field, const char* fieldName)
  912. {
  913. if (!checkHeaderName(fieldName))
  914. return;
  915. addStringField(len, field, fieldName);
  916. }
  917. void CommonCSVWriter::outputBool(bool field, const char* fieldName)
  918. {
  919. if (!checkHeaderName(fieldName))
  920. return;
  921. addContentField((field) ? "true" : "false", fieldName);
  922. }
  923. void CommonCSVWriter::outputData(unsigned len, const void* field, const char* fieldName)
  924. {
  925. if (!checkHeaderName(fieldName))
  926. return;
  927. StringBuffer v;
  928. const unsigned char *value = (const unsigned char *) field;
  929. for (unsigned int i = 0; i < len; i++)
  930. v.append(thorHelperhexchar[value[i] >> 4]).append(thorHelperhexchar[value[i] & 0x0f]);
  931. addContentField(v.str(), fieldName);
  932. }
  933. void CommonCSVWriter::outputInt(__int64 field, unsigned size, const char* fieldName)
  934. {
  935. if (!checkHeaderName(fieldName))
  936. return;
  937. StringBuffer v;
  938. v.append(field);
  939. addContentField(v.str(), fieldName);
  940. }
  941. void CommonCSVWriter::outputUInt(unsigned __int64 field, unsigned size, const char* fieldName)
  942. {
  943. if (!checkHeaderName(fieldName))
  944. return;
  945. StringBuffer v;
  946. v.append(field);
  947. addContentField(v.str(), fieldName);
  948. }
  949. void CommonCSVWriter::outputReal(double field, const char *fieldName)
  950. {
  951. if (!checkHeaderName(fieldName))
  952. return;
  953. StringBuffer v;
  954. v.append(field);
  955. addContentField(v.str(), fieldName);
  956. }
  957. void CommonCSVWriter::outputDecimal(const void* field, unsigned size, unsigned precision, const char* fieldName)
  958. {
  959. if (!checkHeaderName(fieldName))
  960. return;
  961. StringBuffer v;
  962. char dec[50];
  963. BcdCriticalBlock bcdBlock;
  964. if (DecValid(true, size*2-1, field))
  965. {
  966. DecPushDecimal(field, size, precision);
  967. DecPopCString(sizeof(dec), dec);
  968. const char *finger = dec;
  969. while(isspace(*finger)) finger++;
  970. v.append(finger);
  971. }
  972. addContentField(v.str(), fieldName);
  973. }
  974. void CommonCSVWriter::outputUDecimal(const void* field, unsigned size, unsigned precision, const char* fieldName)
  975. {
  976. if (!checkHeaderName(fieldName))
  977. return;
  978. StringBuffer v;
  979. char dec[50];
  980. BcdCriticalBlock bcdBlock;
  981. if (DecValid(false, size*2, field))
  982. {
  983. DecPushUDecimal(field, size, precision);
  984. DecPopCString(sizeof(dec), dec);
  985. const char *finger = dec;
  986. while(isspace(*finger)) finger++;
  987. v.append(finger);
  988. }
  989. addContentField(v.str(), fieldName);
  990. }
  991. void CommonCSVWriter::outputUnicode(unsigned len, const UChar* field, const char* fieldName)
  992. {
  993. if (!checkHeaderName(fieldName))
  994. return;
  995. StringBuffer v;
  996. char * buff = 0;
  997. unsigned bufflen = 0;
  998. rtlUnicodeToCodepageX(bufflen, buff, len, field, "utf-8");
  999. addStringField(bufflen, buff, fieldName);
  1000. rtlFree(buff);
  1001. }
  1002. void CommonCSVWriter::outputQString(unsigned len, const char* field, const char* fieldName)
  1003. {
  1004. if (!checkHeaderName(fieldName))
  1005. return;
  1006. MemoryAttr tempBuffer;
  1007. char * temp;
  1008. if (len <= 100)
  1009. temp = (char *)alloca(len);
  1010. else
  1011. temp = (char *)tempBuffer.allocate(len);
  1012. rtlQStrToStr(len, temp, len, field);
  1013. addStringField(len, temp, fieldName);
  1014. }
  1015. void CommonCSVWriter::outputUtf8(unsigned len, const char* field, const char* fieldName)
  1016. {
  1017. if (!checkHeaderName(fieldName))
  1018. return;
  1019. addStringField(rtlUtf8Size(len, field), field, fieldName);
  1020. }
  1021. void CommonCSVWriter::outputNumericString(const char* field, const char* fieldName)
  1022. {
  1023. if (!checkHeaderName(fieldName))
  1024. return;
  1025. addStringField((size32_t)strlen(field), field, fieldName);
  1026. }
  1027. void CommonCSVWriter::appendDataXPathItem(const char* fieldName, bool isArray)
  1028. {
  1029. Owned<CXPathItem> item = new CXPathItem(fieldName, isArray);
  1030. dataXPath.append(*item.getClear());
  1031. }
  1032. bool CommonCSVWriter::isDataRow(const char* fieldName)
  1033. {
  1034. if (dataXPath.empty())
  1035. return false;
  1036. CXPathItem& xPathItem = dataXPath.item(dataXPath.length() - 1);
  1037. return xPathItem.getIsArray() && strieq(fieldName, xPathItem.getPath());
  1038. }
  1039. void CommonCSVWriter::outputBeginNested(const char* fieldName, bool simpleNested, bool outputHeader)
  1040. {
  1041. //This method is called when retrieving csv headers.
  1042. if (!fieldName || !*fieldName || !readingCSVHeader)
  1043. return;
  1044. addCSVHeader(fieldName, NULL, true, simpleNested, outputHeader);
  1045. if (simpleNested) //ECL SET has only one column (parent name should be used as column name).
  1046. headerColumnID++;
  1047. //nestedHeaderLayerID is used as row ID when output CSV headers.
  1048. if (outputHeader)
  1049. nestedHeaderLayerID++;
  1050. addFieldToParentXPath(fieldName);
  1051. }
  1052. void CommonCSVWriter::outputEndNested(const char* fieldName, bool outputHeader)
  1053. {
  1054. //This method is called when retrieving csv headers.
  1055. if (!fieldName || !*fieldName || !readingCSVHeader)
  1056. return;
  1057. removeFieldFromCurrentParentXPath(fieldName);
  1058. if (outputHeader)
  1059. nestedHeaderLayerID--;
  1060. }
  1061. void CommonCSVWriter::outputBeginNested(const char* fieldName, bool simpleNested)
  1062. {
  1063. if (!fieldName || !*fieldName || readingCSVHeader)
  1064. return;
  1065. if (!isDataRow(fieldName))
  1066. {//A nested item begins.
  1067. //Call appendDataXPathItem() after the isDataRpw()
  1068. //because previous data xpath is used in isDataRpw().
  1069. appendDataXPathItem(fieldName, false);
  1070. addFieldToParentXPath(fieldName);
  1071. }
  1072. else
  1073. {//A new row begins inside a nested item.
  1074. appendDataXPathItem(fieldName, false);
  1075. if (!currentParentXPath.isEmpty())
  1076. {
  1077. //Add row xpath if it is not the 1st xpath.
  1078. addFieldToParentXPath(fieldName);
  1079. CCSVItem* item = getParentCSVItem();
  1080. if (!item)
  1081. return;
  1082. //Check row count for the ParentCSVItem.
  1083. //If this is not the first row, all children of the ParentCSVItem should
  1084. //start from the MaxNextRowID of the last row.
  1085. unsigned rowCount = item->getRowCount();
  1086. if (rowCount > 0)
  1087. {//Starting from the second result row, the NextRowIDs of every children are reset based on the last result row.
  1088. StringBuffer path(currentParentXPath);
  1089. path.setLength(path.length() - 1);
  1090. setChildrenNextRowID(path.str(), getChildrenMaxNextRowID(path.str()));
  1091. }
  1092. item->setCurrentRowEmpty(true);
  1093. }
  1094. }
  1095. }
  1096. void CommonCSVWriter::outputEndNested(const char* fieldName)
  1097. {
  1098. if (!fieldName || !*fieldName || readingCSVHeader)
  1099. return;
  1100. dataXPath.pop();
  1101. if (!isDataRow(fieldName))
  1102. {//This is an end of a nested item.
  1103. removeFieldFromCurrentParentXPath(fieldName);
  1104. }
  1105. else
  1106. {//A row ends inside the nested item
  1107. //Set row count for ParentCSVItem of this field.
  1108. if (!currentParentXPath.isEmpty())
  1109. {
  1110. CCSVItem* item = getParentCSVItem();
  1111. if (item && !item->getCurrentRowEmpty())
  1112. {
  1113. //Increase row count for this item
  1114. item->incrementRowCount();
  1115. item->setCurrentRowEmpty(true);
  1116. }
  1117. }
  1118. removeFieldFromCurrentParentXPath(fieldName);
  1119. //if dataXPath.length() back to 1, this should be the end of a content result row.
  1120. if (dataXPath.length() == 1)
  1121. finishContentResultRow();
  1122. }
  1123. }
  1124. void CommonCSVWriter::outputBeginArray(const char* fieldName)
  1125. {
  1126. appendDataXPathItem(fieldName, true);
  1127. };
  1128. void CommonCSVWriter::outputEndArray(const char* fieldName)
  1129. {
  1130. dataXPath.pop();
  1131. };
  1132. void CommonCSVWriter::outputBeginDataset(const char* dsname, bool nestChildren)
  1133. {
  1134. //This is called to add a <Dataset> tag outside of a wu result xml. No need for csv.
  1135. };
  1136. void CommonCSVWriter::outputEndDataset(const char* dsname)
  1137. {
  1138. };
  1139. IXmlWriterExt& CommonCSVWriter::clear()
  1140. {
  1141. recordCount = /*rowCount =*/ headerColumnID = 0;
  1142. nestedHeaderLayerID = 0;
  1143. readingCSVHeader = true;
  1144. addingSimpleNestedContent = false;
  1145. currentParentXPath.clear();
  1146. headerXPathList.kill();
  1147. topHeaderNameMap.kill();
  1148. contentRowsBuffer.clear();
  1149. csvItems.kill();
  1150. out.clear();
  1151. auditOut.clear();
  1152. return *this;
  1153. };
  1154. void CommonCSVWriter::outputCSVHeader(const char* name, const char* type)
  1155. {
  1156. if (!name || !*name)
  1157. return;
  1158. addCSVHeader(name, type, false, false, true);
  1159. headerColumnID++;
  1160. }
  1161. void CommonCSVWriter::finishCSVHeaders()
  1162. {
  1163. if (options.includeHeader)
  1164. outputHeadersToBuffer();
  1165. readingCSVHeader = false;
  1166. currentParentXPath.clear();
  1167. #ifdef _DEBUG
  1168. auditHeaderInfo();
  1169. #endif
  1170. }
  1171. void CommonCSVWriter::outputHeadersToBuffer()
  1172. {
  1173. CIArrayOf<CCSVRow> rows;
  1174. ForEachItemIn(i, headerXPathList)
  1175. {
  1176. const char* path = headerXPathList.item(i);
  1177. CCSVItem* item = csvItems.getValue(path);
  1178. if (!item || !item->checkOutputHeader())
  1179. continue;
  1180. unsigned colID = item->getColumnID();
  1181. if (item->checkIsNestedItem())
  1182. {
  1183. unsigned maxColumnID = colID;
  1184. getChildrenMaxColumnID(item, maxColumnID);
  1185. colID += (maxColumnID - colID)/2;
  1186. }
  1187. addColumnToRow(rows, item->getNestedLayer(), colID, item->getName(), NULL);
  1188. }
  1189. outputCSVRows(rows, true);
  1190. }
  1191. //Go through every children to find out MaxColumnID.
  1192. unsigned CommonCSVWriter::getChildrenMaxColumnID(CCSVItem* item, unsigned& maxColumnID)
  1193. {
  1194. StringBuffer path(item->getParentXPath());
  1195. path.append(item->getName());
  1196. StringArray& names = item->getChildrenNames();
  1197. ForEachItemIn(i, names)
  1198. {
  1199. StringBuffer childPath(path);
  1200. childPath.append("/").append(names.item(i));
  1201. CCSVItem* childItem = csvItems.getValue(childPath.str());
  1202. if (!childItem)
  1203. continue;
  1204. if (childItem->checkIsNestedItem())
  1205. maxColumnID = getChildrenMaxColumnID(childItem, maxColumnID);
  1206. else
  1207. {
  1208. unsigned columnID = childItem->getColumnID();
  1209. if (columnID > maxColumnID)
  1210. maxColumnID = columnID;
  1211. }
  1212. }
  1213. return maxColumnID;
  1214. }
  1215. void CommonCSVWriter::escapeQuoted(unsigned len, char const* in, StringBuffer& out)
  1216. {
  1217. char const* finger = in;
  1218. while (len--)
  1219. {
  1220. //RFC-4180, paragraph "If double-quotes are used to enclose fields, then a double-quote
  1221. //appearing inside a field must be escaped by preceding it with another double quote."
  1222. //unsigned newLen = 0;
  1223. if (*finger == '"')
  1224. out.append('"');
  1225. out.append(*finger);
  1226. finger++;
  1227. }
  1228. }
  1229. CCSVItem* CommonCSVWriter::getParentCSVItem()
  1230. {
  1231. if (currentParentXPath.isEmpty())
  1232. return NULL;
  1233. StringBuffer path(currentParentXPath);
  1234. path.setLength(path.length() - 1);
  1235. return csvItems.getValue(path.str());
  1236. }
  1237. CCSVItem* CommonCSVWriter::getCSVItemByFieldName(const char* name)
  1238. {
  1239. StringBuffer path;
  1240. if (currentParentXPath.isEmpty())
  1241. path.append(name);
  1242. else
  1243. path.append(currentParentXPath.str()).append(name);
  1244. return csvItems.getValue(path.str());
  1245. }
  1246. bool CommonCSVWriter::checkHeaderName(const char* name)
  1247. {
  1248. if (!name || !*name)
  1249. return false;
  1250. addingSimpleNestedContent = false; //Initialize to false.
  1251. if (currentParentXPath.isEmpty())
  1252. {
  1253. bool* found = topHeaderNameMap.getValue(name);
  1254. return (found && *found);
  1255. }
  1256. CCSVItem* item = getParentCSVItem();
  1257. if (!item)
  1258. return false;
  1259. addingSimpleNestedContent = item->checkSimpleNested();
  1260. if (addingSimpleNestedContent) //ECL: SET OF string, int, etc
  1261. return true;
  1262. return item->hasChildName(name);
  1263. }
  1264. void CommonCSVWriter::addColumnToRow(CIArrayOf<CCSVRow>& rows, unsigned rowID, unsigned colID, const char* columnValue, const char* columnName)
  1265. {
  1266. if (!columnValue)
  1267. columnValue = "";
  1268. if (rowID < rows.length())
  1269. { //add the column to existing row
  1270. CCSVRow& row = rows.item(rowID);
  1271. row.setColumn(colID, NULL, columnValue);
  1272. }
  1273. else
  1274. { //new row
  1275. Owned<CCSVRow> newRow = new CCSVRow(rowID);
  1276. newRow->setColumn(colID, NULL, columnValue);
  1277. rows.append(*newRow.getClear());
  1278. }
  1279. if (currentParentXPath.isEmpty())
  1280. return;
  1281. if (!addingSimpleNestedContent && columnName && *columnName)
  1282. {
  1283. CCSVItem* item = getCSVItemByFieldName(columnName);
  1284. if (item)
  1285. item->incrementNextRowID();
  1286. }
  1287. CCSVItem* parentItem = getParentCSVItem();
  1288. if (parentItem)
  1289. {
  1290. if (addingSimpleNestedContent) //ECL: SET OF string, int, etc. NextRowID should be stored in Parent item.
  1291. parentItem->incrementNextRowID();
  1292. setParentItemRowEmpty(parentItem, false);
  1293. }
  1294. }
  1295. void CommonCSVWriter::setParentItemRowEmpty(CCSVItem* item, bool empty)
  1296. {
  1297. item->setCurrentRowEmpty(empty);
  1298. StringBuffer parentXPath(item->getParentXPath());
  1299. if (parentXPath.isEmpty())
  1300. return;
  1301. //If this item is not empty, its parent is not empty.
  1302. parentXPath.setLength(parentXPath.length() - 1);
  1303. setParentItemRowEmpty(csvItems.getValue(parentXPath), empty);
  1304. }
  1305. void CommonCSVWriter::addCSVHeader(const char* name, const char* type, bool isNested, bool simpleNested, bool outputHeader)
  1306. {
  1307. if (checkHeaderName(name))
  1308. return;//Duplicated header. Should never happen.
  1309. Owned<CCSVItem> headerItem = new CCSVItem();
  1310. headerItem->setName(name);
  1311. headerItem->setIsNestedItem(isNested);
  1312. headerItem->setSimpleNested(simpleNested);
  1313. headerItem->setOutputHeader(outputHeader);
  1314. headerItem->setColumnID(headerColumnID);
  1315. headerItem->setNestedLayer(nestedHeaderLayerID);
  1316. headerItem->setParentXPath(currentParentXPath.str());
  1317. StringBuffer xPath(currentParentXPath);
  1318. xPath.append(name);
  1319. csvItems.setValue(xPath.str(), headerItem);
  1320. headerXPathList.append(xPath.str());
  1321. addChildNameToParentCSVItem(name);
  1322. if (currentParentXPath.isEmpty())
  1323. topHeaderNameMap.setValue(name, true);
  1324. }
  1325. void CommonCSVWriter::addContentField(const char* field, const char* fieldName)
  1326. {
  1327. CCSVItem* item = NULL;
  1328. if (addingSimpleNestedContent) //ECL: SET OF string, int, etc. ColumnID should be stored in Parent item.
  1329. item = getParentCSVItem();
  1330. else
  1331. item = getCSVItemByFieldName(fieldName);
  1332. addColumnToRow(contentRowsBuffer, item ? item->getNextRowID() : 0, item ? item->getColumnID() : 0, field, fieldName);
  1333. }
  1334. void CommonCSVWriter::addStringField(unsigned len, const char* field, const char* fieldName)
  1335. {
  1336. StringBuffer v;
  1337. v.append(csvQuote);
  1338. escapeQuoted(len, field, v);
  1339. v.append(csvQuote);
  1340. addContentField(v.str(), fieldName);
  1341. }
  1342. unsigned CommonCSVWriter::getChildrenMaxNextRowID(const char* path)
  1343. {
  1344. CCSVItem* item = csvItems.getValue(path);
  1345. if (!item)
  1346. return 0; //Should never happen
  1347. if (!item->checkIsNestedItem())
  1348. return item->getNextRowID();
  1349. unsigned maxRowID = item->getNextRowID();
  1350. StringBuffer basePath(path);
  1351. basePath.append("/");
  1352. StringArray& names = item->getChildrenNames();
  1353. ForEachItemIn(i, names)
  1354. {
  1355. StringBuffer childPath(basePath);
  1356. childPath.append(names.item(i));
  1357. unsigned rowID = getChildrenMaxNextRowID(childPath.str());
  1358. if (rowID > maxRowID)
  1359. maxRowID = rowID;
  1360. }
  1361. return maxRowID;
  1362. }
  1363. void CommonCSVWriter::setChildrenNextRowID(const char* path, unsigned rowID)
  1364. {
  1365. CCSVItem* item = csvItems.getValue(path);
  1366. if (!item)
  1367. return;
  1368. if (!item->checkIsNestedItem())
  1369. {
  1370. item->setNextRowID(rowID);
  1371. return;
  1372. }
  1373. StringArray& names = item->getChildrenNames();
  1374. ForEachItemIn(i, names)
  1375. {
  1376. StringBuffer childPath(path);
  1377. childPath.append("/").append(names.item(i));
  1378. CCSVItem* childItem = csvItems.getValue(childPath.str());
  1379. if (!childItem)
  1380. continue;
  1381. childItem->setNextRowID(rowID);//for possible new row
  1382. if (childItem->checkIsNestedItem())
  1383. {
  1384. childItem->setRowCount(0);
  1385. setChildrenNextRowID(childPath.str(), rowID);
  1386. }
  1387. }
  1388. }
  1389. void CommonCSVWriter::addChildNameToParentCSVItem(const char* name)
  1390. {
  1391. if (!name || !*name)
  1392. return;
  1393. if (currentParentXPath.isEmpty())
  1394. return;
  1395. CCSVItem* item = getParentCSVItem();
  1396. if (item)
  1397. item->addChildName(name);
  1398. }
  1399. void CommonCSVWriter::addFieldToParentXPath(const char* fieldName)
  1400. {
  1401. currentParentXPath.append(fieldName).append("/");
  1402. }
  1403. void CommonCSVWriter::removeFieldFromCurrentParentXPath(const char* fieldName)
  1404. {
  1405. unsigned len = strlen(fieldName);
  1406. if (currentParentXPath.length() > len+1)
  1407. currentParentXPath.setLength(currentParentXPath.length() - len - 1);
  1408. else
  1409. currentParentXPath.setLength(0);
  1410. }
  1411. void CommonCSVWriter::outputCSVRows(CIArrayOf<CCSVRow>& rows, bool isHeader)
  1412. {
  1413. bool firstRow = true;
  1414. ForEachItemIn(i, rows)
  1415. {
  1416. if (firstRow && !isHeader)
  1417. {
  1418. out.append(recordCount);
  1419. firstRow = false;
  1420. }
  1421. CCSVRow& row = rows.item(i);
  1422. unsigned len = row.getColumnCount();
  1423. for (unsigned col = 0; col < len; col++)
  1424. out.append(options.delimiter.get()).append(row.getColumnValue(col));
  1425. out.append(options.terminator.get());
  1426. }
  1427. }
  1428. void CommonCSVWriter::finishContentResultRow()
  1429. {
  1430. recordCount++;
  1431. outputCSVRows(contentRowsBuffer, false);
  1432. //Prepare for possible next record
  1433. currentParentXPath.setLength(0);
  1434. contentRowsBuffer.kill();
  1435. ForEachItemIn(i, headerXPathList)
  1436. {
  1437. const char* path = headerXPathList.item(i);
  1438. CCSVItem* item = csvItems.getValue(path);
  1439. if (item)
  1440. item->clearContentVariables();
  1441. }
  1442. };
  1443. void CCSVRow::setColumn(unsigned columnID, const char* columnName, const char* columnValue)
  1444. {
  1445. unsigned len = columns.length();
  1446. if (columnID < len)
  1447. {
  1448. CCSVItem& column = columns.item(columnID);
  1449. if (columnName && *columnName)
  1450. column.setName(columnName);
  1451. column.setValue(columnValue);
  1452. }
  1453. else
  1454. {
  1455. for (unsigned i = len; i <= columnID; i++)
  1456. {
  1457. Owned<CCSVItem> column = new CCSVItem();
  1458. if (i == columnID)
  1459. {
  1460. if (columnName && *columnName)
  1461. column->setName(columnName);
  1462. column->setValue(columnValue);
  1463. }
  1464. columns.append(*column.getClear());
  1465. }
  1466. }
  1467. }
  1468. const char* CCSVRow::getColumnValue(unsigned columnID) const
  1469. {
  1470. if (columnID >= columns.length())
  1471. return ""; //This should never happens.
  1472. CCSVItem& column = columns.item(columnID);
  1473. return column.getValue();
  1474. };
  1475. //=====================================================================================
  1476. inline void outputEncodedXmlString(unsigned len, const char *field, const char *fieldname, StringBuffer &out)
  1477. {
  1478. if (fieldname)
  1479. out.append('<').append(fieldname).append(" xsi:type=\"xsd:string\">");
  1480. encodeXML(field, out, 0, len);
  1481. if (fieldname)
  1482. out.append("</").append(fieldname).append('>');
  1483. }
  1484. inline void outputEncodedXmlBool(bool field, const char *fieldname, StringBuffer &out)
  1485. {
  1486. const char * text = field ? "true" : "false";
  1487. if (fieldname)
  1488. out.append('<').append(fieldname).append(" xsi:type=\"xsd:boolean\">").append(text).append("</").append(fieldname).append('>');
  1489. else
  1490. out.append(text);
  1491. }
  1492. inline void outputEncodedXmlData(unsigned len, const void *_field, const char *fieldname, StringBuffer &out)
  1493. {
  1494. const unsigned char *field = (const unsigned char *) _field;
  1495. if (fieldname)
  1496. out.append('<').append(fieldname).append(" xsi:type=\"xsd:hexBinary\">");
  1497. for (unsigned int i = 0; i < len; i++)
  1498. {
  1499. out.append(thorHelperhexchar[field[i] >> 4]).append(thorHelperhexchar[field[i] & 0x0f]);
  1500. }
  1501. if (fieldname)
  1502. out.append("</").append(fieldname).append('>');
  1503. }
  1504. inline void outputEncoded64XmlData(unsigned len, const void *_field, const char *fieldname, StringBuffer &out)
  1505. {
  1506. if (fieldname)
  1507. out.append('<').append(fieldname).append(" xsi:type=\"xsd:base64Binary\">");
  1508. JBASE64_Encode(_field, len, out, false);
  1509. if (fieldname)
  1510. out.append("</").append(fieldname).append('>');
  1511. }
  1512. inline void outputEncodedXmlInt(__int64 field, const char *fieldname, StringBuffer &out)
  1513. {
  1514. if (fieldname)
  1515. out.append('<').append(fieldname).append(" xsi:type=\"xsd:integer\">").append(field).append("</").append(fieldname).append('>');
  1516. else
  1517. out.append(field);
  1518. }
  1519. inline void outputEncodedXmlUInt(unsigned __int64 field, const char *fieldname, StringBuffer &out)
  1520. {
  1521. if (fieldname)
  1522. out.append('<').append(fieldname).append(" xsi:type=\"xsd:nonNegativeInteger\">").append(field).append("</").append(fieldname).append('>');
  1523. else
  1524. out.append(field);
  1525. }
  1526. inline void outputEncodedXmlReal(double field, const char *fieldname, StringBuffer &out)
  1527. {
  1528. if (fieldname)
  1529. out.append('<').append(fieldname).append(" xsi:type=\"xsd:double\">").append(field).append("</").append(fieldname).append('>');
  1530. else
  1531. out.append(field);
  1532. }
  1533. inline void outputEncodedXmlDecimal(const void *field, unsigned size, unsigned precision, const char *fieldname, StringBuffer &out)
  1534. {
  1535. char dec[50];
  1536. if (fieldname)
  1537. out.append('<').append(fieldname).append(" xsi:type=\"xsd:decimal\">");
  1538. BcdCriticalBlock bcdBlock;
  1539. if (DecValid(true, size*2-1, field))
  1540. {
  1541. DecPushDecimal(field, size, precision);
  1542. DecPopCString(sizeof(dec), dec);
  1543. const char *finger = dec;
  1544. while(isspace(*finger)) finger++;
  1545. out.append(finger);
  1546. }
  1547. else
  1548. out.append("####");
  1549. if (fieldname)
  1550. out.append("</").append(fieldname).append('>');
  1551. }
  1552. inline void outputEncodedXmlUDecimal(const void *field, unsigned size, unsigned precision, const char *fieldname, StringBuffer &out)
  1553. {
  1554. char dec[50];
  1555. if (fieldname)
  1556. out.append('<').append(fieldname).append(" xsi:type=\"xsd:decimal\">");
  1557. BcdCriticalBlock bcdBlock;
  1558. if (DecValid(false, size*2, field))
  1559. {
  1560. DecPushUDecimal(field, size, precision);
  1561. DecPopCString(sizeof(dec), dec);
  1562. const char *finger = dec;
  1563. while(isspace(*finger)) finger++;
  1564. out.append(finger);
  1565. }
  1566. else
  1567. out.append("####");
  1568. if (fieldname)
  1569. out.append("</").append(fieldname).append('>');
  1570. }
  1571. inline void outputEncodedXmlUnicode(unsigned len, const UChar *field, const char *fieldname, StringBuffer &out)
  1572. {
  1573. char * buff = 0;
  1574. unsigned bufflen = 0;
  1575. rtlUnicodeToCodepageX(bufflen, buff, len, field, "utf-8");
  1576. if (fieldname)
  1577. out.append('<').append(fieldname).append(" xsi:type=\"xsd:string\">");
  1578. encodeXML(buff, out, 0, bufflen, true); // output as UTF-8
  1579. if (fieldname)
  1580. out.append("</").append(fieldname).append('>');
  1581. rtlFree(buff);
  1582. }
  1583. inline void outputEncodedXmlUtf8(unsigned len, const char *field, const char *fieldname, StringBuffer &out)
  1584. {
  1585. if (fieldname)
  1586. out.append('<').append(fieldname).append(" xsi:type=\"xsd:string\">");
  1587. encodeXML(field, out, 0, rtlUtf8Size(len, field), true); // output as UTF-8
  1588. if (fieldname)
  1589. out.append("</").append(fieldname).append('>');
  1590. }
  1591. //=====================================================================================
  1592. CommonEncodedXmlWriter::CommonEncodedXmlWriter(unsigned _flags, unsigned initialIndent, IXmlStreamFlusher *_flusher)
  1593. : CommonXmlWriter(_flags, initialIndent, _flusher)
  1594. {
  1595. }
  1596. void CommonEncodedXmlWriter::outputString(unsigned len, const char *field, const char *fieldname)
  1597. {
  1598. if (flags & XWFtrim)
  1599. len = rtlTrimStrLen(len, field);
  1600. if ((flags & XWFopt) && (rtlTrimStrLen(len, field) == 0))
  1601. return;
  1602. if (checkForAttribute(fieldname))
  1603. outputXmlAttrString(len, field, fieldname+1, out);
  1604. else
  1605. {
  1606. if (!nestLimit)
  1607. out.pad(indent);
  1608. outputEncodedXmlString(len, field, fieldname, out);
  1609. if (!nestLimit)
  1610. out.newline();
  1611. }
  1612. }
  1613. void CommonEncodedXmlWriter::outputBool(bool field, const char *fieldname)
  1614. {
  1615. if (checkForAttribute(fieldname))
  1616. outputXmlAttrBool(field, fieldname+1, out);
  1617. else
  1618. {
  1619. if (!nestLimit)
  1620. out.pad(indent);
  1621. outputEncodedXmlBool(field, fieldname, out);
  1622. if (!nestLimit)
  1623. out.newline();
  1624. }
  1625. }
  1626. void CommonEncodedXmlWriter::outputData(unsigned len, const void *field, const char *fieldname)
  1627. {
  1628. if (checkForAttribute(fieldname))
  1629. outputXmlAttrData(len, field, fieldname+1, out);
  1630. else
  1631. {
  1632. if (!nestLimit)
  1633. out.pad(indent);
  1634. outputEncodedXmlData(len, field, fieldname, out);
  1635. if (!nestLimit)
  1636. out.newline();
  1637. }
  1638. }
  1639. void CommonEncodedXmlWriter::outputInt(__int64 field, unsigned size, const char *fieldname)
  1640. {
  1641. if (checkForAttribute(fieldname))
  1642. outputXmlAttrInt(field, fieldname+1, out);
  1643. else
  1644. {
  1645. if (!nestLimit)
  1646. out.pad(indent);
  1647. outputEncodedXmlInt(field, fieldname, out);
  1648. if (!nestLimit)
  1649. out.newline();
  1650. }
  1651. }
  1652. void CommonEncodedXmlWriter::outputUInt(unsigned __int64 field, unsigned size, const char *fieldname)
  1653. {
  1654. if (checkForAttribute(fieldname))
  1655. outputXmlAttrUInt(field, fieldname+1, out);
  1656. else
  1657. {
  1658. if (!nestLimit)
  1659. out.pad(indent);
  1660. outputEncodedXmlUInt(field, fieldname, out);
  1661. if (!nestLimit)
  1662. out.newline();
  1663. }
  1664. }
  1665. void CommonEncodedXmlWriter::outputReal(double field, const char *fieldname)
  1666. {
  1667. if (checkForAttribute(fieldname))
  1668. outputXmlAttrReal(field, fieldname+1, out);
  1669. else
  1670. {
  1671. if (!nestLimit)
  1672. out.pad(indent);
  1673. outputEncodedXmlReal(field, fieldname, out);
  1674. if (!nestLimit)
  1675. out.newline();
  1676. }
  1677. }
  1678. void CommonEncodedXmlWriter::outputDecimal(const void *field, unsigned size, unsigned precision, const char *fieldname)
  1679. {
  1680. if (checkForAttribute(fieldname))
  1681. outputXmlAttrDecimal(field, size, precision, fieldname+1, out);
  1682. else
  1683. {
  1684. if (!nestLimit)
  1685. out.pad(indent);
  1686. outputEncodedXmlDecimal(field, size, precision, fieldname, out);
  1687. if (!nestLimit)
  1688. out.newline();
  1689. }
  1690. }
  1691. void CommonEncodedXmlWriter::outputUDecimal(const void *field, unsigned size, unsigned precision, const char *fieldname)
  1692. {
  1693. if (checkForAttribute(fieldname))
  1694. outputXmlAttrUDecimal(field, size, precision, fieldname+1, out);
  1695. else
  1696. {
  1697. if (!nestLimit)
  1698. out.pad(indent);
  1699. outputEncodedXmlUDecimal(field, size, precision, fieldname, out);
  1700. if (!nestLimit)
  1701. out.newline();
  1702. }
  1703. }
  1704. void CommonEncodedXmlWriter::outputUnicode(unsigned len, const UChar *field, const char *fieldname)
  1705. {
  1706. if (flags & XWFtrim)
  1707. len = rtlTrimUnicodeStrLen(len, field);
  1708. if ((flags & XWFopt) && (rtlTrimUnicodeStrLen(len, field) == 0))
  1709. return;
  1710. if (checkForAttribute(fieldname))
  1711. outputXmlAttrUnicode(len, field, fieldname+1, out);
  1712. else
  1713. {
  1714. if (!nestLimit)
  1715. out.pad(indent);
  1716. outputEncodedXmlUnicode(len, field, fieldname, out);
  1717. if (!nestLimit)
  1718. out.newline();
  1719. }
  1720. }
  1721. void CommonEncodedXmlWriter::outputUtf8(unsigned len, const char *field, const char *fieldname)
  1722. {
  1723. if (flags & XWFtrim)
  1724. len = rtlTrimUtf8StrLen(len, field);
  1725. if ((flags & XWFopt) && (rtlTrimUtf8StrLen(len, field) == 0))
  1726. return;
  1727. if (checkForAttribute(fieldname))
  1728. outputXmlAttrUtf8(len, field, fieldname+1, out);
  1729. else
  1730. {
  1731. if (!nestLimit)
  1732. out.pad(indent);
  1733. outputEncodedXmlUtf8(len, field, fieldname, out);
  1734. if (!nestLimit)
  1735. out.newline();
  1736. }
  1737. }
  1738. //=====================================================================================
  1739. CommonEncoded64XmlWriter::CommonEncoded64XmlWriter(unsigned _flags, unsigned initialIndent, IXmlStreamFlusher *_flusher)
  1740. : CommonEncodedXmlWriter(_flags, initialIndent, _flusher)
  1741. {
  1742. }
  1743. void CommonEncoded64XmlWriter::outputData(unsigned len, const void *field, const char *fieldname)
  1744. {
  1745. if (checkForAttribute(fieldname))
  1746. outputXmlAttrData(len, field, fieldname+1, out);
  1747. else
  1748. {
  1749. if (!nestLimit)
  1750. out.pad(indent);
  1751. outputEncoded64XmlData(len, field, fieldname, out);
  1752. if (!nestLimit)
  1753. out.newline();
  1754. }
  1755. }
  1756. //=====================================================================================
  1757. CommonXmlWriter * CreateCommonXmlWriter(unsigned _flags, unsigned _initialIndent, IXmlStreamFlusher *_flusher, XMLWriterType xmlType)
  1758. {
  1759. switch (xmlType)
  1760. {
  1761. case WTStandard:
  1762. return new CommonXmlWriter(_flags, _initialIndent, _flusher);//standard XML writer
  1763. case WTEncodingData64:
  1764. return new CommonEncoded64XmlWriter(_flags, _initialIndent, _flusher);//writes xsd type attributes, and all data as base64binary
  1765. case WTEncoding:
  1766. return new CommonEncodedXmlWriter(_flags, _initialIndent, _flusher);//writes xsd type attributes, and all data as hexBinary
  1767. default:
  1768. assertex(false);
  1769. return NULL;
  1770. }
  1771. }
  1772. //=====================================================================================
  1773. IXmlWriterExt * createIXmlWriterExt(unsigned _flags, unsigned _initialIndent, IXmlStreamFlusher *_flusher, XMLWriterType xmlType)
  1774. {
  1775. if (xmlType==WTJSONRootless)
  1776. return new CommonJsonWriter(_flags, _initialIndent, _flusher);
  1777. if (xmlType==WTJSONObject)
  1778. return new CommonJsonObjectWriter(_flags, _initialIndent, _flusher);
  1779. return CreateCommonXmlWriter(_flags, _initialIndent, _flusher, xmlType);
  1780. }