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.

187 lines
7.3KB

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