//
// Copyright (c) 2004 Russell E. Gibson
// email: russg@rnstech.com
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is furnished
// to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//

#ifndef __ROOT_HPP__
#define __ROOT_HPP__

#include <string>
#include <vector>
#include <libxml/parser.h>
#include <libxml/xmlwriter.h>

namespace root
{
//=============================================================================
// Element 'child' encapsulation

    class Child
    {
    //-------------------------------------------------------------------------
    // Construction & operators
    public:
        Child();
        Child(const char* f);
        Child(xmlNodePtr n);
        Child(const Child& c);
        virtual ~Child() throw ();
       
        Child& operator=(const Child& r);

    //-------------------------------------------------------------------------
    // Attribute number
    public:
        const std::string& number() const;
        void number(const char* n);
       
    private:
        std::string _number;
       
    //-------------------------------------------------------------------------
    // Read from node
    public:
        void read(xmlNodePtr p);
       
    //-------------------------------------------------------------------------
    // Value of this element
    private:
        typedef std::pair<bool, std::string> VALUE;     // (element here?, text)
        typedef std::vector<VALUE> VALUES;
       
    public:
        VALUES& values();
        const VALUES& values() const;
        void value(std::string& v) const;
       
    private:
        VALUES _values;
       
    //-------------------------------------------------------------------------
    // Write to file or node
    public:
        void write(const char* f, bool i = true, const char* t = "\t") const;
        void write(xmlTextWriterPtr w) const;
    };

//=============================================================================
// Element 'element' encapsulation

    class Element
    {
    //-------------------------------------------------------------------------
    // Construction & operators
    public:
        Element();
        Element(const char* f);
        Element(xmlNodePtr n);
        Element(const Element& c);
        virtual ~Element() throw ();
       
        Element& operator=(const Element& r);

    //-------------------------------------------------------------------------
    // Child element child
    public:
        std::vector<Child>& childs();
        const std::vector<Child>& childs() const;
       
    private:
        std::vector<Child> _childs;
       
    //-------------------------------------------------------------------------
    // Read from node
    public:
        void read(xmlNodePtr p);
       
    //-------------------------------------------------------------------------
    // Value of this element
    private:
        typedef std::pair<bool, std::string> VALUE;     // (element here?, text)
        typedef std::vector<VALUE> VALUES;
       
    public:
        VALUES& values();
        const VALUES& values() const;
        void value(std::string& v) const;
       
    private:
        VALUES _values;
       
    //-------------------------------------------------------------------------
    // Write to file or node
    public:
        void write(const char* f, bool i = true, const char* t = "\t") const;
        void write(xmlTextWriterPtr w) const;
    };

//=============================================================================
// Element 'root' encapsulation

    class Root
    {
    //-------------------------------------------------------------------------
    // Construction & operators
    public:
        Root();
        Root(const char* f);
        Root(xmlNodePtr n);
        Root(const Root& c);
        virtual ~Root() throw ();
       
        Root& operator=(const Root& r);

    //-------------------------------------------------------------------------
    // Child element element
    public:
        std::vector<Element>& elements();
        const std::vector<Element>& elements() const;
       
    private:
        std::vector<Element> _elements;
       
    //-------------------------------------------------------------------------
    // Read from node
    public:
        void read(xmlNodePtr p);
       
    //-------------------------------------------------------------------------
    // Value of this element
    private:
        typedef std::pair<bool, std::string> VALUE;     // (element here?, text)
        typedef std::vector<VALUE> VALUES;
       
    public:
        VALUES& values();
        const VALUES& values() const;
        void value(std::string& v) const;
       
    private:
        VALUES _values;
       
    //-------------------------------------------------------------------------
    // Write to file or node
    public:
        void write(const char* f, bool i = true, const char* t = "\t") const;
        void write(xmlTextWriterPtr w) const;
    };
}

#include "root.inl"

#endif      // __ROOT_HPP__