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 {
28 void writeElement(xmlTextWriterPtr writer, const char * name, DPL::String body)
30 int state = xmlTextWriterWriteElement(writer, BAD_CAST name,
31 BAD_CAST DPL::ToUTF8String(
34 ThrowMsg(LibxmlUtils::Libxml2Error, "xmlTextWriterWriteElement failed");
38 void writeText(xmlTextWriterPtr writer, DPL::String text)
40 int state = xmlTextWriterWriteString(writer,
41 BAD_CAST DPL::ToUTF8String(text).c_str());
43 ThrowMsg(LibxmlUtils::Libxml2Error, "xmlTextWriterWriteText failed");
47 void writeElement(xmlTextWriterPtr writer, const char * name, const char * body)
49 int state = xmlTextWriterWriteElement(writer, BAD_CAST name, BAD_CAST body);
51 ThrowMsg(LibxmlUtils::Libxml2Error, "xmlTextWriterWriteElement failed");
55 void writeElementWithOneAttribute(xmlTextWriterPtr writer,
58 const char * nameAttr,
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);
72 ThrowMsg(LibxmlUtils::Libxml2Error, "xmlTextWriterStartElement failed");
76 void endElement(xmlTextWriterPtr writer)
78 int state = xmlTextWriterEndElement(writer);
80 ThrowMsg(LibxmlUtils::Libxml2Error, "xmlTextWriterEndElement failed");
84 void writeAttribute(xmlTextWriterPtr writer,
87 bool condition = true)
92 int state = xmlTextWriterWriteAttribute(writer, BAD_CAST name,
93 BAD_CAST DPL::ToUTF8String(
96 ThrowMsg(LibxmlUtils::Libxml2Error,
97 "xmlTextWriterWriteAttribute failed");
101 void writeAttribute(xmlTextWriterPtr writer,
104 bool condition = true)
109 int state = xmlTextWriterWriteAttribute(writer,
113 ThrowMsg(LibxmlUtils::Libxml2Error,
114 "xmlTextWriterWriteAttribute failed");
118 void Manifest::generate(DPL::String filename)
120 xmlTextWriterPtr writer;
123 //compression set to 0
124 writer = xmlNewTextWriterFilename(DPL::ToUTF8String(filename).c_str(), 0);
126 if (writer == NULL) {
127 ThrowMsg(LibxmlUtils::Libxml2Error, "xmlNewTextWriterFilename failed");
129 state = xmlTextWriterSetIndent(writer, 1);
131 ThrowMsg(LibxmlUtils::Libxml2Error, "xmlTextWriterSetIndent failed");
134 state = xmlTextWriterStartDocument(writer, NULL, "utf-8", NULL);
136 ThrowMsg(LibxmlUtils::Libxml2Error, "xmlTextWriterStartDocument failed");
138 this->serialize(writer);
139 state = xmlTextWriterEndDocument(writer);
141 ThrowMsg(LibxmlUtils::Libxml2Error, "xmlTextWriterEndDocument failed");
143 if (writer != NULL) {
144 xmlFreeTextWriter(writer);
149 void Manifest::serialize(xmlTextWriterPtr writer)
151 startElement(writer, "manifest");
153 writeAttribute(writer, "xmlns", "http://tizen.org/ns/packages");
154 writeAttribute(writer, "package", this->package);
155 writeAttribute(writer, "type", this->type);
156 writeAttribute(writer, "version", this->version);
157 if (!this->installLocation.IsNull()) {
158 writeAttribute(writer, "install-location", (*this->installLocation));
160 writeAttribute(writer, "storeclient-id", this->storeClientId,
161 !this->storeClientId.empty());
163 FOREACH(l, this->label)
165 writeElementWithOneAttribute(writer, "label", l->getString(),
166 "xml:lang", l->getLang(), l->hasLang());
168 FOREACH(i, this->icon)
170 writeElementWithOneAttribute(writer, "icon", i->getString(),
171 "xml:lang", i->getLang(), i->hasLang());
173 FOREACH(a, this->author)
175 a->serialize(writer);
177 FOREACH(d, this->description)
179 writeElementWithOneAttribute(writer, "description", d->getString(),
180 "xml:lang", d->getLang(), d->hasLang());
182 //FOREACH(c, this->compatibility) { c->serialize(writer); }
183 //FOREACH(d, this->deviceProfile) { d->serialize(writer); }
184 FOREACH(s, this->serviceApplication) {
185 s->serialize(writer);
187 FOREACH(u, this->uiApplication) {
188 u->serialize(writer);
190 FOREACH(i, this->imeApplication) {
191 i->serialize(writer);
193 //FOREACH(f, this->font) { f->serialize(writer); }
194 FOREACH(l, this->livebox) {
195 l->serialize(writer);
197 FOREACH(acc, this->account)
199 acc->serialize(writer);
202 if (!this->privileges.isEmpty()) {
203 this->privileges.serialize(writer);
209 void Author::serialize(xmlTextWriterPtr writer)
211 startElement(writer, "author");
212 writeAttribute(writer, "email", this->email, !this->email.empty());
213 writeAttribute(writer, "href", this->href, !this->href.empty());
214 writeAttribute(writer, "xml:lang", this->lang, !this->lang.empty());
215 writeText(writer, body);
219 void ServiceApplication::serialize(xmlTextWriterPtr writer)
221 startElement(writer, "service-application");
222 writeAttribute(writer, "appid", this->appid);
223 writeAttribute(writer, "auto-restart",
224 (!this->autoRestart.IsNull() &&
225 (*this->autoRestart)) ? "true" :
227 writeAttribute(writer, "exec", this->exec);
228 writeAttribute(writer, "on-boot", (!this->onBoot.IsNull() &&
229 (*this->onBoot)) ? "true" : "false");
230 writeAttribute(writer, "type", this->type);
231 FOREACH(l, this->label)
233 writeElementWithOneAttribute(writer, "label",
234 l->getString(), "xml:lang",
235 l->getLang(), l->hasLang());
237 FOREACH(i, this->icon)
239 writeElementWithOneAttribute(writer, "icon", i->getString(), "xml:lang",
240 i->getLang(), i->hasLang());
242 FOREACH(a, this->appControl)
244 a->serialize(writer);
249 void UiApplication::serialize(xmlTextWriterPtr writer)
251 startElement(writer, "ui-application");
252 writeAttribute(writer, "appid", this->appid);
253 writeAttribute(writer, "exec", this->exec);
254 if (!this->multiple.IsNull()) {
255 writeAttribute(writer, "multiple", (*this->multiple) ? "true" : "false");
257 if (!this->nodisplay.IsNull()) {
258 writeAttribute(writer,
260 (*this->nodisplay) ? "true" : "false");
262 if (!this->taskmanage.IsNull()) {
263 writeAttribute(writer,
265 (*this->taskmanage) ? "true" : "false");
267 writeAttribute(writer, "type", this->type);
268 writeAttribute(writer, "extraid", this->extraid);
269 if (!this->categories.IsNull()) {
270 writeAttribute(writer, "categories", (*this->categories));
272 FOREACH(l, this->label)
274 writeElementWithOneAttribute(writer, "label",
275 l->getString(), "xml:lang",
276 l->getLang(), l->hasLang());
278 FOREACH(i, this->icon)
280 writeElementWithOneAttribute(writer, "icon", i->getString(), "xml:lang",
281 i->getLang(), i->hasLang());
283 FOREACH(a, this->appControl)
285 a->serialize(writer);
287 FOREACH(c, this->appCategory)
289 startElement(writer, "category");
290 writeAttribute(writer, "name", *c);
293 FOREACH(m, this->metadata) {
294 m->serialize(writer);
299 void ImeApplication::serialize(xmlTextWriterPtr writer)
301 startElement(writer, "ime-application");
302 writeAttribute(writer, "appid", this->appid);
303 writeAttribute(writer, "exec", this->exec);
304 if (!this->multiple.IsNull()) {
305 writeAttribute(writer, "multiple", (*this->multiple) ? "true" : "false");
307 if (!this->nodisplay.IsNull()) {
308 writeAttribute(writer,
310 (*this->nodisplay) ? "true" : "false");
312 writeAttribute(writer, "type", this->type);
313 FOREACH(l, this->label)
315 writeElementWithOneAttribute(writer, "label",
316 l->getString(), "xml:lang",
317 l->getLang(), l->hasLang());
319 FOREACH(i, this->icon)
321 writeElementWithOneAttribute(writer, "icon", i->getString(), "xml:lang",
322 i->getLang(), i->hasLang());
327 void AppControl::serialize(xmlTextWriterPtr writer)
329 startElement(writer, "app-control");
330 FOREACH(o, this->operation)
332 startElement(writer, "operation");
333 writeAttribute(writer, "name", *o);
336 FOREACH(u, this->uri)
338 startElement(writer, "uri");
339 writeAttribute(writer, "name", *u);
342 FOREACH(m, this->mime)
344 startElement(writer, "mime");
345 writeAttribute(writer, "name", *m);
351 void LiveBox::serialize(xmlTextWriterPtr writer)
353 startElement(writer, "livebox");
354 if (!this->liveboxId.empty()) {
355 writeAttribute(writer, "appid", this->liveboxId);
358 if (!this->primary.empty()) {
359 writeAttribute(writer, "primary", this->primary);
362 if (!this->updatePeriod.empty()) {
363 writeAttribute(writer, "period", this->updatePeriod);
366 writeAttribute(writer, "abi", "html");
367 writeAttribute(writer, "network", "true");
368 writeAttribute(writer, "nodisplay", "false");
370 if (!this->label.empty()) {
371 int defaultLabelChk = 0;
372 FOREACH(m, this->label)
374 std::pair<DPL::String, DPL::String> boxLabel = *m;
375 startElement(writer, "label");
376 if (!boxLabel.first.empty()) {
377 writeAttribute(writer, "xml:lang", boxLabel.first);
381 writeText(writer, boxLabel.second);
384 if(!defaultLabelChk) {
385 startElement(writer, "label");
386 writeText(writer, DPL::FromUTF8String("NO NAME"));
390 if (!this->icon.empty()) {
391 startElement(writer, "icon");
392 writeText(writer, this->icon);
396 if (!this->autoLaunch.empty()) {
397 startElement(writer, "launch");
398 writeText(writer, this->autoLaunch);
402 if (!this->box.boxSrc.empty() &&
403 !this->box.boxMouseEvent.empty() &&
404 !this->box.boxSize.empty())
406 startElement(writer, "box");
407 writeAttribute(writer, "type", "buffer");
408 writeAttribute(writer, "mouse_event", this->box.boxMouseEvent);
409 writeAttribute(writer, "touch_effect", this->box.boxTouchEffect);
411 FOREACH(it, this->box.boxSize)
413 startElement(writer, "size");
414 if (!(*it).m_preview.empty()) {
415 writeAttribute(writer, "preview", (*it).m_preview);
417 if (!(*it).m_useDecoration.empty()) {
418 writeAttribute(writer, "need_frame", (*it).m_useDecoration);
420 // default value of use-decoration is "true"
421 writeAttribute(writer, "need_frame", DPL::String(L"true"));
424 writeText(writer, (*it).m_size);
428 startElement(writer, "script");
429 writeAttribute(writer, "src", this->box.boxSrc);
434 if (!this->box.pdSrc.empty() &&
435 !this->box.pdWidth.empty() &&
436 !this->box.pdHeight.empty())
438 startElement(writer, "pd");
439 writeAttribute(writer, "type", "buffer");
441 startElement(writer, "size");
442 DPL::String pdSize = this->box.pdWidth + DPL::String(L"x") +
444 writeText(writer, pdSize);
447 startElement(writer, "script");
448 writeAttribute(writer, "src", this->box.pdSrc);
458 void Account::serialize(xmlTextWriterPtr writer)
460 startElement(writer, "account");
462 startElement(writer, "account-provider");
463 writeAttribute(writer, "app-id", this->provider.appid);
464 writeAttribute(writer, "multiple-accounts-support",
465 this->provider.multiAccount);
467 FOREACH(i, this->provider.icon)
469 startElement(writer, "icon");
470 writeAttribute(writer, "section", i->first);
471 writeText(writer, i->second);
474 FOREACH(n, this->provider.name)
476 writeElementWithOneAttribute(writer, "label",
477 n->getString(), "xml:lang",
478 n->getLang(), n->hasLang());
480 FOREACH(c, this->provider.capability)
482 startElement(writer, "capability");
483 writeText(writer, *c);
491 void Privilege::serialize(xmlTextWriterPtr writer)
493 startElement(writer, "privileges");
495 FOREACH(it, this->name)
497 startElement(writer, "privilege");
498 writeText(writer, *it);
505 void Metadata::serialize(xmlTextWriterPtr writer)
507 startElement(writer, "metadata");
508 writeAttribute(writer, "key", *this->key);
509 if (!this->value.IsNull()) {
510 writeAttribute(writer, "value", *this->value);
515 } //namespace WidgetInstall