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.

185 lines
7.0KB

  1. /*
  2. ==============================================================================
  3. This file is part of the Water library.
  4. Copyright (c) 2016 ROLI Ltd.
  5. Copyright (C) 2017-2018 Filipe Coelho <falktx@falktx.com>
  6. Permission is granted to use this software under the terms of the ISC license
  7. http://www.isc.org/downloads/software-support-policy/isc-license/
  8. Permission to use, copy, modify, and/or distribute this software for any
  9. purpose with or without fee is hereby granted, provided that the above
  10. copyright notice and this permission notice appear in all copies.
  11. THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD
  12. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  13. FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
  14. OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  15. USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  16. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  17. OF THIS SOFTWARE.
  18. ==============================================================================
  19. */
  20. #ifndef WATER_XMLDOCUMENT_H_INCLUDED
  21. #define WATER_XMLDOCUMENT_H_INCLUDED
  22. #include "../text/StringArray.h"
  23. #include "CarlaScopeUtils.hpp"
  24. namespace water {
  25. //==============================================================================
  26. /**
  27. Parses a text-based XML document and creates an XmlElement object from it.
  28. The parser will parse DTDs to load external entities but won't
  29. check the document for validity against the DTD.
  30. e.g.
  31. @code
  32. XmlDocument myDocument (File ("myfile.xml"));
  33. ScopedPointer<XmlElement> mainElement (myDocument.getDocumentElement());
  34. if (mainElement == nullptr)
  35. {
  36. String error = myDocument.getLastParseError();
  37. }
  38. else
  39. {
  40. ..use the element
  41. }
  42. @endcode
  43. Or you can use the static helper methods for quick parsing..
  44. @code
  45. ScopedPointer<XmlElement> xml (XmlDocument::parse (myXmlFile));
  46. if (xml != nullptr && xml->hasTagName ("foobar"))
  47. {
  48. ...etc
  49. @endcode
  50. @see XmlElement
  51. */
  52. class XmlDocument
  53. {
  54. public:
  55. //==============================================================================
  56. /** Creates an XmlDocument from the xml text.
  57. The text doesn't actually get parsed until the getDocumentElement() method is called.
  58. */
  59. XmlDocument (const String& documentText);
  60. /** Creates an XmlDocument from a file.
  61. The text doesn't actually get parsed until the getDocumentElement() method is called.
  62. */
  63. XmlDocument (const File& file);
  64. /** Destructor. */
  65. ~XmlDocument();
  66. //==============================================================================
  67. /** Creates an XmlElement object to represent the main document node.
  68. This method will do the actual parsing of the text, and if there's a
  69. parse error, it may returns nullptr (and you can find out the error using
  70. the getLastParseError() method).
  71. See also the parse() methods, which provide a shorthand way to quickly
  72. parse a file or string.
  73. @param onlyReadOuterDocumentElement if true, the parser will only read the
  74. first section of the file, and will only
  75. return the outer document element - this
  76. allows quick checking of large files to
  77. see if they contain the correct type of
  78. tag, without having to parse the entire file
  79. @returns a new XmlElement which the caller will need to delete, or null if
  80. there was an error.
  81. @see getLastParseError
  82. */
  83. XmlElement* getDocumentElement (bool onlyReadOuterDocumentElement = false);
  84. /** Returns the parsing error that occurred the last time getDocumentElement was called.
  85. @returns the error, or an empty string if there was no error.
  86. */
  87. const String& getLastParseError() const noexcept;
  88. /** Sets an input source object to use for parsing documents that reference external entities.
  89. If the document has been created from a file, this probably won't be needed, but
  90. if you're parsing some text and there might be a DTD that references external
  91. files, you may need to create a custom input source that can retrieve the
  92. other files it needs.
  93. The object that is passed-in will be deleted automatically when no longer needed.
  94. @see FileInputSource
  95. */
  96. void setInputSource (FileInputSource* newSource) noexcept;
  97. /** Sets a flag to change the treatment of empty text elements.
  98. If this is true (the default state), then any text elements that contain only
  99. whitespace characters will be ingored during parsing. If you need to catch
  100. whitespace-only text, then you should set this to false before calling the
  101. getDocumentElement() method.
  102. */
  103. void setEmptyTextElementsIgnored (bool shouldBeIgnored) noexcept;
  104. //==============================================================================
  105. /** A handy static method that parses a file.
  106. This is a shortcut for creating an XmlDocument object and calling getDocumentElement() on it.
  107. @returns a new XmlElement which the caller will need to delete, or null if there was an error.
  108. */
  109. static XmlElement* parse (const File& file);
  110. /** A handy static method that parses some XML data.
  111. This is a shortcut for creating an XmlDocument object and calling getDocumentElement() on it.
  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;
  119. bool outOfData, errorOccurred;
  120. String lastError, dtdText;
  121. StringArray tokenisedDTD;
  122. bool needToLoadDTD, ignoreEmptyTextElements;
  123. CarlaScopedPointer<FileInputSource> 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. water_uchar 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. CARLA_DECLARE_NON_COPY_CLASS (XmlDocument)
  139. };
  140. }
  141. #endif // WATER_XMLDOCUMENT_H_INCLUDED