mirror of
https://github.com/id-Software/GtkRadiant.git
synced 2026-03-20 00:49:29 +01:00
The GtkRadiant sources as originally released under the GPL license.
This commit is contained in:
22
libs/xml/ixml.cpp
Normal file
22
libs/xml/ixml.cpp
Normal file
@@ -0,0 +1,22 @@
|
||||
/*
|
||||
Copyright (C) 2001-2006, William Joseph.
|
||||
All Rights Reserved.
|
||||
|
||||
This file is part of GtkRadiant.
|
||||
|
||||
GtkRadiant 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.
|
||||
|
||||
GtkRadiant 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 GtkRadiant; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
#include "ixml.h"
|
||||
60
libs/xml/ixml.h
Normal file
60
libs/xml/ixml.h
Normal file
@@ -0,0 +1,60 @@
|
||||
/*
|
||||
Copyright (C) 2001-2006, William Joseph.
|
||||
All Rights Reserved.
|
||||
|
||||
This file is part of GtkRadiant.
|
||||
|
||||
GtkRadiant 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.
|
||||
|
||||
GtkRadiant 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 GtkRadiant; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
#if !defined(INCLUDED_XML_IXML_H)
|
||||
#define INCLUDED_XML_IXML_H
|
||||
|
||||
#include "itextstream.h"
|
||||
#include "generic/constant.h"
|
||||
|
||||
class XMLAttrVisitor
|
||||
{
|
||||
public:
|
||||
virtual void visit(const char* name, const char* value) = 0;
|
||||
};
|
||||
|
||||
class XMLElement
|
||||
{
|
||||
public:
|
||||
virtual const char* name() const = 0;
|
||||
virtual const char* attribute(const char* name) const = 0;
|
||||
virtual void forEachAttribute(XMLAttrVisitor& visitor) const = 0;
|
||||
};
|
||||
|
||||
class XMLImporter : public TextOutputStream
|
||||
{
|
||||
public:
|
||||
STRING_CONSTANT(Name, "XMLImporter");
|
||||
|
||||
virtual void pushElement(const XMLElement& element) = 0;
|
||||
virtual void popElement(const char* name) = 0;
|
||||
};
|
||||
|
||||
class XMLExporter
|
||||
{
|
||||
public:
|
||||
STRING_CONSTANT(Name, "XMLExporter");
|
||||
|
||||
virtual void exportXML(XMLImporter& importer) = 0;
|
||||
};
|
||||
|
||||
|
||||
#endif
|
||||
22
libs/xml/xmlelement.cpp
Normal file
22
libs/xml/xmlelement.cpp
Normal file
@@ -0,0 +1,22 @@
|
||||
/*
|
||||
Copyright (C) 2001-2006, William Joseph.
|
||||
All Rights Reserved.
|
||||
|
||||
This file is part of GtkRadiant.
|
||||
|
||||
GtkRadiant 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.
|
||||
|
||||
GtkRadiant 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 GtkRadiant; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
#include "xmlelement.h"
|
||||
109
libs/xml/xmlelement.h
Normal file
109
libs/xml/xmlelement.h
Normal file
@@ -0,0 +1,109 @@
|
||||
/*
|
||||
Copyright (C) 2001-2006, William Joseph.
|
||||
All Rights Reserved.
|
||||
|
||||
This file is part of GtkRadiant.
|
||||
|
||||
GtkRadiant 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.
|
||||
|
||||
GtkRadiant 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 GtkRadiant; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
#if !defined(INCLUDED_XML_XMLELEMENT_H)
|
||||
#define INCLUDED_XML_XMLELEMENT_H
|
||||
|
||||
#include "xml/ixml.h"
|
||||
#include "string/string.h"
|
||||
|
||||
#include <map>
|
||||
|
||||
class StaticElement : public XMLElement
|
||||
{
|
||||
struct strless
|
||||
{
|
||||
bool operator()(const char* s1, const char* s2) const
|
||||
{
|
||||
return strcmp(s1, s2) < 0;
|
||||
}
|
||||
};
|
||||
|
||||
typedef std::map<const char*, const char*, strless> attrs_t;
|
||||
public:
|
||||
StaticElement(const char* name)
|
||||
: m_name(name)
|
||||
{
|
||||
}
|
||||
void insertAttribute(const char* name, const char* value)
|
||||
{
|
||||
m_attrs.insert(attrs_t::value_type(name, value));
|
||||
}
|
||||
const char* name() const
|
||||
{
|
||||
return m_name;
|
||||
}
|
||||
const char* attribute(const char* name) const
|
||||
{
|
||||
attrs_t::const_iterator i = m_attrs.find(name);
|
||||
if(i != m_attrs.end())
|
||||
return i->second;
|
||||
else
|
||||
return "";
|
||||
}
|
||||
void forEachAttribute(XMLAttrVisitor& visitor) const
|
||||
{
|
||||
for(attrs_t::const_iterator i = m_attrs.begin(); i != m_attrs.end(); ++i)
|
||||
{
|
||||
visitor.visit(i->first, i->second);
|
||||
}
|
||||
}
|
||||
private:
|
||||
const char* m_name;
|
||||
attrs_t m_attrs;
|
||||
};
|
||||
|
||||
class DynamicElement : public XMLElement
|
||||
{
|
||||
typedef std::map<CopiedString, CopiedString> attrs_t;
|
||||
public:
|
||||
DynamicElement(const char* name)
|
||||
: m_name(name)
|
||||
{}
|
||||
void insertAttribute(const char* name, const char* value)
|
||||
{
|
||||
m_attrs.insert(attrs_t::value_type(name, value));
|
||||
}
|
||||
const char* name() const
|
||||
{
|
||||
return m_name.c_str();
|
||||
}
|
||||
const char* attribute(const char* name) const
|
||||
{
|
||||
attrs_t::const_iterator i = m_attrs.find(name);
|
||||
if(i != m_attrs.end())
|
||||
return i->second.c_str();
|
||||
else
|
||||
return "";
|
||||
}
|
||||
void forEachAttribute(XMLAttrVisitor& visitor) const
|
||||
{
|
||||
for(attrs_t::const_iterator i = m_attrs.begin(); i != m_attrs.end(); ++i)
|
||||
{
|
||||
visitor.visit(i->first.c_str(), i->second.c_str());
|
||||
}
|
||||
}
|
||||
private:
|
||||
CopiedString m_name;
|
||||
attrs_t m_attrs;
|
||||
};
|
||||
|
||||
#endif
|
||||
22
libs/xml/xmlparser.cpp
Normal file
22
libs/xml/xmlparser.cpp
Normal file
@@ -0,0 +1,22 @@
|
||||
/*
|
||||
Copyright (C) 2001-2006, William Joseph.
|
||||
All Rights Reserved.
|
||||
|
||||
This file is part of GtkRadiant.
|
||||
|
||||
GtkRadiant 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.
|
||||
|
||||
GtkRadiant 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 GtkRadiant; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
#include "xmlparser.h"
|
||||
242
libs/xml/xmlparser.h
Normal file
242
libs/xml/xmlparser.h
Normal file
@@ -0,0 +1,242 @@
|
||||
/*
|
||||
Copyright (C) 2001-2006, William Joseph.
|
||||
All Rights Reserved.
|
||||
|
||||
This file is part of GtkRadiant.
|
||||
|
||||
GtkRadiant 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.
|
||||
|
||||
GtkRadiant 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 GtkRadiant; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
#if !defined(INCLUDED_XML_XMLPARSER_H)
|
||||
#define INCLUDED_XML_XMLPARSER_H
|
||||
|
||||
#include <cstdio>
|
||||
#include <string.h>
|
||||
#include "ixml.h"
|
||||
#include "libxml/parser.h"
|
||||
#include "convert.h"
|
||||
|
||||
class TextInputStream;
|
||||
|
||||
class SAXElement : public XMLElement
|
||||
{
|
||||
public:
|
||||
SAXElement(const char* name, const char** atts)
|
||||
: m_name(name), m_atts(atts)
|
||||
{
|
||||
}
|
||||
const char* name() const
|
||||
{
|
||||
return m_name;
|
||||
}
|
||||
const char* attribute(const char* name) const
|
||||
{
|
||||
if(m_atts != 0)
|
||||
{
|
||||
for(const char** att = m_atts; *att != 0; att+=2)
|
||||
{
|
||||
if(strcmp(*att, name) == 0)
|
||||
{
|
||||
return *(++att);
|
||||
}
|
||||
}
|
||||
}
|
||||
return "";
|
||||
}
|
||||
void forEachAttribute(XMLAttrVisitor& visitor) const
|
||||
{
|
||||
if(m_atts != 0)
|
||||
{
|
||||
for(const char** att = m_atts; *att != 0; att+=2)
|
||||
{
|
||||
visitor.visit(*att, *(att+1));
|
||||
}
|
||||
}
|
||||
}
|
||||
private:
|
||||
const char* m_name;
|
||||
const char** m_atts;
|
||||
};
|
||||
|
||||
#include <stdarg.h>
|
||||
|
||||
class FormattedVA
|
||||
{
|
||||
public:
|
||||
const char* m_format;
|
||||
va_list& m_arguments;
|
||||
FormattedVA(const char* format, va_list& m_arguments)
|
||||
: m_format(format), m_arguments(m_arguments)
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
class Formatted
|
||||
{
|
||||
public:
|
||||
const char* m_format;
|
||||
va_list m_arguments;
|
||||
Formatted(const char* format, ...)
|
||||
: m_format(format)
|
||||
{
|
||||
va_start(m_arguments, format);
|
||||
}
|
||||
~Formatted()
|
||||
{
|
||||
va_end(m_arguments);
|
||||
}
|
||||
};
|
||||
|
||||
#ifdef WIN32
|
||||
#if _MSC_VER < 1400
|
||||
#define vsnprintf std::vsnprintf
|
||||
#endif
|
||||
#endif
|
||||
|
||||
template<typename TextOutputStreamType>
|
||||
inline TextOutputStreamType& ostream_write(TextOutputStreamType& ostream, const FormattedVA& formatted)
|
||||
{
|
||||
char buffer[1024];
|
||||
ostream.write(buffer, vsnprintf(buffer, 1023, formatted.m_format, formatted.m_arguments));
|
||||
return ostream;
|
||||
}
|
||||
|
||||
template<typename TextOutputStreamType>
|
||||
inline TextOutputStreamType& ostream_write(TextOutputStreamType& ostream, const Formatted& formatted)
|
||||
{
|
||||
char buffer[1024];
|
||||
ostream.write(buffer, vsnprintf(buffer, 1023, formatted.m_format, formatted.m_arguments));
|
||||
return ostream;
|
||||
}
|
||||
|
||||
class XMLSAXImporter
|
||||
{
|
||||
XMLImporter& m_importer;
|
||||
xmlSAXHandler m_sax;
|
||||
|
||||
static void startElement(void *user_data, const xmlChar *name, const xmlChar **atts)
|
||||
{
|
||||
SAXElement element(reinterpret_cast<const char*>(name), reinterpret_cast<const char**>(atts));
|
||||
reinterpret_cast<XMLSAXImporter*>(user_data)->m_importer.pushElement(element);
|
||||
}
|
||||
static void endElement(void *user_data, const xmlChar *name)
|
||||
{
|
||||
reinterpret_cast<XMLSAXImporter*>(user_data)->m_importer.popElement(reinterpret_cast<const char*>(name));
|
||||
}
|
||||
static void characters(void *user_data, const xmlChar *ch, int len)
|
||||
{
|
||||
reinterpret_cast<XMLSAXImporter*>(user_data)->m_importer
|
||||
<< ConvertUTF8ToLocale(StringRange(reinterpret_cast<const char*>(ch), reinterpret_cast<const char*>(ch + len)));
|
||||
}
|
||||
|
||||
static void warning(void *user_data, const char *msg, ...)
|
||||
{
|
||||
va_list args;
|
||||
va_start(args, msg);
|
||||
globalErrorStream() << "XML WARNING: " << FormattedVA(msg, args);
|
||||
va_end(args);
|
||||
}
|
||||
static void error(void *user_data, const char *msg, ...)
|
||||
{
|
||||
va_list args;
|
||||
va_start(args, msg);
|
||||
globalErrorStream() << "XML ERROR: " << FormattedVA(msg, args);
|
||||
va_end(args);
|
||||
}
|
||||
|
||||
public:
|
||||
XMLSAXImporter(XMLImporter& importer) : m_importer(importer)
|
||||
{
|
||||
m_sax.internalSubset = 0;
|
||||
m_sax.isStandalone = 0;
|
||||
m_sax.hasInternalSubset = 0;
|
||||
m_sax.hasExternalSubset = 0;
|
||||
m_sax.resolveEntity = 0;
|
||||
m_sax.getEntity = 0;
|
||||
m_sax.entityDecl = 0;
|
||||
m_sax.notationDecl = 0;
|
||||
m_sax.attributeDecl = 0;
|
||||
m_sax.elementDecl = 0;
|
||||
m_sax.unparsedEntityDecl = 0;
|
||||
m_sax.setDocumentLocator = 0;
|
||||
m_sax.startDocument = 0;
|
||||
m_sax.endDocument = 0;
|
||||
m_sax.startElement = startElement;
|
||||
m_sax.endElement = endElement;
|
||||
m_sax.reference = 0;
|
||||
m_sax.characters = characters;
|
||||
m_sax.ignorableWhitespace = 0;
|
||||
m_sax.processingInstruction = 0;
|
||||
m_sax.comment = 0;
|
||||
m_sax.warning = warning;
|
||||
m_sax.error = error;
|
||||
m_sax.fatalError = 0;
|
||||
m_sax.getParameterEntity = 0;
|
||||
m_sax.cdataBlock = 0;
|
||||
m_sax.externalSubset = 0;
|
||||
m_sax.initialized = 1;
|
||||
}
|
||||
|
||||
xmlSAXHandler* callbacks()
|
||||
{
|
||||
return &m_sax;
|
||||
}
|
||||
void* context()
|
||||
{
|
||||
return this;
|
||||
}
|
||||
};
|
||||
|
||||
class XMLStreamParser : public XMLExporter
|
||||
{
|
||||
static const int BUFSIZE = 1024;
|
||||
public:
|
||||
XMLStreamParser(TextInputStream& istream)
|
||||
: m_istream(istream)
|
||||
{
|
||||
}
|
||||
virtual void exportXML(XMLImporter& importer)
|
||||
{
|
||||
bool wellFormed = false;
|
||||
|
||||
char chars[BUFSIZE];
|
||||
std::size_t res = m_istream.read(chars, 4);
|
||||
if (res > 0)
|
||||
{
|
||||
XMLSAXImporter sax(importer);
|
||||
|
||||
xmlParserCtxtPtr ctxt = xmlCreatePushParserCtxt(sax.callbacks(), sax.context(), chars, static_cast<int>(res), 0);
|
||||
ctxt->replaceEntities = 1;
|
||||
|
||||
while ((res = m_istream.read(chars, BUFSIZE)) > 0)
|
||||
{
|
||||
xmlParseChunk(ctxt, chars, static_cast<int>(res), 0);
|
||||
}
|
||||
xmlParseChunk(ctxt, chars, 0, 1);
|
||||
|
||||
wellFormed = (ctxt->wellFormed == 1);
|
||||
|
||||
xmlFreeParserCtxt(ctxt);
|
||||
}
|
||||
|
||||
//return wellFormed;
|
||||
}
|
||||
private:
|
||||
TextInputStream& m_istream;
|
||||
};
|
||||
|
||||
|
||||
|
||||
#endif
|
||||
22
libs/xml/xmlwriter.cpp
Normal file
22
libs/xml/xmlwriter.cpp
Normal file
@@ -0,0 +1,22 @@
|
||||
/*
|
||||
Copyright (C) 2001-2006, William Joseph.
|
||||
All Rights Reserved.
|
||||
|
||||
This file is part of GtkRadiant.
|
||||
|
||||
GtkRadiant 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.
|
||||
|
||||
GtkRadiant 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 GtkRadiant; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
#include "xmlwriter.h"
|
||||
240
libs/xml/xmlwriter.h
Normal file
240
libs/xml/xmlwriter.h
Normal file
@@ -0,0 +1,240 @@
|
||||
/*
|
||||
Copyright (C) 2001-2006, William Joseph.
|
||||
All Rights Reserved.
|
||||
|
||||
This file is part of GtkRadiant.
|
||||
|
||||
GtkRadiant 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.
|
||||
|
||||
GtkRadiant 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 GtkRadiant; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
#if !defined(INCLUDED_XML_XMLWRITER_H)
|
||||
#define INCLUDED_XML_XMLWRITER_H
|
||||
|
||||
#include "convert.h"
|
||||
#include <vector>
|
||||
#include "xml/ixml.h"
|
||||
|
||||
class BufferedTextOutputStream : public TextOutputStream
|
||||
{
|
||||
static const int m_bufsize = 1024;
|
||||
TextOutputStream& m_ostream;
|
||||
char m_buffer[m_bufsize];
|
||||
char* m_pos;
|
||||
const char* m_end;
|
||||
|
||||
const char* end() const
|
||||
{
|
||||
return m_end;
|
||||
}
|
||||
void reset()
|
||||
{
|
||||
m_pos = m_buffer;
|
||||
}
|
||||
void flush()
|
||||
{
|
||||
m_ostream.write(m_buffer, m_pos - m_buffer);
|
||||
reset();
|
||||
}
|
||||
public:
|
||||
BufferedTextOutputStream(TextOutputStream& ostream) : m_ostream(ostream), m_pos(m_buffer), m_end(m_buffer+m_bufsize)
|
||||
{
|
||||
}
|
||||
~BufferedTextOutputStream()
|
||||
{
|
||||
flush();
|
||||
}
|
||||
void write(const char c)
|
||||
{
|
||||
if(m_pos == end())
|
||||
{
|
||||
flush();
|
||||
}
|
||||
*m_pos++ = c;
|
||||
}
|
||||
std::size_t write(const char* buffer, std::size_t length)
|
||||
{
|
||||
const char*const end = buffer + length;
|
||||
for(const char* p = buffer; p != end; ++p)
|
||||
{
|
||||
write(*p);
|
||||
}
|
||||
return length;
|
||||
}
|
||||
};
|
||||
|
||||
class XMLEntityOutputStream
|
||||
{
|
||||
BufferedTextOutputStream m_ostream;
|
||||
public:
|
||||
XMLEntityOutputStream(TextOutputStream& ostream)
|
||||
: m_ostream(ostream)
|
||||
{
|
||||
}
|
||||
void write(const char c)
|
||||
{
|
||||
m_ostream.write(c);
|
||||
}
|
||||
void writeEscaped(const char c)
|
||||
{
|
||||
switch(c)
|
||||
{
|
||||
case '<':
|
||||
write('&');
|
||||
write('l');
|
||||
write('t');
|
||||
write(';');
|
||||
break;
|
||||
case '>':
|
||||
write('&');
|
||||
write('g');
|
||||
write('t');
|
||||
write(';');
|
||||
break;
|
||||
case '"':
|
||||
write('&');
|
||||
write('q');
|
||||
write('u');
|
||||
write('o');
|
||||
write('t');
|
||||
write(';');
|
||||
break;
|
||||
case '&':
|
||||
write('&');
|
||||
write('a');
|
||||
write('m');
|
||||
write('p');
|
||||
write(';');
|
||||
break;
|
||||
default:
|
||||
write(c);
|
||||
break;
|
||||
}
|
||||
}
|
||||
std::size_t write(const char* buffer, std::size_t length)
|
||||
{
|
||||
const char*const end = buffer + length;
|
||||
for(const char* p = buffer; p != end; ++p)
|
||||
{
|
||||
writeEscaped(*p);
|
||||
}
|
||||
return length;
|
||||
}
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
inline XMLEntityOutputStream& operator<<(XMLEntityOutputStream& ostream, const T& t)
|
||||
{
|
||||
return ostream_write(ostream, t);
|
||||
}
|
||||
|
||||
|
||||
class XMLStreamWriter : public XMLImporter, public XMLAttrVisitor
|
||||
{
|
||||
class state_t
|
||||
{
|
||||
public:
|
||||
enum EState
|
||||
{
|
||||
eStartElement,
|
||||
eContent,
|
||||
};
|
||||
state_t()
|
||||
: m_state(eStartElement)
|
||||
{}
|
||||
EState m_state;
|
||||
};
|
||||
|
||||
XMLEntityOutputStream m_ostream;
|
||||
std::vector<state_t> m_elements;
|
||||
|
||||
void write_cdata(const char* buffer, std::size_t length)
|
||||
{
|
||||
m_ostream << ConvertLocaleToUTF8(StringRange(buffer, buffer + length));
|
||||
}
|
||||
void write_string(const char* string)
|
||||
{
|
||||
m_ostream << string;
|
||||
}
|
||||
void write_quoted_string(const char* string)
|
||||
{
|
||||
m_ostream.write('"');
|
||||
m_ostream << string;
|
||||
m_ostream.write('"');
|
||||
}
|
||||
public:
|
||||
XMLStreamWriter(TextOutputStream& ostream)
|
||||
: m_ostream(ostream)
|
||||
{
|
||||
m_elements.push_back(state_t());
|
||||
m_elements.back().m_state = state_t::eContent;
|
||||
m_ostream.write('<');
|
||||
m_ostream.write('?');
|
||||
write_string("xml");
|
||||
visit("version", "1.0");
|
||||
m_ostream.write('?');
|
||||
m_ostream.write('>');
|
||||
}
|
||||
|
||||
void pushElement(const XMLElement& element)
|
||||
{
|
||||
if(m_elements.back().m_state == state_t::eStartElement)
|
||||
{
|
||||
m_elements.back().m_state = state_t::eContent;
|
||||
m_ostream.write('>');
|
||||
}
|
||||
|
||||
m_elements.push_back(state_t());
|
||||
|
||||
m_ostream.write('<');
|
||||
write_string(element.name());
|
||||
element.forEachAttribute(*this);
|
||||
}
|
||||
void popElement(const char* name)
|
||||
{
|
||||
if(m_elements.back().m_state == state_t::eStartElement)
|
||||
{
|
||||
m_ostream.write('/');
|
||||
m_ostream.write('>');
|
||||
}
|
||||
else
|
||||
{
|
||||
m_ostream.write('<');
|
||||
m_ostream.write('/');
|
||||
write_string(name);
|
||||
m_ostream.write('>');
|
||||
}
|
||||
}
|
||||
std::size_t write(const char* data, std::size_t length)
|
||||
{
|
||||
if(m_elements.back().m_state == state_t::eStartElement)
|
||||
{
|
||||
m_elements.back().m_state = state_t::eContent;
|
||||
m_ostream.write('>');
|
||||
}
|
||||
write_cdata(data, length);
|
||||
return length;
|
||||
}
|
||||
|
||||
void visit(const char* name, const char* value)
|
||||
{
|
||||
m_ostream.write(' ');
|
||||
write_string(name);
|
||||
m_ostream.write('=');
|
||||
write_quoted_string(value);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user