Added basic foundation for XML reporting

This commit is contained in:
John Wiegley 2009-10-25 05:13:21 -04:00
parent 7411c74d6d
commit 2c80227339
16 changed files with 412 additions and 0 deletions

View file

@ -1125,6 +1125,19 @@ bool amount_t::valid() const
return true;
}
void to_xml(std::ostream& out, const amount_t& amt)
{
push_xml x(out, "amount");
if (amt.has_commodity())
to_xml(out, amt.commodity());
{
push_xml y(out, "number");
out << amt.number();
}
}
#if defined(HAVE_BOOST_SERIALIZATION)
template<class Archive>

View file

@ -745,6 +745,8 @@ inline std::istream& operator>>(std::istream& in, amount_t& amt) {
return in;
}
void to_xml(std::ostream& out, const amount_t& amt);
} // namespace ledger
#endif // _AMOUNT_H

View file

@ -110,6 +110,29 @@ private:
#endif // HAVE_BOOST_SERIALIZATION
};
inline void to_xml(std::ostream& out, const annotation_t& details)
{
push_xml x(out, "annotation");
if (details.price)
{
push_xml y(out, "ann-price");
to_xml(out, *details.price);
}
if (details.date)
{
push_xml y(out, "ann-date");
to_xml(out, *details.date);
}
if (details.tag)
{
push_xml y(out, "ann-tag");
out << y.guard(*details.tag);
}
}
struct keep_details_t
{
bool keep_price;

View file

@ -299,4 +299,12 @@ void balance_t::print(std::ostream& out,
}
}
void to_xml(std::ostream& out, const balance_t& bal)
{
push_xml x(out, "balance");
foreach (const balance_t::amounts_map::value_type& pair, bal.amounts)
to_xml(out, pair.second);
}
} // namespace ledger

View file

@ -574,6 +574,8 @@ inline std::ostream& operator<<(std::ostream& out, const balance_t& bal) {
return out;
}
void to_xml(std::ostream& out, const balance_t& amt);
} // namespace ledger
#endif // _BALANCE_H

View file

@ -654,4 +654,15 @@ bool compare_amount_commodities::operator()(const amount_t * left,
}
}
void to_xml(std::ostream& out, const commodity_t& comm)
{
push_xml x(out, "commodity");
{
push_xml y(out, "symbol");
out << y.guard(comm.symbol());
}
if (comm.is_annotated())
to_xml(out, as_annotated_commodity(comm).details);
}
} // namespace ledger

View file

@ -418,6 +418,8 @@ struct compare_amount_commodities {
bool operator()(const amount_t * left, const amount_t * right) const;
};
void to_xml(std::ostream& out, const commodity_t& comm);
} // namespace ledger
#endif // _COMMODITY_H

View file

@ -146,6 +146,12 @@ inline std::ostream& operator<<(std::ostream& out, const mask_t& mask) {
return out;
}
inline void to_xml(std::ostream& out, const mask_t& mask)
{
push_xml x(out, "mask");
out << x.guard(mask.expr.str());
}
} // namespace ledger
#endif // _MASK_H

View file

@ -43,6 +43,7 @@
#include "stats.h"
#include "generate.h"
#include "draft.h"
#include "xml.h"
#include "emacs.h"
namespace ledger {

View file

@ -116,6 +116,18 @@ std::string format_date(const date_t& when,
void set_date_format(const char * format);
void set_input_date_format(const char * format);
inline void to_xml(std::ostream& out, const datetime_t& when)
{
push_xml x(out, "datetime");
out << format_datetime(when, FMT_WRITTEN);
}
inline void to_xml(std::ostream& out, const date_t& when)
{
push_xml x(out, "date");
out << format_date(when, FMT_WRITTEN);
}
class date_interval_t : public equality_comparable<date_interval_t>
{
public:

View file

@ -646,6 +646,40 @@ inline string to_hex(uint_least32_t * message_digest, const int len = 1)
return buf.str();
}
class push_xml
{
std::ostream& out;
string tag;
public:
push_xml(std::ostream& _out, const string& _tag) : out(_out), tag(_tag) {
out << '<' << tag << '>';
}
~push_xml() {
out << "</" << tag << '>';
}
string guard(const string& str) {
std::ostringstream buf;
foreach (const char& ch, str) {
switch (ch) {
case '<':
buf << "&lt;";
break;
case '>':
buf << "&gt;";
break;
case '&':
buf << "&amp;";
break;
default:
buf << ch;
break;
}
}
return buf.str();
}
};
extern const string version;
} // namespace ledger

View file

@ -1746,4 +1746,58 @@ bool sort_value_is_less_than(const std::list<sort_value_t>& left_values,
return false;
}
void to_xml(std::ostream& out, const value_t& value)
{
push_xml x(out, "value");
switch (value.type()) {
case value_t::VOID:
out << "<void />";
break;
case value_t::BOOLEAN: {
push_xml y(out, "boolean");
out << (value.as_boolean() ? "true" : "false");
break;
}
case value_t::INTEGER: {
push_xml y(out, "integer");
out << value.as_long();
break;
}
case value_t::DATETIME:
to_xml(out, value.as_datetime());
break;
case value_t::DATE:
to_xml(out, value.as_date());
break;
case value_t::STRING: {
push_xml y(out, "string");
out << y.guard(value.as_string());
break;
}
case value_t::MASK:
to_xml(out, value.as_mask());
break;
case value_t::SEQUENCE: {
push_xml y(out, "sequence");
foreach (const value_t& member, value.as_sequence())
to_xml(out, member);
break;
}
case value_t::AMOUNT:
to_xml(out, value.as_amount());
break;
case value_t::BALANCE:
to_xml(out, value.as_balance());
break;
case value_t::SCOPE:
default:
assert(false);
break;
}
}
} // namespace ledger

