GXml.cpp 31 KB
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 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999
/***************************************************************************
 *                          GXml.cpp - XML class                           *
 * ----------------------------------------------------------------------- *
 *  copyright (C) 2010-2013 by Juergen Knoedlseder                         *
 * ----------------------------------------------------------------------- *
 *                                                                         *
 *  This program is free software: you can redistribute it and/or modify   *
 *  it under the terms of the GNU General Public License as published by   *
 *  the Free Software Foundation, either version 3 of the License, or      *
 *  (at your option) any later version.                                    *
 *                                                                         *
 *  This program is distributed in the hope that it will be useful,        *
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of         *
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
 *  GNU General Public License for more details.                           *
 *                                                                         *
 *  You should have received a copy of the GNU General Public License      *
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.  *
 *                                                                         *
 ***************************************************************************/
/**
 * @file GXml.cpp
 * @brief XML class implementation
 * @author Juergen Knoedlseder
 */

/* __ Includes ___________________________________________________________ */
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "GUrlFile.hpp"
#include "GUrlString.hpp"
#include "GXml.hpp"
#include "GXmlNode.hpp"
#include "GXmlDocument.hpp"
#include "GXmlText.hpp"
#include "GXmlElement.hpp"
#include "GXmlComment.hpp"
#include "GXmlPI.hpp"
#include "GException.hpp"
#include "GTools.hpp"

/* __ Method name definitions ____________________________________________ */
#define G_LOAD                                     "GXml::load(std::string&)"
#define G_PARSE                                          "GXml::parse(GUrl&)"
#define G_PROCESS              "GXml::process(GXmlNode*, const std::string&)"

/* __ Macros _____________________________________________________________ */

/* __ Coding definitions _________________________________________________ */

/* __ Debug definitions __________________________________________________ */


/*==========================================================================
 =                                                                         =
 =                        Constructors/destructors                         =
 =                                                                         =
 ==========================================================================*/

/***********************************************************************//**
 * @brief Void constructor
 ***************************************************************************/
GXml::GXml(void)
{
    // Initialise members
    init_members();

    // Return
    return;
}


/***********************************************************************//**
 * @brief Copy constructor
 *
 * @param[in] xml XML object.
 ***************************************************************************/
GXml::GXml(const GXml& xml)
{
    // Initialise members
    init_members();

    // Copy members
    copy_members(xml);

    // Return
    return;
}


/***********************************************************************//**
 * @brief XML document constructor
 *
 * @param[in] xml XML text string or file name.
 *
 * Constructs GXml object by either parsing a text string or a file. If the
 * @p xml argument starts with @p <?xml it is interpreted as a XML file and
 * parsed directly. Otherwise the constructor will interpret @p xml as a
 * filename, and opens the file for parsing.
 ***************************************************************************/
GXml::GXml(const std::string& xml)
{
    // Initialise members
    init_members();

    // If the string is an XML text then parse it directly
    if (xml.compare(0, 5, "<?xml") == 0) {
        GUrlString url(xml);
        read(url);
        url.close();
    }

    // ... otherwise interpret the string as a filename
    else {
        load(xml);
    }

    // Return
    return;
}


/***********************************************************************//**
 * @brief Destructor
 ***************************************************************************/
GXml::~GXml(void)
{
    // Free members
    free_members();

    // Return
    return;
}


/*==========================================================================
 =                                                                         =
 =                                Operators                                =
 =                                                                         =
 ==========================================================================*/

/***********************************************************************//**
 * @brief Assignment operator
 *
 * @param[in] xml XML object.
 * @return XML object.
 ***************************************************************************/
GXml& GXml::operator=(const GXml& xml)
{
    // Execute only if object is not identical
    if (this != &xml) {

        // Free members
        free_members();

        // Initialise members
        init_members();

        // Copy members
        copy_members(xml);

    } // endif: object was not identical

    // Return
    return *this;
}


