aboutsummaryrefslogtreecommitdiff
path: root/src/support/error/error_multiplexer.cc
blob: bbf0e71419cef45ad938e75337c147a45ff5cb65 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#include "error_multiplexer.h"

#include <xercesc/sax/SAXParseException.hpp>

#include <xalanc/PlatformSupport/DOMStringPrintWriter.hpp>

#include <algorithm>

#include "support/xalan_string.h"
#include "support/xerces_string_guard.h"

namespace {

using InputXSLT::XercesStringGuard;
using InputXSLT::ErrorMultiplexer;

inline std::string getMessage(const xercesc::SAXParseException& exception) {
	return (
		std::string(*XercesStringGuard<char>(exception.getMessage()))
		+ ". (Occurred in entity '"
		+ std::string(*XercesStringGuard<char>(exception.getSystemId()))
		+ "', at line "
		+ std::to_string(exception.getLineNumber())
		+ ", column "
		+ std::to_string(exception.getColumnNumber())
		+ ".)"
	);
}

inline ErrorMultiplexer::error_type toErrorType(
	const xalan::ProblemListenerBase::eClassification classification) {
	switch ( classification ) {
		case xalan::ProblemListenerBase::eClassification::eMessage ||
		     xalan::ProblemListenerBase::eClassification::eWarning: {
			return ErrorMultiplexer::error_type::warning;
		}
		default: {
			return ErrorMultiplexer::error_type::error;
		}
	}
}

}

namespace InputXSLT {

ErrorMultiplexer::ErrorMultiplexer(xalan::XalanTransformer* transformer):
	transformer_(transformer),
	receivers_() {
	this->transformer_->setErrorHandler(this);
	this->transformer_->setProblemListener(this);
}

ErrorMultiplexer::~ErrorMultiplexer() {
	this->transformer_->setErrorHandler(nullptr);
	this->transformer_->setProblemListener(nullptr);
}

void ErrorMultiplexer::connectReceiver(receiver* receiver) {
	this->receivers_.push_back(receiver);
}

void ErrorMultiplexer::disconnectReceiver(receiver* receiver) {
	this->receivers_.erase(
		std::remove(
			this->receivers_.begin(),
			this->receivers_.end(),
			receiver
		)
	);
}

void ErrorMultiplexer::warning(const xercesc::SAXParseException& exception) {
	this->multiplex(
		error_type::warning,
		"Warning: " + getMessage(exception)
	);
}

void ErrorMultiplexer::error(const xercesc::SAXParseException& exception) {
	this->multiplex(
		error_type::error,
		"Error: " + getMessage(exception)
	);
}

void ErrorMultiplexer::fatalError(const xercesc::SAXParseException& exception) {
	this->multiplex(
		error_type::error,
		"Fatal error: " + getMessage(exception)
	);
}

void ErrorMultiplexer::resetErrors() { }

void ErrorMultiplexer::problem(
	xalan::ProblemListenerBase::eSource         source,
	xalan::ProblemListenerBase::eClassification classification,
	const xalan::XalanDOMString&                message,
	const xalan::Locator*                       locator,
	const xalan::XalanNode*                     node
) {
	xalan::XalanDOMString problemSummary;
	xalan::DOMStringPrintWriter writer(problemSummary);

	defaultFormat(
		writer,
		source,
		classification,
		message,
		locator,
		node
	);

	this->multiplex(
		toErrorType(classification),
		toString(problemSummary)
	);
}

void ErrorMultiplexer::problem(
	xalan::ProblemListenerBase::eSource         source,
	xalan::ProblemListenerBase::eClassification classification,
	const xalan::XalanDOMString&                message,
	const xalan::XalanNode*                     node
) {
	xalan::XalanDOMString problemSummary;
	xalan::DOMStringPrintWriter writer(problemSummary);

	defaultFormat(
		writer,
		source,
		classification,
		message,
		node
	);

	this->multiplex(
		toErrorType(classification),
		toString(problemSummary)
	);
}

void ErrorMultiplexer::problem(
	xalan::ProblemListenerBase::eSource,
	xalan::ProblemListenerBase::eClassification,
	const xalan::XalanNode*,
	const xalan::ElemTemplateElement*,
	const xalan::XalanDOMString&,
	const xalan::XalanDOMChar*,
	xalan::XalanFileLoc,
	xalan::XalanFileLoc
) { }

void ErrorMultiplexer::setPrintWriter(xalan::PrintWriter*) { }

void ErrorMultiplexer::multiplex(
	const error_type type,
	const std::string& message
) {
	std::for_each(
		this->receivers_.begin(),
		this->receivers_.end(),
		[&type, &message](receiver* const receiver) -> void {
			receiver->receive(type, message);
		}
	);
}

ErrorMultiplexer::receiver::receiver(ErrorMultiplexer* multiplexer):
	multiplexer_(multiplexer) {
	this->multiplexer_->connectReceiver(this);
}

ErrorMultiplexer::receiver::~receiver() {
	this->multiplexer_->disconnectReceiver(this);
}

}