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 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408
|
/**************************************************************************
* *
* Regina - A Normal Surface Theory Calculator *
* Computational Engine *
* *
* Copyright (c) 1999-2008, Ben Burton *
* For further details contact Ben Burton (bab@debian.org). *
* *
* 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 2 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, write to the Free *
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, *
* MA 02110-1301, USA. *
* *
**************************************************************************/
/* end stub */
/*! \file xmlutils.h
* \brief Various classes and routines for XML manipulation, some taken
* or modified from the libxml++ library. The libxml2 library is used
* to do most of the underlying work.
*
* The libxml++ library is copyright (C) 2000 by Ari Johnson, and is
* covered by the GNU Lesser General Public License.
*/
#ifndef __XMLUTILS_H
#ifndef __DOXYGEN
#define __XMLUTILS_H
#endif
#include <string>
#include <sstream>
#include <libxml/parser.h>
#include "utilities/nbooleans.h"
#include "utilities/hashmap.h"
#include "utilities/hashutils.h"
namespace regina {
/**
* Various classes and routines for XML manipulation, some taken or
* modified from the libxml++ library. The libxml2 library is used
* to do most of the underlying work.
*
* See the xmlutils.h file documentation for libxml++ license details.
*/
namespace xml {
/**
* \weakgroup utilities
* @{
*/
class XMLParser;
/**
* Represents a hashed map from property names to property values.
*
* \ifacespython Not present.
*/
class XMLPropertyDict : public stdhash::hash_map<std::string,
std::string, HashString> {
public:
/**
* Create a new hashed map.
*/
XMLPropertyDict();
/**
* Return a value for the given key, even if the key does not
* exist in the hashed map.
*
* @param key the key to look up.
* @param defaultVal the value to return if the key does not exist.
* @return the value associated with the given key, or parameter
* \a default if the key does not exist in the hashed map.
*/
const std::string& lookup(const std::string& key,
const std::string& defaultVal = std::string()) const;
};
/**
* Provides the callbacks for an XMLParser. The various routines in
* this class will be called when corresponding elements of the XML file
* being parsed are encountered.
*
* The routines in this class do nothing; you will need to create a
* derived class that overrides some or all of these routines to do the
* processing that you require.
*
* \ifacespython Not present.
*
* @author This class was taken and modified from the libxml++ library
* (<tt>http://lusis.org/~ari/xml++/</tt>).
*/
class XMLParserCallback {
public:
/**
* Default destructor that does nothing.
*/
virtual ~XMLParserCallback();
/**
* Called at the start of the document.
*
* @param parser the XML parser that is currently parsing this document.
*/
virtual void start_document(XMLParser* parser);
/**
* Called when the document is finalised.
*/
virtual void end_document();
/**
* Called when an element's opening tag is encountered.
*
* @param n the name of the tag.
* @param p a hashed dictionary of all the properties of the tag.
*/
virtual void start_element(const std::string& n,
const regina::xml::XMLPropertyDict& p);
/**
* Called when an element's closing tag is encountered.
* This is called immediately after start_element() if the
* opening tag is in <tt>\<tag/\></tt> format.
*
* @param n the name of the tag.
*/
virtual void end_element(const std::string& n);
/**
* Called when characters are encountered.
*
* @param s the characters encountered.
*/
virtual void characters(const std::string& s);
/**
* Called when a comment is encountered.
*
* @param s the comment string.
*/
virtual void comment(const std::string& s);
/**
* Called when a parser warning occurs.
*
* @param s the warning message.
*/
virtual void warning(const std::string& s);
/**
* Called when a parser error occurs.
*
* @param s the error message.
*/
virtual void error(const std::string& s);
/**
* Called when a parser fatal error occurs.
*
* @param s the error message.
*/
virtual void fatal_error(const std::string& s);
};
/**
* Used to parse an entire XML file. When particular XML components are
* encountered, this will be signalled by calling corresponding routines
* from the XMLParserCallback that is passed to the XMLParser constructor.
*
* To parse an entire XML file, simply call static routine parse_stream(),
* which does not require you to create an XMLParser yourself.
*
* If you desire more fine-grained control over the parsing process, you
* may create an XMLParser yourself and parse the file manually in small
* pieces. To do this, routine parse_chunk() should be called
* repeatedly with consecutive pieces of the XML file. Once the entire
* XML file has been sent through parse_chunk(), routine finish() should
* be called once to signal that processing is complete.
*
* \ifacespython Not present.
*
* @author This class was taken and modified from the libxml++ library
* (<tt>http://lusis.org/~ari/xml++/</tt>).
*/
class XMLParser {
private:
XMLParserCallback& _parser_callback;
/**< Provides the callback routines to use with this parser. */
xmlParserCtxtPtr _context;
/**< The parser context to use with the underlying XML library. */
public:
/**
* Creates a new XML parser.
*
* @param callback the object providing the routines to call
* when particular XML components are encountered during parsing.
*/
XMLParser(XMLParserCallback& callback);
/**
* Destroys this XML parser.
*/
virtual ~XMLParser();
/**
* Parses the given chunk of XML.
*
* @param s the chunk of XML to parse.
*/
void parse_chunk(const std::string& s);
/**
* Signals that there are no more XML chunks to parse.
*/
void finish();
/**
* Parses an entire XML file. The given stream will be read
* from until end-of-file is reached.
*
* @param callback the object providing the routines to call
* when particular XML components are encountered during parsing.
* @param file the stream from which the raw XML will be read.
* @param chunkSize the number of characters to read and process
* at a time (this is the size of each string that will be
* passed to parse_chunk()).
*/
static void parse_stream(XMLParserCallback& callback,
std::istream& file, unsigned chunkSize = 1024);
private:
/**
* Link between C callback routine and C++ callback routine.
*/
static xmlEntityPtr _get_entity(void *_parser, const xmlChar *n);
/**
* Link between C callback routine and C++ callback routine.
*/
static void _start_document(void *_parser);
/**
* Link between C callback routine and C++ callback routine.
*/
static void _end_document(void *_parser);
/**
* Link between C callback routine and C++ callback routine.
*/
static void _start_element(void *_parser, const xmlChar *n,
const xmlChar **p);
/**
* Link between C callback routine and C++ callback routine.
*/
static void _end_element(void *_parser, const xmlChar *n);
/**
* Link between C callback routine and C++ callback routine.
*/
static void _characters(void *_parser, const xmlChar *s, int len);
/**
* Link between C callback routine and C++ callback routine.
*/
static void _comment(void *_parser, const xmlChar *s);
/**
* Link between C callback routine and C++ callback routine.
*/
static void _warning(void *_parser, const char *fmt, ...);
/**
* Link between C callback routine and C++ callback routine.
*/
static void _error(void *_parser, const char *fmt, ...);
/**
* Link between C callback routine and C++ callback routine.
*/
static void _fatal_error(void *_parser, const char *fmt, ...);
};
/**
* Returns the given string with special characters converted to XML
* entities. For instance, the string <tt>"a \< b"</tt> would be
* converted to <tt>"a \< b"</tt>.
*
* \ifacespython Not present.
*
* @param original the string to convert; this string will not be
* changed.
* @return the converted string with special characters replaced by
* XML entities.
*/
std::string xmlEncodeSpecialChars(const std::string& original);
/**
* Returns the given string encoded so it is suitable for use inside an
* XML comment. As well as converting special characters to XML
* entities, this routine will replace dashes with underscores to avoid
* double-hyphens (which are illegal in XML comments).
*
* \ifacespython Not present.
*
* @param comment the string to convert; this string will not be
* changed.
* @return the string converted to be usable inside an XML comment.
*/
std::string xmlEncodeComment(const std::string& comment);
/**
* Returns an XML tag with a single property containing the given value.
* The tag will be of the form <tt>\<tagName value="..."/\></tt>.
*
* The value itself will be written to the tag string using the standard
* output stream operator \<\<.
*
* \pre The property value when written to an output stream does not
* contain any special characters (such as <tt>\<</tt> or <tt>\&</tt>)
* that need to be encoded as XML entities.
*
* \ifacespython Not present.
*
* @param tagName the name of the XML tag to create.
* @param value the value to assign to the <i>value</i> property of the tag.
* @return the corresponding XML tag.
*/
template <class T>
inline std::string xmlValueTag(const std::string& tagName, const T& value) {
std::ostringstream out;
out << '<' << tagName << " value=\"" << value << "\"/>";
return out.str();
}
#ifndef __DOXYGEN
/**
* Specialisations of xmlValueTag():
*/
template <>
inline std::string xmlValueTag<bool>(const std::string& tagName,
const bool& value) {
return '<' + tagName + " value=\"" + (value ? 'T' : 'F') + "\"/>";
}
template <>
inline std::string xmlValueTag<NBoolSet>(const std::string& tagName,
const NBoolSet& value) {
return '<' + tagName + " value=\"" +
(value.hasTrue() ? 'T' : '-') +
(value.hasFalse() ? 'F' : '-') + "\"/>";
}
#endif
/*@}*/
// Inline functions for XMLPropertyDict
inline XMLPropertyDict::XMLPropertyDict() {
}
inline const std::string& XMLPropertyDict::lookup(const std::string& key,
const std::string& defaultVal) const {
const_iterator it = find(key);
return (it == end() ? defaultVal : (*it).second);
}
// Inline functions for XMLParserCallback
inline XMLParserCallback::~XMLParserCallback() {
}
inline void XMLParserCallback::start_document(XMLParser*) {
}
inline void XMLParserCallback::end_document() {
}
inline void XMLParserCallback::start_element(const std::string&,
const regina::xml::XMLPropertyDict&) {
}
inline void XMLParserCallback::end_element(const std::string&) {
}
inline void XMLParserCallback::characters(const std::string&) {
}
inline void XMLParserCallback::comment(const std::string&) {
}
inline void XMLParserCallback::warning(const std::string&) {
}
inline void XMLParserCallback::error(const std::string&) {
}
inline void XMLParserCallback::fatal_error(const std::string&) {
}
// Inline functions for XMLParser
inline XMLParser::~XMLParser() {
if (_context)
xmlFreeParserCtxt(_context);
}
inline void XMLParser::parse_chunk(const std::string& s) {
xmlParseChunk(_context, s.c_str(), s.length(), 0);
}
inline void XMLParser::finish() {
xmlParseChunk(_context, 0, 0, 1);
}
} } // namespace regina::xml
#endif
|