/***********************************************************************//**
 * @brief Return pointer to child of XML document root element
 *
 * @param[in] index Node index [0,...,size()-1].
 * @return Pointer to child of XML document root element.
 *
 * Returns a pointer to the child number @p index of the XML document root
 * element. An exception will be thrown if the @p index is not valid.
 ***************************************************************************/
GXmlNode* GXml::operator[](const int& index)
{
    // Return pointer
    return m_root[index];
}


/***********************************************************************//**
 * @brief Return pointer to child of XML document root element (const variant)
 *
 * @param[in] index Node index [0,...,size()-1].
 * @return Pointer to child of XML document root element.
 *
 * Returns a pointer to the child number @p index of the XML document root
 * element. An exception will be thrown if the @p index is not valid.
 ***************************************************************************/
const GXmlNode* GXml::operator[](const int& index) const
{
    // Return pointer
    return m_root[index];
}


/*==========================================================================
 =                                                                         =
 =                             Public methods                              =
 =                                                                         =
 ==========================================================================*/

/***********************************************************************//**
 * @brief Clear XML object
 *
 * Resets XML object to a clean initial state.
 ***************************************************************************/
void GXml::clear(void)
{
    // Free memory and initialise members
    free_members();
    init_members();

    // Return
    return;
}


/***********************************************************************//**
 * @brief Clone XML object
 *
 * @return Pointer to deep copy of XML object.
 ***************************************************************************/
GXml* GXml::clone(void) const
{
    // Clone object
    return new GXml(*this);
}


/***********************************************************************//**
 * @brief Set child node in XML document root
 *
 * @param[in] index Child node index [0,...,size()-1].
 * @param[in] node XML child node.
 * @return Pointer to deep copy of child node.
 *
 * Set @p node with @p index of XML document root.
 ***************************************************************************/
GXmlNode* GXml::set(const int& index, const GXmlNode& node)
{
    // Set node and return pointer
    return (m_root.set(index, node));
}


/***********************************************************************//**
 * @brief Append child node to XML document root
 *
 * @param[in] node Child node.
 * @return Pointer to appended child node.
 *
 * Appends node to XML document root by making a deep copy of the @p node.
 ***************************************************************************/
GXmlNode* GXml::append(const GXmlNode& node)
{
    // Append node and return pointer
    return (m_root.append(node));
}


/***********************************************************************//**
 * @brief Append child node to XML document root
 *
 * @param[in] segment XML child node.
 * @return Pointer to appended child node.
 *
 * Appends XML element that is constructed from a text @p segment. The text
 * segment is parsed and the element name and attributes are extracted using
 * the GXmlElement::parse_start() method. The method returns a pointer to the
 * XML element child node that has been appended.
 ***************************************************************************/
GXmlElement* GXml::append(const std::string& segment)
{
    // Append node and return pointer
    return (m_root.append(segment));
}


/***********************************************************************//**
 * @brief Insert child node into XML document root
 *
 * @param[in] index Child node index [0,...,size()-1].
 * @param[in] node XML child node.
 * @return Pointer to inserted child node.
 *
 * Inserts the XML child @p node before the node with the specified @p index.
 * A deep copy of the node will be made and the pointer to this node will be
 * stored.
 ***************************************************************************/
GXmlNode* GXml::insert(const int& index, const GXmlNode& node)
{
    // Insert node and return pointer
    return (m_root.insert(index, node));
}


/***********************************************************************//**
 * @brief Remove child node from XML document root
 *
 * @param[in] index Child node index [0,...,size()-1].
 *
 * Remove XML child node at @p index from the XML document root.
 ***************************************************************************/
void GXml::remove(const int& index)
{
    // Remove node
    m_root.remove(index);

    // Return
    return;
}


/***********************************************************************//**
 * @brief Reserve space for child nodes in XML document root
 *
 * @param[in] num Number of nodes.
 *
 * Reserves space for @p num nodes in the XML document root.
 ***************************************************************************/
void GXml::reserve(const int& num)
{
    // Reservers space node
    m_root.reserve(num);

    // Return
    return;
}


