File: SocketSAX2Handler.cpp

package info (click to toggle)
evqueue-core 2.1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,312 kB
  • sloc: cpp: 16,147; sql: 274; sh: 122; ansic: 72; makefile: 11
file content (197 lines) | stat: -rw-r--r-- 4,826 bytes parent folder | download
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*
 * This file is part of evQueue
 * 
 * evQueue 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.
 * 
 * evQueue 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 evQueue. If not, see <http://www.gnu.org/licenses/>.
 * 
 * Author: Thibault Kummer <bob@coldsource.net>
 */

#include <SocketSAX2Handler.h>
#include <NetworkInputSource.h>
#include <Exception.h>

#include <xercesc/sax2/SAX2XMLReader.hpp>
#include <xercesc/sax2/XMLReaderFactory.hpp>
#include <xercesc/sax2/DefaultHandler.hpp>
#include <xercesc/framework/MemBufInputSource.hpp>
#include <xercesc/framework/Wrapper4InputSource.hpp>

using namespace std;
using namespace xercesc;

SocketSAX2HandlerInterface::SocketSAX2HandlerInterface(const string &context)
{
	this->context = context;
}

const std::string &SocketSAX2HandlerInterface::GetRootAttribute(const std::string &name)
{
	auto it = root_attributes.find(name);
	if(it==root_attributes.end())
		throw Exception(context,"Missing '"+name+"' attribute");
	
	return it->second;
}

const std::string &SocketSAX2HandlerInterface::GetRootAttribute(const std::string &name, const std::string &default_value)
{
	auto it = root_attributes.find(name);
	if(it==root_attributes.end())
		return default_value;
	
	return it->second;
}

int SocketSAX2HandlerInterface::GetRootAttributeInt(const std::string &name)
{
	auto it = root_attributes.find(name);
	if(it==root_attributes.end())
		throw Exception(context,"Missing '"+name+"' attribute");
	
	try
	{
		return std::stoi(it->second);
	}
	catch(...)
	{
		throw Exception(context,"Attribute '"+name+"' has invalid integer value");
	}
}

int SocketSAX2HandlerInterface::GetRootAttributeInt(const std::string &name, int default_value)
{
	auto it = root_attributes.find(name);
	if(it==root_attributes.end())
		return default_value;

	try
	{
		return std::stoi(it->second);
	}
	catch(...)
	{
		throw Exception(context,"Attribute '"+name+"' has invalid integer value");
	}
}

bool SocketSAX2HandlerInterface::GetRootAttributeBool(const std::string &name)
{
	auto it = root_attributes.find(name);
	if(it==root_attributes.end())
		throw Exception(context,"Missing '"+name+"' attribute");
	
	if(it->second=="yes")
		return true;
	else if(it->second=="no")
		return false;
	
	try
	{
		return std::stoi(it->second)?true:false;
	}
	catch(...)
	{
		throw Exception(context,"Attribute '"+name+"' has invalid boolean value");
	}
}

bool SocketSAX2HandlerInterface::GetRootAttributeBool(const std::string &name, bool default_value)
{
	auto it = root_attributes.find(name);
	if(it==root_attributes.end())
		return default_value;
	
	if(it->second=="yes")
		return true;
	else if(it->second=="no")
		return false;

	try
	{
		return std::stoi(it->second)?true:false;
	}
	catch(...)
	{
		throw Exception(context,"Attribute '"+name+"' has invalid boolean value");
	}
}

SocketSAX2Handler::SocketSAX2Handler(int socket)
{
	this->socket = socket;
}

void SocketSAX2Handler::HandleQuery(SocketSAX2HandlerInterface *handler)
{
	SAX2XMLReader *parser = 0;
	
	try
	{
		parser = XMLReaderFactory::createXMLReader();
		parser->setFeature(XMLUni::fgSAX2CoreValidation, true);
		parser->setFeature(XMLUni::fgSAX2CoreNameSpaces, true);
		
		XMLSize_t lowWaterMark = 0;
		parser->setProperty(XMLUni::fgXercesLowWaterMark, &lowWaterMark);
		
		parser->setContentHandler(handler);
		parser->setErrorHandler(handler);
		
		// Create a progressive scan token
		XMLPScanToken token;
		NetworkInputSource source(socket);
		
		try
		{
			if (!parser->parseFirst(source, token))
				throw Exception("core","parseFirst failed");
			
			bool gotMore = true;
			while (gotMore && !handler->IsReady()) {
				gotMore = parser->parseNext(token);
			}
		}
		catch (const SAXParseException& toCatch)
		{
			char *message = XMLString::transcode(toCatch.getMessage());
			string excpt_msg = "Invalid query XML structure : ";
			excpt_msg.append(message);
			XMLString::release(&message);
			
			throw Exception("Query XML parsing",excpt_msg);
		}
		catch(Exception &e)
		{
			throw e;
		}
		catch(int e)  // int exception thrown to indicate that we have received a complete XML (usual case)
		{
			; // nothing to do, just let the code roll
		}
		catch (...)
		{
			throw Exception("Query XML parsing","Unexpected error trying to parse query XML");
		}
	}
	catch (Exception &e)
	{
		if (parser!=0)
			delete parser;
		
		throw e;
	}
	
	if (parser!=0)
		delete parser;
}