move string conversion method to a separate class + report SAX exception details if any
--- a/src/XMLCommand.h Fri Jan 11 15:01:02 2019 +0100
+++ b/src/XMLCommand.h Fri Jan 11 16:13:21 2019 +0100
@@ -32,6 +32,7 @@
#include <relpipe/writer/typedefs.h>
#include "StreamInputSource.h"
+#include "XercesStringConvertor.h"
namespace relpipe {
namespace in {
@@ -46,21 +47,7 @@
class RelpipeSaxHandler : public xercesc::DefaultHandler {
private:
unique_ptr<RelationalWriter> writer;
-
- std::wstring_convert<std::codecvt_utf8<wchar_t>> convertor; // TODO: support also other encodings.
-
- string_t toString(const XMLCh * const chars) {
- // XMLCh = char16_t
- // „All XML data is handled within Xerces-C++ as strings of XMLCh characters. Regardless of the size of the type chosen, the data stored in variables of type XMLCh will always be utf-16 encoded values.“
- // see https://xerces.apache.org/xerces-c/program-others-3.html
- // other solution (depends on boost): https://flylib.com/books/en/2.131.1/working_with_xerces_strings.html
-
- // TODO: review this text conversion and test on various platforms
- char* x = XMLString::transcode(chars);
- string s = string(x);
- XMLString::release(&x);
- return convertor.from_bytes(s);
- }
+ XercesStringConvertor xConvertor;
public:
@@ -81,25 +68,25 @@
void startElement(const XMLCh * const uri, const XMLCh * const localname, const XMLCh * const qname, const Attributes& attrs) override {
writer->writeAttribute(L"startElement");
- writer->writeAttribute(toString(uri));
- writer->writeAttribute(toString(localname));
- writer->writeAttribute(toString(qname));
+ writer->writeAttribute(xConvertor.toString(uri));
+ writer->writeAttribute(xConvertor.toString(localname));
+ writer->writeAttribute(xConvertor.toString(qname));
writer->writeAttribute(L"");
for (int i = 0; i < attrs.getLength(); i++) {
writer->writeAttribute(L"attribute");
- writer->writeAttribute(toString(attrs.getURI(i)));
- writer->writeAttribute(toString(attrs.getLocalName(i)));
- writer->writeAttribute(toString(attrs.getQName(i)));
- writer->writeAttribute(toString(attrs.getValue(i)));
+ writer->writeAttribute(xConvertor.toString(attrs.getURI(i)));
+ writer->writeAttribute(xConvertor.toString(attrs.getLocalName(i)));
+ writer->writeAttribute(xConvertor.toString(attrs.getQName(i)));
+ writer->writeAttribute(xConvertor.toString(attrs.getValue(i)));
}
}
void endElement(const XMLCh * const uri, const XMLCh * const localname, const XMLCh * const qname) override {
writer->writeAttribute(L"endElement");
- writer->writeAttribute(toString(uri));
- writer->writeAttribute(toString(localname));
- writer->writeAttribute(toString(qname));
+ writer->writeAttribute(xConvertor.toString(uri));
+ writer->writeAttribute(xConvertor.toString(localname));
+ writer->writeAttribute(xConvertor.toString(qname));
writer->writeAttribute(L"");
}
@@ -108,7 +95,7 @@
writer->writeAttribute(L"");
writer->writeAttribute(L"");
writer->writeAttribute(L"");
- writer->writeAttribute(toString(chars));
+ writer->writeAttribute(xConvertor.toString(chars));
}
void comment(const XMLCh * const chars, const XMLSize_t length) override {
@@ -116,7 +103,7 @@
writer->writeAttribute(L"");
writer->writeAttribute(L"");
writer->writeAttribute(L"");
- writer->writeAttribute(toString(chars));
+ writer->writeAttribute(xConvertor.toString(chars));
}
void startCDATA() override {
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/XercesStringConvertor.h Fri Jan 11 16:13:21 2019 +0100
@@ -0,0 +1,57 @@
+/**
+ * Relational pipes
+ * Copyright © 2019 František Kučera (Frantovo.cz, GlobalCode.info)
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+#pragma once
+
+#include <string>
+#include <codecvt>
+
+#include <xercesc/util/XMLString.hpp>
+
+#include <relpipe/writer/typedefs.h>
+
+namespace relpipe {
+namespace in {
+namespace xml {
+
+using namespace std;
+using namespace xercesc;
+using namespace relpipe::writer;
+
+class XercesStringConvertor {
+private:
+ wstring_convert<codecvt_utf8<wchar_t>> convertor; // TODO: support also other encodings.
+
+public:
+
+ string_t toString(const XMLCh * const chars) {
+ // XMLCh = char16_t
+ // „All XML data is handled within Xerces-C++ as strings of XMLCh characters. Regardless of the size of the type chosen, the data stored in variables of type XMLCh will always be utf-16 encoded values.“
+ // see https://xerces.apache.org/xerces-c/program-others-3.html
+ // other solution (depends on boost): https://flylib.com/books/en/2.131.1/working_with_xerces_strings.html
+
+ // TODO: review this text conversion and test on various platforms
+ char* x = XMLString::transcode(chars);
+ string s = string(x);
+ XMLString::release(&x);
+ return convertor.from_bytes(s);
+ }
+};
+
+}
+}
+}
--- a/src/relpipe-in-xml.cpp Fri Jan 11 15:01:02 2019 +0100
+++ b/src/relpipe-in-xml.cpp Fri Jan 11 16:13:21 2019 +0100
@@ -30,6 +30,7 @@
#include <relpipe/cli/CLI.h>
#include "XMLCommand.h"
+#include "XercesStringConvertor.h"
using namespace relpipe::cli;
using namespace relpipe::writer;
@@ -51,15 +52,14 @@
fwprintf(stderr, L"Debug: Input stream: eof=%ls, lastRead=%d\n", (cin.eof() ? L"true" : L"false"), cin.gcount());
resultCode = CLI::EXIT_CODE_DATA_ERROR;
} catch (const xercesc::XMLException& e) {
- // TODO: print message
- // char* message = xercesc::XMLString::transcode(e.getMessage());
- fwprintf(stderr, L"Caught xercesc::XMLException\n");
+ XercesStringConvertor xConvertor;
+ fwprintf(stderr, L"Caught xercesc::XMLException: %ls\n", xConvertor.toString(e.getMessage()).c_str());
fwprintf(stderr, L"Debug: Input stream: eof=%ls, lastRead=%d\n", (cin.eof() ? L"true" : L"false"), cin.gcount());
resultCode = CLI::EXIT_CODE_UNEXPECTED_ERROR;
} catch (const xercesc::SAXParseException& e) {
- // TODO: print message
- // char* message = xercesc::XMLString::transcode(e.getMessage());
- fwprintf(stderr, L"Caught xercesc::SAXParseException\n");
+ XercesStringConvertor xConvertor;
+ fwprintf(stderr, L"Caught xercesc::SAXParseException: %ls\n", xConvertor.toString(e.getMessage()).c_str());
+ fwprintf(stderr, L"\tat line: %d, column: %d\n", e.getLineNumber(), e.getColumnNumber());
fwprintf(stderr, L"Debug: Input stream: eof=%ls, lastRead=%d\n", (cin.eof() ? L"true" : L"false"), cin.gcount());
resultCode = CLI::EXIT_CODE_UNEXPECTED_ERROR;
}