/***********************************************************************//**
 * @brief Append all XML child nodes from another XML node in the XML
 *        document root
 *
 * @param[in] node XML child node.
 *
 * Append all XML child nodes found in @p node to the XML document root.
 * Nodes are copied deeply so that they live now on their on in the actual
 * object.
 ***************************************************************************/
void GXml::extend(const GXmlNode& node)
{
    // Extend node
    m_root.extend(node);

    // Return
    return;
}


/***********************************************************************//**
 * @brief Return number of GXMLElement children in XML document root
 *
 * @return Number of GXMLElement children in XML document root.
 *
 * Returns the number of GXMLElement child elements of the XML document root.
 * GXMLElement child elements are nodes of type NT_ELEMENT.
 ***************************************************************************/
int GXml::elements(void) const
{
    // Return number
    return m_root.elements();
}


/***********************************************************************//**
 * @brief Return number of GXMLElement children with a given name in XML
 *        document root
 *
 * @param[in] name Name of child elements.
 * @return Number of GXMLElement children with a given @p name in XML
 *         document root.
 *
 * Returns the number of GXMLElement child elements of the XML document root
 * that have a given @p name. GXMLElement child elements are nodes of type
 * NT_ELEMENT.
 ***************************************************************************/
int GXml::elements(const std::string& name) const
{
    // Return number
    return m_root.elements(name);
}


/***********************************************************************//**
 * @brief Return pointer to GXMLElement child
 *
 * @param[in] index Node index [0,...,elements()-1].
 * @return Pointer to GXMLElement child.
 *
 * Returns a pointer to the child number @p index of the XML document root.
 * An exception will be thrown if the @p index is not valid.
 ***************************************************************************/
GXmlElement* GXml::element(const int& index)
{
    // Return pointer
    return m_root.element(index);
}


/***********************************************************************//**
 * @brief Return pointer to GXMLElement child (const variant)
 *
 * @param[in] index Node index [0,...,elements()-1].
 * @return Pointer to GXMLElement child.
 *
 * Returns a pointer to the child number @p index of the XML document root.
 * An exception will be thrown if the @p index is not valid.
 ***************************************************************************/
const GXmlElement* GXml::element(const int& index) const
{
    // Return pointer
    return m_root.element(index);
}


/***********************************************************************//**
 * @brief Return pointer on GXMLElement child of a given name
 *
 * @param[in] name Name of child element.
 * @param[in] index Node index [0,...,elements()-1].
 * @return Pointer to GXMLElement child.
 *
 * Returns a pointer to the child number @p index with @p name of the XML
 * document root. An exception will be thrown if the @p index is not valid.
 ***************************************************************************/
GXmlElement* GXml::element(const std::string& name, const int& index)
{
    // Return pointer
    return m_root.element(name, index);
}


/***********************************************************************//**
 * @brief Return pointer on GXMLElement child of a given name (const variant)
 *
 * @param[in] name Name of child element.
 * @param[in] index Node index [0,...,elements()-1].
 * @return Pointer to GXMLElement child.
 *
 * Returns a pointer to the child number @p index with @p name of the XML
 * document root. An exception will be thrown if the @p index is not valid.
 ***************************************************************************/
const GXmlElement* GXml::element(const std::string& name, const int& index) const
{
    // Return pointer
    return m_root.element(name, index);
}


/***********************************************************************//**
 * @brief Load XML document from file
 *
 * @param[in] filename File name.
 *
 * Loads a XML document from a file by reading from the file's Unified
 * Resource Locator (URL). The read() method is invoked for this purpose.
 *
 * The method uses the GUrlFile file opening constructor to open the URL.
 * This constructor will automatically expand any environment variables that
 * are present in the filename.
 *
 * @todo Ideally, we would like to extract the URL type from the filename
 * so that any kind of URL can be used for loading.
 ***************************************************************************/
void GXml::load(const std::string& filename)
{
    // Open XML URL as file for reading
    GUrlFile url(filename.c_str(), "r");

    // Read XML document from URL
    read(url);

    // Close URL
    url.close();

    // Return
    return;
}


