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.

172 lines
6.5KB

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