summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/amount.cc13
-rw-r--r--src/amount.h2
-rw-r--r--src/annotate.h23
-rw-r--r--src/balance.cc8
-rw-r--r--src/balance.h2
-rw-r--r--src/commodity.cc11
-rw-r--r--src/commodity.h2
-rw-r--r--src/mask.h6
-rw-r--r--src/report.cc1
-rw-r--r--src/times.h12
-rw-r--r--src/utils.h34
-rw-r--r--src/value.cc54
-rw-r--r--src/value.h2
-rw-r--r--src/xml.cc150
-rw-r--r--src/xml.h90
-rw-r--r--tools/Makefile.am2
16 files changed, 412 insertions, 0 deletions
diff --git a/src/amount.cc b/src/amount.cc
index 8c78a86c..456ef8cf 100644
--- a/src/amount.cc
+++ b/src/amount.cc
@@ -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>
diff --git a/src/amount.h b/src/amount.h
index add32b03..d8a19f91 100644
--- a/src/amount.h
+++ b/src/amount.h
@@ -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
diff --git a/src/annotate.h b/src/annotate.h
index 9ca994c9..37810fb9 100644
--- a/src/annotate.h
+++ b/src/annotate.h
@@ -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;
diff --git a/src/balance.cc b/src/balance.cc
index 1c096e01..59eb4d92 100644
--- a/src/balance.cc
+++ b/src/balance.cc
@@ -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
diff --git a/src/balance.h b/src/balance.h
index a4f922a8..81a7ff13 100644
--- a/src/balance.h
+++ b/src/balance.h
@@ -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
diff --git a/src/commodity.cc b/src/commodity.cc
index 0d6c11c6..7669b3db 100644
--- a/src/commodity.cc
+++ b/src/commodity.cc
@@ -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
diff --git a/src/commodity.h b/src/commodity.h
index 0d31b2bd..ef9ef5c4 100644
--- a/src/commodity.h
+++ b/src/commodity.h
@@ -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
diff --git a/src/mask.h b/src/mask.h
index a7dea7cf..18f1893d 100644
--- a/src/mask.h
+++ b/src/mask.h
@@ -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
diff --git a/src/report.cc b/src/report.cc
index 3da71616..1443dd91 100644
--- a/src/report.cc
+++ b/src/report.cc
@@ -43,6 +43,7 @@
#include "stats.h"
#include "generate.h"
#include "draft.h"
+#include "xml.h"
#include "emacs.h"
namespace ledger {
diff --git a/src/times.h b/src/times.h
index 9387320e..c2c2530e 100644
--- a/src/times.h
+++ b/src/times.h
@@ -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:
diff --git a/src/utils.h b/src/utils.h
index 48435844..6bd67146 100644
--- a/src/utils.h
+++ b/src/utils.h
@@ -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
diff --git a/src/value.cc b/src/value.cc
index e2c9dc8b..2029b9c5 100644
--- a/src/value.cc
+++ b/src/value.cc
@@ -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
diff --git a/src/value.h b/src/value.h
index db45e593..94002bef 100644
--- a/src/value.h
+++ b/src/value.h
@@ -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
diff --git a/src/xml.cc b/src/xml.cc
new file mode 100644
index 00000000..02fa7137
--- /dev/null
+++ b/src/xml.cc
@@ -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
diff --git a/src/xml.h b/src/xml.h
new file mode 100644
index 00000000..30a7b1b1
--- /dev/null
+++ b/src/xml.h
@@ -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
diff --git a/tools/Makefile.am b/tools/Makefile.am
index a41c47ce..c0404606 100644
--- a/tools/Makefile.am
+++ b/tools/Makefile.am
@@ -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 \