Refactoring of StepGenerateXml 99/71499/3
authorPiotr Ganicz <p.ganicz@samsung.com>
Wed, 25 May 2016 12:43:38 +0000 (14:43 +0200)
committerPiotr Ganicz <p.ganicz@samsung.com>
Fri, 3 Jun 2016 10:00:40 +0000 (12:00 +0200)
This patch provides refactoring for StepGenerateXml::process() method
into smaller pieces.

Change-Id: I3889f127bb4344a79cd18fb2875d6983a9ab20a7

src/wgt/step/pkgmgr/step_generate_xml.cc
src/wgt/step/pkgmgr/step_generate_xml.h

index 6bf7403..724203d 100644 (file)
@@ -243,6 +243,13 @@ common_installer::Step::Status StepGenerateXml::GenerateApplicationCommonXml(
   return Step::Status::OK;
 }
 
+common_installer::Step::Status StepGenerateXml::undo() {
+  bs::error_code error;
+  if (bf::exists(context_->xml_path.get()))
+    bf::remove_all(context_->xml_path.get(), error);
+  return Status::OK;
+}
+
 common_installer::Step::Status StepGenerateXml::precheck() {
   if (!context_->manifest_data.get()) {
     LOG(ERROR) << "manifest_data attribute is empty";
@@ -278,7 +285,6 @@ common_installer::Step::Status StepGenerateXml::process() {
   }
 
   xmlTextWriterPtr writer;
-
   writer = xmlNewTextWriterFilename(context_->xml_path.get().c_str(), 0);
   if (!writer) {
     LOG(ERROR) << "Failed to create new file";
@@ -286,12 +292,52 @@ common_installer::Step::Status StepGenerateXml::process() {
   }
 
   xmlTextWriterStartDocument(writer, nullptr, nullptr, nullptr);
-
   xmlTextWriterSetIndent(writer, 1);
 
-  // add manifest Element
+  Status status = GenerateManifestElement(writer);
+  if (status != Status::OK) {
+    return status;
+  }
+
+  xmlTextWriterEndDocument(writer);
+  xmlFreeTextWriter(writer);
+
+  if (pkgmgr_parser_check_manifest_validation(
+      context_->xml_path.get().c_str()) != 0) {
+    LOG(ERROR) << "Manifest is not valid";
+    return Step::Status::MANIFEST_ERROR;
+  }
+
+  LOG(DEBUG) << "Successfully create manifest xml "
+      << context_->xml_path.get();
+  return Status::OK;
+}
+
+common_installer::Step::Status StepGenerateXml::GenerateManifestElement(
+        xmlTextWriterPtr writer) {
   xmlTextWriterStartElement(writer, BAD_CAST "manifest");
 
+  GenerateManifestElementAttributes(writer);
+  GenerateLangLabels(writer);
+  GenerateAuthor(writer);
+  GenerateDescription(writer);
+  Status status = GenerateApplications(writer);
+  if (status != Status::OK) {
+    return status;
+  }
+  GeneratePrivilege(writer);
+  GenerateAccount(writer);
+  GenerateIme(writer);
+  GenerateProfiles(writer);
+  GenerateShortcuts(writer);
+  GenerateWidget(writer);
+
+  xmlTextWriterEndElement(writer);
+  return Status::OK;
+}
+
+void StepGenerateXml::GenerateManifestElementAttributes(
+        xmlTextWriterPtr writer) {
   xmlTextWriterWriteAttribute(writer, BAD_CAST "xmlns",
       BAD_CAST "http://tizen.org/ns/packages");
   xmlTextWriterWriteAttribute(writer, BAD_CAST "package",
@@ -304,7 +350,9 @@ common_installer::Step::Status StepGenerateXml::process() {
       BAD_CAST context_->manifest_data.get()->api_version);
   xmlTextWriterWriteAttribute(writer, BAD_CAST "nodisplay-setting",
       BAD_CAST context_->manifest_data.get()->nodisplay_setting);
+}
 
+void StepGenerateXml::GenerateLangLabels(xmlTextWriterPtr writer) {
   for (label_x* label :
        GListRange<label_x*>(context_->manifest_data.get()->label)) {
     xmlTextWriterStartElement(writer, BAD_CAST "label");
@@ -315,7 +363,9 @@ common_installer::Step::Status StepGenerateXml::process() {
     xmlTextWriterWriteString(writer, BAD_CAST label->name);
     xmlTextWriterEndElement(writer);
   }
+}
 
+void StepGenerateXml::GenerateAuthor(xmlTextWriterPtr writer) {
   for (author_x* author :
        GListRange<author_x*>(context_->manifest_data.get()->author)) {
     xmlTextWriterStartElement(writer, BAD_CAST "author");
@@ -330,7 +380,9 @@ common_installer::Step::Status StepGenerateXml::process() {
     xmlTextWriterWriteString(writer, BAD_CAST author->text);
     xmlTextWriterEndElement(writer);
   }
+}
 
+void StepGenerateXml::GenerateDescription(xmlTextWriterPtr writer) {
   for (description_x* description :
        GListRange<description_x*>(context_->manifest_data.get()->description)) {
     xmlTextWriterStartElement(writer, BAD_CAST "description");
@@ -341,8 +393,10 @@ common_installer::Step::Status StepGenerateXml::process() {
     xmlTextWriterWriteString(writer, BAD_CAST description->text);
     xmlTextWriterEndElement(writer);
   }
+}
 
-  // add application
+common_installer::Step::Status StepGenerateXml::GenerateApplications(
+        xmlTextWriterPtr writer) {
   for (application_x* app :
        GListRange<application_x*>(context_->manifest_data.get()->application)) {
     AppCompType type;
@@ -366,12 +420,11 @@ common_installer::Step::Status StepGenerateXml::process() {
     GenerateApplicationCommonXml(app, writer, type);
     xmlTextWriterEndElement(writer);
   }
+  return Status::OK;
+}
 
-  const auto &ime = context_->manifest_plugins_data.get().ime_info.get();
-  const auto ime_uuid = ime.uuid();
-
-  // add privilege element
-  if (context_->manifest_data.get()->privileges) {
+void StepGenerateXml::GeneratePrivilege(xmlTextWriterPtr writer) {
+    if (context_->manifest_data.get()->privileges) {
     xmlTextWriterStartElement(writer, BAD_CAST "privileges");
     for (const char* priv :
          GListRange<char*>(context_->manifest_data.get()->privileges)) {
@@ -381,7 +434,9 @@ common_installer::Step::Status StepGenerateXml::process() {
 
     xmlTextWriterEndElement(writer);
   }
+}
 
+void StepGenerateXml::GenerateAccount(xmlTextWriterPtr writer) {
   const auto& accounts =
       context_->manifest_plugins_data.get().account_info.get().accounts();
   if (!accounts.empty()) {
@@ -431,7 +486,11 @@ common_installer::Step::Status StepGenerateXml::process() {
     }
     xmlTextWriterEndElement(writer);
   }
+}
 
+void StepGenerateXml::GenerateIme(xmlTextWriterPtr writer) {
+  const auto &ime = context_->manifest_plugins_data.get().ime_info.get();
+  const auto ime_uuid = ime.uuid();
   if (!ime_uuid.empty()) {
     xmlTextWriterStartElement(writer, BAD_CAST "ime");
 
@@ -461,7 +520,9 @@ common_installer::Step::Status StepGenerateXml::process() {
 
     xmlTextWriterEndElement(writer);
   }
+}
 
+void StepGenerateXml::GenerateProfiles(xmlTextWriterPtr writer) {
   for (const char* profile :
        GListRange<char*>(context_->manifest_data.get()->deviceprofile)) {
     xmlTextWriterStartElement(writer, BAD_CAST "profile");
@@ -469,7 +530,9 @@ common_installer::Step::Status StepGenerateXml::process() {
                                 BAD_CAST profile);
     xmlTextWriterEndElement(writer);
   }
+}
 
+void StepGenerateXml::GenerateShortcuts(xmlTextWriterPtr writer) {
   const auto& shortcuts =
       context_->manifest_plugins_data.get().shortcut_info.get();
   if (!shortcuts.empty()) {
@@ -502,7 +565,9 @@ common_installer::Step::Status StepGenerateXml::process() {
     }
     xmlTextWriterEndElement(writer);
   }
+}
 
+void StepGenerateXml::GenerateWidget(xmlTextWriterPtr writer) {
   WgtBackendData* backend_data =
       static_cast<WgtBackendData*>(context_->backend_data.get());
   bf::path widget_content_path = context_->pkg_path.get() / kResWgt;
@@ -573,28 +638,6 @@ common_installer::Step::Status StepGenerateXml::process() {
 
     xmlTextWriterEndElement(writer);
   }
-
-  xmlTextWriterEndElement(writer);
-
-  xmlTextWriterEndDocument(writer);
-  xmlFreeTextWriter(writer);
-
-  if (pkgmgr_parser_check_manifest_validation(
-      context_->xml_path.get().c_str()) != 0) {
-    LOG(ERROR) << "Manifest is not valid";
-    return Step::Status::MANIFEST_ERROR;
-  }
-
-  LOG(DEBUG) << "Successfully create manifest xml "
-      << context_->xml_path.get();
-  return Status::OK;
-}
-
-common_installer::Step::Status StepGenerateXml::undo() {
-  bs::error_code error;
-  if (bf::exists(context_->xml_path.get()))
-    bf::remove_all(context_->xml_path.get(), error);
-  return Status::OK;
 }
 
 }  // namespace pkgmgr
index a00b9d3..f4be3a7 100644 (file)
@@ -37,6 +37,19 @@ class StepGenerateXml : public common_installer::Step {
                                             xmlTextWriterPtr writer,
                                             AppCompType type);
 
+  Step::Status GenerateManifestElement(xmlTextWriterPtr writer);
+  void GenerateManifestElementAttributes(xmlTextWriterPtr writer);
+  void GenerateLangLabels(xmlTextWriterPtr writer);
+  void GenerateAuthor(xmlTextWriterPtr writer);
+  void GenerateDescription(xmlTextWriterPtr writer);
+  Step::Status GenerateApplications(xmlTextWriterPtr writer);
+  void GeneratePrivilege(xmlTextWriterPtr writer);
+  void GenerateAccount(xmlTextWriterPtr writer);
+  void GenerateIme(xmlTextWriterPtr writer);
+  void GenerateProfiles(xmlTextWriterPtr writer);
+  void GenerateShortcuts(xmlTextWriterPtr writer);
+  void GenerateWidget(xmlTextWriterPtr writer);
+
   STEP_NAME(GenerateXML)
 };