thxmlwriteslave.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 "jio.hpp"
  14. #include "jtime.hpp"
  15. #include "jfile.ipp"
  16. #include "thbuf.hpp"
  17. #include "thexception.hpp"
  18. #include "thbufdef.hpp"
  19. #include "thmfilemanager.hpp"
  20. #include "slave.ipp"
  21. #include "thactivityutil.ipp"
  22. #include "thorxmlwrite.hpp"
  23. #include "thxmlwriteslave.ipp"
  24. class CXmlWriteSlaveActivity : public CDiskWriteSlaveActivityBase
  25. {
  26. IHThorXmlWriteArg *helper;
  27. ThorActivityKind kind;
  28. public:
  29. CXmlWriteSlaveActivity(CGraphElementBase *container, ThorActivityKind _kind) : CDiskWriteSlaveActivityBase(container), kind(_kind)
  30. {
  31. helper = static_cast <IHThorXmlWriteArg *> (queryHelper());
  32. }
  33. virtual void write()
  34. {
  35. StringBuffer rowTag;
  36. OwnedRoxieString xmlpath(helper->getXmlIteratorPath());
  37. if (!xmlpath)
  38. {
  39. rowTag.append(DEFAULTXMLROWTAG);
  40. }
  41. else
  42. {
  43. const char *path = xmlpath;
  44. if (*path == '/') path++;
  45. if (strchr(path, '/')) UNIMPLEMENTED;
  46. rowTag.append(path);
  47. }
  48. StringBuffer out;
  49. if (!dlfn.isExternal() || firstNode()) // if external, 1 header,footer
  50. {
  51. OwnedRoxieString suppliedHeader(helper->getHeader());
  52. if (kind==TAKjsonwrite)
  53. buildJsonHeader(out, suppliedHeader, rowTag);
  54. else if (suppliedHeader)
  55. out.set(suppliedHeader);
  56. else
  57. out.set(DEFAULTXMLHEADER).newline();
  58. outraw->write(out.length(), out.str());
  59. if (calcFileCrc)
  60. fileCRC.tally(out.length(), out.str());
  61. }
  62. Owned<IXmlWriterExt> writer = createIXmlWriterExt(helper->getXmlFlags(), 0, NULL, (kind==TAKjsonwrite) ? WTJSONRootless : WTStandard);
  63. writer->outputBeginArray(rowTag); //need this to format rows, even if not outputting it below
  64. while(!abortSoon)
  65. {
  66. OwnedConstThorRow row = inputStream->ungroupedNextRow();
  67. if (!row)
  68. break;
  69. writer->clear().outputBeginNested(rowTag, false);
  70. helper->toXML((const byte *)row.get(), *writer);
  71. writer->outputEndNested(rowTag);
  72. outraw->write(writer->length(), writer->str());
  73. if (calcFileCrc)
  74. fileCRC.tally(writer->length(), writer->str());
  75. processed++;
  76. }
  77. if (!dlfn.isExternal() || lastNode()) // if external, 1 header,footer
  78. {
  79. OwnedRoxieString suppliedFooter(helper->getFooter());
  80. if (kind==TAKjsonwrite)
  81. buildJsonFooter(out.clear().newline(), suppliedFooter, rowTag);
  82. else if (suppliedFooter)
  83. out.set(suppliedFooter);
  84. else
  85. out.set(DEFAULTXMLFOOTER).newline();
  86. outraw->write(out.length(), out.str());
  87. if (calcFileCrc)
  88. fileCRC.tally(out.length(), out.str());
  89. }
  90. }
  91. virtual bool wantRaw() { return true; }
  92. };
  93. CActivityBase *createXmlWriteSlave(CGraphElementBase *container, ThorActivityKind kind)
  94. {
  95. return new CXmlWriteSlaveActivity(container, kind);
  96. }