/***********************************************************************//**
 * @brief Save XML document into file
 *
 * @param[in] filename File name.
 *
 * Saves the XML document into a file by writing into the file's Unified
 * Resource Locator (URL). The write() method is invoked for this purpose.
 *
 * The method uses the GUrlFile file opening constructor to open the URL.
 * This constructor will automatically expand any environment variables that
 * are present in the filename.
 *
 * @todo Ideally, we would like to extract the URL type from the filename
 * so that any kind of URL can be used for loading.
 ***************************************************************************/
void GXml::save(const std::string& filename)
{
    // Open XML file for writing
    GUrlFile url(filename.c_str(), "w");

    // Write XML document
    write(url, 0);

    // Close file
    url.close();

    // Return
    return;
}


/***********************************************************************//**
 * @brief Read XML document from URL
 *
 * @param[in] url Unified Resource Locator.
 *
 * Reads in the XML document by parsing a Unified Resource Locator of any
 * type.
 ***************************************************************************/
void GXml::read(const GUrl& url)
{
    // Clear object
    clear();

    // Parse URL
    parse(url);

    // Return
    return;
}


/***********************************************************************//**
 * @brief Write XML document into URL
 *
 * @param[in] url Unified Resource Locator.
 * @param[in] indent Indentation (default = 0).
 *
 * Writes the XML document in a Unified Resource Locator. Formatting of the
 * document can be adapted using the @p indent parameter.
 ***************************************************************************/
void GXml::write(GUrl& url, const int& indent) const
{
    // Write XML document
    m_root.write(url);

    // Return
    return;
}


/***********************************************************************//**
 * @brief Print XML object
 *
 * @param[in] chatter Chattiness (defaults to NORMAL).
 * @param[in] indent Text indentation (default to 0).
 * @return String containing XML object.
 ***************************************************************************/
std::string GXml::print(const GChatter& chatter, const int& indent) const
{
    // Initialise result string
    std::string result;

    // Continue only if chatter is not silent
    if (chatter != SILENT) {

        // Append header
        result.append("=== GXml ===");

        // Append model
        result.append("\n"+m_root.print(chatter, 0));

    } // endif: chatter was not silent

    // Return result
    return result;
}


/***********************************************************************//**
 * @brief Print XML object
 *
 * @param[in] chatter Chattiness (defaults to NORMAL).
 * @return String containing XML object.
 ***************************************************************************/
std::string GXml::print(const GChatter& chatter) const
{
    // Set result string
    std::string result = print(chatter, 0);

    // Return result
    return result;
}


/*==========================================================================
 =                                                                         =
 =                             Private methods                             =
 =                                                                         =
 ==========================================================================*/

/***********************************************************************//**
 * @brief Initialise class members
 ***************************************************************************/
void GXml::init_members(void)
{
    // Initialise members
    m_root.clear();

    // Return
    return;
}


/***********************************************************************//**
 * @brief Copy class members
 *
 * @param[in] xml Object from which members which should be copied.
 ***************************************************************************/
void GXml::copy_members(const GXml& xml)
{
    // Copy attributes
    m_root = xml.m_root;

    // Return
    return;
}


/***********************************************************************//**
 * @brief Delete class members
 ***************************************************************************/
void GXml::free_members(void)
{
    // Return
    return;
}


/***********************************************************************//**
 * @brief Parse XML URL
 *
 * @param[in] url Unified Resource Locator.
 *
 * @exception GException::xml_syntax_error
 *            XML syntax error.
 *
 * Parses either a XML file or a XML text string and creates all associated
 * nodes. The XML file is split into segments, made either of text or of
 * tags.
 ***************************************************************************/
