|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944 |
- /*
- ==============================================================================
-
- This file is part of the Water library.
- Copyright (c) 2016 ROLI Ltd.
- Copyright (C) 2017-2019 Filipe Coelho <falktx@falktx.com>
-
- Permission is granted to use this software under the terms of the ISC license
- http://www.isc.org/downloads/software-support-policy/isc-license/
-
- Permission to use, copy, modify, and/or distribute this software for any
- purpose with or without fee is hereby granted, provided that the above
- copyright notice and this permission notice appear in all copies.
-
- THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD
- TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
- FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
- OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
- USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
- TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
- OF THIS SOFTWARE.
-
- ==============================================================================
- */
-
- #include "XmlElement.h"
- #include "../streams/MemoryOutputStream.h"
- #include "../streams/OutputStream.h"
- #include "../text/NewLine.h"
-
- namespace water {
-
- inline bool isValidXmlNameStartCharacter (const water_uchar character) noexcept
- {
- return character == ':'
- || character == '_'
- || (character >= 'a' && character <= 'z')
- || (character >= 'A' && character <= 'Z')
- || (character >= 0xc0 && character <= 0xd6)
- || (character >= 0xd8 && character <= 0xf6)
- || (character >= 0xf8 && character <= 0x2ff)
- || (character >= 0x370 && character <= 0x37d)
- || (character >= 0x37f && character <= 0x1fff)
- || (character >= 0x200c && character <= 0x200d)
- || (character >= 0x2070 && character <= 0x218f)
- || (character >= 0x2c00 && character <= 0x2fef)
- || (character >= 0x3001 && character <= 0xd7ff)
- || (character >= 0xf900 && character <= 0xfdcf)
- || (character >= 0xfdf0 && character <= 0xfffd)
- || (character >= 0x10000 && character <= 0xeffff);
- }
-
- inline bool isValidXmlNameBodyCharacter (const water_uchar character) noexcept
- {
- return isValidXmlNameStartCharacter (character)
- || character == '-'
- || character == '.'
- || character == 0xb7
- || (character >= '0' && character <= '9')
- || (character >= 0x300 && character <= 0x036f)
- || (character >= 0x203f && character <= 0x2040);
- }
-
- XmlElement::XmlAttributeNode::XmlAttributeNode (const XmlAttributeNode& other) noexcept
- : name (other.name),
- value (other.value)
- {
- }
-
- XmlElement::XmlAttributeNode::XmlAttributeNode (const Identifier& n, const String& v) noexcept
- : name (n), value (v)
- {
- wassert (isValidXmlName (name));
- }
-
- XmlElement::XmlAttributeNode::XmlAttributeNode (String::CharPointerType nameStart, String::CharPointerType nameEnd)
- : name (nameStart, nameEnd)
- {
- wassert (isValidXmlName (name));
- }
-
- //==============================================================================
- XmlElement::XmlElement (const String& tag)
- : tagName (tag)
- {
- wassert (isValidXmlName (tagName));
- }
-
- XmlElement::XmlElement (const char* tag)
- : tagName (tag)
- {
- wassert (isValidXmlName (tagName));
- }
-
- XmlElement::XmlElement (StringRef tag)
- : tagName (tag)
- {
- wassert (isValidXmlName (tagName));
- }
-
- XmlElement::XmlElement (const Identifier& tag)
- : tagName (tag.toString())
- {
- wassert (isValidXmlName (tagName));
- }
-
- XmlElement::XmlElement (String::CharPointerType tagNameStart, String::CharPointerType tagNameEnd)
- : tagName (StartEndString (tagNameStart, tagNameEnd))
- {
- wassert (isValidXmlName (tagName));
- }
-
- XmlElement::XmlElement (int /*dummy*/) noexcept
- {
- }
-
- XmlElement::XmlElement (const XmlElement& other)
- : tagName (other.tagName)
- {
- copyChildrenAndAttributesFrom (other);
- }
-
- XmlElement& XmlElement::operator= (const XmlElement& other)
- {
- if (this != &other)
- {
- removeAllAttributes();
- deleteAllChildElements();
- tagName = other.tagName;
- copyChildrenAndAttributesFrom (other);
- }
-
- return *this;
- }
-
- #if WATER_COMPILER_SUPPORTS_MOVE_SEMANTICS
- XmlElement::XmlElement (XmlElement&& other) noexcept
- : nextListItem (static_cast<LinkedListPointer<XmlElement>&&> (other.nextListItem)),
- firstChildElement (static_cast<LinkedListPointer<XmlElement>&&> (other.firstChildElement)),
- attributes (static_cast<LinkedListPointer<XmlAttributeNode>&&> (other.attributes)),
- tagName (static_cast<String&&> (other.tagName))
- {
- }
-
- XmlElement& XmlElement::operator= (XmlElement&& other) noexcept
- {
- wassert (this != &other); // hopefully the compiler should make this situation impossible!
-
- removeAllAttributes();
- deleteAllChildElements();
-
- nextListItem = static_cast<LinkedListPointer<XmlElement>&&> (other.nextListItem);
- firstChildElement = static_cast<LinkedListPointer<XmlElement>&&> (other.firstChildElement);
- attributes = static_cast<LinkedListPointer<XmlAttributeNode>&&> (other.attributes);
- tagName = static_cast<String&&> (other.tagName);
-
- return *this;
- }
- #endif
-
- void XmlElement::copyChildrenAndAttributesFrom (const XmlElement& other)
- {
- wassert (firstChildElement.get() == nullptr);
- firstChildElement.addCopyOfList (other.firstChildElement);
-
- wassert (attributes.get() == nullptr);
- attributes.addCopyOfList (other.attributes);
- }
-
- XmlElement::~XmlElement() noexcept
- {
- firstChildElement.deleteAll();
- attributes.deleteAll();
- }
-
- //==============================================================================
- namespace XmlOutputFunctions
- {
- #if 0 // (These functions are just used to generate the lookup table used below)
- bool isLegalXmlCharSlow (const water_uchar character) noexcept
- {
- if ((character >= 'a' && character <= 'z')
- || (character >= 'A' && character <= 'Z')
- || (character >= '0' && character <= '9'))
- return true;
-
- const char* t = " .,;:-()_+=?!'#@[]/\\*%~{}$|";
-
- do
- {
- if (((water_uchar) (uint8) *t) == character)
- return true;
- }
- while (*++t != 0);
-
- return false;
- }
-
- void generateLegalCharLookupTable()
- {
- uint8 n[32] = { 0 };
- for (int i = 0; i < 256; ++i)
- if (isLegalXmlCharSlow (i))
- n[i >> 3] |= (1 << (i & 7));
-
- String s;
- for (int i = 0; i < 32; ++i)
- s << (int) n[i] << ", ";
-
- DBG (s);
- }
- #endif
-
- static bool isLegalXmlChar (const uint32 c) noexcept
- {
- static const unsigned char legalChars[] = { 0, 0, 0, 0, 187, 255, 255, 175, 255,
- 255, 255, 191, 254, 255, 255, 127 };
- return c < sizeof (legalChars) * 8
- && (legalChars [c >> 3] & (1 << (c & 7))) != 0;
- }
-
- static void escapeIllegalXmlChars (OutputStream& outputStream, const String& text, const bool changeNewLines)
- {
- String::CharPointerType t (text.getCharPointer());
-
- for (;;)
- {
- const uint32 character = (uint32) t.getAndAdvance();
-
- if (character == 0)
- break;
-
- if (isLegalXmlChar (character))
- {
- outputStream << (char) character;
- }
- else
- {
- switch (character)
- {
- case '&': outputStream << "&"; break;
- case '"': outputStream << """; break;
- case '>': outputStream << ">"; break;
- case '<': outputStream << "<"; break;
-
- case '\n':
- case '\r':
- if (! changeNewLines)
- {
- outputStream << (char) character;
- break;
- }
- // fall-through
- default:
- outputStream << "&#" << ((int) character) << ';';
- break;
- }
- }
- }
- }
-
- static void writeSpaces (OutputStream& out, const size_t numSpaces)
- {
- out.writeRepeatedByte (' ', numSpaces);
- }
- }
-
- void XmlElement::writeElementAsText (OutputStream& outputStream,
- const int indentationLevel,
- const int lineWrapLength) const
- {
- using namespace XmlOutputFunctions;
-
- NewLine newLine;
-
- if (indentationLevel >= 0)
- writeSpaces (outputStream, (size_t) indentationLevel);
-
- if (! isTextElement())
- {
- outputStream.writeByte ('<');
- outputStream << tagName;
-
- {
- const size_t attIndent = (size_t) (indentationLevel + tagName.length() + 1);
- int lineLen = 0;
-
- for (const XmlAttributeNode* att = attributes; att != nullptr; att = att->nextListItem)
- {
- if (lineLen > lineWrapLength && indentationLevel >= 0)
- {
- outputStream << newLine;
- writeSpaces (outputStream, attIndent);
- lineLen = 0;
- }
-
- const int64 startPos = outputStream.getPosition();
- outputStream.writeByte (' ');
- outputStream << att->name;
- outputStream.write ("=\"", 2);
- escapeIllegalXmlChars (outputStream, att->value, true);
- outputStream.writeByte ('"');
- lineLen += (int) (outputStream.getPosition() - startPos);
- }
- }
-
- if (firstChildElement != nullptr)
- {
- outputStream.writeByte ('>');
-
- bool lastWasTextNode = false;
-
- for (XmlElement* child = firstChildElement; child != nullptr; child = child->nextListItem)
- {
- if (child->isTextElement())
- {
- escapeIllegalXmlChars (outputStream, child->getText(), false);
- lastWasTextNode = true;
- }
- else
- {
- if (indentationLevel >= 0 && ! lastWasTextNode)
- outputStream << newLine;
-
- child->writeElementAsText (outputStream,
- lastWasTextNode ? 0 : (indentationLevel + (indentationLevel >= 0 ? 2 : 0)), lineWrapLength);
- lastWasTextNode = false;
- }
- }
-
- if (indentationLevel >= 0 && ! lastWasTextNode)
- {
- outputStream << newLine;
- writeSpaces (outputStream, (size_t) indentationLevel);
- }
-
- outputStream.write ("</", 2);
- outputStream << tagName;
- outputStream.writeByte ('>');
- }
- else
- {
- outputStream.write ("/>", 2);
- }
- }
- else
- {
- escapeIllegalXmlChars (outputStream, getText(), false);
- }
- }
-
- String XmlElement::createDocument (StringRef dtdToUse,
- const bool allOnOneLine,
- const bool includeXmlHeader,
- StringRef encodingType,
- const int lineWrapLength) const
- {
- MemoryOutputStream mem (2048);
- writeToStream (mem, dtdToUse, allOnOneLine, includeXmlHeader, encodingType, lineWrapLength);
-
- return mem.toUTF8();
- }
-
- void XmlElement::writeToStream (OutputStream& output,
- StringRef dtdToUse,
- const bool allOnOneLine,
- const bool includeXmlHeader,
- StringRef encodingType,
- const int lineWrapLength) const
- {
- using namespace XmlOutputFunctions;
-
- NewLine newLine;
-
- if (includeXmlHeader)
- {
- output << "<?xml version=\"1.0\" encoding=\"" << encodingType << "\"?>";
-
- if (allOnOneLine)
- output.writeByte (' ');
- else
- output << newLine << newLine;
- }
-
- if (dtdToUse.isNotEmpty())
- {
- output << dtdToUse;
-
- if (allOnOneLine)
- output.writeByte (' ');
- else
- output << newLine;
- }
-
- writeElementAsText (output, allOnOneLine ? -1 : 0, lineWrapLength);
-
- if (! allOnOneLine)
- output << newLine;
- }
-
- #if 0
- bool XmlElement::writeToFile (const File& file,
- StringRef dtdToUse,
- StringRef encodingType,
- const int lineWrapLength) const
- {
- TemporaryFile tempFile (file);
-
- {
- FileOutputStream out (tempFile.getFile());
-
- if (! out.openedOk())
- return false;
-
- writeToStream (out, dtdToUse, false, true, encodingType, lineWrapLength);
-
- out.flush(); // (called explicitly to force an fsync on posix)
-
- if (out.getStatus().failed())
- return false;
- }
-
- return tempFile.overwriteTargetFileWithTemporary();
- }
- #endif
-
- //==============================================================================
- bool XmlElement::hasTagName (StringRef possibleTagName) const noexcept
- {
- const bool matches = tagName.equalsIgnoreCase (possibleTagName);
-
- // XML tags should be case-sensitive, so although this method allows a
- // case-insensitive match to pass, you should try to avoid this.
- wassert ((! matches) || tagName == possibleTagName);
-
- return matches;
- }
-
- String XmlElement::getNamespace() const
- {
- return tagName.upToFirstOccurrenceOf (":", false, false);
- }
-
- String XmlElement::getTagNameWithoutNamespace() const
- {
- return tagName.fromLastOccurrenceOf (":", false, false);
- }
-
- bool XmlElement::hasTagNameIgnoringNamespace (StringRef possibleTagName) const
- {
- return hasTagName (possibleTagName) || getTagNameWithoutNamespace() == possibleTagName;
- }
-
- XmlElement* XmlElement::getNextElementWithTagName (StringRef requiredTagName) const
- {
- XmlElement* e = nextListItem;
-
- while (e != nullptr && ! e->hasTagName (requiredTagName))
- e = e->nextListItem;
-
- return e;
- }
-
- //==============================================================================
- int XmlElement::getNumAttributes() const noexcept
- {
- return attributes.size();
- }
-
- static const String& getEmptyStringRef() noexcept
- {
- static String empty;
- return empty;
- }
-
- const String& XmlElement::getAttributeName (const int index) const noexcept
- {
- if (const XmlAttributeNode* const att = attributes [index])
- return att->name.toString();
-
- return getEmptyStringRef();
- }
-
- const String& XmlElement::getAttributeValue (const int index) const noexcept
- {
- if (const XmlAttributeNode* const att = attributes [index])
- return att->value;
-
- return getEmptyStringRef();
- }
-
- XmlElement::XmlAttributeNode* XmlElement::getAttribute (StringRef attributeName) const noexcept
- {
- for (XmlAttributeNode* att = attributes; att != nullptr; att = att->nextListItem)
- if (att->name == attributeName)
- return att;
-
- return nullptr;
- }
-
- bool XmlElement::hasAttribute (StringRef attributeName) const noexcept
- {
- return getAttribute (attributeName) != nullptr;
- }
-
- //==============================================================================
- const String& XmlElement::getStringAttribute (StringRef attributeName) const noexcept
- {
- if (const XmlAttributeNode* att = getAttribute (attributeName))
- return att->value;
-
- return getEmptyStringRef();
- }
-
- String XmlElement::getStringAttribute (StringRef attributeName, const String& defaultReturnValue) const
- {
- if (const XmlAttributeNode* att = getAttribute (attributeName))
- return att->value;
-
- return defaultReturnValue;
- }
-
- int XmlElement::getIntAttribute (StringRef attributeName, const int defaultReturnValue) const
- {
- if (const XmlAttributeNode* att = getAttribute (attributeName))
- return att->value.getIntValue();
-
- return defaultReturnValue;
- }
-
- double XmlElement::getDoubleAttribute (StringRef attributeName, const double defaultReturnValue) const
- {
- if (const XmlAttributeNode* att = getAttribute (attributeName))
- return att->value.getDoubleValue();
-
- return defaultReturnValue;
- }
-
- bool XmlElement::getBoolAttribute (StringRef attributeName, const bool defaultReturnValue) const
- {
- if (const XmlAttributeNode* att = getAttribute (attributeName))
- {
- const water_uchar firstChar = *(att->value.getCharPointer().findEndOfWhitespace());
-
- return firstChar == '1'
- || firstChar == 't'
- || firstChar == 'y'
- || firstChar == 'T'
- || firstChar == 'Y';
- }
-
- return defaultReturnValue;
- }
-
- bool XmlElement::compareAttribute (StringRef attributeName,
- StringRef stringToCompareAgainst,
- const bool ignoreCase) const noexcept
- {
- if (const XmlAttributeNode* att = getAttribute (attributeName))
- return ignoreCase ? att->value.equalsIgnoreCase (stringToCompareAgainst)
- : att->value == stringToCompareAgainst;
-
- return false;
- }
-
- //==============================================================================
- void XmlElement::setAttribute (const Identifier& attributeName, const String& value)
- {
- if (attributes == nullptr)
- {
- attributes = new XmlAttributeNode (attributeName, value);
- }
- else
- {
- for (XmlAttributeNode* att = attributes; ; att = att->nextListItem)
- {
- if (att->name == attributeName)
- {
- att->value = value;
- break;
- }
-
- if (att->nextListItem == nullptr)
- {
- att->nextListItem = new XmlAttributeNode (attributeName, value);
- break;
- }
- }
- }
- }
-
- void XmlElement::setAttribute (const Identifier& attributeName, const int number)
- {
- setAttribute (attributeName, String (number));
- }
-
- void XmlElement::setAttribute (const Identifier& attributeName, const double number)
- {
- setAttribute (attributeName, String (number, 20));
- }
-
- void XmlElement::removeAttribute (const Identifier& attributeName) noexcept
- {
- for (LinkedListPointer<XmlAttributeNode>* att = &attributes;
- att->get() != nullptr;
- att = &(att->get()->nextListItem))
- {
- if (att->get()->name == attributeName)
- {
- delete att->removeNext();
- break;
- }
- }
- }
-
- void XmlElement::removeAllAttributes() noexcept
- {
- attributes.deleteAll();
- }
-
- //==============================================================================
- int XmlElement::getNumChildElements() const noexcept
- {
- return firstChildElement.size();
- }
-
- XmlElement* XmlElement::getChildElement (const int index) const noexcept
- {
- return firstChildElement [index].get();
- }
-
- XmlElement* XmlElement::getChildByName (StringRef childName) const noexcept
- {
- wassert (! childName.isEmpty());
-
- for (XmlElement* child = firstChildElement; child != nullptr; child = child->nextListItem)
- if (child->hasTagName (childName))
- return child;
-
- return nullptr;
- }
-
- XmlElement* XmlElement::getChildByAttribute (StringRef attributeName, StringRef attributeValue) const noexcept
- {
- wassert (! attributeName.isEmpty());
-
- for (XmlElement* child = firstChildElement; child != nullptr; child = child->nextListItem)
- if (child->compareAttribute (attributeName, attributeValue))
- return child;
-
- return nullptr;
- }
-
- void XmlElement::addChildElement (XmlElement* const newNode) noexcept
- {
- if (newNode != nullptr)
- {
- // The element being added must not be a child of another node!
- wassert (newNode->nextListItem == nullptr);
-
- firstChildElement.append (newNode);
- }
- }
-
- void XmlElement::insertChildElement (XmlElement* const newNode, int indexToInsertAt) noexcept
- {
- if (newNode != nullptr)
- {
- // The element being added must not be a child of another node!
- wassert (newNode->nextListItem == nullptr);
-
- firstChildElement.insertAtIndex (indexToInsertAt, newNode);
- }
- }
-
- void XmlElement::prependChildElement (XmlElement* newNode) noexcept
- {
- if (newNode != nullptr)
- {
- // The element being added must not be a child of another node!
- wassert (newNode->nextListItem == nullptr);
-
- firstChildElement.insertNext (newNode);
- }
- }
-
- XmlElement* XmlElement::createNewChildElement (StringRef childTagName)
- {
- XmlElement* const newElement = new XmlElement (childTagName);
- addChildElement (newElement);
- return newElement;
- }
-
- bool XmlElement::replaceChildElement (XmlElement* const currentChildElement,
- XmlElement* const newNode) noexcept
- {
- if (newNode != nullptr)
- {
- if (LinkedListPointer<XmlElement>* const p = firstChildElement.findPointerTo (currentChildElement))
- {
- if (currentChildElement != newNode)
- delete p->replaceNext (newNode);
-
- return true;
- }
- }
-
- return false;
- }
-
- void XmlElement::removeChildElement (XmlElement* const childToRemove,
- const bool shouldDeleteTheChild) noexcept
- {
- if (childToRemove != nullptr)
- {
- firstChildElement.remove (childToRemove);
-
- if (shouldDeleteTheChild)
- delete childToRemove;
- }
- }
-
- bool XmlElement::isEquivalentTo (const XmlElement* const other,
- const bool ignoreOrderOfAttributes) const noexcept
- {
- if (this != other)
- {
- if (other == nullptr || tagName != other->tagName)
- return false;
-
- if (ignoreOrderOfAttributes)
- {
- int totalAtts = 0;
-
- for (const XmlAttributeNode* att = attributes; att != nullptr; att = att->nextListItem)
- {
- if (! other->compareAttribute (att->name, att->value))
- return false;
-
- ++totalAtts;
- }
-
- if (totalAtts != other->getNumAttributes())
- return false;
- }
- else
- {
- const XmlAttributeNode* thisAtt = attributes;
- const XmlAttributeNode* otherAtt = other->attributes;
-
- for (;;)
- {
- if (thisAtt == nullptr || otherAtt == nullptr)
- {
- if (thisAtt == otherAtt) // both nullptr, so it's a match
- break;
-
- return false;
- }
-
- if (thisAtt->name != otherAtt->name
- || thisAtt->value != otherAtt->value)
- {
- return false;
- }
-
- thisAtt = thisAtt->nextListItem;
- otherAtt = otherAtt->nextListItem;
- }
- }
-
- const XmlElement* thisChild = firstChildElement;
- const XmlElement* otherChild = other->firstChildElement;
-
- for (;;)
- {
- if (thisChild == nullptr || otherChild == nullptr)
- {
- if (thisChild == otherChild) // both 0, so it's a match
- break;
-
- return false;
- }
-
- if (! thisChild->isEquivalentTo (otherChild, ignoreOrderOfAttributes))
- return false;
-
- thisChild = thisChild->nextListItem;
- otherChild = otherChild->nextListItem;
- }
- }
-
- return true;
- }
-
- void XmlElement::deleteAllChildElements() noexcept
- {
- firstChildElement.deleteAll();
- }
-
- void XmlElement::deleteAllChildElementsWithTagName (StringRef name) noexcept
- {
- for (XmlElement* child = firstChildElement; child != nullptr;)
- {
- XmlElement* const nextChild = child->nextListItem;
-
- if (child->hasTagName (name))
- removeChildElement (child, true);
-
- child = nextChild;
- }
- }
-
- bool XmlElement::containsChildElement (const XmlElement* const possibleChild) const noexcept
- {
- return firstChildElement.contains (possibleChild);
- }
-
- XmlElement* XmlElement::findParentElementOf (const XmlElement* const elementToLookFor) noexcept
- {
- if (this == elementToLookFor || elementToLookFor == nullptr)
- return nullptr;
-
- for (XmlElement* child = firstChildElement; child != nullptr; child = child->nextListItem)
- {
- if (elementToLookFor == child)
- return this;
-
- if (XmlElement* const found = child->findParentElementOf (elementToLookFor))
- return found;
- }
-
- return nullptr;
- }
-
- void XmlElement::getChildElementsAsArray (XmlElement** elems) const noexcept
- {
- firstChildElement.copyToArray (elems);
- }
-
- void XmlElement::reorderChildElements (XmlElement** const elems, const int num) noexcept
- {
- XmlElement* e = firstChildElement = elems[0];
-
- for (int i = 1; i < num; ++i)
- {
- e->nextListItem = elems[i];
- e = e->nextListItem;
- }
-
- e->nextListItem = nullptr;
- }
-
- //==============================================================================
- bool XmlElement::isTextElement() const noexcept
- {
- return tagName.isEmpty();
- }
-
- static const String water_xmltextContentAttributeName ()
- {
- return String ("text");
- }
-
- const String& XmlElement::getText() const noexcept
- {
- wassert (isTextElement()); // you're trying to get the text from an element that
- // isn't actually a text element.. If this contains text sub-nodes, you
- // probably want to use getAllSubText instead.
-
- return getStringAttribute (water_xmltextContentAttributeName());
- }
-
- void XmlElement::setText (const String& newText)
- {
- CARLA_SAFE_ASSERT_RETURN(isTextElement(),);
- setAttribute (water_xmltextContentAttributeName(), newText);
- }
-
- String XmlElement::getAllSubText() const
- {
- if (isTextElement())
- return getText();
-
- if (getNumChildElements() == 1)
- return firstChildElement.get()->getAllSubText();
-
- MemoryOutputStream mem (1024);
-
- for (const XmlElement* child = firstChildElement; child != nullptr; child = child->nextListItem)
- mem << child->getAllSubText();
-
- return mem.toUTF8();
- }
-
- String XmlElement::getChildElementAllSubText (StringRef childTagName, const String& defaultReturnValue) const
- {
- if (const XmlElement* const child = getChildByName (childTagName))
- return child->getAllSubText();
-
- return defaultReturnValue;
- }
-
- XmlElement* XmlElement::createTextElement (const String& text)
- {
- XmlElement* const e = new XmlElement ((int) 0);
- e->setAttribute (water_xmltextContentAttributeName(), text);
- return e;
- }
-
- bool XmlElement::isValidXmlName (StringRef text) noexcept
- {
- if (text.isEmpty() || ! isValidXmlNameStartCharacter (text.text.getAndAdvance()))
- return false;
-
- for (;;)
- {
- if (text.isEmpty())
- return true;
-
- if (! isValidXmlNameBodyCharacter (text.text.getAndAdvance()))
- return false;
- }
- }
-
- void XmlElement::addTextElement (const String& text)
- {
- addChildElement (createTextElement (text));
- }
-
- void XmlElement::deleteAllTextElements() noexcept
- {
- for (XmlElement* child = firstChildElement; child != nullptr;)
- {
- XmlElement* const next = child->nextListItem;
-
- if (child->isTextElement())
- removeChildElement (child, true);
-
- child = next;
- }
- }
-
- }
|