thorxmlwrite.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*##############################################################################
  2. HPCC SYSTEMS software Copyright (C) 2012 HPCC Systems®.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ############################################################################## */
  13. #include "platform.h"
  14. #include "jlib.hpp"
  15. #include "thorxmlwrite.hpp"
  16. #include "eclrtl.hpp"
  17. #include "rtlkey.hpp"
  18. #include "eclhelper.hpp"
  19. #include "deftype.hpp"
  20. #include "rtlformat.hpp"
  21. #include "rtlbcd.hpp"
  22. #include "eclrtl_imp.hpp"
  23. #include "nbcd.hpp"
  24. CommonFieldProcessor::CommonFieldProcessor(StringBuffer &_result, bool _trim) : result(_result), trim(_trim)
  25. {
  26. }
  27. void CommonFieldProcessor::processString(unsigned len, const char *value, const RtlFieldInfo * field)
  28. {
  29. if (trim)
  30. len = rtlTrimStrLen(len, value);
  31. result.append("'");
  32. outputXmlString(len, value, NULL, result);
  33. result.append("'");
  34. }
  35. void CommonFieldProcessor::processBool(bool value, const RtlFieldInfo * field)
  36. {
  37. outputXmlBool(value, NULL, result);
  38. }
  39. void CommonFieldProcessor::processData(unsigned len, const void *value, const RtlFieldInfo * field)
  40. {
  41. outputXmlData(len, value, NULL, result);
  42. }
  43. void CommonFieldProcessor::processInt(__int64 value, const RtlFieldInfo * field)
  44. {
  45. outputXmlInt(value, NULL, result);
  46. }
  47. void CommonFieldProcessor::processUInt(unsigned __int64 value, const RtlFieldInfo * field)
  48. {
  49. outputXmlUInt(value, NULL, result);
  50. }
  51. void CommonFieldProcessor::processReal(double value, const RtlFieldInfo * field)
  52. {
  53. outputXmlReal(value, NULL, result);
  54. }
  55. void CommonFieldProcessor::processDecimal(const void *value, unsigned digits, unsigned precision, const RtlFieldInfo * field)
  56. {
  57. outputXmlDecimal(value, digits, precision, NULL, result);
  58. }
  59. void CommonFieldProcessor::processUDecimal(const void *value, unsigned digits, unsigned precision, const RtlFieldInfo * field)
  60. {
  61. outputXmlUDecimal(value, digits, precision, NULL, result);
  62. }
  63. void CommonFieldProcessor::processUnicode(unsigned len, const UChar *value, const RtlFieldInfo * field)
  64. {
  65. if (trim)
  66. len = rtlTrimUnicodeStrLen(len, value);
  67. outputXmlUnicode(len, value, NULL, result);
  68. }
  69. void CommonFieldProcessor::processQString(unsigned len, const char *value, const RtlFieldInfo * field)
  70. {
  71. MemoryAttr tempBuffer;
  72. char * temp;
  73. if (len <= 100)
  74. temp = (char *)alloca(len);
  75. else
  76. temp = (char *)tempBuffer.allocate(len);
  77. rtlQStrToStr(len, temp, len, value);
  78. processString(len, temp, field);
  79. }
  80. void CommonFieldProcessor::processUtf8(unsigned len, const char *value, const RtlFieldInfo * field)
  81. {
  82. if (trim)
  83. len = rtlTrimUtf8StrLen(len, value);
  84. outputXmlUtf8(len, value, NULL, result);
  85. }
  86. bool CommonFieldProcessor::processBeginSet(const RtlFieldInfo * field, unsigned numElements, bool isAll, const byte *data)
  87. {
  88. result.append('[');
  89. if (isAll)
  90. result.append("ALL");
  91. return true;
  92. }
  93. bool CommonFieldProcessor::processBeginDataset(const RtlFieldInfo * field, unsigned numRows)
  94. {
  95. result.append('[');
  96. return true;
  97. }
  98. bool CommonFieldProcessor::processBeginRow(const RtlFieldInfo * field)
  99. {
  100. result.append('{');
  101. return true;
  102. }
  103. void CommonFieldProcessor::processEndSet(const RtlFieldInfo * field)
  104. {
  105. result.append(']');
  106. }
  107. void CommonFieldProcessor::processEndDataset(const RtlFieldInfo * field)
  108. {
  109. result.append(']');
  110. }
  111. void CommonFieldProcessor::processEndRow(const RtlFieldInfo * field)
  112. {
  113. result.append('}');
  114. }
  115. //=============================================================================================
  116. extern thorhelper_decl void convertRowToXML(size32_t & lenResult, char * & result, IOutputMetaData & info, const void * row, unsigned flags)
  117. {
  118. const byte * self = (const byte *)row;
  119. if (flags == (unsigned)-1)
  120. flags = XWFtrim|XWFopt|XWFnoindent;
  121. CommonXmlWriter writer(flags);
  122. info.toXML(self, writer);
  123. //could use detach...
  124. unsigned sizeResult;
  125. rtlStrToStrX(sizeResult, result, writer.length(), writer.str());
  126. lenResult = rtlUtf8Length(sizeResult, result);
  127. }
  128. extern thorhelper_decl void convertRowToJSON(size32_t & lenResult, char * & result, IOutputMetaData & info, const void * row, unsigned flags)
  129. {
  130. const byte * self = (const byte *)row;
  131. if (flags == (unsigned)-1)
  132. flags = XWFtrim|XWFopt|XWFnoindent;
  133. CommonJsonWriter writer(flags);
  134. info.toXML(self, writer);
  135. //could use detach...
  136. unsigned sizeResult;
  137. rtlStrToStrX(sizeResult, result, writer.length(), writer.str());
  138. lenResult = rtlUtf8Length(sizeResult, result);
  139. }