void GXml::parse(const GUrl& url)
{
    // Initialise parser
    int         c;
    bool        in_markup  = false;
    bool        in_comment = false;
    std::string segment;
    GXmlNode*   current = &m_root;

    // Main parsing loop
    while ((c = url.get_char()) != EOF) {

        // Convert special characters into line feeds
        if (c == '\x85' || c == L'\x2028') {
            if (in_markup) {
                 throw GException::xml_syntax_error(G_PARSE, segment,
                                   "invalid character encountered");
            }
            else {
                c = '\x0a';
            }
        }

        // If we are not within a markup and if a markup is reached then add
        // the text segment to the nodes and switch to in_markup mode
        if (in_markup == false) {

            // Markup start reached?
            if (c == '<') {

                // Add text segment to nodes (ignores empty segments)
                process_text(&current, segment);

                // Prepare new segment and signal that we are within tag
                segment.clear();
                segment.append(1, (char)c);
                in_markup = true;

            }

            // Markup stop encountered?
            else if (c == '>') {
                 segment.append(1, (char)c);
                 throw GException::xml_syntax_error(G_PARSE, segment,
                       "unexpected closing bracket \">\" encountered");
            }

            // ... otherwise add character to segment
            else {
                segment.append(1, (char)c);
            }
        }

        // If we are within a markup and if a markup end is reached then process
        // the markup and switch to not in_tag mode
        else {

            // Markup stop reached?
            if (c == '>') {

                // Append character to segment
                segment.append(1, (char)c);

                // If we are in comment then check if this is the end of
                // the comment
                if (in_comment) {
                    int n = segment.length();
                    if (n > 2) {
                        if (segment.compare(n-3,3,"-->") == 0) {
                            in_comment = false;
                        }
                    }
                }

                // If we are not in the comment, then process markup
                if (!in_comment) {

                    // Process markup
                    process_markup(&current, segment);

                    // Prepare new segment and signal that we are not
                    // within markup
                    segment.clear();
                    in_markup  = false;
                }
            }

            // Markup start encountered?
            else if (!in_comment && c == '<') {

                // Append character to segment
                segment.append(1, (char)c);

                // If we encounter an opening bracket then throw an exception
                throw GException::xml_syntax_error(G_PARSE, segment,
                      "unexpected opening bracket \"<\" encountered");
            }

            // ... otherwise add character to segment
            else {
                segment.append(1, (char)c);
                if (!in_comment && segment == "<!--") {
                    in_comment = true;
                }
            }
        }

    } // endwhile: main parsing loop

    // Process any pending segment
    if (segment.size() > 0) {
        if (in_markup) {
            process_markup(&current, segment);
        }
        else {
            process_text(&current, segment);
        }
    }

    // Verify that we are back to the root node
    if (current != &m_root) {
        std::string message = "closing tag ";
        GXmlElement* element = dynamic_cast<GXmlElement*>(current);
        if (element != NULL) {
            message += "for GXmlElement \""+element->name()+"\"";
        }
        message += " is missing";
        throw GException::xml_syntax_error(G_PARSE, "", message);
    }

    // Return
    return;
}


/***********************************************************************//**
 * @brief Process markup segment
 *
 * @param[in] current Handle to current node.
 * @param[in] segment Segment string.
 *
 * Process markup segment.
 ***************************************************************************/
