jptree.hpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. #ifndef _PTREE_HPP
  14. #define _PTREE_HPP
  15. #include "jlib.hpp"
  16. #include "jexcept.hpp"
  17. #include "jiter.hpp"
  18. enum TextMarkupFormat
  19. {
  20. MarkupFmt_Unknown=0,
  21. MarkupFmt_XML,
  22. MarkupFmt_JSON
  23. };
  24. enum PTreeExceptionCodes
  25. {
  26. PTreeExcpt_XPath_Ambiguity,
  27. PTreeExcpt_XPath_ParseError,
  28. PTreeExcpt_XPath_Unsupported,
  29. PTreeExcpt_InvalidTagName,
  30. PTreeExcpt_Unsupported,
  31. PTreeExcpt_InternalError,
  32. };
  33. interface jlib_thrown_decl IPTreeException : extends IException { };
  34. interface IAttributeIterator : extends IInterface
  35. {
  36. virtual bool first()=0;
  37. virtual bool next()=0;
  38. virtual bool isValid()=0;
  39. virtual unsigned count()=0;
  40. virtual const char *queryName() const = 0;
  41. virtual const char *queryValue() const = 0;
  42. virtual StringBuffer &getValue(StringBuffer &out) = 0;
  43. };
  44. interface IPropertyTree;
  45. interface IPropertyTreeIterator : extends IIteratorOf<IPropertyTree> { };
  46. typedef unsigned IPTIteratorCodes;
  47. #define iptiter_null 0x00
  48. #define iptiter_sort 0x01
  49. #define iptiter_remote 0x02
  50. #define iptiter_remoteget 0x06
  51. #define iptiter_remotegetbranch 0x0e
  52. interface jlib_decl IPropertyTree : extends serializable
  53. {
  54. virtual bool hasProp(const char *xpath) const = 0;
  55. virtual bool isBinary(const char *xpath=NULL) const = 0;
  56. virtual bool isCompressed(const char *xpath=NULL) const = 0; // needed for external/internal efficiency e.g. for clone
  57. virtual bool renameProp(const char *xpath, const char *newName) = 0;
  58. virtual bool renameTree(IPropertyTree *tree, const char *newName) = 0;
  59. // string - all types can be converted to string
  60. virtual bool getProp(const char *xpath, StringBuffer &ret) const = 0;
  61. virtual const char *queryProp(const char * xpath) const = 0;
  62. virtual void setProp(const char *xpath, const char *val) = 0;
  63. virtual void addProp(const char *xpath, const char *val) = 0;
  64. virtual void appendProp(const char *xpath, const char *val) = 0;
  65. virtual bool getPropBool(const char *xpath, bool dft=false) const = 0;
  66. virtual void setPropBool(const char *xpath, bool val) = 0;
  67. virtual void addPropBool(const char *xpath, bool val) = 0;
  68. virtual int getPropInt(const char *xpath, int dft=0) const = 0;
  69. virtual void setPropInt(const char *xpath, int val) = 0;
  70. virtual void addPropInt(const char *xpath, int val) = 0;
  71. virtual __int64 getPropInt64(const char *xpath, __int64 dft=0) const = 0;
  72. virtual void setPropInt64(const char *xpath, __int64 val) = 0;
  73. virtual void addPropInt64(const char *xpath, __int64 val) = 0;
  74. virtual bool getPropBin(const char *xpath, MemoryBuffer &ret) const = 0;
  75. virtual void setPropBin(const char *xpath, size32_t size, const void *data) = 0;
  76. virtual void addPropBin(const char *xpath, size32_t size, const void *data) = 0;
  77. virtual void appendPropBin(const char *xpath, size32_t size, const void *data) = 0;
  78. virtual IPropertyTree *getPropTree(const char *xpath) const = 0;
  79. virtual IPropertyTree *queryPropTree(const char *xpath) const = 0;
  80. virtual IPropertyTree *setPropTree(const char *xpath, IPropertyTree *val) = 0;
  81. virtual IPropertyTree *addPropTree(const char *xpath, IPropertyTree *val) = 0;
  82. virtual bool removeProp(const char *xpath) = 0;
  83. virtual bool removeTree(IPropertyTree *child) = 0;
  84. virtual aindex_t queryChildIndex(IPropertyTree *child) = 0;
  85. virtual StringBuffer &getName(StringBuffer &) const = 0;
  86. virtual const char *queryName() const = 0;
  87. virtual IPropertyTreeIterator *getElements(const char *xpath, IPTIteratorCodes flags = iptiter_null) const = 0;
  88. virtual IAttributeIterator *getAttributes(bool sorted=false) const = 0;
  89. virtual IPropertyTree *getBranch(const char *xpath) const = 0;
  90. virtual IPropertyTree *queryBranch(const char *xpath) const = 0;
  91. virtual bool hasChildren() const = 0;
  92. virtual unsigned numUniq() = 0;
  93. virtual unsigned numChildren() = 0;
  94. virtual bool isCaseInsensitive() = 0;
  95. virtual bool IsShared() const = 0;
  96. virtual void localizeElements(const char *xpath, bool allTail=false) = 0;
  97. virtual unsigned getCount(const char *xpath) = 0;
  98. private:
  99. void setProp(const char *, int); // dummy to catch accidental use of setProp when setPropInt() intended
  100. void addProp(const char *, int); // likewise
  101. };
  102. jlib_decl bool validateXMLTag(const char *name);
  103. interface IPTreeNotifyEvent : extends IInterface
  104. {
  105. virtual void beginNode(const char *tag, offset_t startOffset) = 0;
  106. virtual void newAttribute(const char *name, const char *value) = 0;
  107. virtual void beginNodeContent(const char *tag) = 0; // attributes parsed
  108. virtual void endNode(const char *tag, unsigned length, const void *value, bool binary, offset_t endOffset) = 0;
  109. };
  110. enum PTreeReadExcptCode { PTreeRead_undefined, PTreeRead_EOS, PTreeRead_syntax };
  111. interface jlib_thrown_decl IPTreeReadException : extends IException
  112. {
  113. virtual const char *queryDescription() = 0;
  114. virtual unsigned queryLine() = 0;
  115. virtual offset_t queryOffset() = 0;
  116. virtual const char *queryContext() = 0;
  117. };
  118. extern jlib_decl IPTreeReadException *createPTreeReadException(int code, const char *msg, const char *context, unsigned line, offset_t offset);
  119. enum PTreeReaderOptions { ptr_none=0x00, ptr_ignoreWhiteSpace=0x01, ptr_noRoot=0x02, ptr_ignoreNameSpaces=0x04 };
  120. interface IPTreeReader : extends IInterface
  121. {
  122. virtual void load() = 0;
  123. virtual offset_t queryOffset() = 0;
  124. };
  125. interface IPullPTreeReader : extends IPTreeReader
  126. {
  127. virtual bool next() = 0;
  128. virtual void reset() = 0;
  129. };
  130. interface IPTreeMaker : extends IPTreeNotifyEvent
  131. {
  132. virtual IPropertyTree *queryRoot() = 0;
  133. virtual IPropertyTree *queryCurrentNode() = 0;
  134. virtual void reset() = 0;
  135. virtual IPropertyTree *create(const char *tag) = 0;
  136. };
  137. interface IPTreeNodeCreator : extends IInterface
  138. {
  139. virtual IPropertyTree *create(const char *tag) = 0;
  140. };
  141. // NB ipt_ext4 - used by SDS
  142. // NB ipt_ext5 - used by SDS
  143. enum ipt_flags { ipt_none=0x00, ipt_caseInsensitive=0x01, ipt_binary=0x02, ipt_ordered=0x04, ipt_ext1=0x08, ipt_ext2=16, ipt_ext3=32, ipt_ext4=64, ipt_ext5=128 };
  144. jlib_decl IPTreeMaker *createPTreeMaker(byte flags=ipt_none, IPropertyTree *root=NULL, IPTreeNodeCreator *nodeCreator=NULL);
  145. jlib_decl IPTreeMaker *createRootLessPTreeMaker(byte flags=ipt_none, IPropertyTree *root=NULL, IPTreeNodeCreator *nodeCreator=NULL);
  146. jlib_decl IPTreeReader *createXMLStreamReader(ISimpleReadStream &stream, IPTreeNotifyEvent &iEvent, PTreeReaderOptions xmlReaderOptions=ptr_ignoreWhiteSpace, size32_t bufSize=0);
  147. jlib_decl IPTreeReader *createXMLStringReader(const char *xml, IPTreeNotifyEvent &iEvent, PTreeReaderOptions xmlReaderOptions=ptr_ignoreWhiteSpace);
  148. jlib_decl IPTreeReader *createXMLBufferReader(const void *buf, size32_t bufLength, IPTreeNotifyEvent &iEvent, PTreeReaderOptions xmlReaderOptions=ptr_ignoreWhiteSpace);
  149. jlib_decl IPullPTreeReader *createPullXMLStreamReader(ISimpleReadStream &stream, IPTreeNotifyEvent &iEvent, PTreeReaderOptions xmlReaderOptions=ptr_ignoreWhiteSpace, size32_t bufSize=0);
  150. jlib_decl IPullPTreeReader *createPullXMLStringReader(const char *xml, IPTreeNotifyEvent &iEvent, PTreeReaderOptions xmlReaderOptions=ptr_ignoreWhiteSpace);
  151. jlib_decl IPullPTreeReader *createPullXMLBufferReader(const void *buf, size32_t bufLength, IPTreeNotifyEvent &iEvent, PTreeReaderOptions xmlReaderOptions=ptr_ignoreWhiteSpace);
  152. jlib_decl IPTreeReader *createJSONStreamReader(ISimpleReadStream &stream, IPTreeNotifyEvent &iEvent, PTreeReaderOptions readerOptions=ptr_ignoreWhiteSpace, size32_t bufSize=0);
  153. jlib_decl IPTreeReader *createJSONStringReader(const char *json, IPTreeNotifyEvent &iEvent, PTreeReaderOptions readerOptions=ptr_ignoreWhiteSpace);
  154. jlib_decl IPTreeReader *createJSONBufferReader(const void *buf, size32_t bufLength, IPTreeNotifyEvent &iEvent, PTreeReaderOptions jsonReaderOptions=ptr_ignoreWhiteSpace);
  155. jlib_decl IPullPTreeReader *createPullJSONStreamReader(ISimpleReadStream &stream, IPTreeNotifyEvent &iEvent, PTreeReaderOptions readerOptions=ptr_ignoreWhiteSpace, size32_t bufSize=0);
  156. jlib_decl IPullPTreeReader *createPullJSONStringReader(const char *json, IPTreeNotifyEvent &iEvent, PTreeReaderOptions readerOptions=ptr_ignoreWhiteSpace);
  157. jlib_decl IPullPTreeReader *createPullJSONBufferReader(const void *buf, size32_t bufLength, IPTreeNotifyEvent &iEvent, PTreeReaderOptions readerOptions=ptr_ignoreWhiteSpace);
  158. jlib_decl void mergePTree(IPropertyTree *target, IPropertyTree *toMerge);
  159. jlib_decl void synchronizePTree(IPropertyTree *target, IPropertyTree *source);
  160. jlib_decl IPropertyTree *ensurePTree(IPropertyTree *root, const char *xpath);
  161. jlib_decl bool areMatchingPTrees(IPropertyTree * left, IPropertyTree * right);
  162. jlib_decl IPropertyTree *createPTree(MemoryBuffer &src);
  163. jlib_decl IPropertyTree *createPTree(byte flags=ipt_none);
  164. jlib_decl IPropertyTree *createPTree(const char *name, byte flags=ipt_none);
  165. jlib_decl IPropertyTree *createPTree(IFile &ifile, byte flags=ipt_none, PTreeReaderOptions readFlags=ptr_ignoreWhiteSpace, IPTreeMaker *iMaker=NULL);
  166. jlib_decl IPropertyTree *createPTree(IFileIO &ifileio, byte flags=ipt_none, PTreeReaderOptions readFlags=ptr_ignoreWhiteSpace, IPTreeMaker *iMaker=NULL);
  167. jlib_decl IPropertyTree *createPTree(ISimpleReadStream &stream, byte flags=ipt_none, PTreeReaderOptions readFlags=ptr_ignoreWhiteSpace, IPTreeMaker *iMaker=NULL);
  168. jlib_decl IPropertyTree *createPTreeFromXMLString(const char *xml, byte flags=ipt_none, PTreeReaderOptions readFlags=ptr_ignoreWhiteSpace, IPTreeMaker *iMaker=NULL);
  169. jlib_decl IPropertyTree *createPTreeFromXMLString(unsigned len, const char *xml, byte flags=ipt_none, PTreeReaderOptions readFlags=ptr_ignoreWhiteSpace, IPTreeMaker *iMaker=NULL);
  170. jlib_decl IPropertyTree *createPTreeFromXMLFile(const char *filename, byte flags=ipt_none, PTreeReaderOptions readFlags=ptr_ignoreWhiteSpace, IPTreeMaker *iMaker=NULL);
  171. jlib_decl IPropertyTree *createPTreeFromIPT(const IPropertyTree *srcTree, ipt_flags flags=ipt_none);
  172. jlib_decl IPropertyTree *createPTreeFromJSONString(const char *json, byte flags=ipt_none, PTreeReaderOptions readFlags=ptr_ignoreWhiteSpace, IPTreeMaker *iMaker=NULL);
  173. jlib_decl IPropertyTree *createPTreeFromJSONString(unsigned len, const char *json, byte flags=ipt_none, PTreeReaderOptions readFlags=ptr_ignoreWhiteSpace, IPTreeMaker *iMaker=NULL);
  174. #define XML_SortTags 0x01
  175. #define XML_Embed 0x02
  176. #define XML_NoEncode 0x04
  177. #define XML_Sanitize 0x08
  178. #define XML_SanitizeAttributeValues 0x10
  179. #define XML_SingleQuoteAttributeValues 0x20
  180. #define XML_NoBinaryEncode64 0x40
  181. #define XML_LineBreak 0x100
  182. #define XML_LineBreakAttributes 0x80
  183. #define XML_Format (XML_Embed|XML_LineBreakAttributes|XML_LineBreak)
  184. jlib_decl StringBuffer &toXML(const IPropertyTree *tree, StringBuffer &ret, unsigned indent = 0, unsigned flags=XML_Format);
  185. jlib_decl void toXML(const IPropertyTree *tree, IIOStream &out, unsigned indent = 0, unsigned flags=XML_Format);
  186. jlib_decl void saveXML(const char *filename, const IPropertyTree *tree, unsigned indent = 0, unsigned flags=XML_Format);
  187. jlib_decl void saveXML(IFile &ifile, const IPropertyTree *tree, unsigned indent = 0, unsigned=XML_Format);
  188. jlib_decl void saveXML(IFileIO &ifileio, const IPropertyTree *tree, unsigned indent = 0, unsigned flags=XML_Format);
  189. jlib_decl void saveXML(IIOStream &stream, const IPropertyTree *tree, unsigned indent = 0, unsigned flags=XML_Format);
  190. #define JSON_SortTags 0x01
  191. #define JSON_Format 0x02
  192. #define JSON_Sanitize 0x08
  193. #define JSON_SanitizeAttributeValues 0x10
  194. jlib_decl StringBuffer &toJSON(const IPropertyTree *tree, StringBuffer &ret, unsigned indent = 0, byte flags=JSON_Format);
  195. jlib_decl void toJSON(const IPropertyTree *tree, IIOStream &out, unsigned indent = 0, byte flags=JSON_Format);
  196. jlib_decl const char *splitXPath(const char *xpath, StringBuffer &head); // returns tail, fills 'head' with leading xpath
  197. jlib_decl bool validateXPathSyntax(const char *xpath, StringBuffer *error=NULL);
  198. jlib_decl bool validateXMLParseXPath(const char *xpath, StringBuffer *error=NULL);
  199. jlib_decl IPropertyTree *getXPathMatchTree(IPropertyTree &parent, const char *xpath);
  200. jlib_decl IPropertyTreeIterator *createNullPTreeIterator();
  201. jlib_decl bool isEmptyPTree(IPropertyTree *t);
  202. jlib_decl void extractJavadoc(IPropertyTree * result, const char * text); // Pass in a javadoc style comment (without head/tail) and extract information into a property tree.
  203. typedef IPropertyTree IPTree;
  204. typedef IPropertyTreeIterator IPTreeIterator;
  205. typedef Owned<IPTree> OwnedPTree;
  206. #endif