rtlformat.cpp 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036
  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. if (size < 7) //JavaScript only supports 53 significant bits
  551. {
  552. checkDelimit();
  553. appendJSONValue(out, checkItemName(fieldname), field);
  554. }
  555. else
  556. {
  557. appendJSONNameOrDelimit(out, checkItemName(fieldname));
  558. out.append('"').append(field).append('"');
  559. }
  560. }
  561. void CommonJsonWriter::outputUInt(unsigned __int64 field, unsigned size, const char *fieldname)
  562. {
  563. if (size < 7) //JavaScript doesn't support unsigned, and only supports 53 significant bits
  564. {
  565. checkDelimit();
  566. appendJSONValue(out, checkItemName(fieldname), field);
  567. }
  568. else
  569. {
  570. appendJSONNameOrDelimit(out, checkItemName(fieldname));
  571. out.append('"').append(field).append('"');
  572. }
  573. }
  574. void CommonJsonWriter::outputReal(double field, const char *fieldname)
  575. {
  576. checkDelimit();
  577. appendJSONValue(out, checkItemName(fieldname), field);
  578. }
  579. void CommonJsonWriter::outputDecimal(const void *field, unsigned size, unsigned precision, const char *fieldname)
  580. {
  581. checkDelimit();
  582. outputJsonDecimal(field, size, precision, checkItemName(fieldname), out);
  583. }
  584. void CommonJsonWriter::outputUDecimal(const void *field, unsigned size, unsigned precision, const char *fieldname)
  585. {
  586. checkDelimit();
  587. outputJsonUDecimal(field, size, precision, checkItemName(fieldname), out);
  588. }
  589. void CommonJsonWriter::outputUnicode(unsigned len, const UChar *field, const char *fieldname)
  590. {
  591. if (flags & XWFtrim)
  592. len = rtlTrimUnicodeStrLen(len, field);
  593. if ((flags & XWFopt) && (rtlTrimUnicodeStrLen(len, field) == 0))
  594. return;
  595. checkDelimit();
  596. outputJsonUnicode(len, field, checkItemName(fieldname), out);
  597. }
  598. void CommonJsonWriter::outputUtf8(unsigned len, const char *field, const char *fieldname)
  599. {
  600. if (flags & XWFtrim)
  601. len = rtlTrimUtf8StrLen(len, field);
  602. if ((flags & XWFopt) && (rtlTrimUtf8StrLen(len, field) == 0))
  603. return;
  604. checkDelimit();
  605. appendJSONStringValue(out, checkItemName(fieldname), rtlUtf8Size(len, field), field, true);
  606. }
  607. void CommonJsonWriter::prepareBeginArray(const char *fieldname)
  608. {
  609. arrays.append(*new CJsonWriterItem(fieldname));
  610. const char * sep = (fieldname) ? strchr(fieldname, '/') : NULL;
  611. while (sep)
  612. {
  613. StringAttr leading(fieldname, sep-fieldname);
  614. appendJSONName(out, leading).append(" {");
  615. fieldname = sep+1;
  616. sep = strchr(fieldname, '/');
  617. }
  618. checkFormat(false, false, 1);
  619. }
  620. void CommonJsonWriter::outputBeginArray(const char *fieldname)
  621. {
  622. prepareBeginArray(fieldname);
  623. appendJSONName(out, fieldname).append('[');
  624. }
  625. void CommonJsonWriter::outputEndArray(const char *fieldname)
  626. {
  627. arrays.pop();
  628. checkFormat(false, true, -1);
  629. out.append(']');
  630. const char * sep = (fieldname) ? strchr(fieldname, '/') : NULL;
  631. while (sep)
  632. {
  633. out.append('}');
  634. sep = strchr(sep+1, '/');
  635. }
  636. }
  637. void CommonJsonWriter::outputBeginDataset(const char *dsname, bool nestChildren)
  638. {
  639. if (dsname && *dsname)
  640. outputBeginNested(dsname, nestChildren);
  641. }
  642. void CommonJsonWriter::outputEndDataset(const char *dsname)
  643. {
  644. if (dsname && *dsname)
  645. outputEndNested(dsname);
  646. }
  647. void CommonJsonWriter::cutFrom(IInterface *location, StringBuffer& databuf)
  648. {
  649. if (flusher)
  650. throwUnexpected();
  651. CommonXmlPosition *position = dynamic_cast<CommonXmlPosition *>(location);
  652. if (!position)
  653. return;
  654. if (position->pos < out.length())
  655. {
  656. databuf.append(out.length() - position->pos, out.str() + position->pos);
  657. out.setLength(position->pos);
  658. needDelimiter = position->needDelimiter;
  659. indent = position->indent;
  660. nestLimit = position->nestLimit;
  661. }
  662. }
  663. void CommonJsonWriter::outputBeginNested(const char *fieldname, bool nestChildren)
  664. {
  665. if (!fieldname)
  666. return;
  667. if (!*fieldname && !checkUnamedArrayItem(true))
  668. return;
  669. checkFormat(true, false, 1);
  670. fieldname = checkItemNameBeginNested(fieldname);
  671. if (fieldname && *fieldname)
  672. {
  673. const char * sep = (fieldname) ? strchr(fieldname, '/') : NULL;
  674. while (sep)
  675. {
  676. StringAttr leading(fieldname, sep-fieldname);
  677. appendJSONName(out, leading).append("{");
  678. fieldname = sep+1;
  679. sep = strchr(fieldname, '/');
  680. }
  681. appendJSONName(out, fieldname);
  682. }
  683. out.append("{");
  684. if (!nestChildren && !nestLimit)
  685. nestLimit = indent;
  686. }
  687. void CommonJsonWriter::outputEndNested(const char *fieldname)
  688. {
  689. if (!fieldname)
  690. return;
  691. if (!*fieldname && !checkUnamedArrayItem(false))
  692. return;
  693. checkFormat(false, true, -1);
  694. fieldname = checkItemNameEndNested(fieldname);
  695. if (fieldname && *fieldname)
  696. {
  697. const char * sep = (fieldname) ? strchr(fieldname, '/') : NULL;
  698. while (sep)
  699. {
  700. out.append('}');
  701. sep = strchr(sep+1, '/');
  702. }
  703. }
  704. out.append("}");
  705. if (indent==nestLimit)
  706. nestLimit = 0;
  707. }
  708. void CommonJsonWriter::outputSetAll()
  709. {
  710. checkDelimit();
  711. appendJSONValue(out, "All", true);
  712. }
  713. //=====================================================================================
  714. CPropertyTreeWriter::CPropertyTreeWriter(IPropertyTree *_root, unsigned _flags) : root(_root), flags(_flags)
  715. {
  716. target = root;
  717. }
  718. void CPropertyTreeWriter::setRoot(IPropertyTree &_root)
  719. {
  720. nestedLevels.clear();
  721. root.set(&_root);
  722. target = root;
  723. }
  724. void CPropertyTreeWriter::outputInlineXml(const char *text)
  725. {
  726. Owned<IPropertyTree> xmlTree = createPTreeFromXMLString(text);
  727. target->addPropTree(xmlTree->queryName(), xmlTree.getLink());
  728. }
  729. void CPropertyTreeWriter::outputQuoted(const char *text)
  730. {
  731. target->setProp(nullptr, text);
  732. }
  733. void CPropertyTreeWriter::outputString(unsigned len, const char *field, const char *fieldname)
  734. {
  735. if (flags & XWFtrim)
  736. len = rtlTrimStrLen(len, field);
  737. if ((flags & XWFopt) && (rtlTrimStrLen(len, field) == 0))
  738. return;
  739. StringBuffer tmp(len, field);
  740. target->setProp(fieldname, tmp.str());
  741. }
  742. void CPropertyTreeWriter::outputQString(unsigned len, const char *field, const char *fieldname)
  743. {
  744. MemoryAttr tempBuffer;
  745. char * temp;
  746. if (len <= 100)
  747. temp = (char *)alloca(len);
  748. else
  749. temp = (char *)tempBuffer.allocate(len);
  750. rtlQStrToStr(len, temp, len, field);
  751. // outputString(len, temp, fieldname, isnumeric);
  752. outputString(len, temp, fieldname);
  753. }
  754. void CPropertyTreeWriter::outputBool(bool field, const char *fieldname)
  755. {
  756. target->addPropBool(fieldname, field);
  757. }
  758. void CPropertyTreeWriter::outputData(unsigned len, const void *field, const char *fieldname)
  759. {
  760. target->addPropBin(fieldname, len, field);
  761. }
  762. void CPropertyTreeWriter::outputInt(__int64 field, unsigned size, const char *fieldname)
  763. {
  764. target->addPropInt64(fieldname, field);
  765. }
  766. void CPropertyTreeWriter::outputUInt(unsigned __int64 field, unsigned size, const char *fieldname)
  767. {
  768. target->addPropInt64(fieldname, field);
  769. }
  770. void CPropertyTreeWriter::outputReal(double field, const char *fieldname)
  771. {
  772. StringBuffer fieldStr;
  773. target->setProp(fieldname, fieldStr.append(field).str());
  774. }
  775. void CPropertyTreeWriter::outputDecimal(const void *field, unsigned size, unsigned precision, const char *fieldname)
  776. {
  777. StringBuffer fieldStr;
  778. outputXmlDecimal(field, size, precision, nullptr, fieldStr);
  779. target->setProp(fieldname, fieldStr);
  780. }
  781. void CPropertyTreeWriter::outputUDecimal(const void *field, unsigned size, unsigned precision, const char *fieldname)
  782. {
  783. StringBuffer fieldStr;
  784. outputXmlUDecimal(field, size, precision, nullptr, fieldStr);
  785. target->setProp(fieldname, fieldStr);
  786. }
  787. void CPropertyTreeWriter::outputUnicode(unsigned len, const UChar *field, const char *fieldname)
  788. {
  789. if (flags & XWFtrim)
  790. len = rtlTrimUnicodeStrLen(len, field);
  791. if ((flags & XWFopt) && (rtlTrimUnicodeStrLen(len, field) == 0))
  792. return;
  793. StringBuffer fieldStr;
  794. outputXmlUnicode(len, field, fieldname, fieldStr);
  795. target->setProp(fieldname, fieldStr);
  796. }
  797. void CPropertyTreeWriter::outputUtf8(unsigned len, const char *field, const char *fieldname)
  798. {
  799. if (flags & XWFtrim)
  800. len = rtlTrimUtf8StrLen(len, field);
  801. if ((flags & XWFopt) && (rtlTrimUtf8StrLen(len, field) == 0))
  802. return;
  803. StringBuffer fieldStr;
  804. outputXmlUtf8(len, field, nullptr, fieldStr);
  805. target->setProp(fieldname, fieldStr);
  806. }
  807. void CPropertyTreeWriter::outputXmlns(const char *name, const char *uri)
  808. {
  809. StringBuffer fieldname;
  810. if (!streq(name, "xmlns"))
  811. fieldname.append("xmlns:");
  812. StringBuffer fieldStr;
  813. outputXmlUtf8(rtlUtf8Length(strlen(uri), uri), uri, nullptr, fieldStr);
  814. target->setProp(fieldname, fieldStr);
  815. }
  816. void CPropertyTreeWriter::outputBeginDataset(const char *dsname, bool nestChildren)
  817. {
  818. outputBeginNested("Dataset", nestChildren);
  819. if (!dsname || !*dsname)
  820. return;
  821. StringBuffer dsNameUtf8;
  822. outputXmlUtf8(rtlUtf8Length(strlen(dsname), dsname), dsname, nullptr, dsNameUtf8);
  823. target->addProp("@name", dsNameUtf8);
  824. }
  825. void CPropertyTreeWriter::outputEndDataset(const char *dsname)
  826. {
  827. outputEndNested("Dataset");
  828. }
  829. void CPropertyTreeWriter::outputBeginNested(const char *fieldname, bool nestChildren)
  830. {
  831. if (!fieldname || !*fieldname)
  832. return;
  833. const char * sep = strchr(fieldname, '/');
  834. if (sep)
  835. {
  836. StringAttr leading(fieldname, sep-fieldname);
  837. outputBeginNested(leading, nestChildren);
  838. outputBeginNested(sep+1, nestChildren);
  839. return;
  840. }
  841. nestedLevels.append(*target);
  842. target = target->addPropTree(fieldname);
  843. }
  844. void CPropertyTreeWriter::outputEndNested(const char *fieldname)
  845. {
  846. if (!fieldname || !*fieldname)
  847. return;
  848. const char * sep = strchr(fieldname, '/');
  849. if (sep)
  850. {
  851. StringAttr leading(fieldname, sep-fieldname);
  852. outputEndNested(sep+1);
  853. outputEndNested(leading);
  854. return;
  855. }
  856. target = &nestedLevels.popGet();
  857. }
  858. void CPropertyTreeWriter::outputSetAll()
  859. {
  860. // JCS->GH what's this for?
  861. target->addPropTree("All");
  862. }
  863. //=====================================================================================
  864. StringBuffer &buildJsonHeader(StringBuffer &header, const char *suppliedHeader, const char *rowTag)
  865. {
  866. if (suppliedHeader)
  867. {
  868. header.append(suppliedHeader);
  869. if (rowTag && *rowTag)
  870. appendJSONName(header, rowTag).append('[');
  871. return header;
  872. }
  873. if (rowTag && *rowTag)
  874. {
  875. header.append('{');
  876. appendJSONName(header, rowTag);
  877. }
  878. return header.append('[');
  879. }
  880. StringBuffer &buildJsonFooter(StringBuffer &footer, const char *suppliedFooter, const char *rowTag)
  881. {
  882. if (suppliedFooter)
  883. {
  884. if (rowTag && *rowTag)
  885. footer.append(']');
  886. footer.append(suppliedFooter);
  887. return footer;
  888. }
  889. return footer.append((rowTag && *rowTag) ? "]}" : "]");
  890. }
  891. static char thorHelperhexchar[] = "0123456789ABCDEF";
  892. //=====================================================================================
  893. static char csvQuote = '\"';
  894. CommonCSVWriter::CommonCSVWriter(unsigned _flags, CSVOptions& _options, IXmlStreamFlusher* _flusher)
  895. {
  896. flusher = _flusher;
  897. flags = _flags;
  898. options.terminator.set(_options.terminator.get());
  899. options.delimiter.set(_options.delimiter.get());
  900. options.includeHeader = _options.includeHeader; //output CSV headers
  901. recordCount = headerColumnID = 0;
  902. nestedHeaderLayerID = 0;
  903. readingCSVHeader = true;
  904. addingSimpleNestedContent = false; //Set by CommonCSVWriter::checkHeaderName()
  905. }
  906. CommonCSVWriter::~CommonCSVWriter()
  907. {
  908. flush(true);
  909. }
  910. void CommonCSVWriter::outputString(unsigned len, const char* field, const char* fieldName)
  911. {
  912. if (!checkHeaderName(fieldName))
  913. return;
  914. addStringField(len, field, fieldName);
  915. }
  916. void CommonCSVWriter::outputBool(bool field, const char* fieldName)
  917. {
  918. if (!checkHeaderName(fieldName))
  919. return;
  920. addContentField((field) ? "true" : "false", fieldName);
  921. }
  922. void CommonCSVWriter::outputData(unsigned len, const void* field, const char* fieldName)
  923. {
  924. if (!checkHeaderName(fieldName))
  925. return;
  926. StringBuffer v;
  927. const unsigned char *value = (const unsigned char *) field;
  928. for (unsigned int i = 0; i < len; i++)
  929. v.append(thorHelperhexchar[value[i] >> 4]).append(thorHelperhexchar[value[i] & 0x0f]);
  930. addContentField(v.str(), fieldName);
  931. }
  932. void CommonCSVWriter::outputInt(__int64 field, unsigned size, const char* fieldName)
  933. {
  934. if (!checkHeaderName(fieldName))
  935. return;
  936. StringBuffer v;
  937. v.append(field);
  938. addContentField(v.str(), fieldName);
  939. }
  940. void CommonCSVWriter::outputUInt(unsigned __int64 field, unsigned size, const char* fieldName)
  941. {
  942. if (!checkHeaderName(fieldName))
  943. return;
  944. StringBuffer v;
  945. v.append(field);
  946. addContentField(v.str(), fieldName);
  947. }
  948. void CommonCSVWriter::outputReal(double field, const char *fieldName)
  949. {
  950. if (!checkHeaderName(fieldName))
  951. return;
  952. StringBuffer v;
  953. v.append(field);
  954. addContentField(v.str(), fieldName);
  955. }
  956. void CommonCSVWriter::outputDecimal(const void* field, unsigned size, unsigned precision, const char* fieldName)
  957. {
  958. if (!checkHeaderName(fieldName))
  959. return;
  960. StringBuffer v;
  961. char dec[50];
  962. BcdCriticalBlock bcdBlock;
  963. if (DecValid(true, size*2-1, field))
  964. {
  965. DecPushDecimal(field, size, precision);
  966. DecPopCString(sizeof(dec), dec);
  967. const char *finger = dec;
  968. while(isspace(*finger)) finger++;
  969. v.append(finger);
  970. }
  971. addContentField(v.str(), fieldName);
  972. }
  973. void CommonCSVWriter::outputUDecimal(const void* field, unsigned size, unsigned precision, const char* fieldName)
  974. {
  975. if (!checkHeaderName(fieldName))
  976. return;
  977. StringBuffer v;
  978. char dec[50];
  979. BcdCriticalBlock bcdBlock;
  980. if (DecValid(false, size*2, field))
  981. {
  982. DecPushUDecimal(field, size, precision);
  983. DecPopCString(sizeof(dec), dec);
  984. const char *finger = dec;
  985. while(isspace(*finger)) finger++;
  986. v.append(finger);
  987. }
  988. addContentField(v.str(), fieldName);
  989. }
  990. void CommonCSVWriter::outputUnicode(unsigned len, const UChar* field, const char* fieldName)
  991. {
  992. if (!checkHeaderName(fieldName))
  993. return;
  994. StringBuffer v;
  995. char * buff = 0;
  996. unsigned bufflen = 0;
  997. rtlUnicodeToCodepageX(bufflen, buff, len, field, "utf-8");
  998. addStringField(bufflen, buff, fieldName);
  999. rtlFree(buff);
  1000. }
  1001. void CommonCSVWriter::outputQString(unsigned len, const char* field, const char* fieldName)
  1002. {
  1003. if (!checkHeaderName(fieldName))
  1004. return;
  1005. MemoryAttr tempBuffer;
  1006. char * temp;
  1007. if (len <= 100)
  1008. temp = (char *)alloca(len);
  1009. else
  1010. temp = (char *)tempBuffer.allocate(len);
  1011. rtlQStrToStr(len, temp, len, field);
  1012. addStringField(len, temp, fieldName);
  1013. }
  1014. void CommonCSVWriter::outputUtf8(unsigned len, const char* field, const char* fieldName)
  1015. {
  1016. if (!checkHeaderName(fieldName))
  1017. return;
  1018. addStringField(rtlUtf8Size(len, field), field, fieldName);
  1019. }
  1020. void CommonCSVWriter::outputNumericString(const char* field, const char* fieldName)
  1021. {
  1022. if (!checkHeaderName(fieldName))
  1023. return;
  1024. addStringField((size32_t)strlen(field), field, fieldName);
  1025. }
  1026. void CommonCSVWriter::appendDataXPathItem(const char* fieldName, bool isArray)
  1027. {
  1028. Owned<CXPathItem> item = new CXPathItem(fieldName, isArray);
  1029. dataXPath.append(*item.getClear());
  1030. }
  1031. bool CommonCSVWriter::isDataRow(const char* fieldName)
  1032. {
  1033. if (dataXPath.empty())
  1034. return false;
  1035. CXPathItem& xPathItem = dataXPath.item(dataXPath.length() - 1);
  1036. return xPathItem.getIsArray() && strieq(fieldName, xPathItem.getPath());
  1037. }
  1038. void CommonCSVWriter::outputBeginNested(const char* fieldName, bool simpleNested, bool outputHeader)
  1039. {
  1040. //This method is called when retrieving csv headers.
  1041. if (!fieldName || !*fieldName || !readingCSVHeader)
  1042. return;
  1043. addCSVHeader(fieldName, NULL, true, simpleNested, outputHeader);
  1044. if (simpleNested) //ECL SET has only one column (parent name should be used as column name).
  1045. headerColumnID++;
  1046. //nestedHeaderLayerID is used as row ID when output CSV headers.
  1047. if (outputHeader)
  1048. nestedHeaderLayerID++;
  1049. addFieldToParentXPath(fieldName);
  1050. }
  1051. void CommonCSVWriter::outputEndNested(const char* fieldName, bool outputHeader)
  1052. {
  1053. //This method is called when retrieving csv headers.
  1054. if (!fieldName || !*fieldName || !readingCSVHeader)
  1055. return;
  1056. removeFieldFromCurrentParentXPath(fieldName);
  1057. if (outputHeader)
  1058. nestedHeaderLayerID--;
  1059. }
  1060. void CommonCSVWriter::outputBeginNested(const char* fieldName, bool simpleNested)
  1061. {
  1062. if (!fieldName || !*fieldName || readingCSVHeader)
  1063. return;
  1064. if (!isDataRow(fieldName))
  1065. {//A nested item begins.
  1066. //Call appendDataXPathItem() after the isDataRpw()
  1067. //because previous data xpath is used in isDataRpw().
  1068. appendDataXPathItem(fieldName, false);
  1069. addFieldToParentXPath(fieldName);
  1070. }
  1071. else
  1072. {//A new row begins inside a nested item.
  1073. appendDataXPathItem(fieldName, false);
  1074. if (!currentParentXPath.isEmpty())
  1075. {
  1076. //Add row xpath if it is not the 1st xpath.
  1077. addFieldToParentXPath(fieldName);
  1078. CCSVItem* item = getParentCSVItem();
  1079. if (!item)
  1080. return;
  1081. //Check row count for the ParentCSVItem.
  1082. //If this is not the first row, all children of the ParentCSVItem should
  1083. //start from the MaxNextRowID of the last row.
  1084. unsigned rowCount = item->getRowCount();
  1085. if (rowCount > 0)
  1086. {//Starting from the second result row, the NextRowIDs of every children are reset based on the last result row.
  1087. StringBuffer path = currentParentXPath;
  1088. path.setLength(path.length() - 1);
  1089. setChildrenNextRowID(path.str(), getChildrenMaxNextRowID(path.str()));
  1090. }
  1091. item->setCurrentRowEmpty(true);
  1092. }
  1093. }
  1094. }
  1095. void CommonCSVWriter::outputEndNested(const char* fieldName)
  1096. {
  1097. if (!fieldName || !*fieldName || readingCSVHeader)
  1098. return;
  1099. dataXPath.pop();
  1100. if (!isDataRow(fieldName))
  1101. {//This is an end of a nested item.
  1102. removeFieldFromCurrentParentXPath(fieldName);
  1103. }
  1104. else
  1105. {//A row ends inside the nested item
  1106. //Set row count for ParentCSVItem of this field.
  1107. if (!currentParentXPath.isEmpty())
  1108. {
  1109. CCSVItem* item = getParentCSVItem();
  1110. if (item && !item->getCurrentRowEmpty())
  1111. {
  1112. //Increase row count for this item
  1113. item->incrementRowCount();
  1114. item->setCurrentRowEmpty(true);
  1115. }
  1116. }
  1117. removeFieldFromCurrentParentXPath(fieldName);
  1118. //if dataXPath.length() back to 1, this should be the end of a content result row.
  1119. if (dataXPath.length() == 1)
  1120. finishContentResultRow();
  1121. }
  1122. }
  1123. void CommonCSVWriter::outputBeginArray(const char* fieldName)
  1124. {
  1125. appendDataXPathItem(fieldName, true);
  1126. };
  1127. void CommonCSVWriter::outputEndArray(const char* fieldName)
  1128. {
  1129. dataXPath.pop();
  1130. };
  1131. void CommonCSVWriter::outputBeginDataset(const char* dsname, bool nestChildren)
  1132. {
  1133. //This is called to add a <Dataset> tag outside of a wu result xml. No need for csv.
  1134. };
  1135. void CommonCSVWriter::outputEndDataset(const char* dsname)
  1136. {
  1137. };
  1138. IXmlWriterExt& CommonCSVWriter::clear()
  1139. {
  1140. recordCount = /*rowCount =*/ headerColumnID = 0;
  1141. nestedHeaderLayerID = 0;
  1142. readingCSVHeader = true;
  1143. addingSimpleNestedContent = false;
  1144. currentParentXPath.clear();
  1145. headerXPathList.kill();
  1146. topHeaderNameMap.kill();
  1147. contentRowsBuffer.clear();
  1148. csvItems.kill();
  1149. out.clear();
  1150. auditOut.clear();
  1151. return *this;
  1152. };
  1153. void CommonCSVWriter::outputCSVHeader(const char* name, const char* type)
  1154. {
  1155. if (!name || !*name)
  1156. return;
  1157. addCSVHeader(name, type, false, false, true);
  1158. headerColumnID++;
  1159. }
  1160. void CommonCSVWriter::finishCSVHeaders()
  1161. {
  1162. if (options.includeHeader)
  1163. outputHeadersToBuffer();
  1164. readingCSVHeader = false;
  1165. currentParentXPath.clear();
  1166. #ifdef _DEBUG
  1167. auditHeaderInfo();
  1168. #endif
  1169. }
  1170. void CommonCSVWriter::outputHeadersToBuffer()
  1171. {
  1172. CIArrayOf<CCSVRow> rows;
  1173. ForEachItemIn(i, headerXPathList)
  1174. {
  1175. const char* path = headerXPathList.item(i);
  1176. CCSVItem* item = csvItems.getValue(path);
  1177. if (!item || !item->checkOutputHeader())
  1178. continue;
  1179. unsigned colID = item->getColumnID();
  1180. if (item->checkIsNestedItem())
  1181. {
  1182. unsigned maxColumnID = colID;
  1183. getChildrenMaxColumnID(item, maxColumnID);
  1184. colID += (maxColumnID - colID)/2;
  1185. }
  1186. addColumnToRow(rows, item->getNestedLayer(), colID, item->getName(), NULL);
  1187. }
  1188. outputCSVRows(rows, true);
  1189. }
  1190. //Go through every children to find out MaxColumnID.
  1191. unsigned CommonCSVWriter::getChildrenMaxColumnID(CCSVItem* item, unsigned& maxColumnID)
  1192. {
  1193. StringBuffer path = item->getParentXPath();
  1194. path.append(item->getName());
  1195. StringArray& names = item->getChildrenNames();
  1196. ForEachItemIn(i, names)
  1197. {
  1198. StringBuffer childPath = path;
  1199. childPath.append("/").append(names.item(i));
  1200. CCSVItem* childItem = csvItems.getValue(childPath.str());
  1201. if (!childItem)
  1202. continue;
  1203. if (childItem->checkIsNestedItem())
  1204. maxColumnID = getChildrenMaxColumnID(childItem, maxColumnID);
  1205. else
  1206. {
  1207. unsigned columnID = childItem->getColumnID();
  1208. if (columnID > maxColumnID)
  1209. maxColumnID = columnID;
  1210. }
  1211. }
  1212. return maxColumnID;
  1213. }
  1214. void CommonCSVWriter::escapeQuoted(unsigned len, char const* in, StringBuffer& out)
  1215. {
  1216. char const* finger = in;
  1217. while (len--)
  1218. {
  1219. //RFC-4180, paragraph "If double-quotes are used to enclose fields, then a double-quote
  1220. //appearing inside a field must be escaped by preceding it with another double quote."
  1221. //unsigned newLen = 0;
  1222. if (*finger == '"')
  1223. out.append('"');
  1224. out.append(*finger);
  1225. finger++;
  1226. }
  1227. }
  1228. CCSVItem* CommonCSVWriter::getParentCSVItem()
  1229. {
  1230. if (currentParentXPath.isEmpty())
  1231. return NULL;
  1232. StringBuffer path = currentParentXPath;
  1233. path.setLength(path.length() - 1);
  1234. return csvItems.getValue(path.str());
  1235. }
  1236. CCSVItem* CommonCSVWriter::getCSVItemByFieldName(const char* name)
  1237. {
  1238. StringBuffer path;
  1239. if (currentParentXPath.isEmpty())
  1240. path.append(name);
  1241. else
  1242. path.append(currentParentXPath.str()).append(name);
  1243. return csvItems.getValue(path.str());
  1244. }
  1245. bool CommonCSVWriter::checkHeaderName(const char* name)
  1246. {
  1247. if (!name || !*name)
  1248. return false;
  1249. if (currentParentXPath.isEmpty())
  1250. {
  1251. bool* found = topHeaderNameMap.getValue(name);
  1252. return (found && *found);
  1253. }
  1254. CCSVItem* item = getParentCSVItem();
  1255. if (!item)
  1256. return false;
  1257. addingSimpleNestedContent = item->checkSimpleNested();
  1258. if (addingSimpleNestedContent) //ECL: SET OF string, int, etc
  1259. return true;
  1260. return item->hasChildName(name);
  1261. }
  1262. void CommonCSVWriter::addColumnToRow(CIArrayOf<CCSVRow>& rows, unsigned rowID, unsigned colID, const char* columnValue, const char* columnName)
  1263. {
  1264. if (!columnValue)
  1265. columnValue = "";
  1266. if (rowID < rows.length())
  1267. { //add the column to existing row
  1268. CCSVRow& row = rows.item(rowID);
  1269. row.setColumn(colID, NULL, columnValue);
  1270. }
  1271. else
  1272. { //new row
  1273. Owned<CCSVRow> newRow = new CCSVRow(rowID);
  1274. newRow->setColumn(colID, NULL, columnValue);
  1275. rows.append(*newRow.getClear());
  1276. }
  1277. if (currentParentXPath.isEmpty())
  1278. return;
  1279. if (!addingSimpleNestedContent && columnName && *columnName)
  1280. {
  1281. CCSVItem* item = getCSVItemByFieldName(columnName);
  1282. if (item)
  1283. item->incrementNextRowID();
  1284. }
  1285. CCSVItem* parentItem = getParentCSVItem();
  1286. if (parentItem)
  1287. {
  1288. if (addingSimpleNestedContent) //ECL: SET OF string, int, etc. NextRowID should be stored in Parent item.
  1289. parentItem->incrementNextRowID();
  1290. setParentItemRowEmpty(parentItem, false);
  1291. }
  1292. }
  1293. void CommonCSVWriter::setParentItemRowEmpty(CCSVItem* item, bool empty)
  1294. {
  1295. item->setCurrentRowEmpty(empty);
  1296. StringBuffer parentXPath = item->getParentXPath();
  1297. if (parentXPath.isEmpty())
  1298. return;
  1299. //If this item is not empty, its parent is not empty.
  1300. parentXPath.setLength(parentXPath.length() - 1);
  1301. setParentItemRowEmpty(csvItems.getValue(parentXPath), empty);
  1302. }
  1303. void CommonCSVWriter::addCSVHeader(const char* name, const char* type, bool isNested, bool simpleNested, bool outputHeader)
  1304. {
  1305. if (checkHeaderName(name))
  1306. return;//Duplicated header. Should never happen.
  1307. Owned<CCSVItem> headerItem = new CCSVItem();
  1308. headerItem->setName(name);
  1309. headerItem->setIsNestedItem(isNested);
  1310. headerItem->setSimpleNested(simpleNested);
  1311. headerItem->setOutputHeader(outputHeader);
  1312. headerItem->setColumnID(headerColumnID);
  1313. headerItem->setNestedLayer(nestedHeaderLayerID);
  1314. headerItem->setParentXPath(currentParentXPath.str());
  1315. StringBuffer xPath = currentParentXPath;
  1316. xPath.append(name);
  1317. csvItems.setValue(xPath.str(), headerItem);
  1318. headerXPathList.append(xPath.str());
  1319. addChildNameToParentCSVItem(name);
  1320. if (currentParentXPath.isEmpty())
  1321. topHeaderNameMap.setValue(name, true);
  1322. }
  1323. void CommonCSVWriter::addContentField(const char* field, const char* fieldName)
  1324. {
  1325. CCSVItem* item = NULL;
  1326. if (addingSimpleNestedContent) //ECL: SET OF string, int, etc. ColumnID should be stored in Parent item.
  1327. item = getParentCSVItem();
  1328. else
  1329. item = getCSVItemByFieldName(fieldName);
  1330. addColumnToRow(contentRowsBuffer, item ? item->getNextRowID() : 0, item ? item->getColumnID() : 0, field, fieldName);
  1331. }
  1332. void CommonCSVWriter::addStringField(unsigned len, const char* field, const char* fieldName)
  1333. {
  1334. StringBuffer v;
  1335. v.append(csvQuote);
  1336. escapeQuoted(len, field, v);
  1337. v.append(csvQuote);
  1338. addContentField(v.str(), fieldName);
  1339. }
  1340. unsigned CommonCSVWriter::getChildrenMaxNextRowID(const char* path)
  1341. {
  1342. CCSVItem* item = csvItems.getValue(path);
  1343. if (!item)
  1344. return 0; //Should never happen
  1345. if (!item->checkIsNestedItem())
  1346. return item->getNextRowID();
  1347. unsigned maxRowID = item->getNextRowID();
  1348. StringBuffer basePath = path;
  1349. basePath.append("/");
  1350. StringArray& names = item->getChildrenNames();
  1351. ForEachItemIn(i, names)
  1352. {
  1353. StringBuffer childPath = basePath;
  1354. childPath.append(names.item(i));
  1355. unsigned rowID = getChildrenMaxNextRowID(childPath.str());
  1356. if (rowID > maxRowID)
  1357. maxRowID = rowID;
  1358. }
  1359. return maxRowID;
  1360. }
  1361. void CommonCSVWriter::setChildrenNextRowID(const char* path, unsigned rowID)
  1362. {
  1363. CCSVItem* item = csvItems.getValue(path);
  1364. if (!item)
  1365. return;
  1366. if (!item->checkIsNestedItem())
  1367. {
  1368. item->setNextRowID(rowID);
  1369. return;
  1370. }
  1371. StringArray& names = item->getChildrenNames();
  1372. ForEachItemIn(i, names)
  1373. {
  1374. StringBuffer childPath = path;
  1375. childPath.append("/").append(names.item(i));
  1376. CCSVItem* childItem = csvItems.getValue(childPath.str());
  1377. if (!childItem)
  1378. continue;
  1379. childItem->setNextRowID(rowID);//for possible new row
  1380. if (childItem->checkIsNestedItem())
  1381. {
  1382. childItem->setRowCount(0);
  1383. setChildrenNextRowID(childPath.str(), rowID);
  1384. }
  1385. }
  1386. }
  1387. void CommonCSVWriter::addChildNameToParentCSVItem(const char* name)
  1388. {
  1389. if (!name || !*name)
  1390. return;
  1391. if (currentParentXPath.isEmpty())
  1392. return;
  1393. CCSVItem* item = getParentCSVItem();
  1394. if (item)
  1395. item->addChildName(name);
  1396. }
  1397. void CommonCSVWriter::addFieldToParentXPath(const char* fieldName)
  1398. {
  1399. currentParentXPath.append(fieldName).append("/");
  1400. }
  1401. void CommonCSVWriter::removeFieldFromCurrentParentXPath(const char* fieldName)
  1402. {
  1403. unsigned len = strlen(fieldName);
  1404. if (currentParentXPath.length() > len+1)
  1405. currentParentXPath.setLength(currentParentXPath.length() - len - 1);
  1406. else
  1407. currentParentXPath.setLength(0);
  1408. }
  1409. void CommonCSVWriter::outputCSVRows(CIArrayOf<CCSVRow>& rows, bool isHeader)
  1410. {
  1411. bool firstRow = true;
  1412. ForEachItemIn(i, rows)
  1413. {
  1414. if (firstRow && !isHeader)
  1415. {
  1416. out.append(recordCount);
  1417. firstRow = false;
  1418. }
  1419. CCSVRow& row = rows.item(i);
  1420. unsigned len = row.getColumnCount();
  1421. for (unsigned col = 0; col < len; col++)
  1422. out.append(options.delimiter.get()).append(row.getColumnValue(col));
  1423. out.append(options.terminator.get());
  1424. }
  1425. }
  1426. void CommonCSVWriter::finishContentResultRow()
  1427. {
  1428. recordCount++;
  1429. outputCSVRows(contentRowsBuffer, false);
  1430. //Prepare for possible next record
  1431. currentParentXPath.setLength(0);
  1432. contentRowsBuffer.kill();
  1433. ForEachItemIn(i, headerXPathList)
  1434. {
  1435. const char* path = headerXPathList.item(i);
  1436. CCSVItem* item = csvItems.getValue(path);
  1437. if (item)
  1438. item->clearContentVariables();
  1439. }
  1440. };
  1441. void CCSVRow::setColumn(unsigned columnID, const char* columnName, const char* columnValue)
  1442. {
  1443. unsigned len = columns.length();
  1444. if (columnID < len)
  1445. {
  1446. CCSVItem& column = columns.item(columnID);
  1447. if (columnName && *columnName)
  1448. column.setName(columnName);
  1449. column.setValue(columnValue);
  1450. }
  1451. else
  1452. {
  1453. for (unsigned i = len; i <= columnID; i++)
  1454. {
  1455. Owned<CCSVItem> column = new CCSVItem();
  1456. if (i == columnID)
  1457. {
  1458. if (columnName && *columnName)
  1459. column->setName(columnName);
  1460. column->setValue(columnValue);
  1461. }
  1462. columns.append(*column.getClear());
  1463. }
  1464. }
  1465. }
  1466. const char* CCSVRow::getColumnValue(unsigned columnID) const
  1467. {
  1468. if (columnID >= columns.length())
  1469. return ""; //This should never happens.
  1470. CCSVItem& column = columns.item(columnID);
  1471. return column.getValue();
  1472. };
  1473. //=====================================================================================
  1474. inline void outputEncodedXmlString(unsigned len, const char *field, const char *fieldname, StringBuffer &out)
  1475. {
  1476. if (fieldname)
  1477. out.append('<').append(fieldname).append(" xsi:type=\"xsd:string\">");
  1478. encodeXML(field, out, 0, len);
  1479. if (fieldname)
  1480. out.append("</").append(fieldname).append('>');
  1481. }
  1482. inline void outputEncodedXmlBool(bool field, const char *fieldname, StringBuffer &out)
  1483. {
  1484. const char * text = field ? "true" : "false";
  1485. if (fieldname)
  1486. out.append('<').append(fieldname).append(" xsi:type=\"xsd:boolean\">").append(text).append("</").append(fieldname).append('>');
  1487. else
  1488. out.append(text);
  1489. }
  1490. inline void outputEncodedXmlData(unsigned len, const void *_field, const char *fieldname, StringBuffer &out)
  1491. {
  1492. const unsigned char *field = (const unsigned char *) _field;
  1493. if (fieldname)
  1494. out.append('<').append(fieldname).append(" xsi:type=\"xsd:hexBinary\">");
  1495. for (unsigned int i = 0; i < len; i++)
  1496. {
  1497. out.append(thorHelperhexchar[field[i] >> 4]).append(thorHelperhexchar[field[i] & 0x0f]);
  1498. }
  1499. if (fieldname)
  1500. out.append("</").append(fieldname).append('>');
  1501. }
  1502. inline void outputEncoded64XmlData(unsigned len, const void *_field, const char *fieldname, StringBuffer &out)
  1503. {
  1504. if (fieldname)
  1505. out.append('<').append(fieldname).append(" xsi:type=\"xsd:base64Binary\">");
  1506. JBASE64_Encode(_field, len, out, false);
  1507. if (fieldname)
  1508. out.append("</").append(fieldname).append('>');
  1509. }
  1510. inline void outputEncodedXmlInt(__int64 field, const char *fieldname, StringBuffer &out)
  1511. {
  1512. if (fieldname)
  1513. out.append('<').append(fieldname).append(" xsi:type=\"xsd:integer\">").append(field).append("</").append(fieldname).append('>');
  1514. else
  1515. out.append(field);
  1516. }
  1517. inline void outputEncodedXmlUInt(unsigned __int64 field, const char *fieldname, StringBuffer &out)
  1518. {
  1519. if (fieldname)
  1520. out.append('<').append(fieldname).append(" xsi:type=\"xsd:nonNegativeInteger\">").append(field).append("</").append(fieldname).append('>');
  1521. else
  1522. out.append(field);
  1523. }
  1524. inline void outputEncodedXmlReal(double field, const char *fieldname, StringBuffer &out)
  1525. {
  1526. if (fieldname)
  1527. out.append('<').append(fieldname).append(" xsi:type=\"xsd:double\">").append(field).append("</").append(fieldname).append('>');
  1528. else
  1529. out.append(field);
  1530. }
  1531. inline void outputEncodedXmlDecimal(const void *field, unsigned size, unsigned precision, const char *fieldname, StringBuffer &out)
  1532. {
  1533. char dec[50];
  1534. if (fieldname)
  1535. out.append('<').append(fieldname).append(" xsi:type=\"xsd:decimal\">");
  1536. BcdCriticalBlock bcdBlock;
  1537. if (DecValid(true, size*2-1, field))
  1538. {
  1539. DecPushDecimal(field, size, precision);
  1540. DecPopCString(sizeof(dec), dec);
  1541. const char *finger = dec;
  1542. while(isspace(*finger)) finger++;
  1543. out.append(finger);
  1544. }
  1545. else
  1546. out.append("####");
  1547. if (fieldname)
  1548. out.append("</").append(fieldname).append('>');
  1549. }
  1550. inline void outputEncodedXmlUDecimal(const void *field, unsigned size, unsigned precision, const char *fieldname, StringBuffer &out)
  1551. {
  1552. char dec[50];
  1553. if (fieldname)
  1554. out.append('<').append(fieldname).append(" xsi:type=\"xsd:decimal\">");
  1555. BcdCriticalBlock bcdBlock;
  1556. if (DecValid(false, size*2, field))
  1557. {
  1558. DecPushUDecimal(field, size, precision);
  1559. DecPopCString(sizeof(dec), dec);
  1560. const char *finger = dec;
  1561. while(isspace(*finger)) finger++;
  1562. out.append(finger);
  1563. }
  1564. else
  1565. out.append("####");
  1566. if (fieldname)
  1567. out.append("</").append(fieldname).append('>');
  1568. }
  1569. inline void outputEncodedXmlUnicode(unsigned len, const UChar *field, const char *fieldname, StringBuffer &out)
  1570. {
  1571. char * buff = 0;
  1572. unsigned bufflen = 0;
  1573. rtlUnicodeToCodepageX(bufflen, buff, len, field, "utf-8");
  1574. if (fieldname)
  1575. out.append('<').append(fieldname).append(" xsi:type=\"xsd:string\">");
  1576. encodeXML(buff, out, 0, bufflen, true); // output as UTF-8
  1577. if (fieldname)
  1578. out.append("</").append(fieldname).append('>');
  1579. rtlFree(buff);
  1580. }
  1581. inline void outputEncodedXmlUtf8(unsigned len, const char *field, const char *fieldname, StringBuffer &out)
  1582. {
  1583. if (fieldname)
  1584. out.append('<').append(fieldname).append(" xsi:type=\"xsd:string\">");
  1585. encodeXML(field, out, 0, rtlUtf8Size(len, field), true); // output as UTF-8
  1586. if (fieldname)
  1587. out.append("</").append(fieldname).append('>');
  1588. }
  1589. //=====================================================================================
  1590. CommonEncodedXmlWriter::CommonEncodedXmlWriter(unsigned _flags, unsigned initialIndent, IXmlStreamFlusher *_flusher)
  1591. : CommonXmlWriter(_flags, initialIndent, _flusher)
  1592. {
  1593. }
  1594. void CommonEncodedXmlWriter::outputString(unsigned len, const char *field, const char *fieldname)
  1595. {
  1596. if (flags & XWFtrim)
  1597. len = rtlTrimStrLen(len, field);
  1598. if ((flags & XWFopt) && (rtlTrimStrLen(len, field) == 0))
  1599. return;
  1600. if (checkForAttribute(fieldname))
  1601. outputXmlAttrString(len, field, fieldname+1, out);
  1602. else
  1603. {
  1604. if (!nestLimit)
  1605. out.pad(indent);
  1606. outputEncodedXmlString(len, field, fieldname, out);
  1607. if (!nestLimit)
  1608. out.newline();
  1609. }
  1610. }
  1611. void CommonEncodedXmlWriter::outputBool(bool field, const char *fieldname)
  1612. {
  1613. if (checkForAttribute(fieldname))
  1614. outputXmlAttrBool(field, fieldname+1, out);
  1615. else
  1616. {
  1617. if (!nestLimit)
  1618. out.pad(indent);
  1619. outputEncodedXmlBool(field, fieldname, out);
  1620. if (!nestLimit)
  1621. out.newline();
  1622. }
  1623. }
  1624. void CommonEncodedXmlWriter::outputData(unsigned len, const void *field, const char *fieldname)
  1625. {
  1626. if (checkForAttribute(fieldname))
  1627. outputXmlAttrData(len, field, fieldname+1, out);
  1628. else
  1629. {
  1630. if (!nestLimit)
  1631. out.pad(indent);
  1632. outputEncodedXmlData(len, field, fieldname, out);
  1633. if (!nestLimit)
  1634. out.newline();
  1635. }
  1636. }
  1637. void CommonEncodedXmlWriter::outputInt(__int64 field, unsigned size, const char *fieldname)
  1638. {
  1639. if (checkForAttribute(fieldname))
  1640. outputXmlAttrInt(field, fieldname+1, out);
  1641. else
  1642. {
  1643. if (!nestLimit)
  1644. out.pad(indent);
  1645. outputEncodedXmlInt(field, fieldname, out);
  1646. if (!nestLimit)
  1647. out.newline();
  1648. }
  1649. }
  1650. void CommonEncodedXmlWriter::outputUInt(unsigned __int64 field, unsigned size, const char *fieldname)
  1651. {
  1652. if (checkForAttribute(fieldname))
  1653. outputXmlAttrUInt(field, fieldname+1, out);
  1654. else
  1655. {
  1656. if (!nestLimit)
  1657. out.pad(indent);
  1658. outputEncodedXmlUInt(field, fieldname, out);
  1659. if (!nestLimit)
  1660. out.newline();
  1661. }
  1662. }
  1663. void CommonEncodedXmlWriter::outputReal(double field, const char *fieldname)
  1664. {
  1665. if (checkForAttribute(fieldname))
  1666. outputXmlAttrReal(field, fieldname+1, out);
  1667. else
  1668. {
  1669. if (!nestLimit)
  1670. out.pad(indent);
  1671. outputEncodedXmlReal(field, fieldname, out);
  1672. if (!nestLimit)
  1673. out.newline();
  1674. }
  1675. }
  1676. void CommonEncodedXmlWriter::outputDecimal(const void *field, unsigned size, unsigned precision, const char *fieldname)
  1677. {
  1678. if (checkForAttribute(fieldname))
  1679. outputXmlAttrDecimal(field, size, precision, fieldname+1, out);
  1680. else
  1681. {
  1682. if (!nestLimit)
  1683. out.pad(indent);
  1684. outputEncodedXmlDecimal(field, size, precision, fieldname, out);
  1685. if (!nestLimit)
  1686. out.newline();
  1687. }
  1688. }
  1689. void CommonEncodedXmlWriter::outputUDecimal(const void *field, unsigned size, unsigned precision, const char *fieldname)
  1690. {
  1691. if (checkForAttribute(fieldname))
  1692. outputXmlAttrUDecimal(field, size, precision, fieldname+1, out);
  1693. else
  1694. {
  1695. if (!nestLimit)
  1696. out.pad(indent);
  1697. outputEncodedXmlUDecimal(field, size, precision, fieldname, out);
  1698. if (!nestLimit)
  1699. out.newline();
  1700. }
  1701. }
  1702. void CommonEncodedXmlWriter::outputUnicode(unsigned len, const UChar *field, const char *fieldname)
  1703. {
  1704. if (flags & XWFtrim)
  1705. len = rtlTrimUnicodeStrLen(len, field);
  1706. if ((flags & XWFopt) && (rtlTrimUnicodeStrLen(len, field) == 0))
  1707. return;
  1708. if (checkForAttribute(fieldname))
  1709. outputXmlAttrUnicode(len, field, fieldname+1, out);
  1710. else
  1711. {
  1712. if (!nestLimit)
  1713. out.pad(indent);
  1714. outputEncodedXmlUnicode(len, field, fieldname, out);
  1715. if (!nestLimit)
  1716. out.newline();
  1717. }
  1718. }
  1719. void CommonEncodedXmlWriter::outputUtf8(unsigned len, const char *field, const char *fieldname)
  1720. {
  1721. if (flags & XWFtrim)
  1722. len = rtlTrimUtf8StrLen(len, field);
  1723. if ((flags & XWFopt) && (rtlTrimUtf8StrLen(len, field) == 0))
  1724. return;
  1725. if (checkForAttribute(fieldname))
  1726. outputXmlAttrUtf8(len, field, fieldname+1, out);
  1727. else
  1728. {
  1729. if (!nestLimit)
  1730. out.pad(indent);
  1731. outputEncodedXmlUtf8(len, field, fieldname, out);
  1732. if (!nestLimit)
  1733. out.newline();
  1734. }
  1735. }
  1736. //=====================================================================================
  1737. CommonEncoded64XmlWriter::CommonEncoded64XmlWriter(unsigned _flags, unsigned initialIndent, IXmlStreamFlusher *_flusher)
  1738. : CommonEncodedXmlWriter(_flags, initialIndent, _flusher)
  1739. {
  1740. }
  1741. void CommonEncoded64XmlWriter::outputData(unsigned len, const void *field, const char *fieldname)
  1742. {
  1743. if (checkForAttribute(fieldname))
  1744. outputXmlAttrData(len, field, fieldname+1, out);
  1745. else
  1746. {
  1747. if (!nestLimit)
  1748. out.pad(indent);
  1749. outputEncoded64XmlData(len, field, fieldname, out);
  1750. if (!nestLimit)
  1751. out.newline();
  1752. }
  1753. }
  1754. //=====================================================================================
  1755. CommonXmlWriter * CreateCommonXmlWriter(unsigned _flags, unsigned _initialIndent, IXmlStreamFlusher *_flusher, XMLWriterType xmlType)
  1756. {
  1757. switch (xmlType)
  1758. {
  1759. case WTStandard:
  1760. return new CommonXmlWriter(_flags, _initialIndent, _flusher);//standard XML writer
  1761. case WTEncodingData64:
  1762. return new CommonEncoded64XmlWriter(_flags, _initialIndent, _flusher);//writes xsd type attributes, and all data as base64binary
  1763. case WTEncoding:
  1764. return new CommonEncodedXmlWriter(_flags, _initialIndent, _flusher);//writes xsd type attributes, and all data as hexBinary
  1765. default:
  1766. assertex(false);
  1767. return NULL;
  1768. }
  1769. }
  1770. //=====================================================================================
  1771. IXmlWriterExt * createIXmlWriterExt(unsigned _flags, unsigned _initialIndent, IXmlStreamFlusher *_flusher, XMLWriterType xmlType)
  1772. {
  1773. if (xmlType==WTJSON)
  1774. return new CommonJsonWriter(_flags, _initialIndent, _flusher);
  1775. return CreateCommonXmlWriter(_flags, _initialIndent, _flusher, xmlType);
  1776. }