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.

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