The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

208 lines
8.4KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - Raw Material Software Limited
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. The code included in this file is provided under the terms of the ISC license
  8. http://www.isc.org/downloads/software-support-policy/isc-license. Permission
  9. To use, copy, modify, and/or distribute this software for any purpose with or
  10. without fee is hereby granted provided that the above copyright notice and
  11. this permission notice appear in all copies.
  12. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  13. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  14. DISCLAIMED.
  15. ==============================================================================
  16. */
  17. namespace juce
  18. {
  19. //==============================================================================
  20. /**
  21. Parses a text-based XML document and creates an XmlElement object from it.
  22. The parser will parse DTDs to load external entities but won't
  23. check the document for validity against the DTD.
  24. e.g.
  25. @code
  26. XmlDocument myDocument (File ("myfile.xml"));
  27. if (auto mainElement = myDocument.getDocumentElement())
  28. {
  29. ..use the element
  30. }
  31. else
  32. {
  33. String error = myDocument.getLastParseError();
  34. }
  35. @endcode
  36. Or you can use the helper functions for much less verbose parsing..
  37. @code
  38. if (auto xml = parseXML (myXmlFile))
  39. {
  40. if (xml->hasTagName ("foobar"))
  41. {
  42. ...etc
  43. }
  44. }
  45. @endcode
  46. @see XmlElement
  47. @tags{Core}
  48. */
  49. class JUCE_API XmlDocument
  50. {
  51. public:
  52. //==============================================================================
  53. /** Creates an XmlDocument from the xml text.
  54. The text doesn't actually get parsed until the getDocumentElement() method is called.
  55. */
  56. XmlDocument (const String& documentText);
  57. /** Creates an XmlDocument from a file.
  58. The text doesn't actually get parsed until the getDocumentElement() method is called.
  59. */
  60. XmlDocument (const File& file);
  61. /** Destructor. */
  62. ~XmlDocument();
  63. //==============================================================================
  64. /** Creates an XmlElement object to represent the main document node.
  65. This method will do the actual parsing of the text, and if there's a
  66. parse error, it may returns nullptr (and you can find out the error using
  67. the getLastParseError() method).
  68. See also the parse() methods, which provide a shorthand way to quickly
  69. parse a file or string.
  70. @param onlyReadOuterDocumentElement if true, the parser will only read the
  71. first section of the file, and will only
  72. return the outer document element - this
  73. allows quick checking of large files to
  74. see if they contain the correct type of
  75. tag, without having to parse the entire file
  76. @returns a new XmlElement, or nullptr if there was an error.
  77. @see getLastParseError, getDocumentElementIfTagMatches
  78. */
  79. std::unique_ptr<XmlElement> getDocumentElement (bool onlyReadOuterDocumentElement = false);
  80. /** Does an inexpensive check to see whether the outer element has the given tag name, and
  81. then does a full parse if it matches.
  82. If the tag is different, or the XML parse fails, this will return nullptr.
  83. */
  84. std::unique_ptr<XmlElement> getDocumentElementIfTagMatches (StringRef requiredTag);
  85. /** Returns the parsing error that occurred the last time getDocumentElement was called.
  86. @returns the error, or an empty string if there was no error.
  87. */
  88. const String& getLastParseError() const noexcept;
  89. /** Sets an input source object to use for parsing documents that reference external entities.
  90. If the document has been created from a file, this probably won't be needed, but
  91. if you're parsing some text and there might be a DTD that references external
  92. files, you may need to create a custom input source that can retrieve the
  93. other files it needs.
  94. The object that is passed-in will be deleted automatically when no longer needed.
  95. @see InputSource
  96. */
  97. void setInputSource (InputSource* newSource) noexcept;
  98. /** Sets a flag to change the treatment of empty text elements.
  99. If this is true (the default state), then any text elements that contain only
  100. whitespace characters will be ignored during parsing. If you need to catch
  101. whitespace-only text, then you should set this to false before calling the
  102. getDocumentElement() method.
  103. */
  104. void setEmptyTextElementsIgnored (bool shouldBeIgnored) noexcept;
  105. //==============================================================================
  106. /** A handy static method that parses a file.
  107. This is a shortcut for creating an XmlDocument object and calling getDocumentElement() on it.
  108. @returns a new XmlElement, or nullptr if there was an error.
  109. */
  110. static std::unique_ptr<XmlElement> parse (const File& file);
  111. /** A handy static method that parses some XML data.
  112. This is a shortcut for creating an XmlDocument object and calling getDocumentElement() on it.
  113. @returns a new XmlElement, or nullptr if there was an error.
  114. */
  115. static std::unique_ptr<XmlElement> parse (const String& xmlData);
  116. //==============================================================================
  117. private:
  118. String originalText;
  119. String::CharPointerType input { nullptr };
  120. bool outOfData = false, errorOccurred = false;
  121. String lastError, dtdText;
  122. StringArray tokenisedDTD;
  123. bool needToLoadDTD = false, ignoreEmptyTextElements = true;
  124. std::unique_ptr<InputSource> inputSource;
  125. std::unique_ptr<XmlElement> parseDocumentElement (String::CharPointerType, bool outer);
  126. void setLastError (const String&, bool carryOn);
  127. bool parseHeader();
  128. bool parseDTD();
  129. void skipNextWhiteSpace();
  130. juce_wchar readNextChar() noexcept;
  131. XmlElement* readNextElement (bool alsoParseSubElements);
  132. void readChildElements (XmlElement&);
  133. void readQuotedString (String&);
  134. void readEntity (String&);
  135. String getFileContents (const String&) const;
  136. String expandEntity (const String&);
  137. String expandExternalEntity (const String&);
  138. String getParameterEntity (const String&);
  139. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (XmlDocument)
  140. };
  141. //==============================================================================
  142. /** Attempts to parse some XML text, returning a new XmlElement if it was valid.
  143. If the parse fails, this will return a nullptr - if you need more information about
  144. errors or more parsing options, see the XmlDocument class instead.
  145. @see XmlDocument, parseXMLIfTagMatches
  146. */
  147. std::unique_ptr<XmlElement> parseXML (const String& textToParse);
  148. /** Attempts to parse some XML text, returning a new XmlElement if it was valid.
  149. If the parse fails, this will return a nullptr - if you need more information about
  150. errors or more parsing options, see the XmlDocument class instead.
  151. @see XmlDocument, parseXMLIfTagMatches
  152. */
  153. std::unique_ptr<XmlElement> parseXML (const File& fileToParse);
  154. /** Does an inexpensive check to see whether the top-level element has the given tag
  155. name, and if that's true, does a full parse and returns the result.
  156. If the outer tag doesn't match, or the XML has errors, this will return nullptr;
  157. @see parseXML
  158. */
  159. std::unique_ptr<XmlElement> parseXMLIfTagMatches (const String& textToParse, StringRef requiredTag);
  160. /** Does an inexpensive check to see whether the top-level element has the given tag
  161. name, and if that's true, does a full parse and returns the result.
  162. If the outer tag doesn't match, or the XML has errors, this will return nullptr;
  163. @see parseXML
  164. */
  165. std::unique_ptr<XmlElement> parseXMLIfTagMatches (const File& fileToParse, StringRef requiredTag);
  166. } // namespace juce