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.

182 lines
7.1KB

  1. /*
  2. ==============================================================================
  3. This file is part of the juce_core module of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software Ltd.
  5. Permission to use, copy, modify, and/or distribute this software for any purpose with
  6. or without fee is hereby granted, provided that the above copyright notice and this
  7. permission notice appear in all copies.
  8. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
  9. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
  10. NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  11. DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
  12. IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  13. CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  14. ------------------------------------------------------------------------------
  15. NOTE! This permissive ISC license applies ONLY to files within the juce_core module!
  16. All other JUCE modules are covered by a dual GPL/commercial license, so if you are
  17. using any other modules, be sure to check that you also comply with their license.
  18. For more details, visit www.juce.com
  19. ==============================================================================
  20. */
  21. #ifndef JUCE_XMLDOCUMENT_H_INCLUDED
  22. #define JUCE_XMLDOCUMENT_H_INCLUDED
  23. //==============================================================================
  24. /**
  25. Parses a text-based XML document and creates an XmlElement object from it.
  26. The parser will parse DTDs to load external entities but won't
  27. check the document for validity against the DTD.
  28. e.g.
  29. @code
  30. XmlDocument myDocument (File ("myfile.xml"));
  31. XmlElement* mainElement = myDocument.getDocumentElement();
  32. if (mainElement == nullptr)
  33. {
  34. String error = myDocument.getLastParseError();
  35. }
  36. else
  37. {
  38. ..use the element
  39. }
  40. @endcode
  41. Or you can use the static helper methods for quick parsing..
  42. @code
  43. XmlElement* xml = XmlDocument::parse (myXmlFile);
  44. if (xml != nullptr && xml->hasTagName ("foobar"))
  45. {
  46. ...etc
  47. @endcode
  48. @see XmlElement
  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. @returns a new XmlElement which the caller will need to delete, or null if there was an error.
  106. */
  107. static XmlElement* parse (const File& file);
  108. /** A handy static method that parses some XML data.
  109. This is a shortcut for creating an XmlDocument object and calling getDocumentElement() on it.
  110. @returns a new XmlElement which the caller will need to delete, or null if there was an error.
  111. */
  112. static XmlElement* parse (const String& xmlData);
  113. //==============================================================================
  114. private:
  115. String originalText;
  116. String::CharPointerType input;
  117. bool outOfData, errorOccurred;
  118. String lastError, dtdText;
  119. StringArray tokenisedDTD;
  120. bool needToLoadDTD, ignoreEmptyTextElements;
  121. ScopedPointer<InputSource> inputSource;
  122. XmlElement* parseDocumentElement (String::CharPointerType, bool outer);
  123. void setLastError (const String&, bool carryOn);
  124. bool parseHeader();
  125. bool parseDTD();
  126. void skipNextWhiteSpace();
  127. juce_wchar readNextChar() noexcept;
  128. XmlElement* readNextElement (bool alsoParseSubElements);
  129. void readChildElements (XmlElement&);
  130. void readQuotedString (String&);
  131. void readEntity (String&);
  132. String getFileContents (const String&) const;
  133. String expandEntity (const String&);
  134. String expandExternalEntity (const String&);
  135. String getParameterEntity (const String&);
  136. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (XmlDocument)
  137. };
  138. #endif // JUCE_XMLDOCUMENT_H_INCLUDED