void GXml::process_markup(GXmlNode** current, const std::string& segment)
{
    // Determine segment tag type
    MarkupType type = get_markuptype(segment);

    // Do tag specific processing
    switch (type) {

    // Handle element start tag
    case MT_ELEMENT_START:
        {
            // Create new element node, set it's parent, append it to the
            // current node and make it the current node
            GXmlElement element(segment);
            element.parent(*current);
            (*current)->append(element);
            int last = (*current)->size() - 1;
            (*current) = (*(*current))[last];
        }
        break;

    // Handle element end tag
    case MT_ELEMENT_END:
        {
            // Check if we expect an element end tag
            if ((*current)->type() != GXmlNode::NT_ELEMENT) {
                throw GException::xml_syntax_error(G_PROCESS, segment,
                      "unexpected element end tag encountered");
            }

            // Check if we have the correct end tag
            GXmlElement* element = (GXmlElement*)(*current);
            element->parse_stop(segment);

            // Set current node pointer back to parent of the current node
            (*current) = element->parent();
        }
        break;

    // Append empty-element tag
    case MT_ELEMENT_EMPTY:
        {
            GXmlElement element(segment);
            element.parent(*current);
            (*current)->append(element);
        }
        break;

    // Append comment markup
    case MT_COMMENT:
        {
            GXmlComment comment(segment);
            (*current)->append(comment);
        }
        break;

    // Declaration markup
    case MT_DECLARATION:
        {
            // Verify if declaration tag is allowed
            if (*current != &m_root) {
                throw GException::xml_syntax_error(G_PROCESS, segment,
                      "unexpected declaration markup encountered");
            }
            if (!m_root.is_empty()) {
                throw GException::xml_syntax_error(G_PROCESS, segment,
                      "declaration markup only allowed in first line");
            }

            // Create temporary element to read in declaration attributes
            GXmlElement* element = new GXmlElement(segment);
            size_t       pos     = 5;
            while (pos != std::string::npos) {
                element->parse_attribute(&pos, segment);
            }

            // Set attribute values
            std::string version    = element->attribute("version");
            std::string encoding   = element->attribute("encoding");
            std::string standalone = element->attribute("standalone");
            if (version.length() > 0) {
                m_root.version(version);
            }
            if (encoding.length() > 0) {
                m_root.encoding(encoding);
            }
            if (standalone.length() > 0) {
                m_root.standalone(standalone);
            }

            // Delete temporary element
            delete element;
        }
        break;

    // Processing tag
    case MT_PROCESSING:
        {
            GXmlPI pi(segment);
            (*current)->append(pi);
        }
        break;

    // Invalid tag, throw an error
    case MT_INVALID:
        throw GException::xml_syntax_error(G_PROCESS, segment, "invalid tag");
        break;
    }

    // Return
    return;
}


/***********************************************************************//**
 * @brief Process text segment
 *
 * @param[in] current Handle to current node.
 * @param[in] segment Segment string.
 *
 * Process text segment.
 ***************************************************************************/
void GXml::process_text(GXmlNode** current, const std::string& segment)
{
    // Continue only if text segment is not empty
    if (segment.size() > 0) {

        // Continue only if non whitespace characters are found
        size_t pos = segment.find_first_not_of("\x20\x09\x0d\x0a\x85");
        if (pos != std::string::npos) {

            // Allocate and append node
            GXmlText* node = new GXmlText(segment);
            (*current)->append(*node);

        } // endif: there was not only whitespace

    } // endif: segment was not empty

    // Return
    return;
}


/***********************************************************************//**
 * @brief Get Markup type of segment
 *
 * @param[in] segment Segment for which Markup Type should be determined.
 *
 * Returns Markup Type of segment.
 ***************************************************************************/
GXml::MarkupType GXml::get_markuptype(const std::string& segment) const
{
    // Initialise with invalid Markup Type
    MarkupType type = MT_INVALID;

    // Get length of segment
    int n = segment.length();

    // Check for comment
    if (n >= 7 && (segment.compare(0,4,"<!--") == 0) &&
                  (segment.compare(n-3,3,"-->") == 0)) {
        type = MT_COMMENT;
    }

    // Check for declaration
    else if (n >= 7 && (segment.compare(0,6,"<?xml ") == 0) &&
                       (segment.compare(n-2,2,"?>") == 0)) {
        type = MT_DECLARATION;
    }

    // Check for processing instruction
    else if (n >= 4 && (segment.compare(0,2,"<?") == 0) &&
                       (segment.compare(n-2,2,"?>") == 0)) {
        type = MT_PROCESSING;
    }

    // Check for empty element tag
    else if (n >= 3 && (segment.compare(0,1,"<") == 0) &&
                       (segment.compare(n-2,2,"/>") == 0)) {
        type = MT_ELEMENT_EMPTY;
    }

    // Check for element end tag
    else if (n >= 3 && (segment.compare(0,2,"</") == 0) &&
                       (segment.compare(n-1,1,">") == 0)) {
        type = MT_ELEMENT_END;
    }

    // Check for element start tag
    else if (n >= 2 && (segment.compare(0,1,"<") == 0) &&
                       (segment.compare(n-1,1,">") == 0)) {
        type = MT_ELEMENT_START;
    }

    // Return type
    return type;
}