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 {
29 void writeElement(xmlTextWriterPtr writer, const char * name, DPL::String body)
31 int state = xmlTextWriterWriteElement(writer, BAD_CAST name,
32 BAD_CAST DPL::ToUTF8String(body).c_str());
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());
45 ThrowMsg(LibxmlUtils::Libxml2Error, "xmlTextWriterWriteText failed");
49 void writeElement(xmlTextWriterPtr writer, const char * name, const char * body)
51 int state = xmlTextWriterWriteElement(writer, BAD_CAST name, BAD_CAST body);
54 ThrowMsg(LibxmlUtils::Libxml2Error, "xmlTextWriterWriteElement failed");
58 void writeElementWithOneAttribute(xmlTextWriterPtr writer, const char * name,
59 DPL::String body, const char * nameAttr, DPL::String bodyAttr,
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);
73 ThrowMsg(LibxmlUtils::Libxml2Error, "xmlTextWriterStartElement failed");
77 void endElement(xmlTextWriterPtr writer)
79 int state = xmlTextWriterEndElement(writer);
82 ThrowMsg(LibxmlUtils::Libxml2Error, "xmlTextWriterEndElement failed");
86 void writeAttribute(xmlTextWriterPtr writer, const char * name, DPL::String body,
87 bool condition = true)
91 int state = xmlTextWriterWriteAttribute(writer, BAD_CAST name,
92 BAD_CAST DPL::ToUTF8String(body).c_str());
95 ThrowMsg(LibxmlUtils::Libxml2Error, "xmlTextWriterWriteAttribute failed");
99 void writeAttribute(xmlTextWriterPtr writer, const char * name, const char * body,
100 bool condition = true)
104 int state = xmlTextWriterWriteAttribute(writer, BAD_CAST name, BAD_CAST body);
107 ThrowMsg(LibxmlUtils::Libxml2Error, "xmlTextWriterWriteAttribute failed");
111 void Manifest::generate(DPL::String filename)
113 xmlTextWriterPtr writer;
115 writer = xmlNewTextWriterFilename(DPL::ToUTF8String(filename).c_str(), 0); //compression set to 0
117 ThrowMsg(LibxmlUtils::Libxml2Error, "xmlNewTextWriterFilename failed");
118 state = xmlTextWriterSetIndent (writer, 1);
121 ThrowMsg(LibxmlUtils::Libxml2Error, "xmlTextWriterSetIndent failed");
124 state = xmlTextWriterStartDocument(writer, NULL, "utf-8", NULL);
127 ThrowMsg(LibxmlUtils::Libxml2Error, "xmlTextWriterStartDocument failed");
129 this->serialize(writer);
130 state = xmlTextWriterEndDocument(writer);
133 ThrowMsg(LibxmlUtils::Libxml2Error, "xmlTextWriterEndDocument failed");
137 xmlFreeTextWriter(writer);
142 void Manifest::serialize(xmlTextWriterPtr writer)
144 startElement(writer, "manifest");
146 writeAttribute(writer, "xmlns", "http://tizen.org/ns/packages");
147 writeAttribute(writer, "package", this->package);
148 writeAttribute(writer, "type", this->type);
149 writeAttribute(writer, "version", this->version);
150 if (!this->installLocation.IsNull())
151 writeAttribute(writer, "install-location", (*this->installLocation),
152 (*this->installLocation).empty());
154 FOREACH(l, this->label)
156 writeElementWithOneAttribute(writer, "label", l->getString(),
157 "xml:lang", l->getLang(), l->hasLang());
159 FOREACH(i, this->icon)
161 writeElementWithOneAttribute(writer, "icon", i->getString(),
162 "xml:lang", i->getLang(), i->hasLang());
164 FOREACH(a, this->author)
166 a->serialize(writer);
168 FOREACH(d, this->description)
170 writeElementWithOneAttribute(writer, "description", d->getString(),
171 "xml:lang", d->getLang(), d->hasLang());
173 //FOREACH(c, this->compatibility) { c->serialize(writer); }
174 //FOREACH(d, this->deviceProfile) { d->serialize(writer); }
175 FOREACH(s, this->serviceApplication) { s->serialize(writer); }
176 FOREACH(u, this->uiApplication) { u->serialize(writer); }
177 FOREACH(i, this->imeApplication) { i->serialize(writer); }
178 //FOREACH(f, this->font) { f->serialize(writer); }
179 FOREACH(l, this->livebox) { l->serialize(writer); }
184 void Author::serialize(xmlTextWriterPtr writer)
186 startElement(writer, "author");
187 writeAttribute(writer, "email", this->email, !this->email.empty());
188 writeAttribute(writer, "href", this->href, !this->href.empty());
189 writeAttribute(writer, "xml:lang", this->lang, !this->lang.empty());
190 writeText(writer, body);
194 void ServiceApplication::serialize(xmlTextWriterPtr writer)
196 startElement(writer, "service-application");
197 writeAttribute(writer, "appid", this->appid);
198 writeAttribute(writer, "auto-restart", (!this->autoRestart.IsNull() &&
199 (*this->autoRestart)) ? "true" : "false");
200 writeAttribute(writer, "exec", this->exec);
201 writeAttribute(writer, "on-boot", (!this->onBoot.IsNull() &&
202 (*this->onBoot)) ? "true" : "false");
203 writeAttribute(writer, "type", this->type);
204 FOREACH(l, this->label)
206 writeElementWithOneAttribute(writer, "label", l->getString(), "xml:lang",
207 l->getLang(), l->hasLang());
209 FOREACH(i, this->icon)
211 writeElementWithOneAttribute(writer, "icon", i->getString(), "xml:lang",
212 i->getLang(), i->hasLang());
214 FOREACH(a, this->appControl)
216 a->serialize(writer);
221 void UiApplication::serialize(xmlTextWriterPtr writer)
223 startElement(writer, "ui-application");
224 writeAttribute(writer, "appid", this->appid);
225 writeAttribute(writer, "exec", this->exec);
226 if (!this->multiple.IsNull())
227 writeAttribute(writer, "multiple", (*this->multiple) ? "true" : "false");
228 if (!this->nodisplay.IsNull())
229 writeAttribute(writer, "nodisplay", (*this->nodisplay) ? "true" : "false");
230 if (!this->taskmanage.IsNull())
231 writeAttribute(writer, "taskmanage", (*this->taskmanage) ? "true" : "false");
232 writeAttribute(writer, "type", this->type);
233 writeAttribute(writer, "extraid", this->extraid);
234 if (!this->categories.IsNull())
235 writeAttribute(writer, "categories", (*this->categories));
236 FOREACH(l, this->label)
238 writeElementWithOneAttribute(writer, "label", l->getString(), "xml:lang",
239 l->getLang(), l->hasLang());
241 FOREACH(i, this->icon)
243 writeElementWithOneAttribute(writer, "icon", i->getString(), "xml:lang",
244 i->getLang(), i->hasLang());
246 FOREACH(a, this->appControl)
248 a->serialize(writer);
250 FOREACH(c, this->appCategory)
252 startElement(writer, "category");
253 writeAttribute(writer, "name", *c);
259 void ImeApplication::serialize(xmlTextWriterPtr writer)
261 startElement(writer, "ime-application");
262 writeAttribute(writer, "appid", this->appid);
263 writeAttribute(writer, "exec", this->exec);
264 if (!this->multiple.IsNull())
265 writeAttribute(writer, "multiple", (*this->multiple) ? "true" : "false");
266 if (!this->nodisplay.IsNull())
267 writeAttribute(writer, "nodisplay", (*this->nodisplay) ? "true" : "false");
268 writeAttribute(writer, "type", this->type);
269 FOREACH(l, this->label)
271 writeElementWithOneAttribute(writer, "label", l->getString(), "xml:lang",
272 l->getLang(), l->hasLang());
274 FOREACH(i, this->icon)
276 writeElementWithOneAttribute(writer, "icon", i->getString(), "xml:lang",
277 i->getLang(), i->hasLang());
282 void AppControl::serialize(xmlTextWriterPtr writer)
284 startElement(writer, "app-control");
285 FOREACH(o, this->operation)
287 startElement(writer, "operation");
288 writeAttribute(writer, "name", *o);
291 FOREACH(u, this->uri)
293 startElement(writer, "uri");
294 writeAttribute(writer, "name", *u);
297 FOREACH(m, this->mime)
299 startElement(writer, "mime");
300 writeAttribute(writer, "name", *m);
306 void LiveBox::serialize(xmlTextWriterPtr writer)
308 startElement(writer, "livebox");
309 if(!this->appid.empty()) {
310 writeAttribute(writer, "appid", this->appid);
312 if(!this->auto_launch.empty()) {
313 writeAttribute(writer, "auto_launch", this->auto_launch);
315 if(!this->network.empty()) {
316 writeAttribute(writer, "network", this->network);
318 writeAttribute(writer, "abi", "html");
319 if(!this->period.empty()) {
320 writeAttribute(writer, "period", this->period);
322 if(!this->nodisplay.empty()) {
323 writeAttribute(writer, "nodisplay", this->nodisplay);
325 if(!this->primary.empty()) {
326 writeAttribute(writer, "primary", this->primary);
328 if(!this->timeout.empty()) {
329 writeAttribute(writer, "timeout", this->timeout);
331 if(!this->label.empty()) {
332 startElement(writer, "label");
333 writeText(writer, this->label);
337 if(!this->icon.empty()) {
338 startElement(writer, "icon");
339 writeText(writer, this->icon);
343 if(!this->box.boxSrc.empty() && !this->box.boxSize.empty())
345 startElement(writer, "box");
346 writeAttribute(writer, "type", "buffer");
348 FOREACH(m, this->box.boxSize)
350 std::pair<DPL::String, DPL::String> boxSize = *m;
351 startElement(writer, "size");
352 if(!boxSize.second.empty())
353 writeAttribute(writer, "preview", boxSize.second);
354 writeText(writer, boxSize.first);
358 startElement(writer, "script");
359 writeAttribute(writer, "src", this->box.boxSrc);
364 if(!this->box.pdSrc.empty() && ! this->box.pdWidth.empty() && ! this->box.pdHeight.empty()) {
365 startElement(writer, "pd");
366 writeAttribute(writer, "type", "buffer");
368 startElement(writer, "size");
369 DPL::String pdSize = this->box.pdWidth+DPL::String(L"x")+this->box.pdHeight;
370 writeText(writer, pdSize);
373 startElement(writer, "script");
374 writeAttribute(writer, "src", this->box.pdSrc);
385 } //namespace WidgetInstall