tizen beta release
[framework/web/wrt-installer.git] / src / config_generator / config_generator.h
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Apache License, Version 2.0 (the "License");
5  *    you may not use this file except in compliance with the License.
6  *    You may obtain a copy of the License at
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *    Unless required by applicable law or agreed to in writing, software
11  *    distributed under the License is distributed on an "AS IS" BASIS,
12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *    See the License for the specific language governing permissions and
14  *    limitations under the License.
15  */
16 /*
17  * @file       config_generator.h
18  * @author     Krzysztof Jackiewicz (k.jackiewicz@samsung.com)
19  * @version    1.0
20  */
21
22 #ifndef CONFIG_GENERATOR_H_
23 #define CONFIG_GENERATOR_H_
24
25 #include <dpl/abstract_output.h>
26 #include <dpl/assert.h>
27 #include <dpl/exception.h>
28 #include <list>
29 #include <memory>
30
31 namespace ConfigXml {
32
33 enum Tag {
34     WIDGET = 0,
35     NAME,
36     ICON,
37     CONTENT,
38     AUTHOR,
39     LICENSE,
40     FEATURE,
41     PARAM,
42     DESCRIPTION,
43     PREFERENCE,
44     ACCESS,
45     TIZEN_SETTING,
46
47     TAG_COUNT,
48     INVALID
49 };
50
51 /*
52  * TODO
53  * -Global attributes support xml:lang, dir
54  * -Children cardinality
55  * -Other...
56  */
57
58 DECLARE_EXCEPTION_TYPE(DPL::Exception, Base);
59 DECLARE_EXCEPTION_TYPE(Base, NodeCreationError);
60 DECLARE_EXCEPTION_TYPE(Base, DocCreationError);
61 DECLARE_EXCEPTION_TYPE(Base, ChildCreationError);
62 DECLARE_EXCEPTION_TYPE(Base, PropertyCreationError);
63
64 // ELEMENT ///////////////////////////////////////////////////////////
65
66 class Element;
67 typedef std::shared_ptr<Element> ElementPtr;
68
69 class XmlNodeWrapper;
70 typedef std::shared_ptr<XmlNodeWrapper> XmlNodeWrapperPtr;
71
72 class Element {
73 public:
74     template <Tag TagId, typename... Params>
75     ElementPtr Add(Params... parameters);
76
77 private:
78     friend class Document;
79
80     // helper class for handling nodes
81     template <Tag TagId,typename... Params>
82     class Handler {
83     public:
84         explicit Handler(XmlNodeWrapperPtr parent) : m_parent(parent) {}
85
86         XmlNodeWrapperPtr CreateNode(Params... parameters);
87     private:
88         // not owned!
89         XmlNodeWrapperPtr m_parent;
90     };
91
92     explicit Element(XmlNodeWrapperPtr node) : m_node(node)
93     {
94         Assert(m_node);
95     }
96
97     // not owned!
98     XmlNodeWrapperPtr m_node;
99 };
100
101 template <Tag TagId, typename... Params>
102 ElementPtr Element::Add(Params... parameters)
103 {
104     Handler<TagId,Params...> eh(m_node);
105     ElementPtr e(new Element(eh.CreateNode(parameters...)));
106     return e;
107 }
108
109
110 // DOCUMENT //////////////////////////////////////////////////////////
111
112 class Document;
113 typedef std::shared_ptr<Document> DocumentPtr;
114
115 class XmlDocWrapper;
116 typedef std::shared_ptr<XmlDocWrapper> XmlDocWrapperPtr;
117
118 class Document {
119 public:
120     static DocumentPtr Create();
121
122     void Write(DPL::AbstractOutput& output);
123
124     template <Tag TagId, typename... Params>
125     ElementPtr Add(Params... parameters);
126
127     ~Document();
128
129 private:
130     explicit Document(XmlDocWrapperPtr document) :
131         m_document(document)
132     {
133         Assert(m_document);
134     }
135
136     // helper class for handling root node
137     template <Tag TagId,typename... Params>
138     class Handler {
139     public:
140         explicit Handler(XmlDocWrapperPtr document) : m_document(document) {}
141
142         XmlNodeWrapperPtr CreateRoot(const Params... parameters);
143     private:
144         // not owned!
145         XmlDocWrapperPtr m_document;
146     };
147
148     XmlDocWrapperPtr m_document;
149 };
150
151 template <Tag TagId, typename... Params>
152 ElementPtr Document::Add(Params... parameters)
153 {
154     Handler<TagId,Params...> dh(m_document);
155     ElementPtr e(new Element(dh.CreateRoot(parameters...)));
156     return e;
157 }
158
159 }; // ConfigXml
160
161 #endif /* CONFIG_GENERATOR_H_ */