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.

196 lines
7.6KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  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. std::unique_ptr<XmlElement> mainElement (myDocument.getDocumentElement());
  28. if (mainElement == nullptr)
  29. {
  30. String error = myDocument.getLastParseError();
  31. }
  32. else
  33. {
  34. ..use the element
  35. }
  36. @endcode
  37. Or you can use the helper functions for much less verbose parsing..
  38. @code
  39. if (auto xml = parseXML (myXmlFile))
  40. {
  41. if (xml->hasTagName ("foobar"))
  42. {
  43. ...etc
  44. }
  45. }
  46. @endcode
  47. @see XmlElement
  48. @tags{Core}
  49. */
  50. class JUCE_API XmlDocument
  51. {
  52. public:
  53. //==============================================================================
  54. /** Creates an XmlDocument from the xml text.
  55. The text doesn't actually get parsed until the getDocumentElement() method is called.
  56. */
  57. XmlDocument (const String& documentText);
  58. /** Creates an XmlDocument from a file.
  59. The text doesn't actually get parsed until the getDocumentElement() method is called.
  60. */
  61. XmlDocument (const File& file);
  62. /** Destructor. */
  63. ~XmlDocument();
  64. //==============================================================================
  65. /** Creates an XmlElement object to represent the main document node.
  66. This method will do the actual parsing of the text, and if there's a
  67. parse error, it may returns nullptr (and you can find out the error using
  68. the getLastParseError() method).
  69. See also the parse() methods, which provide a shorthand way to quickly
  70. parse a file or string.
  71. @param onlyReadOuterDocumentElement if true, the parser will only read the
  72. first section of the file, and will only
  73. return the outer document element - this
  74. allows quick checking of large files to
  75. see if they contain the correct type of
  76. tag, without having to parse the entire file
  77. @returns a new XmlElement which the caller will need to delete, or null if
  78. there was an error.
  79. @see getLastParseError
  80. */
  81. XmlElement* getDocumentElement (bool onlyReadOuterDocumentElement = false);
  82. /** Returns the parsing error that occurred the last time getDocumentElement was called.
  83. @returns the error, or an empty string if there was no error.
  84. */
  85. const String& getLastParseError() const noexcept;
  86. /** Sets an input source object to use for parsing documents that reference external entities.
  87. If the document has been created from a file, this probably won't be needed, but
  88. if you're parsing some text and there might be a DTD that references external
  89. files, you may need to create a custom input source that can retrieve the
  90. other files it needs.
  91. The object that is passed-in will be deleted automatically when no longer needed.
  92. @see InputSource
  93. */
  94. void setInputSource (InputSource* newSource) noexcept;
  95. /** Sets a flag to change the treatment of empty text elements.
  96. If this is true (the default state), then any text elements that contain only
  97. whitespace characters will be ingored during parsing. If you need to catch
  98. whitespace-only text, then you should set this to false before calling the
  99. getDocumentElement() method.
  100. */
  101. void setEmptyTextElementsIgnored (bool shouldBeIgnored) noexcept;
  102. //==============================================================================
  103. /** A handy static method that parses a file.
  104. This is a shortcut for creating an XmlDocument object and calling getDocumentElement() on it.
  105. An even better shortcut is the juce::parseXML() function, which returns a std::unique_ptr<XmlElement>!
  106. @returns a new XmlElement which the caller will need to delete, or null if there was an error.
  107. */
  108. static XmlElement* parse (const File& file);
  109. /** A handy static method that parses some XML data.
  110. This is a shortcut for creating an XmlDocument object and calling getDocumentElement() on it.
  111. An even better shortcut is the juce::parseXML() function, which returns a std::unique_ptr<XmlElement>!
  112. @returns a new XmlElement which the caller will need to delete, or null if there was an error.
  113. */
  114. static XmlElement* parse (const String& xmlData);
  115. //==============================================================================
  116. private:
  117. String originalText;
  118. String::CharPointerType input { nullptr };
  119. bool outOfData = false, errorOccurred = false;
  120. String lastError, dtdText;
  121. StringArray tokenisedDTD;
  122. bool needToLoadDTD = false, ignoreEmptyTextElements = true;
  123. std::unique_ptr<InputSource> inputSource;
  124. XmlElement* parseDocumentElement (String::CharPointerType, bool outer);
  125. void setLastError (const String&, bool carryOn);
  126. bool parseHeader();
  127. bool parseDTD();
  128. void skipNextWhiteSpace();
  129. juce_wchar readNextChar() noexcept;
  130. XmlElement* readNextElement (bool alsoParseSubElements);
  131. void readChildElements (XmlElement&);
  132. void readQuotedString (String&);
  133. void readEntity (String&);
  134. String getFileContents (const String&) const;
  135. String expandEntity (const String&);
  136. String expandExternalEntity (const String&);
  137. String getParameterEntity (const String&);
  138. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (XmlDocument)
  139. };
  140. //==============================================================================
  141. /** Attempts to parse some XML text, returning a new XmlElement if it was valid.
  142. If the parse fails, this will return a nullptr - if you need more information about
  143. errors or more parsing options, see the XmlDocument instead.
  144. @see XmlDocument
  145. */
  146. std::unique_ptr<XmlElement> parseXML (const String& textToParse);
  147. /** Attempts to parse some XML text, returning a new XmlElement if it was valid.
  148. If the parse fails, this will return a nullptr - if you need more information about
  149. errors or more parsing options, see the XmlDocument instead.
  150. @see XmlDocument
  151. */
  152. std::unique_ptr<XmlElement> parseXML (const File& fileToParse);
  153. } // namespace juce