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);
200 void Author::serialize(xmlTextWriterPtr writer)
202 startElement(writer, "author");
203 writeAttribute(writer, "email", this->email, !this->email.empty());
204 writeAttribute(writer, "href", this->href, !this->href.empty());
205 writeAttribute(writer, "xml:lang", this->lang, !this->lang.empty());
206 writeText(writer, body);
210 void ServiceApplication::serialize(xmlTextWriterPtr writer)
212 startElement(writer, "service-application");
213 writeAttribute(writer, "appid", this->appid);
214 writeAttribute(writer, "auto-restart",
215 (!this->autoRestart.IsNull() &&
216 (*this->autoRestart)) ? "true" :
218 writeAttribute(writer, "exec", this->exec);
219 writeAttribute(writer, "on-boot", (!this->onBoot.IsNull() &&
220 (*this->onBoot)) ? "true" : "false");
221 writeAttribute(writer, "type", this->type);
222 FOREACH(l, this->label)
224 writeElementWithOneAttribute(writer, "label",
225 l->getString(), "xml:lang",
226 l->getLang(), l->hasLang());
228 FOREACH(i, this->icon)
230 writeElementWithOneAttribute(writer, "icon", i->getString(), "xml:lang",
231 i->getLang(), i->hasLang());
233 FOREACH(a, this->appControl)
235 a->serialize(writer);
240 void UiApplication::serialize(xmlTextWriterPtr writer)
242 startElement(writer, "ui-application");
243 writeAttribute(writer, "appid", this->appid);
244 writeAttribute(writer, "exec", this->exec);
245 if (!this->multiple.IsNull()) {
246 writeAttribute(writer, "multiple", (*this->multiple) ? "true" : "false");
248 if (!this->nodisplay.IsNull()) {
249 writeAttribute(writer,
251 (*this->nodisplay) ? "true" : "false");
253 if (!this->taskmanage.IsNull()) {
254 writeAttribute(writer,
256 (*this->taskmanage) ? "true" : "false");
258 writeAttribute(writer, "type", this->type);
259 writeAttribute(writer, "extraid", this->extraid);
260 if (!this->categories.IsNull()) {
261 writeAttribute(writer, "categories", (*this->categories));
263 FOREACH(l, this->label)
265 writeElementWithOneAttribute(writer, "label",
266 l->getString(), "xml:lang",
267 l->getLang(), l->hasLang());
269 FOREACH(i, this->icon)
271 writeElementWithOneAttribute(writer, "icon", i->getString(), "xml:lang",
272 i->getLang(), i->hasLang());
274 FOREACH(a, this->appControl)
276 a->serialize(writer);
278 FOREACH(c, this->appCategory)
280 startElement(writer, "category");
281 writeAttribute(writer, "name", *c);
287 void ImeApplication::serialize(xmlTextWriterPtr writer)
289 startElement(writer, "ime-application");
290 writeAttribute(writer, "appid", this->appid);
291 writeAttribute(writer, "exec", this->exec);
292 if (!this->multiple.IsNull()) {
293 writeAttribute(writer, "multiple", (*this->multiple) ? "true" : "false");
295 if (!this->nodisplay.IsNull()) {
296 writeAttribute(writer,
298 (*this->nodisplay) ? "true" : "false");
300 writeAttribute(writer, "type", this->type);
301 FOREACH(l, this->label)
303 writeElementWithOneAttribute(writer, "label",
304 l->getString(), "xml:lang",
305 l->getLang(), l->hasLang());
307 FOREACH(i, this->icon)
309 writeElementWithOneAttribute(writer, "icon", i->getString(), "xml:lang",
310 i->getLang(), i->hasLang());
315 void AppControl::serialize(xmlTextWriterPtr writer)
317 startElement(writer, "app-control");
318 FOREACH(o, this->operation)
320 startElement(writer, "operation");
321 writeAttribute(writer, "name", *o);
324 FOREACH(u, this->uri)
326 startElement(writer, "uri");
327 writeAttribute(writer, "name", *u);
330 FOREACH(m, this->mime)
332 startElement(writer, "mime");
333 writeAttribute(writer, "name", *m);
339 void LiveBox::serialize(xmlTextWriterPtr writer)
341 startElement(writer, "livebox");
342 if (!this->liveboxId.empty()) {
343 writeAttribute(writer, "appid", this->liveboxId);
346 if (!this->primary.empty()) {
347 writeAttribute(writer, "primary", this->primary);
350 if (!this->updatePeriod.empty()) {
351 writeAttribute(writer, "period", this->updatePeriod);
354 writeAttribute(writer, "abi", "html");
355 writeAttribute(writer, "network", "true");
356 writeAttribute(writer, "nodisplay", "false");
358 if (!this->label.empty()) {
359 startElement(writer, "label");
360 writeText(writer, this->label);
364 if (!this->icon.empty()) {
365 startElement(writer, "icon");
366 writeText(writer, this->icon);
370 if (!this->autoLaunch.empty()) {
371 startElement(writer, "launch");
372 writeText(writer, this->autoLaunch);
376 if (!this->box.boxSrc.empty() &&
377 !this->box.boxMouseEvent.empty() &&
378 !this->box.boxSize.empty())
380 startElement(writer, "box");
381 writeAttribute(writer, "type", "buffer");
382 writeAttribute(writer, "mouse_event", this->box.boxMouseEvent);
383 writeAttribute(writer, "touch_effect", this->box.boxTouchEffect);
385 FOREACH(m, this->box.boxSize)
387 std::pair<DPL::String, DPL::String> boxSize = *m;
388 startElement(writer, "size");
389 if (!boxSize.second.empty()) {
390 writeAttribute(writer, "preview", boxSize.second);
392 writeText(writer, boxSize.first);
396 startElement(writer, "script");
397 writeAttribute(writer, "src", this->box.boxSrc);
402 if (!this->box.pdSrc.empty() &&
403 !this->box.pdWidth.empty() &&
404 !this->box.pdHeight.empty())
406 startElement(writer, "pd");
407 writeAttribute(writer, "type", "buffer");
409 startElement(writer, "size");
410 DPL::String pdSize = this->box.pdWidth + DPL::String(L"x") +
412 writeText(writer, pdSize);
415 startElement(writer, "script");
416 writeAttribute(writer, "src", this->box.pdSrc);
426 } //namespace WidgetInstall