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>
25 #include <installer_log.h>
28 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(
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());
44 ThrowMsg(LibxmlUtils::Libxml2Error, "xmlTextWriterWriteText failed");
48 void writeElement(xmlTextWriterPtr writer, const char * name, const char * body)
50 int state = xmlTextWriterWriteElement(writer, BAD_CAST name, BAD_CAST body);
52 ThrowMsg(LibxmlUtils::Libxml2Error, "xmlTextWriterWriteElement failed");
56 void writeElementWithOneAttribute(xmlTextWriterPtr writer,
59 const char * nameAttr,
61 bool condition = true)
63 startElement(writer, name);
64 writeAttribute(writer, nameAttr, bodyAttr, condition);
65 writeText(writer, body);
69 void startElement(xmlTextWriterPtr writer, const char * name)
71 int state = xmlTextWriterStartElement(writer, BAD_CAST name);
73 ThrowMsg(LibxmlUtils::Libxml2Error, "xmlTextWriterStartElement failed");
77 void endElement(xmlTextWriterPtr writer)
79 int state = xmlTextWriterEndElement(writer);
81 ThrowMsg(LibxmlUtils::Libxml2Error, "xmlTextWriterEndElement failed");
85 void writeAttribute(xmlTextWriterPtr writer,
88 bool condition = true)
93 int state = xmlTextWriterWriteAttribute(writer, BAD_CAST name,
94 BAD_CAST DPL::ToUTF8String(
97 ThrowMsg(LibxmlUtils::Libxml2Error,
98 "xmlTextWriterWriteAttribute failed");
102 void writeAttribute(xmlTextWriterPtr writer,
105 bool condition = true)
110 int state = xmlTextWriterWriteAttribute(writer,
114 ThrowMsg(LibxmlUtils::Libxml2Error,
115 "xmlTextWriterWriteAttribute failed");
119 void Manifest::generate(DPL::String filename)
121 xmlTextWriterPtr writer;
124 //compression set to 0
125 writer = xmlNewTextWriterFilename(DPL::ToUTF8String(filename).c_str(), 0);
127 if (writer == NULL) {
128 ThrowMsg(LibxmlUtils::Libxml2Error, "xmlNewTextWriterFilename failed");
130 state = xmlTextWriterSetIndent(writer, 1);
132 ThrowMsg(LibxmlUtils::Libxml2Error, "xmlTextWriterSetIndent failed");
135 state = xmlTextWriterStartDocument(writer, NULL, "utf-8", NULL);
137 ThrowMsg(LibxmlUtils::Libxml2Error, "xmlTextWriterStartDocument failed");
139 this->serialize(writer);
140 state = xmlTextWriterEndDocument(writer);
142 ThrowMsg(LibxmlUtils::Libxml2Error, "xmlTextWriterEndDocument failed");
144 if (writer != NULL) {
145 xmlFreeTextWriter(writer);
150 void Manifest::serialize(xmlTextWriterPtr writer)
152 startElement(writer, "manifest");
154 writeAttribute(writer, "xmlns", "http://tizen.org/ns/packages");
155 writeAttribute(writer, "package", this->package);
156 writeAttribute(writer, "type", this->type);
157 writeAttribute(writer, "version", this->version);
158 if (!!this->installLocation) {
159 writeAttribute(writer, "install-location", (*this->installLocation));
161 writeAttribute(writer, "storeclient-id", this->storeClientId,
162 !this->storeClientId.empty());
163 writeAttribute(writer, "csc_path", this->cscPath,
164 !this->cscPath.empty());
166 FOREACH(l, this->label)
168 writeElementWithOneAttribute(writer, "label", l->getString(),
169 "xml:lang", l->getLang(), l->hasLang());
171 FOREACH(i, this->icon)
173 writeElementWithOneAttribute(writer, "icon", i->getString(),
174 "xml:lang", i->getLang(), i->hasLang());
176 FOREACH(a, this->author)
178 a->serialize(writer);
180 FOREACH(d, this->description)
182 writeElementWithOneAttribute(writer, "description", d->getString(),
183 "xml:lang", d->getLang(), d->hasLang());
185 //FOREACH(c, this->compatibility) { c->serialize(writer); }
186 //FOREACH(d, this->deviceProfile) { d->serialize(writer); }
187 #ifdef SERVICE_ENABLED
188 FOREACH(s, this->serviceApplication) {
189 s->serialize(writer);
192 FOREACH(u, this->uiApplication) {
193 u->serialize(writer);
196 FOREACH(i, this->imeApplication) {
197 i->serialize(writer);
200 //FOREACH(f, this->font) { f->serialize(writer); }
202 FOREACH(l, this->livebox) {
203 l->serialize(writer);
206 FOREACH(acc, this->account)
208 acc->serialize(writer);
211 if (!this->privileges.isEmpty()) {
212 this->privileges.serialize(writer);
218 void Author::serialize(xmlTextWriterPtr writer)
220 startElement(writer, "author");
221 writeAttribute(writer, "email", this->email, !this->email.empty());
222 writeAttribute(writer, "href", this->href, !this->href.empty());
223 writeAttribute(writer, "xml:lang", this->lang, !this->lang.empty());
224 writeText(writer, body);
228 void UiApplication::serialize(xmlTextWriterPtr writer)
230 startElement(writer, "ui-application");
231 writeAttribute(writer, "appid", this->appid);
232 writeAttribute(writer, "exec", this->exec);
233 if (!!this->multiple) {
234 writeAttribute(writer, "multiple", (*this->multiple) ? "true" : "false");
236 if (!!this->nodisplay) {
237 writeAttribute(writer,
239 (*this->nodisplay) ? "true" : "false");
241 if (!!this->taskmanage) {
242 writeAttribute(writer,
244 (*this->taskmanage) ? "true" : "false");
246 writeAttribute(writer, "type", this->type);
247 writeAttribute(writer, "extraid", this->extraid);
248 if (!!this->categories) {
249 writeAttribute(writer, "categories", (*this->categories));
251 FOREACH(l, this->label)
253 writeElementWithOneAttribute(writer, "label",
254 l->getString(), "xml:lang",
255 l->getLang(), l->hasLang());
257 FOREACH(i, this->icon)
259 startElement(writer, "icon");
261 writeAttribute(writer, "xml:lang", i->getLang(), i->hasLang());
262 writeAttribute(writer, "section", "small", i->isSmall());
263 writeText(writer, i->getString());
267 FOREACH(a, this->appControl)
269 a->serialize(writer);
271 FOREACH(c, this->appCategory)
273 startElement(writer, "category");
274 writeAttribute(writer, "name", *c);
277 FOREACH(m, this->metadata) {
278 m->serialize(writer);
284 void ImeApplication::serialize(xmlTextWriterPtr writer)
286 startElement(writer, "ime");
287 writeAttribute(writer, "appid", this->appid);
289 startElement(writer, "uuid");
290 writeText(writer, this->uuid);
292 FOREACH(l, this->label)
294 writeElementWithOneAttribute(writer, "label", l->getString(), "xml:lang", l->getLang(), l->hasLang());
296 startElement(writer, "languages");
298 FOREACH(g, this->language)
300 startElement(writer, "language");
301 writeText(writer, *g);
306 startElement(writer, "type");
307 writeText(writer, this->iseType);
309 startElement(writer, "options");
311 FOREACH(o, this->option)
313 startElement(writer, "option");
314 writeText(writer, *o);
324 #ifdef SERVICE_ENABLED
325 void ServiceApplication::serialize(xmlTextWriterPtr writer)
327 startElement(writer, "ui-application");
328 writeAttribute(writer, "component-type", this->component);
329 writeAttribute(writer, "auto-restart", (!!this->autoRestart && (*this->autoRestart)) ? "true" : "false");
330 writeAttribute(writer, "on-boot", (!!this->onBoot && (*this->onBoot)) ? "true" : "false");
331 writeAttribute(writer, "appid", this->appid);
332 writeAttribute(writer, "exec", this->exec);
333 writeAttribute(writer, "extraid", this->extraid);
334 if (!!this->nodisplay) {
335 writeAttribute(writer, "nodisplay", (*this->nodisplay) ? "true" : "false");
337 if (!!this->multiple) {
338 writeAttribute(writer, "multiple", (*this->multiple) ? "true" : "false");
340 writeAttribute(writer, "type", this->type);
341 if (!!this->taskmanage) {
342 writeAttribute(writer, "taskmanage", (*this->taskmanage) ? "true" : "false");
344 if (!!this->categories) {
345 writeAttribute(writer, "categories", (*this->categories));
347 FOREACH(l, this->label)
349 writeElementWithOneAttribute(writer, "label",
350 l->getString(), "xml:lang",
351 l->getLang(), l->hasLang());
353 FOREACH(i, this->icon)
355 writeElementWithOneAttribute(writer, "icon", i->getString(), "xml:lang",
356 i->getLang(), i->hasLang());
358 FOREACH(a, this->appControl)
360 a->serialize(writer);
362 FOREACH(c, this->appCategory)
364 startElement(writer, "category");
365 writeAttribute(writer, "name", *c);
368 FOREACH(m, this->metadata) {
369 m->serialize(writer);
375 void AppControl::serialize(xmlTextWriterPtr writer)
377 startElement(writer, "app-control");
378 FOREACH(o, this->operation)
380 startElement(writer, "operation");
381 writeAttribute(writer, "name", *o);
384 FOREACH(u, this->uri)
386 startElement(writer, "uri");
387 writeAttribute(writer, "name", *u);
390 FOREACH(m, this->mime)
392 startElement(writer, "mime");
393 writeAttribute(writer, "name", *m);
400 void LiveBox::serialize(xmlTextWriterPtr writer)
402 startElement(writer, "livebox");
403 if (!this->liveboxId.empty()) {
404 writeAttribute(writer, "appid", this->liveboxId);
407 if (!this->primary.empty()) {
408 writeAttribute(writer, "primary", this->primary);
411 if (!this->updatePeriod.empty()) {
412 writeAttribute(writer, "period", this->updatePeriod);
415 writeAttribute(writer, "abi", "html");
416 writeAttribute(writer, "network", "true");
417 writeAttribute(writer, "nodisplay", "false");
419 if (!this->label.empty()) {
420 int defaultLabelChk = 0;
421 FOREACH(m, this->label)
423 std::pair<DPL::String, DPL::String> boxLabel = *m;
424 startElement(writer, "label");
425 if (!boxLabel.first.empty()) {
426 writeAttribute(writer, "xml:lang", boxLabel.first);
430 writeText(writer, boxLabel.second);
433 if(!defaultLabelChk) {
434 startElement(writer, "label");
435 writeText(writer, DPL::FromUTF8String("NO NAME"));
439 if (!this->icon.empty()) {
440 startElement(writer, "icon");
441 writeText(writer, this->icon);
445 if (!this->autoLaunch.empty()) {
446 startElement(writer, "launch");
447 writeText(writer, this->autoLaunch);
451 if (!this->box.boxSrc.empty() &&
452 !this->box.boxMouseEvent.empty() &&
453 !this->box.boxSize.empty())
455 startElement(writer, "box");
456 writeAttribute(writer, "type", "buffer");
457 writeAttribute(writer, "mouse_event", this->box.boxMouseEvent);
458 writeAttribute(writer, "touch_effect", this->box.boxTouchEffect);
460 FOREACH(it, this->box.boxSize)
462 startElement(writer, "size");
463 if (!(*it).m_preview.empty()) {
464 writeAttribute(writer, "preview", (*it).m_preview);
466 if (!(*it).m_useDecoration.empty()) {
467 writeAttribute(writer, "need_frame", (*it).m_useDecoration);
469 // default value of use-decoration is "true"
470 writeAttribute(writer, "need_frame", DPL::String(L"true"));
473 writeText(writer, (*it).m_size);
477 startElement(writer, "script");
478 writeAttribute(writer, "src", this->box.boxSrc);
483 if (!this->box.pdSrc.empty() &&
484 !this->box.pdWidth.empty() &&
485 !this->box.pdHeight.empty())
487 startElement(writer, "pd");
488 writeAttribute(writer, "type", "buffer");
490 startElement(writer, "size");
491 DPL::String pdSize = this->box.pdWidth + DPL::String(L"x") +
493 writeText(writer, pdSize);
496 startElement(writer, "script");
497 writeAttribute(writer, "src", this->box.pdSrc);
508 void Account::serialize(xmlTextWriterPtr writer)
510 startElement(writer, "account");
512 startElement(writer, "account-provider");
513 writeAttribute(writer, "appid", this->provider.appid);
514 writeAttribute(writer, "multiple-accounts-support",
515 this->provider.multiAccount);
517 FOREACH(i, this->provider.icon)
519 startElement(writer, "icon");
520 writeAttribute(writer, "section", i->first);
521 writeText(writer, i->second);
525 bool setDefaultLang = false;
526 FOREACH(n, this->provider.name)
528 if (!setDefaultLang && n->getLang() == L"en-gb") {
529 writeElement(writer, "label", n->getString());
530 setDefaultLang = true;
532 writeElementWithOneAttribute(writer, "label",
533 n->getString(), "xml:lang",
534 n->getLang(), n->hasLang());
536 if (!setDefaultLang) {
537 writeElement(writer, "label", this->provider.name.begin()->getString());
540 FOREACH(c, this->provider.capability)
542 startElement(writer, "capability");
543 writeText(writer, *c);
551 void Privilege::serialize(xmlTextWriterPtr writer)
553 startElement(writer, "privileges");
555 FOREACH(it, this->name)
557 startElement(writer, "privilege");
558 writeText(writer, *it);
565 void Metadata::serialize(xmlTextWriterPtr writer)
567 startElement(writer, "metadata");
568 writeAttribute(writer, "key", *this->key);
570 writeAttribute(writer, "value", *this->value);
575 } //namespace WidgetInstall