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, "root_path", this->rootPath);
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); }
195 FOREACH(l, this->livebox) {
196 l->serialize(writer);
198 FOREACH(acc, this->account)
200 acc->serialize(writer);
203 if (!this->privileges.isEmpty()) {
204 this->privileges.serialize(writer);
210 void Author::serialize(xmlTextWriterPtr writer)
212 startElement(writer, "author");
213 writeAttribute(writer, "email", this->email, !this->email.empty());
214 writeAttribute(writer, "href", this->href, !this->href.empty());
215 writeAttribute(writer, "xml:lang", this->lang, !this->lang.empty());
216 writeText(writer, body);
220 void ServiceApplication::serialize(xmlTextWriterPtr writer)
222 startElement(writer, "service-application");
223 writeAttribute(writer, "appid", this->appid);
224 writeAttribute(writer, "auto-restart",
225 (!this->autoRestart.IsNull() &&
226 (*this->autoRestart)) ? "true" :
228 writeAttribute(writer, "exec", this->exec);
229 writeAttribute(writer, "on-boot", (!this->onBoot.IsNull() &&
230 (*this->onBoot)) ? "true" : "false");
231 writeAttribute(writer, "type", this->type);
232 FOREACH(l, this->label)
234 writeElementWithOneAttribute(writer, "label",
235 l->getString(), "xml:lang",
236 l->getLang(), l->hasLang());
238 FOREACH(i, this->icon)
240 writeElementWithOneAttribute(writer, "icon", i->getString(), "xml:lang",
241 i->getLang(), i->hasLang());
243 FOREACH(a, this->appControl)
245 a->serialize(writer);
250 void UiApplication::serialize(xmlTextWriterPtr writer)
252 startElement(writer, "ui-application");
253 writeAttribute(writer, "appid", this->appid);
254 writeAttribute(writer, "exec", this->exec);
255 if (!this->multiple.IsNull()) {
256 writeAttribute(writer, "multiple", (*this->multiple) ? "true" : "false");
258 if (!this->nodisplay.IsNull()) {
259 writeAttribute(writer,
261 (*this->nodisplay) ? "true" : "false");
263 if (!this->taskmanage.IsNull()) {
264 writeAttribute(writer,
266 (*this->taskmanage) ? "true" : "false");
268 writeAttribute(writer, "type", this->type);
269 writeAttribute(writer, "extraid", this->extraid);
270 if (!this->categories.IsNull()) {
271 writeAttribute(writer, "categories", (*this->categories));
273 FOREACH(l, this->label)
275 writeElementWithOneAttribute(writer, "label",
276 l->getString(), "xml:lang",
277 l->getLang(), l->hasLang());
279 FOREACH(i, this->icon)
281 writeElementWithOneAttribute(writer, "icon", i->getString(), "xml:lang",
282 i->getLang(), i->hasLang());
284 FOREACH(a, this->appControl)
286 a->serialize(writer);
288 FOREACH(c, this->appCategory)
290 startElement(writer, "category");
291 writeAttribute(writer, "name", *c);
294 FOREACH(m, this->metadata) {
295 m->serialize(writer);
300 void ImeApplication::serialize(xmlTextWriterPtr writer)
302 startElement(writer, "ime-application");
303 writeAttribute(writer, "appid", this->appid);
304 writeAttribute(writer, "exec", this->exec);
305 if (!this->multiple.IsNull()) {
306 writeAttribute(writer, "multiple", (*this->multiple) ? "true" : "false");
308 if (!this->nodisplay.IsNull()) {
309 writeAttribute(writer,
311 (*this->nodisplay) ? "true" : "false");
313 writeAttribute(writer, "type", this->type);
314 FOREACH(l, this->label)
316 writeElementWithOneAttribute(writer, "label",
317 l->getString(), "xml:lang",
318 l->getLang(), l->hasLang());
320 FOREACH(i, this->icon)
322 writeElementWithOneAttribute(writer, "icon", i->getString(), "xml:lang",
323 i->getLang(), i->hasLang());
328 void AppControl::serialize(xmlTextWriterPtr writer)
330 startElement(writer, "app-control");
331 FOREACH(o, this->operation)
333 startElement(writer, "operation");
334 writeAttribute(writer, "name", *o);
337 FOREACH(u, this->uri)
339 startElement(writer, "uri");
340 writeAttribute(writer, "name", *u);
343 FOREACH(m, this->mime)
345 startElement(writer, "mime");
346 writeAttribute(writer, "name", *m);
352 void LiveBox::serialize(xmlTextWriterPtr writer)
354 startElement(writer, "livebox");
355 if (!this->liveboxId.empty()) {
356 writeAttribute(writer, "appid", this->liveboxId);
359 if (!this->primary.empty()) {
360 writeAttribute(writer, "primary", this->primary);
363 if (!this->updatePeriod.empty()) {
364 writeAttribute(writer, "period", this->updatePeriod);
367 writeAttribute(writer, "abi", "html");
368 writeAttribute(writer, "network", "true");
369 writeAttribute(writer, "nodisplay", "false");
371 if (!this->label.empty()) {
372 int defaultLabelChk = 0;
373 FOREACH(m, this->label)
375 std::pair<DPL::String, DPL::String> boxLabel = *m;
376 startElement(writer, "label");
377 if (!boxLabel.first.empty()) {
378 writeAttribute(writer, "xml:lang", boxLabel.first);
382 writeText(writer, boxLabel.second);
385 if(!defaultLabelChk) {
386 startElement(writer, "label");
387 writeText(writer, DPL::FromUTF8String("NO NAME"));
391 if (!this->icon.empty()) {
392 startElement(writer, "icon");
393 writeText(writer, this->icon);
397 if (!this->autoLaunch.empty()) {
398 startElement(writer, "launch");
399 writeText(writer, this->autoLaunch);
403 if (!this->box.boxSrc.empty() &&
404 !this->box.boxMouseEvent.empty() &&
405 !this->box.boxSize.empty())
407 startElement(writer, "box");
408 writeAttribute(writer, "type", "buffer");
409 writeAttribute(writer, "mouse_event", this->box.boxMouseEvent);
410 writeAttribute(writer, "touch_effect", this->box.boxTouchEffect);
412 FOREACH(it, this->box.boxSize)
414 startElement(writer, "size");
415 if (!(*it).m_preview.empty()) {
416 writeAttribute(writer, "preview", (*it).m_preview);
418 if (!(*it).m_useDecoration.empty()) {
419 writeAttribute(writer, "need_frame", (*it).m_useDecoration);
421 // default value of use-decoration is "true"
422 writeAttribute(writer, "need_frame", DPL::String(L"true"));
425 writeText(writer, (*it).m_size);
429 startElement(writer, "script");
430 writeAttribute(writer, "src", this->box.boxSrc);
435 if (!this->box.pdSrc.empty() &&
436 !this->box.pdWidth.empty() &&
437 !this->box.pdHeight.empty())
439 startElement(writer, "pd");
440 writeAttribute(writer, "type", "buffer");
442 startElement(writer, "size");
443 DPL::String pdSize = this->box.pdWidth + DPL::String(L"x") +
445 writeText(writer, pdSize);
448 startElement(writer, "script");
449 writeAttribute(writer, "src", this->box.pdSrc);
459 void Account::serialize(xmlTextWriterPtr writer)
461 startElement(writer, "account");
463 startElement(writer, "account-provider");
464 writeAttribute(writer, "app-id", this->provider.appid);
465 writeAttribute(writer, "multiple-accounts-support",
466 this->provider.multiAccount);
468 FOREACH(i, this->provider.icon)
470 startElement(writer, "icon");
471 writeAttribute(writer, "section", i->first);
472 writeText(writer, i->second);
475 FOREACH(n, this->provider.name)
477 writeElementWithOneAttribute(writer, "label",
478 n->getString(), "xml:lang",
479 n->getLang(), n->hasLang());
481 FOREACH(c, this->provider.capability)
483 startElement(writer, "capability");
484 writeText(writer, *c);
492 void Privilege::serialize(xmlTextWriterPtr writer)
494 startElement(writer, "privileges");
496 FOREACH(it, this->name)
498 startElement(writer, "privilege");
499 writeText(writer, *it);
506 void Metadata::serialize(xmlTextWriterPtr writer)
508 startElement(writer, "metadata");
509 writeAttribute(writer, "key", *this->key);
510 if (!this->value.IsNull()) {
511 writeAttribute(writer, "value", *this->value);
516 } //namespace WidgetInstall