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),
159 (*this->installLocation).empty());
162 FOREACH(l, this->label)
164 writeElementWithOneAttribute(writer, "label", l->getString(),
165 "xml:lang", l->getLang(), l->hasLang());
167 FOREACH(i, this->icon)
169 writeElementWithOneAttribute(writer, "icon", i->getString(),
170 "xml:lang", i->getLang(), i->hasLang());
172 FOREACH(a, this->author)
174 a->serialize(writer);
176 FOREACH(d, this->description)
178 writeElementWithOneAttribute(writer, "description", d->getString(),
179 "xml:lang", d->getLang(), d->hasLang());
181 //FOREACH(c, this->compatibility) { c->serialize(writer); }
182 //FOREACH(d, this->deviceProfile) { d->serialize(writer); }
183 FOREACH(s, this->serviceApplication) {
184 s->serialize(writer);
186 FOREACH(u, this->uiApplication) {
187 u->serialize(writer);
189 FOREACH(i, this->imeApplication) {
190 i->serialize(writer);
192 //FOREACH(f, this->font) { f->serialize(writer); }
193 FOREACH(l, this->livebox) {
194 l->serialize(writer);
196 FOREACH(acc, this->account)
198 acc->serialize(writer);
201 if (!this->privileges.isEmpty()) {
202 this->privileges.serialize(writer);
208 void Author::serialize(xmlTextWriterPtr writer)
210 startElement(writer, "author");
211 writeAttribute(writer, "email", this->email, !this->email.empty());
212 writeAttribute(writer, "href", this->href, !this->href.empty());
213 writeAttribute(writer, "xml:lang", this->lang, !this->lang.empty());
214 writeText(writer, body);
218 void ServiceApplication::serialize(xmlTextWriterPtr writer)
220 startElement(writer, "service-application");
221 writeAttribute(writer, "appid", this->appid);
222 writeAttribute(writer, "auto-restart",
223 (!this->autoRestart.IsNull() &&
224 (*this->autoRestart)) ? "true" :
226 writeAttribute(writer, "exec", this->exec);
227 writeAttribute(writer, "on-boot", (!this->onBoot.IsNull() &&
228 (*this->onBoot)) ? "true" : "false");
229 writeAttribute(writer, "type", this->type);
230 FOREACH(l, this->label)
232 writeElementWithOneAttribute(writer, "label",
233 l->getString(), "xml:lang",
234 l->getLang(), l->hasLang());
236 FOREACH(i, this->icon)
238 writeElementWithOneAttribute(writer, "icon", i->getString(), "xml:lang",
239 i->getLang(), i->hasLang());
241 FOREACH(a, this->appControl)
243 a->serialize(writer);
248 void UiApplication::serialize(xmlTextWriterPtr writer)
250 startElement(writer, "ui-application");
251 writeAttribute(writer, "appid", this->appid);
252 writeAttribute(writer, "exec", this->exec);
253 if (!this->multiple.IsNull()) {
254 writeAttribute(writer, "multiple", (*this->multiple) ? "true" : "false");
256 if (!this->nodisplay.IsNull()) {
257 writeAttribute(writer,
259 (*this->nodisplay) ? "true" : "false");
261 if (!this->taskmanage.IsNull()) {
262 writeAttribute(writer,
264 (*this->taskmanage) ? "true" : "false");
266 writeAttribute(writer, "type", this->type);
267 writeAttribute(writer, "extraid", this->extraid);
268 if (!this->categories.IsNull()) {
269 writeAttribute(writer, "categories", (*this->categories));
271 FOREACH(l, this->label)
273 writeElementWithOneAttribute(writer, "label",
274 l->getString(), "xml:lang",
275 l->getLang(), l->hasLang());
277 FOREACH(i, this->icon)
279 writeElementWithOneAttribute(writer, "icon", i->getString(), "xml:lang",
280 i->getLang(), i->hasLang());
282 FOREACH(a, this->appControl)
284 a->serialize(writer);
286 FOREACH(c, this->appCategory)
288 startElement(writer, "category");
289 writeAttribute(writer, "name", *c);
292 FOREACH(m, this->metadata) {
293 m->serialize(writer);
298 void ImeApplication::serialize(xmlTextWriterPtr writer)
300 startElement(writer, "ime-application");
301 writeAttribute(writer, "appid", this->appid);
302 writeAttribute(writer, "exec", this->exec);
303 if (!this->multiple.IsNull()) {
304 writeAttribute(writer, "multiple", (*this->multiple) ? "true" : "false");
306 if (!this->nodisplay.IsNull()) {
307 writeAttribute(writer,
309 (*this->nodisplay) ? "true" : "false");
311 writeAttribute(writer, "type", this->type);
312 FOREACH(l, this->label)
314 writeElementWithOneAttribute(writer, "label",
315 l->getString(), "xml:lang",
316 l->getLang(), l->hasLang());
318 FOREACH(i, this->icon)
320 writeElementWithOneAttribute(writer, "icon", i->getString(), "xml:lang",
321 i->getLang(), i->hasLang());
326 void AppControl::serialize(xmlTextWriterPtr writer)
328 startElement(writer, "app-control");
329 FOREACH(o, this->operation)
331 startElement(writer, "operation");
332 writeAttribute(writer, "name", *o);
335 FOREACH(u, this->uri)
337 startElement(writer, "uri");
338 writeAttribute(writer, "name", *u);
341 FOREACH(m, this->mime)
343 startElement(writer, "mime");
344 writeAttribute(writer, "name", *m);
350 void LiveBox::serialize(xmlTextWriterPtr writer)
352 startElement(writer, "livebox");
353 if (!this->liveboxId.empty()) {
354 writeAttribute(writer, "appid", this->liveboxId);
357 if (!this->primary.empty()) {
358 writeAttribute(writer, "primary", this->primary);
361 if (!this->updatePeriod.empty()) {
362 writeAttribute(writer, "period", this->updatePeriod);
365 writeAttribute(writer, "abi", "html");
366 writeAttribute(writer, "network", "true");
367 writeAttribute(writer, "nodisplay", "false");
369 if (!this->label.empty()) {
370 int defaultLabelChk = 0;
371 FOREACH(m, this->label)
373 std::pair<DPL::String, DPL::String> boxLabel = *m;
374 startElement(writer, "label");
375 if (!boxLabel.first.empty()) {
376 writeAttribute(writer, "xml:lang", boxLabel.first);
380 writeText(writer, boxLabel.second);
383 if(!defaultLabelChk) {
384 startElement(writer, "label");
385 writeText(writer, DPL::FromUTF8String("NO NAME"));
389 if (!this->icon.empty()) {
390 startElement(writer, "icon");
391 writeText(writer, this->icon);
395 if (!this->autoLaunch.empty()) {
396 startElement(writer, "launch");
397 writeText(writer, this->autoLaunch);
401 if (!this->box.boxSrc.empty() &&
402 !this->box.boxMouseEvent.empty() &&
403 !this->box.boxSize.empty())
405 startElement(writer, "box");
406 writeAttribute(writer, "type", "buffer");
407 writeAttribute(writer, "mouse_event", this->box.boxMouseEvent);
408 writeAttribute(writer, "touch_effect", this->box.boxTouchEffect);
410 FOREACH(it, this->box.boxSize)
412 startElement(writer, "size");
413 if (!(*it).m_preview.empty()) {
414 writeAttribute(writer, "preview", (*it).m_preview);
416 if (!(*it).m_useDecoration.empty()) {
417 writeAttribute(writer, "need_frame", (*it).m_useDecoration);
419 // default value of use-decoration is "true"
420 writeAttribute(writer, "need_frame", DPL::String(L"true"));
423 writeText(writer, (*it).m_size);
427 startElement(writer, "script");
428 writeAttribute(writer, "src", this->box.boxSrc);
433 if (!this->box.pdSrc.empty() &&
434 !this->box.pdWidth.empty() &&
435 !this->box.pdHeight.empty())
437 startElement(writer, "pd");
438 writeAttribute(writer, "type", "buffer");
440 startElement(writer, "size");
441 DPL::String pdSize = this->box.pdWidth + DPL::String(L"x") +
443 writeText(writer, pdSize);
446 startElement(writer, "script");
447 writeAttribute(writer, "src", this->box.pdSrc);
457 void Account::serialize(xmlTextWriterPtr writer)
459 startElement(writer, "account");
461 startElement(writer, "account-provider");
462 writeAttribute(writer, "app-id", this->provider.appid);
463 writeAttribute(writer, "multiple-accounts-support",
464 this->provider.multiAccount);
466 FOREACH(i, this->provider.icon)
468 startElement(writer, "icon");
469 writeAttribute(writer, "section", i->first);
470 writeText(writer, i->second);
473 FOREACH(n, this->provider.name)
475 writeElementWithOneAttribute(writer, "label",
476 n->getString(), "xml:lang",
477 n->getLang(), n->hasLang());
479 FOREACH(c, this->provider.capability)
481 startElement(writer, "capability");
482 writeText(writer, *c);
490 void Privilege::serialize(xmlTextWriterPtr writer)
492 startElement(writer, "privileges");
494 FOREACH(it, this->name)
496 startElement(writer, "privilege");
497 writeText(writer, *it);
504 void Metadata::serialize(xmlTextWriterPtr writer)
506 startElement(writer, "metadata");
507 writeAttribute(writer, "key", *this->key);
508 if (!this->value.IsNull()) {
509 writeAttribute(writer, "value", *this->value);
514 } //namespace WidgetInstall