Implemented J2K desc to/from MD
[asdcplib.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$
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 <set>
39 #include <string>
40
41 namespace Kumu
42 {
43   class XMLElement;
44
45   //
46   struct NVPair
47   {
48     std::string name;
49     std::string value;
50   };
51
52   //
53   typedef std::list<NVPair> AttributeList;
54   typedef AttributeList::const_iterator Attr_i;
55   typedef std::list<XMLElement*> ElementList;
56   typedef ElementList::const_iterator Elem_i;
57
58   bool GetXMLDocType(const ByteString& buf, std::string& ns_prefix, std::string& type_name,
59                      std::string& namespace_name, AttributeList& doc_attr_list);
60
61   bool GetXMLDocType(const std::string& buf, std::string& ns_prefix, std::string& type_name,
62                      std::string& namespace_name, AttributeList& doc_attr_list);
63
64   bool GetXMLDocType(const byte_t* buf, ui32_t buf_len, std::string& ns_prefix, std::string& type_name,
65                      std::string& namespace_name, AttributeList& doc_attr_list);
66
67   //
68   class XMLNamespace
69   {
70     std::string   m_Prefix;
71     std::string   m_Name;
72
73     KM_NO_COPY_CONSTRUCT(XMLNamespace);
74     XMLNamespace();
75
76     public:
77   XMLNamespace(const char* prefix, const char* name) : m_Prefix(prefix), m_Name(name) {}
78     ~XMLNamespace() {}
79
80     inline const std::string& Prefix() const { return m_Prefix; }
81     inline const std::string& Name() const { return m_Name; }
82   };
83
84   //
85   class XMLElement
86     {
87       KM_NO_COPY_CONSTRUCT(XMLElement);
88       XMLElement();
89
90     protected:
91       AttributeList       m_AttrList;
92       ElementList         m_ChildList;
93       const XMLNamespace* m_Namespace;
94       void*               m_NamespaceOwner;
95
96       std::string   m_Name;
97       std::string   m_Body;
98
99     public:
100       XMLElement(const char* name);
101       ~XMLElement();
102
103       inline const XMLNamespace* Namespace() const { return m_Namespace; }
104       inline void                SetNamespace(const XMLNamespace* ns) { assert(ns); m_Namespace = ns; }
105
106       bool        ParseString(const char* document, ui32_t doc_len);
107       bool        ParseString(const ByteString& document);
108       bool        ParseString(const std::string& document);
109
110       bool        ParseFirstFromString(const char* document, ui32_t doc_len);
111       bool        ParseFirstFromString(const ByteString& document);
112       bool        ParseFirstFromString(const std::string& document);
113
114       // building
115       void        SetName(const char* name);
116       void        SetBody(const std::string& value);
117       void        AppendBody(const std::string& value);
118       void        SetAttr(const char* name, const char* value);
119       void        SetAttr(const char* name, const std::string& value) { SetAttr(name, value.c_str()); }
120       XMLElement* AddChild(XMLElement* element);
121       XMLElement* AddChild(const char* name);
122       XMLElement* AddChildWithContent(const char* name, const char* value);
123       XMLElement* AddChildWithContent(const char* name, const std::string& value);
124       XMLElement* AddChildWithPrefixedContent(const char* name, const char* prefix, const char* value);
125       void        AddComment(const char* value);
126       void        Render(std::string& str) const { Render(str, true); }
127       void        Render(std::string&, const bool& pretty) const;
128       void        RenderElement(std::string& outbuf, const ui32_t& depth, const bool& pretty) const;
129
130       // querying
131       inline const std::string&   GetBody() const { return m_Body; }
132       inline const ElementList&   GetChildren() const { return m_ChildList; }
133       inline const std::string&   GetName() const { return m_Name; }
134       inline const AttributeList& GetAttributes() const { return m_AttrList; }
135       const char*        GetAttrWithName(const char* name) const;
136       XMLElement*        GetChildWithName(const char* name) const;
137       const ElementList& GetChildrenWithName(const char* name, ElementList& outList) const;
138       bool               HasName(const char* name) const;
139
140       // altering
141       void        DeleteAttributes();
142       void        DeleteAttrWithName(const char* name);
143       void        DeleteChildren();
144       void        DeleteChild(const XMLElement* element);
145       void        ForgetChild(const XMLElement* element);
146     };
147
148   //
149   template <class VisitorType>
150     bool
151     apply_visitor(const XMLElement& element, VisitorType& visitor)
152     {
153       const ElementList& l = element.GetChildren();
154       ElementList::const_iterator i;
155       
156       for ( i = l.begin(); i != l.end(); ++i )
157         {
158           if ( ! visitor.Element(**i) )
159             {
160               return false;
161             }
162
163           if ( ! apply_visitor(**i, visitor) )
164             {
165               return false;
166             }
167         }
168
169       return true;
170     }
171
172   //
173   class AttributeVisitor
174   {
175     std::string attr_name;
176
177   public:
178   AttributeVisitor(const std::string& n) : attr_name(n) {}
179     std::set<std::string> value_list;
180
181     bool Element(const XMLElement& e)
182     {
183       const AttributeList& l = e.GetAttributes();
184       AttributeList::const_iterator i;
185  
186       for ( i = l.begin(); i != l.end(); ++i )
187         {
188           if ( i->name == attr_name )
189             {
190               value_list.insert(i->value);
191             }
192         }
193
194       return true;
195     }
196   };
197
198   //
199   class ElementVisitor
200   {
201     std::string element_name;
202
203   public:
204   ElementVisitor(const std::string& n) : element_name(n) {}
205     std::set<std::string> value_list;
206
207     bool Element(const XMLElement& e)
208     {
209       if ( e.GetBody() == element_name )
210         {
211           value_list.insert(e.GetBody());
212         }
213
214       return true;
215     }
216   };
217
218 } // namespace Kumu
219
220 #endif // _KM_XML_H_
221
222 //
223 // end KM_xml.h
224 //