View file

@ -982,6 +982,8 @@ struct sort_value_t
bool sort_value_is_less_than(const std::list<sort_value_t>& left_values,
const std::list<sort_value_t>& right_values);
void to_xml(std::ostream& out, const value_t& value);
} // namespace ledger
#endif // _VALUE_H

150
src/xml.cc Normal file
View file

@ -0,0 +1,150 @@
/*
* Copyright (c) 2003-2009, John Wiegley. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* - Neither the name of New Artisans LLC nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <system.hh>
#include "xml.h"
#include "xact.h"
#include "post.h"
#include "account.h"
#include "session.h"
#include "report.h"
namespace ledger {
namespace {
void xml_account(std::ostream& out, const account_t * acct) {
if ((acct->has_xdata() &&
acct->xdata().has_flags(ACCOUNT_EXT_VISITED)) ||
acct->children_with_flags(ACCOUNT_EXT_VISITED)) {
out << "<account id=\"";
out.width(sizeof(unsigned long) * 2);
out.fill('0');
out << std::hex << reinterpret_cast<unsigned long>(acct);
out << "\">\n";
out << "<name>" << acct->name << "</name>\n";
value_t total = acct->amount();
if (! total.is_null()) {
out << "<amount>\n";
to_xml(out, total);
out << "</amount>\n";
}
total = acct->total();
if (! total.is_null()) {
out << "<total>\n";
to_xml(out, total);
out << "</total>\n";
}
out << "</account>\n";
}
foreach (const accounts_map::value_type& pair, acct->accounts)
xml_account(out, pair.second);
}
void xml_transaction(std::ostream& out, const xact_t * xact) {
out << "<transaction>\n";
out << "<payee>" << xact->payee << "</payee>\n";
foreach (const post_t * post, xact->posts) {
if (post->has_xdata() &&
post->xdata().has_flags(POST_EXT_VISITED)) {
out << "<posting>\n";
out << "<account ref=\"";
out.width(sizeof(unsigned long) * 2);
out.fill('0');
out << std::hex << reinterpret_cast<unsigned long>(post->account);
out << "\">" << post->account->fullname() << "</account>\n";
out << "<quantity>\n";
to_xml(out, post->amount);
out << "</quantity>\n";
out << "<cost>\n";
if (post->cost)
to_xml(out, *post->cost);
else
to_xml(out, post->amount);
out << "</cost>\n";
if (post->assigned_amount) {
out << "<balance-assert>\n";
to_xml(out, *post->assigned_amount);
out << "</balance-assert>\n";
}
out << "</posting>\n";
}
}
out << "</transaction>\n";
}
}
void format_xml::flush()
{
std::ostream& out(report.output_stream);
out << "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<ledger>\n<journal>\n";
out << "<commodities>\n";
foreach (const commodities_pair& pair, commodities) {
to_xml(out, *pair.second);
out << '\n';
}
out << "</commodities>\n";
out << "<accounts>\n";
xml_account(out, report.session.journal->master);
out << "</accounts>\n";
out << "<transactions>\n";
foreach (const xact_t * xact, transactions)
xml_transaction(out, xact);
out << "</transactions>\n";
out << "</journal>\n</ledger>\n";
out.flush();
}
void format_xml::operator()(post_t& post)
{
assert(post.xdata().has_flags(POST_EXT_VISITED));
commodities.insert(commodities_pair(post.amount.commodity().symbol(),
&post.amount.commodity()));
if (transactions_set.find(post.xact) == transactions_set.end())
transactions.push_back(post.xact);
}
} // namespace ledger

90
src/xml.h Normal file
View file

@ -0,0 +1,90 @@
/*
* Copyright (c) 2003-2009, John Wiegley. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* - Neither the name of New Artisans LLC nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/**
* @addtogroup report
*/
/**
* @file xml.h
* @author John Wiegley
*
* @ingroup report
*
* @brief Brief
*
* Long.
*/
#ifndef _XML_H
#define _XML_H
#include "chain.h"
namespace ledger {
class xact_t;
class account_t;
class commodity_t;
class post_t;
class report_t;
/**
* @brief Brief
*
* Long.
*/
class format_xml : public item_handler<post_t>
{
protected:
report_t& report;
typedef std::map<string, commodity_t *> commodities_map;
typedef std::pair<string, commodity_t *> commodities_pair;
commodities_map commodities;
std::set<xact_t *> transactions_set;
std::deque<xact_t *> transactions;
public:
format_xml(report_t& _report) : report(_report) {
TRACE_CTOR(format_xml, "report&");
}
virtual ~format_xml() {
TRACE_DTOR(format_xml);
}
virtual void flush();
virtual void operator()(post_t& post);
};
} // namespace ledger
#endif // _XML_H

View file

@ -73,6 +73,7 @@ libledger_report_la_SOURCES = \
src/generate.cc \
src/draft.cc \
src/emacs.cc \
src/xml.cc \
src/output.cc \
src/precmd.cc \
src/chain.cc \
@ -136,6 +137,7 @@ pkginclude_HEADERS = \
src/generate.h \
src/stats.h \
src/output.h \
src/xml.h \
src/emacs.h \
\
src/global.h \