From a6d370e9702694770f8fd92e17f76ef1eb0be337 Mon Sep 17 00:00:00 2001 From: Michael Andres Date: Fri, 15 Apr 2011 21:52:31 +0200 Subject: [PATCH] Fix search xml output to actually write xml. (bnc#687529) --- src/Table.h | 8 ++++++- src/Zypper.cc | 3 ++- src/output/Out.cc | 10 ++++++++- src/output/Out.h | 61 +++++++++++++++++++++++++++++++-------------------- src/output/OutXML.cc | 54 +++++++++++++++++++++++++++++++++++++++++++++ src/output/OutXML.h | 2 ++ src/output/xmlout.rnc | 18 +++++++++++++-- src/search.cc | 4 ++++ 8 files changed, 131 insertions(+), 29 deletions(-) diff --git a/src/Table.h b/src/Table.h index e4a30bb..13e5508 100644 --- a/src/Table.h +++ b/src/Table.h @@ -67,6 +67,9 @@ public: } }; + const container & columns() const + { return _columns; } + private: container _columns; friend class Table; @@ -84,7 +87,7 @@ TableRow& operator << (TableRow& tr, const string& s) { return tr; } - +/** \todo nice idea but poor interface */ class Table { public: typedef list container; @@ -102,6 +105,9 @@ public: void allowAbbrev(unsigned column); void margin(unsigned margin); + const container & rows() const + { return _rows; } + Table (); private: diff --git a/src/Zypper.cc b/src/Zypper.cc index 9b7a444..3f8a1ee 100644 --- a/src/Zypper.cc +++ b/src/Zypper.cc @@ -3721,7 +3721,8 @@ void Zypper::doCommand() t.allowAbbrev(2); } - cout << t; //! \todo out().table()? + //cout << t; //! \todo out().table()? + out().searchResult( t ); } } catch (const Exception & e) diff --git a/src/output/Out.cc b/src/output/Out.cc index b4f77bf..7b5deb8 100644 --- a/src/output/Out.cc +++ b/src/output/Out.cc @@ -1,10 +1,12 @@ +#include #include #include "Out.h" +#include "Table.h" Out::~Out() { - + } bool Out::progressFilter() @@ -35,3 +37,9 @@ std::string Out::zyppExceptionReport(const zypp::Exception & e) return s.str(); } + +void Out::searchResult( const Table & table_r ) +{ + std::cout << table_r; +} + diff --git a/src/output/Out.h b/src/output/Out.h index f1cb3d3..98c4b76 100644 --- a/src/output/Out.h +++ b/src/output/Out.h @@ -11,25 +11,27 @@ #include "output/prompt.h" +class Table; + /** * Base class for producing common (for now) zypper output. - * + * * This is an abstract class providing interface for writing output like * info messages, warnings, error messages, user prompts, progress reports, * and download progress reports. See descriptions of the methods for more * details. - * + * * The output is produced using Out derived class instances. - * + * * - * + * * // create output object * SomePointerType out; * if (options.count("xmlout")) * out = new OutXML(); * else * out = new OutNormal(); - * + * * out->info("output instance ready to use", Out::HIGH); * out->info("Doing foo"); * try @@ -83,7 +85,7 @@ public: public: /** * Show an info message. - * + * * \param msg The message to be displayed. * \param verbosity Minimal level o verbosity in which the message will be * shown. Out::QUIET means the message will be always be @@ -92,13 +94,13 @@ public: * (-vv). * \param mask Determines the types of output for which is this message * intended. By default, the message will be shown in all - * types of output. + * types of output. */ virtual void info(const std::string & msg, Verbosity verbosity = NORMAL, Type mask = TYPE_ALL) = 0; /** * Show a warning. - * + * * \param msg The warning message to be displayed. * \param verbosity Minimal level o verbosity in which the message will be * shown. Out::QUIET means the message will be always be @@ -107,15 +109,15 @@ public: * (-vv). * \param mask Determines the types of output for which is this message * intended. By default, the message will be shown in all - * types of output. + * types of output. */ virtual void warning(const std::string & msg, Verbosity verbosity = NORMAL, Type mask = TYPE_ALL) = 0; /** * Show an error message and an optional hint. - * + * * An error message should be shown regardless of the verbosity level. - * + * * \param problem_desc Problem description (what happend) * \param hint Hint for the user (what to do, or explanation) */ @@ -124,7 +126,7 @@ public: /** * Prints the problem description caused by an exception, its cause and, * optionaly, a hint for the user. - * + * * \param e Exception which caused the problem. * \param Problem description for the user. * \param Hint for the user how to cope with the problem. @@ -139,7 +141,7 @@ public: //@{ /** * Start of an operation with reported progress. - * + * * \param id Identifier. Any string used to match multiple overlapping * progress reports (doesn't happen now, * but probably will in the future). @@ -153,7 +155,7 @@ public: /** * Progress report for an on-going operation. - * + * * \param id Identifier. Any string used to match multiple overlapping * progress reports. * \param label Progress description. @@ -166,7 +168,7 @@ public: /** * End of an operation with reported progress. - * + * * \param id Identifier. Any string used to match multiple overlapping * progress reports. * \param label Progress description. @@ -182,15 +184,15 @@ public: //@{ /** * Reoprt start of a download. - * - * \param uri Uri of the file to download. + * + * \param uri Uri of the file to download. */ virtual void dwnldProgressStart(const zypp::Url & uri) = 0; /** * Reports download progress. - * - * \param uri Uri of the file being downloaded. + * + * \param uri Uri of the file being downloaded. * \param value Value of the progress in percents. -1 if unknown. * \param rate Current download rate in B/s. -1 if unknown. */ @@ -199,8 +201,8 @@ public: long rate = -1) = 0; /** * Reports end of a download. - * - * \param uri Uri of the file to download. + * + * \param uri Uri of the file to download. * \param rate Average download rate at the end. -1 if unknown. * \param error Error flag - did the download finish with error? */ @@ -210,15 +212,26 @@ public: //@} /** + * Print out a search result. + * + * Default implementation prints \a table_r on \c stdout. + * + * \param table_r Table containing the search result. + * + * \todo Using a more generic format than a Table is desired. + */ + virtual void searchResult( const Table & table_r ); + + /** * Prompt the user for a decision. - * + * * \param id Unique prompt identifier for use by machines. * \param prompt Prompt text. * \param options A PromptOptions object * \param startdesc Initial detailed description of the prompt to be * prepended to the \a prompt text. Should be used * only whe prompting for the first time and left empty - * when retrying after an invalid answer has been given. + * when retrying after an invalid answer has been given. * \see prompt.h * \see ../zypper-prompt.h */ @@ -251,7 +264,7 @@ protected: /** * Determine whether to show progress. - * + * * \return true if the progress should be filtered out, * false if it should be shown. */ diff --git a/src/output/OutXML.cc b/src/output/OutXML.cc index 6d0c584..0cc042e 100644 --- a/src/output/OutXML.cc +++ b/src/output/OutXML.cc @@ -3,9 +3,11 @@ #include #include "zypp/base/String.h" +#include "zypp/base/String.h" #include "OutXML.h" #include "utils/misc.h" +#include "Table.h" using std::cout; using std::endl; @@ -186,6 +188,58 @@ void OutXML::dwnldProgressEnd(const zypp::Url & uri, long rate, bool error) << "/>" << endl; } +void OutXML::searchResult( const Table & table_r ) +{ + cout << "" << endl; + cout << "" << endl; + + const Table::container & rows( table_r.rows() ); + if ( ! rows.empty() ) + { + // + // *** CAUTION: It's a mess, but must mtch the header list defined + // in FillSearchTableSolvable ctor (search.cc) + // + static const char * header[] = { + "status", + "name", + "kind", + "edition", + "arch", + "repository" + }; + for_( it, rows.begin(), rows.end() ) + { + cout << "columns() ); + unsigned cidx = 0; + for_( cit, cols.begin(), cols.end() ) + { + cout << ' ' << (cidx < 6 ? header[cidx] : "?" ) << "=\""; + if ( cidx == 0 ) + { + if ( *cit == "i" ) + cout << "installed\""; + else if ( *cit == "v" ) + cout << "other-version\""; + else + cout << "not-installed\""; + } + else + { + cout << *cit << '"'; + } + ++cidx; + } + cout << "/>" << endl; + } + } + //Out::searchResult( table_r ); + + cout << "" << endl; + cout << "" << endl; +} + void OutXML::prompt(PromptId id, const string & prompt, const PromptOptions & poptions, diff --git a/src/output/OutXML.h b/src/output/OutXML.h index 45763e6..201c402 100644 --- a/src/output/OutXML.h +++ b/src/output/OutXML.h @@ -37,6 +37,8 @@ public: long rate = -1, bool error = false); + virtual void searchResult( const Table & table_r ); + virtual void prompt(PromptId id, const std::string & prompt, const PromptOptions & poptions, diff --git a/src/output/xmlout.rnc b/src/output/xmlout.rnc index 087b1c6..ef0ce9d 100644 --- a/src/output/xmlout.rnc +++ b/src/output/xmlout.rnc @@ -14,7 +14,8 @@ stream-element = install-summary-element* | # for zypper install/remove/update repo-list-element? | # for zypper repos service-list-element? | - selectable-list-element? | # for zypper search + selectable-list-element? | + search-result-element? | # for zypper search selectable-info-element? | # for zypper info # random text can appear between tags - this text should be ignored @@ -107,15 +108,28 @@ patch-update = attribute interactive { xsd:boolean } # needs user interaction? } +search-result-element = + element search-result { + attribute version {xsd:string}, + solvable-list-element + } + +solvable-list-element = + element solvable-list { + solvable-element* + } + solvable-element = element solvable { - attribute type { "package" | "patch" | "pattern" | "product" }, + attribute status { "installed" | "other-version" | "not-installed" }, + attribute kind { "package" | "patch" | "pattern" | "product" }, attribute name { xsd:string }, attribute edition { xsd:string }, # target edition attribute edition-old { xsd:string }?, # currently installed version in case of upgrade attribute arch { xsd:string }, attribute arch-old { xsd:string }?, # currently installed architecture attribute summary { xsd:string }?, + attribute repository { xsd:string }?, text? } diff --git a/src/search.cc b/src/search.cc index 6ae2c62..bb9bf63 100644 --- a/src/search.cc +++ b/src/search.cc @@ -40,6 +40,10 @@ FillSearchTableSolvable::FillSearchTableSolvable( TableHeader header; + // + // *** CAUTION: It's a mess, but adding/changing colums here requires + // adapting OutXML::searchResult ! + // if (_gopts.is_rug_compatible) { header -- 2.7.4