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.IsNull()) {
159 writeAttribute(writer, "install-location", (*this->installLocation));
161 writeAttribute(writer, "storeclient-id", this->storeClientId,
162 !this->storeClientId.empty());
164 FOREACH(l, this->label)
166 writeElementWithOneAttribute(writer, "label", l->getString(),
167 "xml:lang", l->getLang(), l->hasLang());
169 FOREACH(i, this->icon)
171 writeElementWithOneAttribute(writer, "icon", i->getString(),
172 "xml:lang", i->getLang(), i->hasLang());
174 FOREACH(a, this->author)
176 a->serialize(writer);
178 FOREACH(d, this->description)
180 writeElementWithOneAttribute(writer, "description", d->getString(),
181 "xml:lang", d->getLang(), d->hasLang());
183 //FOREACH(c, this->compatibility) { c->serialize(writer); }
184 //FOREACH(d, this->deviceProfile) { d->serialize(writer); }
185 FOREACH(s, this->serviceApplication) {
186 s->serialize(writer);
188 FOREACH(u, this->uiApplication) {
189 u->serialize(writer);
191 FOREACH(i, this->imeApplication) {
192 i->serialize(writer);
194 //FOREACH(f, this->font) { f->serialize(writer); }
196 FOREACH(l, this->livebox) {
197 l->serialize(writer);
200 FOREACH(acc, this->account)
202 acc->serialize(writer);
205 if (!this->privileges.isEmpty()) {
206 this->privileges.serialize(writer);
212 void Author::serialize(xmlTextWriterPtr writer)
214 startElement(writer, "author");
215 writeAttribute(writer, "email", this->email, !this->email.empty());
216 writeAttribute(writer, "href", this->href, !this->href.empty());
217 writeAttribute(writer, "xml:lang", this->lang, !this->lang.empty());
218 writeText(writer, body);
222 void ServiceApplication::serialize(xmlTextWriterPtr writer)
224 startElement(writer, "service-application");
225 writeAttribute(writer, "appid", this->appid);
226 writeAttribute(writer, "auto-restart",
227 (!this->autoRestart.IsNull() &&
228 (*this->autoRestart)) ? "true" :
230 writeAttribute(writer, "exec", this->exec);
231 writeAttribute(writer, "on-boot", (!this->onBoot.IsNull() &&
232 (*this->onBoot)) ? "true" : "false");
233 writeAttribute(writer, "type", this->type);
234 FOREACH(l, this->label)
236 writeElementWithOneAttribute(writer, "label",
237 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->appControl)
247 a->serialize(writer);
252 void UiApplication::serialize(xmlTextWriterPtr writer)
254 startElement(writer, "ui-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");
260 if (!this->nodisplay.IsNull()) {
261 writeAttribute(writer,
263 (*this->nodisplay) ? "true" : "false");
265 if (!this->taskmanage.IsNull()) {
266 writeAttribute(writer,
268 (*this->taskmanage) ? "true" : "false");
270 writeAttribute(writer, "type", this->type);
271 writeAttribute(writer, "extraid", this->extraid);
272 if (!this->categories.IsNull()) {
273 writeAttribute(writer, "categories", (*this->categories));
275 FOREACH(l, this->label)
277 writeElementWithOneAttribute(writer, "label",
278 l->getString(), "xml:lang",
279 l->getLang(), l->hasLang());
281 FOREACH(i, this->icon)
283 writeElementWithOneAttribute(writer, "icon", i->getString(), "xml:lang",
284 i->getLang(), i->hasLang());
286 FOREACH(a, this->appControl)
288 a->serialize(writer);
290 FOREACH(c, this->appCategory)
292 startElement(writer, "category");
293 writeAttribute(writer, "name", *c);
296 FOREACH(m, this->metadata) {
297 m->serialize(writer);
302 void ImeApplication::serialize(xmlTextWriterPtr writer)
304 startElement(writer, "ime-application");
305 writeAttribute(writer, "appid", this->appid);
306 writeAttribute(writer, "exec", this->exec);
307 if (!this->multiple.IsNull()) {
308 writeAttribute(writer, "multiple", (*this->multiple) ? "true" : "false");
310 if (!this->nodisplay.IsNull()) {
311 writeAttribute(writer,
313 (*this->nodisplay) ? "true" : "false");
315 writeAttribute(writer, "type", this->type);
316 FOREACH(l, this->label)
318 writeElementWithOneAttribute(writer, "label",
319 l->getString(), "xml:lang",
320 l->getLang(), l->hasLang());
322 FOREACH(i, this->icon)
324 writeElementWithOneAttribute(writer, "icon", i->getString(), "xml:lang",
325 i->getLang(), i->hasLang());
330 void AppControl::serialize(xmlTextWriterPtr writer)
332 startElement(writer, "app-control");
333 FOREACH(o, this->operation)
335 startElement(writer, "operation");
336 writeAttribute(writer, "name", *o);
339 FOREACH(u, this->uri)
341 startElement(writer, "uri");
342 writeAttribute(writer, "name", *u);
345 FOREACH(m, this->mime)
347 startElement(writer, "mime");
348 writeAttribute(writer, "name", *m);
356 void LiveBox::serialize(xmlTextWriterPtr writer)
358 startElement(writer, "livebox");
359 if (!this->liveboxId.empty()) {
360 writeAttribute(writer, "appid", this->liveboxId);
363 if (!this->primary.empty()) {
364 writeAttribute(writer, "primary", this->primary);
367 if (!this->updatePeriod.empty()) {
368 writeAttribute(writer, "period", this->updatePeriod);
371 writeAttribute(writer, "abi", "html");
372 writeAttribute(writer, "network", "true");
373 writeAttribute(writer, "nodisplay", "false");
375 if (!this->label.empty()) {
376 int defaultLabelChk = 0;
377 FOREACH(m, this->label)
379 std::pair<DPL::String, DPL::String> boxLabel = *m;
380 startElement(writer, "label");
381 if (!boxLabel.first.empty()) {
382 writeAttribute(writer, "xml:lang", boxLabel.first);
386 writeText(writer, boxLabel.second);
389 if(!defaultLabelChk) {
390 startElement(writer, "label");
391 writeText(writer, DPL::FromUTF8String("NO NAME"));
395 if (!this->icon.empty()) {
396 startElement(writer, "icon");
397 writeText(writer, this->icon);
401 if (!this->autoLaunch.empty()) {
402 startElement(writer, "launch");
403 writeText(writer, this->autoLaunch);
407 if (!this->box.boxSrc.empty() &&
408 !this->box.boxMouseEvent.empty() &&
409 !this->box.boxSize.empty())
411 startElement(writer, "box");
412 writeAttribute(writer, "type", "buffer");
413 writeAttribute(writer, "mouse_event", this->box.boxMouseEvent);
414 writeAttribute(writer, "touch_effect", this->box.boxTouchEffect);
416 FOREACH(it, this->box.boxSize)
418 startElement(writer, "size");
419 if (!(*it).m_preview.empty()) {
420 writeAttribute(writer, "preview", (*it).m_preview);
422 if (!(*it).m_useDecoration.empty()) {
423 writeAttribute(writer, "need_frame", (*it).m_useDecoration);
425 // default value of use-decoration is "true"
426 writeAttribute(writer, "need_frame", DPL::String(L"true"));
429 writeText(writer, (*it).m_size);
433 startElement(writer, "script");
434 writeAttribute(writer, "src", this->box.boxSrc);
439 if (!this->box.pdSrc.empty() &&
440 !this->box.pdWidth.empty() &&
441 !this->box.pdHeight.empty())
443 startElement(writer, "pd");
444 writeAttribute(writer, "type", "buffer");
446 startElement(writer, "size");
447 DPL::String pdSize = this->box.pdWidth + DPL::String(L"x") +
449 writeText(writer, pdSize);
452 startElement(writer, "script");
453 writeAttribute(writer, "src", this->box.pdSrc);
464 void Account::serialize(xmlTextWriterPtr writer)
466 startElement(writer, "account");
468 startElement(writer, "account-provider");
469 writeAttribute(writer, "appid", this->provider.appid);
470 writeAttribute(writer, "multiple-accounts-support",
471 this->provider.multiAccount);
473 FOREACH(i, this->provider.icon)
475 startElement(writer, "icon");
476 writeAttribute(writer, "section", i->first);
477 writeText(writer, i->second);
481 bool setDefaultLang = false;
482 FOREACH(n, this->provider.name)
484 if (!setDefaultLang && n->getLang() == L"en-gb") {
485 writeElement(writer, "label", n->getString());
486 setDefaultLang = true;
488 writeElementWithOneAttribute(writer, "label",
489 n->getString(), "xml:lang",
490 n->getLang(), n->hasLang());
492 if (!setDefaultLang) {
493 writeElement(writer, "label", this->provider.name.begin()->getString());
496 FOREACH(c, this->provider.capability)
498 startElement(writer, "capability");
499 writeText(writer, *c);
507 void Privilege::serialize(xmlTextWriterPtr writer)
509 startElement(writer, "privileges");
511 FOREACH(it, this->name)
513 startElement(writer, "privilege");
514 writeText(writer, *it);
521 void Metadata::serialize(xmlTextWriterPtr writer)
523 startElement(writer, "metadata");
524 writeAttribute(writer, "key", *this->key);
525 if (!this->value.IsNull()) {
526 writeAttribute(writer, "value", *this->value);
531 } //namespace WidgetInstall