1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973
|
/*
==============================================================================
This file is part of the JUCE library.
Copyright (c) 2017 - ROLI Ltd.
JUCE is an open source library subject to commercial or open-source
licensing.
The code included in this file is provided 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.
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
DISCLAIMED.
==============================================================================
*/
namespace juce
{
inline static bool isValidXmlNameStartCharacter (juce_wchar 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 static bool isValidXmlNameBodyCharacter (juce_wchar 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)
{
jassert (isValidXmlName (name));
}
XmlElement::XmlAttributeNode::XmlAttributeNode (String::CharPointerType nameStart, String::CharPointerType nameEnd)
: name (nameStart, nameEnd)
{
jassert (isValidXmlName (name));
}
//==============================================================================
XmlElement::XmlElement (const String& tag)
: tagName (StringPool::getGlobalPool().getPooledString (tag))
{
jassert (isValidXmlName (tagName));
}
XmlElement::XmlElement (const char* tag)
: tagName (StringPool::getGlobalPool().getPooledString (tag))
{
jassert (isValidXmlName (tagName));
}
XmlElement::XmlElement (StringRef tag)
: tagName (StringPool::getGlobalPool().getPooledString (tag))
{
jassert (isValidXmlName (tagName));
}
XmlElement::XmlElement (const Identifier& tag)
: tagName (tag.toString())
{
jassert (isValidXmlName (tagName));
}
XmlElement::XmlElement (String::CharPointerType tagNameStart, String::CharPointerType tagNameEnd)
: tagName (StringPool::getGlobalPool().getPooledString (tagNameStart, tagNameEnd))
{
jassert (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;
}
XmlElement::XmlElement (XmlElement&& other) noexcept
: nextListItem (std::move (other.nextListItem)),
firstChildElement (std::move (other.firstChildElement)),
attributes (std::move (other.attributes)),
tagName (std::move (other.tagName))
{
}
XmlElement& XmlElement::operator= (XmlElement&& other) noexcept
{
jassert (this != &other); // hopefully the compiler should make this situation impossible!
removeAllAttributes();
deleteAllChildElements();
nextListItem = std::move (other.nextListItem);
firstChildElement = std::move (other.firstChildElement);
attributes = std::move (other.attributes);
tagName = std::move (other.tagName);
return *this;
}
void XmlElement::copyChildrenAndAttributesFrom (const XmlElement& other)
{
jassert (firstChildElement.get() == nullptr);
firstChildElement.addCopyOfList (other.firstChildElement);
jassert (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 juce_wchar character) noexcept
{
if ((character >= 'a' && character <= 'z')
|| (character >= 'A' && character <= 'Z')
|| (character >= '0' && character <= '9'))
return true;
const char* t = " .,;:-()_+=?!'#@[]/\\*%~{}$|";
do
{
if (((juce_wchar) (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)
{
auto t = text.getCharPointer();
for (;;)
{
auto 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;
}
// Note: Deliberate fall-through here!
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;
if (indentationLevel >= 0)
writeSpaces (outputStream, (size_t) indentationLevel);
if (! isTextElement())
{
outputStream.writeByte ('<');
outputStream << tagName;
{
auto attIndent = (size_t) (indentationLevel + tagName.length() + 1);
int lineLen = 0;
for (auto* att = attributes.get(); att != nullptr; att = att->nextListItem)
{
if (lineLen > lineWrapLength && indentationLevel >= 0)
{
outputStream << newLine;
writeSpaces (outputStream, attIndent);
lineLen = 0;
}
auto startPos = outputStream.getPosition();
outputStream.writeByte (' ');
outputStream << att->name;
outputStream.write ("=\"", 2);
escapeIllegalXmlChars (outputStream, att->value, true);
outputStream.writeByte ('"');
lineLen += (int) (outputStream.getPosition() - startPos);
}
}
if (auto* child = firstChildElement.get())
{
outputStream.writeByte ('>');
bool lastWasTextNode = false;
for (; 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,
bool allOnOneLine, bool includeXmlHeader,
StringRef encodingType, int lineWrapLength) const
{
using namespace XmlOutputFunctions;
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;
}
bool XmlElement::writeToFile (const File& file, StringRef dtdToUse,
StringRef encodingType, 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();
}
//==============================================================================
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.
jassert ((! 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
{
auto* e = nextListItem.get();
while (e != nullptr && ! e->hasTagName (requiredTagName))
e = e->nextListItem;
return e;
}
void XmlElement::setTagName (StringRef newTagName)
{
jassert (isValidXmlName (newTagName));
tagName = StringPool::getGlobalPool().getPooledString (newTagName);
}
//==============================================================================
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 (auto* att = attributes[index].get())
return att->name.toString();
return getEmptyStringRef();
}
const String& XmlElement::getAttributeValue (const int index) const noexcept
{
if (auto* att = attributes[index].get())
return att->value;
return getEmptyStringRef();
}
XmlElement::XmlAttributeNode* XmlElement::getAttribute (StringRef attributeName) const noexcept
{
for (auto* att = attributes.get(); 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 (auto* att = getAttribute (attributeName))
return att->value;
return getEmptyStringRef();
}
String XmlElement::getStringAttribute (StringRef attributeName, const String& defaultReturnValue) const
{
if (auto* att = getAttribute (attributeName))
return att->value;
return defaultReturnValue;
}
int XmlElement::getIntAttribute (StringRef attributeName, const int defaultReturnValue) const
{
if (auto* att = getAttribute (attributeName))
return att->value.getIntValue();
return defaultReturnValue;
}
double XmlElement::getDoubleAttribute (StringRef attributeName, const double defaultReturnValue) const
{
if (auto* att = getAttribute (attributeName))
return att->value.getDoubleValue();
return defaultReturnValue;
}
bool XmlElement::getBoolAttribute (StringRef attributeName, const bool defaultReturnValue) const
{
if (auto* att = getAttribute (attributeName))
{
auto 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 (auto* 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 (auto* att = attributes.get(); ; 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, serialiseDouble (number));
}
void XmlElement::removeAttribute (const Identifier& attributeName) noexcept
{
for (auto* 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
{
jassert (! childName.isEmpty());
for (auto* child = firstChildElement.get(); child != nullptr; child = child->nextListItem)
if (child->hasTagName (childName))
return child;
return nullptr;
}
XmlElement* XmlElement::getChildByAttribute (StringRef attributeName, StringRef attributeValue) const noexcept
{
jassert (! attributeName.isEmpty());
for (auto* child = firstChildElement.get(); 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!
jassert (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!
jassert (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!
jassert (newNode->nextListItem == nullptr);
firstChildElement.insertNext (newNode);
}
}
XmlElement* XmlElement::createNewChildElement (StringRef childTagName)
{
auto newElement = new XmlElement (childTagName);
addChildElement (newElement);
return newElement;
}
bool XmlElement::replaceChildElement (XmlElement* const currentChildElement,
XmlElement* const newNode) noexcept
{
if (newNode != nullptr)
{
if (auto* 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)
{
jassert (containsChildElement (childToRemove));
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 (auto* att = attributes.get(); att != nullptr; att = att->nextListItem)
{
if (! other->compareAttribute (att->name, att->value))
return false;
++totalAtts;
}
if (totalAtts != other->getNumAttributes())
return false;
}
else
{
auto* thisAtt = attributes.get();
auto* otherAtt = other->attributes.get();
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;
}
}
auto* thisChild = firstChildElement.get();
auto* otherChild = other->firstChildElement.get();
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 (auto* child = firstChildElement.get(); child != nullptr;)
{
auto* nextChild = child->nextListItem.get();
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 (auto* child = firstChildElement.get(); child != nullptr; child = child->nextListItem)
{
if (elementToLookFor == child)
return this;
if (auto* found = child->findParentElementOf (elementToLookFor))
return found;
}
return nullptr;
}
void XmlElement::getChildElementsAsArray (XmlElement** elems) const noexcept
{
firstChildElement.copyToArray (elems);
}
void XmlElement::reorderChildElements (XmlElement** elems, int num) noexcept
{
auto* e = elems[0];
firstChildElement = e;
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 juce_xmltextContentAttributeName ("text");
const String& XmlElement::getText() const noexcept
{
jassert (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 (juce_xmltextContentAttributeName);
}
void XmlElement::setText (const String& newText)
{
if (isTextElement())
setAttribute (juce_xmltextContentAttributeName, newText);
else
jassertfalse; // you can only change the text in a text element, not a normal one.
}
String XmlElement::getAllSubText() const
{
if (isTextElement())
return getText();
if (getNumChildElements() == 1)
return firstChildElement.get()->getAllSubText();
MemoryOutputStream mem (1024);
for (auto* child = firstChildElement.get(); child != nullptr; child = child->nextListItem)
mem << child->getAllSubText();
return mem.toUTF8();
}
String XmlElement::getChildElementAllSubText (StringRef childTagName, const String& defaultReturnValue) const
{
if (auto* child = getChildByName (childTagName))
return child->getAllSubText();
return defaultReturnValue;
}
XmlElement* XmlElement::createTextElement (const String& text)
{
auto e = new XmlElement ((int) 0);
e->setAttribute (juce_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 (auto* child = firstChildElement.get(); child != nullptr;)
{
auto* next = child->nextListItem.get();
if (child->isTextElement())
removeChildElement (child, true);
child = next;
}
}
//==============================================================================
#if JUCE_UNIT_TESTS
class XmlElementTests : public UnitTest
{
public:
XmlElementTests() : UnitTest ("XmlElement", "XML") {}
void runTest() override
{
{
beginTest ("Float formatting");
auto element = std::make_unique<XmlElement> ("test");
Identifier number ("number");
std::map<double, String> tests;
tests[1] = "1.0";
tests[1.1] = "1.1";
tests[1.01] = "1.01";
tests[0.76378] = "0.76378";
tests[-10] = "-10.0";
tests[10.01] = "10.01";
tests[0.0123] = "0.0123";
tests[-3.7e-27] = "-3.7e-27";
tests[1e+40] = "1.0e40";
tests[-12345678901234567.0] = "-1.234567890123457e16";
tests[192000] = "192000.0";
tests[1234567] = "1.234567e6";
tests[0.00006] = "0.00006";
tests[0.000006] = "6.0e-6";
for (auto& test : tests)
{
element->setAttribute (number, test.first);
expectEquals (element->getStringAttribute (number), test.second);
}
}
}
};
static XmlElementTests xmlElementTests;
#endif
} // namespace juce
|