Audio plugin host https://kx.studio/carla
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.

186 lines
7.1KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2016 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of the ISC license
  6. http://www.isc.org/downloads/software-support-policy/isc-license/
  7. Permission to use, copy, modify, and/or distribute this software for any
  8. purpose with or without fee is hereby granted, provided that the above
  9. copyright notice and this permission notice appear in all copies.
  10. THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD
  11. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  12. FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
  13. OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  14. USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  15. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  16. OF THIS SOFTWARE.
  17. -----------------------------------------------------------------------------
  18. To release a closed-source product which uses other parts of JUCE not
  19. licensed under the ISC terms, commercial licenses are available: visit
  20. www.juce.com for more information.
  21. ==============================================================================
  22. */
  23. #ifndef JUCE_XMLDOCUMENT_H_INCLUDED
  24. #define JUCE_XMLDOCUMENT_H_INCLUDED
  25. namespace water {
  26. //==============================================================================
  27. /**
  28. Parses a text-based XML document and creates an XmlElement object from it.
  29. The parser will parse DTDs to load external entities but won't
  30. check the document for validity against the DTD.
  31. e.g.
  32. @code
  33. XmlDocument myDocument (File ("myfile.xml"));
  34. ScopedPointer<XmlElement> mainElement (myDocument.getDocumentElement());
  35. if (mainElement == nullptr)
  36. {
  37. String error = myDocument.getLastParseError();
  38. }
  39. else
  40. {
  41. ..use the element
  42. }
  43. @endcode
  44. Or you can use the static helper methods for quick parsing..
  45. @code
  46. ScopedPointer<XmlElement> xml (XmlDocument::parse (myXmlFile));
  47. if (xml != nullptr && xml->hasTagName ("foobar"))
  48. {
  49. ...etc
  50. @endcode
  51. @see XmlElement
  52. */
  53. class XmlDocument
  54. {
  55. public:
  56. //==============================================================================
  57. /** Creates an XmlDocument from the xml text.
  58. The text doesn't actually get parsed until the getDocumentElement() method is called.
  59. */
  60. XmlDocument (const String& documentText);
  61. /** Creates an XmlDocument from a file.
  62. The text doesn't actually get parsed until the getDocumentElement() method is called.
  63. */
  64. XmlDocument (const File& file);
  65. /** Destructor. */
  66. ~XmlDocument();
  67. //==============================================================================
  68. /** Creates an XmlElement object to represent the main document node.
  69. This method will do the actual parsing of the text, and if there's a
  70. parse error, it may returns nullptr (and you can find out the error using
  71. the getLastParseError() method).
  72. See also the parse() methods, which provide a shorthand way to quickly
  73. parse a file or string.
  74. @param onlyReadOuterDocumentElement if true, the parser will only read the
  75. first section of the file, and will only
  76. return the outer document element - this
  77. allows quick checking of large files to
  78. see if they contain the correct type of
  79. tag, without having to parse the entire file
  80. @returns a new XmlElement which the caller will need to delete, or null if
  81. there was an error.
  82. @see getLastParseError
  83. */
  84. XmlElement* getDocumentElement (bool onlyReadOuterDocumentElement = false);
  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 ingored 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 which the caller will need to delete, or null if there was an error.
  109. */
  110. static 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 which the caller will need to delete, or null if there was an error.
  114. */
  115. static XmlElement* parse (const String& xmlData);
  116. //==============================================================================
  117. private:
  118. String originalText;
  119. String::CharPointerType input;
  120. bool outOfData, errorOccurred;
  121. String lastError, dtdText;
  122. StringArray tokenisedDTD;
  123. bool needToLoadDTD, ignoreEmptyTextElements;
  124. ScopedPointer<InputSource> inputSource;
  125. 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. #endif // JUCE_XMLDOCUMENT_H_INCLUDED