31f506471cabacffe1e714a224e0590b1db172e9
[asdcplib.git] / src / KM_xml.h
1 /*
2 Copyright (c) 2005-2006, John Hurst
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions
7 are met:
8 1. Redistributions of source code must retain the above copyright
9    notice, this list of conditions and the following disclaimer.
10 2. Redistributions in binary form must reproduce the above copyright
11    notice, this list of conditions and the following disclaimer in the
12    documentation and/or other materials provided with the distribution.
13 3. The name of the author may not be used to endorse or promote products
14    derived from this software without specific prior written permission.
15
16 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27 /*! \file    KM_xml.h
28     \version $Id$
29     \brief   XML writer
30 */
31
32
33 #ifndef _KM_XML_H_
34 #define _KM_XML_H_
35
36 #include <KM_util.h>
37 #include <list>
38 #include <string>
39
40 namespace Kumu
41 {
42   class XMLElement;
43
44   //
45   struct NVPair
46   {
47     std::string name;
48     std::string value;
49   };
50
51   //
52   typedef std::list<NVPair> AttributeList;
53   typedef AttributeList::const_iterator Attr_i;
54   typedef std::list<XMLElement*> ElementList;
55   typedef ElementList::const_iterator Elem_i;
56
57   //
58   class XMLNamespace
59   {
60     std::string   m_Prefix;
61     std::string   m_Name;
62
63     KM_NO_COPY_CONSTRUCT(XMLNamespace);
64     XMLNamespace();
65
66     public:
67   XMLNamespace(const char* prefix, const char* name) : m_Prefix(prefix), m_Name(name) {}
68     ~XMLNamespace() {}
69
70     inline const std::string& Prefix() const { return m_Prefix; }
71     inline const std::string& Name() const { return m_Name; }
72   };
73
74   //
75   class XMLElement
76     {
77       AttributeList       m_AttrList;
78       ElementList         m_ChildList;
79       const XMLNamespace* m_Namespace;
80       void*               m_NamespaceOwner;
81
82       std::string   m_Name;
83       std::string   m_Body;
84
85       KM_NO_COPY_CONSTRUCT(XMLElement);
86       XMLElement();
87
88     public:
89       XMLElement(const char* name);
90       ~XMLElement();
91
92       inline const XMLNamespace* Namespace() const { return m_Namespace; }
93       inline void                SetNamespace(const XMLNamespace* ns) { assert(ns); m_Namespace = ns; }
94
95       bool        ParseString(const std::string& document);
96       bool        TestString(const char* document, ui32_t len = 0);
97
98       // building
99       void        SetName(const char* name);
100       void        AppendBody(const std::string& value);
101       void        SetAttr(const char* name, const char* value);
102       XMLElement* AddChild(const char* name);
103       XMLElement* AddChildWithContent(const char* name, const char* value);
104       XMLElement* AddChildWithContent(const char* name, const std::string& value);
105       XMLElement* AddChildWithPrefixedContent(const char* name, const char* prefix, const char* value);
106       void        AddComment(const char* value);
107       void        Render(std::string&) const;
108       void        RenderElement(std::string& outbuf, ui32_t depth) const;
109
110       // querying
111       inline const std::string& GetBody() const { return m_Body; }
112       const char*    GetAttrWithName(const char* name) const;
113       XMLElement*    GetChildWithName(const char* name) const;
114       const ElementList& GetChildrenWithName(const char* name, ElementList& outList) const;
115       bool           HasName(const char* name) const;
116
117     };
118 } // namespace Kumu
119
120 #endif // _KM_XML_H_
121
122 //
123 // end KM_xml.h
124 //