2 * Copyright (c) 2012 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * @author Mariusz Domanski (m.domanski@samsung.com)
22 #include "libxml_utils.h"
23 #include <widget_install/task_manifest_file.h>
24 #include <dpl/foreach.h>
27 namespace WidgetInstall {
29 void writeElement(xmlTextWriterPtr writer, const char * name, DPL::String body)
31 int state = xmlTextWriterWriteElement(writer, BAD_CAST name,
32 BAD_CAST DPL::ToUTF8String(body).c_str());
35 ThrowMsg(LibxmlUtils::Libxml2Error, "xmlTextWriterWriteElement failed");
39 void writeText(xmlTextWriterPtr writer, DPL::String text)
41 int state = xmlTextWriterWriteString(writer,
42 BAD_CAST DPL::ToUTF8String(text).c_str());
45 ThrowMsg(LibxmlUtils::Libxml2Error, "xmlTextWriterWriteText failed");
49 void writeElement(xmlTextWriterPtr writer, const char * name, const char * body)
51 int state = xmlTextWriterWriteElement(writer, BAD_CAST name, BAD_CAST body);
54 ThrowMsg(LibxmlUtils::Libxml2Error, "xmlTextWriterWriteElement failed");
58 void writeElementWithOneAttribute(xmlTextWriterPtr writer, const char * name,
59 DPL::String body, const char * nameAttr, DPL::String bodyAttr,
60 bool condition = true)
62 startElement(writer, name);
63 writeAttribute(writer, nameAttr, bodyAttr, condition);
64 writeText(writer, body);
68 void startElement(xmlTextWriterPtr writer, const char * name)
70 int state = xmlTextWriterStartElement(writer, BAD_CAST name);
73 ThrowMsg(LibxmlUtils::Libxml2Error, "xmlTextWriterStartElement failed");
77 void endElement(xmlTextWriterPtr writer)
79 int state = xmlTextWriterEndElement(writer);
82 ThrowMsg(LibxmlUtils::Libxml2Error, "xmlTextWriterEndElement failed");
86 void writeAttribute(xmlTextWriterPtr writer, const char * name, DPL::String body,
87 bool condition = true)
91 int state = xmlTextWriterWriteAttribute(writer, BAD_CAST name,
92 BAD_CAST DPL::ToUTF8String(body).c_str());
95 ThrowMsg(LibxmlUtils::Libxml2Error, "xmlTextWriterWriteAttribute failed");
99 void writeAttribute(xmlTextWriterPtr writer, const char * name, const char * body,
100 bool condition = true)
104 int state = xmlTextWriterWriteAttribute(writer, BAD_CAST name, BAD_CAST body);
107 ThrowMsg(LibxmlUtils::Libxml2Error, "xmlTextWriterWriteAttribute failed");
111 void Manifest::generate(DPL::String filename)
113 xmlTextWriterPtr writer;
115 writer = xmlNewTextWriterFilename(DPL::ToUTF8String(filename).c_str(), 0); //compression set to 0
117 ThrowMsg(LibxmlUtils::Libxml2Error, "xmlNewTextWriterFilename failed");
118 state = xmlTextWriterSetIndent (writer, 1);
121 ThrowMsg(LibxmlUtils::Libxml2Error, "xmlTextWriterSetIndent failed");
124 state = xmlTextWriterStartDocument(writer, NULL, "utf-8", NULL);
127 ThrowMsg(LibxmlUtils::Libxml2Error, "xmlTextWriterStartDocument failed");
129 this->serialize(writer);
130 state = xmlTextWriterEndDocument(writer);
133 ThrowMsg(LibxmlUtils::Libxml2Error, "xmlTextWriterEndDocument failed");
137 xmlFreeTextWriter(writer);
142 void Manifest::serialize(xmlTextWriterPtr writer)
144 startElement(writer, "manifest");
146 writeAttribute(writer, "xmlns", "http://tizen.org/ns/packages");
147 writeAttribute(writer, "package", this->package);
148 writeAttribute(writer, "type", this->type);
149 writeAttribute(writer, "version", this->version);
150 if (!this->installLocation.IsNull())
151 writeAttribute(writer, "install-location", (*this->installLocation),
152 (*this->installLocation).empty());
154 FOREACH(l, this->label)
156 writeElementWithOneAttribute(writer, "label", l->getString(),
157 "xml:lang", l->getLang(), l->hasLang());
159 FOREACH(i, this->icon)
161 writeElementWithOneAttribute(writer, "icon", i->getString(),
162 "xml:lang", i->getLang(), i->hasLang());
164 FOREACH(a, this->author)
166 a->serialize(writer);
168 FOREACH(d, this->description)
170 writeElementWithOneAttribute(writer, "description", d->getString(),
171 "xml:lang", d->getLang(), d->hasLang());
173 //FOREACH(c, this->compatibility) { c->serialize(writer); }
174 //FOREACH(d, this->deviceProfile) { d->serialize(writer); }
175 FOREACH(s, this->serviceApplication) { s->serialize(writer); }
176 FOREACH(u, this->uiApplication) { u->serialize(writer); }
177 FOREACH(i, this->imeApplication) { i->serialize(writer); }
178 //FOREACH(f, this->font) { f->serialize(writer); }
183 void Author::serialize(xmlTextWriterPtr writer)
185 startElement(writer, "author");
186 writeAttribute(writer, "email", this->email, !this->email.empty());
187 writeAttribute(writer, "href", this->href, !this->href.empty());
188 writeAttribute(writer, "xml:lang", this->lang, !this->lang.empty());
189 writeText(writer, body);
193 void ServiceApplication::serialize(xmlTextWriterPtr writer)
195 startElement(writer, "service-application");
196 writeAttribute(writer, "appid", this->appid);
197 writeAttribute(writer, "auto-restart", (!this->autoRestart.IsNull() &&
198 (*this->autoRestart)) ? "true" : "false");
199 writeAttribute(writer, "exec", this->exec);
200 writeAttribute(writer, "on-boot", (!this->onBoot.IsNull() &&
201 (*this->onBoot)) ? "true" : "false");
202 writeAttribute(writer, "type", this->type);
203 FOREACH(l, this->label)
205 writeElementWithOneAttribute(writer, "label", l->getString(), "xml:lang",
206 l->getLang(), l->hasLang());
208 FOREACH(i, this->icon)
210 writeElementWithOneAttribute(writer, "icon", i->getString(), "xml:lang",
211 i->getLang(), i->hasLang());
213 FOREACH(a, this->applicationService)
215 a->serialize(writer);
220 void UiApplication::serialize(xmlTextWriterPtr writer)
222 startElement(writer, "ui-application");
223 writeAttribute(writer, "appid", this->appid);
224 writeAttribute(writer, "exec", this->exec);
225 if (!this->multiple.IsNull())
226 writeAttribute(writer, "multiple", (*this->multiple) ? "true" : "false");
227 if (!this->nodisplay.IsNull())
228 writeAttribute(writer, "nodisplay", (*this->nodisplay) ? "true" : "false");
229 if (!this->taskmanage.IsNull())
230 writeAttribute(writer, "taskmanage", (*this->taskmanage) ? "true" : "false");
231 writeAttribute(writer, "type", this->type);
232 writeAttribute(writer, "extraid", this->extraid);
233 if (!this->categories.IsNull())
234 writeAttribute(writer, "categories", (*this->categories));
235 FOREACH(l, this->label)
237 writeElementWithOneAttribute(writer, "label", l->getString(), "xml:lang",
238 l->getLang(), l->hasLang());
240 FOREACH(i, this->icon)
242 writeElementWithOneAttribute(writer, "icon", i->getString(), "xml:lang",
243 i->getLang(), i->hasLang());
245 FOREACH(a, this->applicationService)
247 a->serialize(writer);
252 void ImeApplication::serialize(xmlTextWriterPtr writer)
254 startElement(writer, "ime-application");
255 writeAttribute(writer, "appid", this->appid);
256 writeAttribute(writer, "exec", this->exec);
257 if (!this->multiple.IsNull())
258 writeAttribute(writer, "multiple", (*this->multiple) ? "true" : "false");
259 if (!this->nodisplay.IsNull())
260 writeAttribute(writer, "nodisplay", (*this->nodisplay) ? "true" : "false");
261 writeAttribute(writer, "type", this->type);
262 FOREACH(l, this->label)
264 writeElementWithOneAttribute(writer, "label", l->getString(), "xml:lang",
265 l->getLang(), l->hasLang());
267 FOREACH(i, this->icon)
269 writeElementWithOneAttribute(writer, "icon", i->getString(), "xml:lang",
270 i->getLang(), i->hasLang());
275 void ApplicationService::serialize(xmlTextWriterPtr writer)
277 startElement(writer, "application-service");
278 FOREACH(o, this->operation)
280 startElement(writer, "operation");
281 writeAttribute(writer, "name", *o);
284 FOREACH(u, this->uri)
286 startElement(writer, "uri");
287 writeAttribute(writer, "name", *u);
290 FOREACH(m, this->mime)
292 startElement(writer, "mime");
293 writeAttribute(writer, "name", *m);
300 } //namespace WidgetInstall