Fix a type-punning warning.
[asdcplib-cth.git] / src / KM_xml.h
1 /*
2 Copyright (c) 2005-2015, 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: KM_xml.h,v 1.11 2015/02/19 19:06:57 jhurst Exp $
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   bool GetXMLDocType(const ByteString& buf, std::string& ns_prefix, std::string& type_name,
58                      std::string& namespace_name, AttributeList& doc_attr_list);
59
60   bool GetXMLDocType(const std::string& buf, std::string& ns_prefix, std::string& type_name,
61                      std::string& namespace_name, AttributeList& doc_attr_list);
62
63   bool GetXMLDocType(const byte_t* buf, ui32_t buf_len, std::string& ns_prefix, std::string& type_name,
64                      std::string& namespace_name, AttributeList& doc_attr_list);
65
66   //
67   class XMLNamespace
68   {
69     std::string   m_Prefix;
70     std::string   m_Name;
71
72     KM_NO_COPY_CONSTRUCT(XMLNamespace);
73     XMLNamespace();
74
75     public:
76   XMLNamespace(const char* prefix, const char* name) : m_Prefix(prefix), m_Name(name) {}
77     ~XMLNamespace() {}
78
79     inline const std::string& Prefix() const { return m_Prefix; }
80     inline const std::string& Name() const { return m_Name; }
81   };
82
83   //
84   class XMLElement
85     {
86       KM_NO_COPY_CONSTRUCT(XMLElement);
87       XMLElement();
88
89     protected:
90       AttributeList       m_AttrList;
91       ElementList         m_ChildList;
92       const XMLNamespace* m_Namespace;
93       void*               m_NamespaceOwner;
94
95       std::string   m_Name;
96       std::string   m_Body;
97
98     public:
99       XMLElement(const char* name);
100       ~XMLElement();
101
102       inline const XMLNamespace* Namespace() const { return m_Namespace; }
103       inline void                SetNamespace(const XMLNamespace* ns) { assert(ns); m_Namespace = ns; }
104
105       bool        ParseString(const char* document, ui32_t doc_len);
106       bool        ParseString(const ByteString& document);
107       bool        ParseString(const std::string& document);
108
109       bool        ParseFirstFromString(const char* document, ui32_t doc_len);
110       bool        ParseFirstFromString(const ByteString& document);
111       bool        ParseFirstFromString(const std::string& document);
112
113       // building
114       void        SetName(const char* name);
115       void        SetBody(const std::string& value);
116       void        AppendBody(const std::string& value);
117       void        SetAttr(const char* name, const char* value);
118       void        SetAttr(const char* name, const std::string& value) { SetAttr(name, value.c_str()); }
119       XMLElement* AddChild(XMLElement* element);
120       XMLElement* AddChild(const char* name);
121       XMLElement* AddChildWithContent(const char* name, const char* value);
122       XMLElement* AddChildWithContent(const char* name, const std::string& value);
123       XMLElement* AddChildWithPrefixedContent(const char* name, const char* prefix, const char* value);
124       void        AddComment(const char* value);
125       void        Render(std::string& str) const { Render(str, true); }
126       void        Render(std::string&, const bool& pretty) const;
127       void        RenderElement(std::string& outbuf, const ui32_t& depth, const bool& pretty) const;
128
129       // querying
130       inline const std::string&   GetBody() const { return m_Body; }
131       inline const ElementList&   GetChildren() const { return m_ChildList; }
132       inline const std::string&   GetName() const { return m_Name; }
133       inline const AttributeList& GetAttributes() const { return m_AttrList; }
134       const char*        GetAttrWithName(const char* name) const;
135       XMLElement*        GetChildWithName(const char* name) const;
136       const ElementList& GetChildrenWithName(const char* name, ElementList& outList) const;
137       bool               HasName(const char* name) const;
138
139       // altering
140       void        DeleteAttributes();
141       void        DeleteAttrWithName(const char* name);
142       void        DeleteChildren();
143       void        DeleteChild(const XMLElement* element);
144       void        ForgetChild(const XMLElement* element);
145     };
146 } // namespace Kumu
147
148 #endif // _KM_XML_H_
149
150 //
151 // end KM_xml.h
152 //