#ifdef __GNUG__
#pragma implementation "xml.h"
-#pragma implementation "xmlio.h"
#endif
// For compilers that support precompilation, includes "wx.h".
// wxXmlDocument
//-----------------------------------------------------------------------------
+wxXmlDocument::wxXmlDocument()
+ : m_version(wxT("1.0")), m_fileEncoding(wxT("utf-8")), m_root(NULL)
+{
+#if !wxUSE_UNICODE
+ m_encoding = wxT("UTF-8");
+#endif
+}
+
wxXmlDocument::wxXmlDocument(const wxString& filename, const wxString& encoding)
: wxObject(), m_root(NULL)
{
// wxXmlDocument loading routines
//-----------------------------------------------------------------------------
-/*
+/*
FIXME:
- process all elements, including CDATA
*/
{
size_t nLen = (len != wxSTRING_MAXLEN) ? len :
nLen = wxConvUTF8.MB2WC((wchar_t*) NULL, s, 0);
-
+
wchar_t *buf = new wchar_t[nLen+1];
wxConvUTF8.MB2WC(buf, s, nLen);
buf[nLen] = 0;
- return wxString(buf, *conv, len);
+ wxString str(buf, *conv, len);
delete[] buf;
+ return str;
}
else
return wxString(s, len);
}
static int UnknownEncodingHnd(void * WXUNUSED(encodingHandlerData),
- const XML_Char *name, XML_Encoding *info)
+ const XML_Char *name, XML_Encoding *info)
{
// We must build conversion table for expat. The easiest way to do so
// is to let wxCSConv convert as string containing all characters to
// wide character representation:
- wxCSConv conv(name);
- char mbBuf[255];
- wchar_t wcBuf[255];
+ wxCSConv conv(wxString(name, wxConvLibc));
+ char mbBuf[2];
+ wchar_t wcBuf[10];
size_t i;
-
- for (i = 0; i < 255; i++)
- mbBuf[i] = i+1;
- mbBuf[255] = 0;
- conv.MB2WC(wcBuf, mbBuf, 255);
- wcBuf[255] = 0;
-
+
+ mbBuf[1] = 0;
info->map[0] = 0;
for (i = 0; i < 255; i++)
- info->map[i+1] = (int)wcBuf[i];
+ {
+ mbBuf[0] = (char)(i+1);
+ if (conv.MB2WC(wcBuf, mbBuf, 2) == (size_t)-1)
+ {
+ // invalid/undefined byte in the encoding:
+ info->map[i+1] = -1;
+ }
+ info->map[i+1] = (int)wcBuf[0];
+ }
info->data = NULL;
info->convert = NULL;
info->release = NULL;
-
+
return 1;
}
if ( encoding != wxT("UTF-8") && encoding != wxT("utf-8") )
ctx.conv = new wxCSConv(encoding);
#endif
-
+
XML_SetUserData(parser, (void*)&ctx);
XML_SetElementHandler(parser, StartElementHnd, EndElementHnd);
XML_SetCharacterDataHandler(parser, TextHnd);
XML_SetDefaultHandler(parser, DefaultHnd);
XML_SetUnknownEncodingHandler(parser, UnknownEncodingHnd, NULL);
+ bool ok = true;
do
{
size_t len = stream.Read(buf, BUFSIZE).LastRead();
wxLogError(_("XML parsing error: '%s' at line %d"),
XML_ErrorString(XML_GetErrorCode(parser)),
XML_GetCurrentLineNumber(parser));
- return FALSE;
+ ok = false;
+ break;
}
} while (!done);
- SetVersion(ctx.version);
- SetFileEncoding(ctx.encoding);
- SetRoot(ctx.root);
+ if (ok)
+ {
+ SetVersion(ctx.version);
+ SetFileEncoding(ctx.encoding);
+ SetRoot(ctx.root);
+ }
XML_ParserFree(parser);
#if !wxUSE_UNICODE
if ( ctx.conv )
delete ctx.conv;
#endif
-
- return TRUE;
+
+ return ok;
}
{
if (str.IsEmpty()) return;
#if wxUSE_UNICODE
- const wxW2MBbuf *buf = str.mb_str(convFile ? *convFile : wxConvUTF8);
+ const wxWX2MBbuf buf(str.mb_str(convFile ? *convFile : wxConvUTF8));
stream.Write((const char*)buf, strlen((const char*)buf));
#else
if ( convFile == NULL )
#endif
}
-// Same as above, but create entities first.
+// Same as above, but create entities first.
// Translates '<' to "<", '>' to ">" and '&' to "&"
static void OutputStringEnt(wxOutputStream& stream, const wxString& str,
wxMBConv *convMem, wxMBConv *convFile)
wxString buf;
size_t i, last, len;
wxChar c;
-
+
len = str.Len();
last = 0;
for (i = 0; i < len; i++)
{
c = str.GetChar(i);
- if (c == wxT('<') || c == wxT('>') ||
+ if (c == wxT('<') || c == wxT('>') ||
(c == wxT('&') && str.Mid(i+1, 4) != wxT("amp;")))
{
OutputString(stream, str.Mid(last, i - last), convMem, convFile);
switch (c)
{
- case wxT('<'):
+ case wxT('<'):
OutputString(stream, wxT("<"), NULL, NULL);
break;
- case wxT('>'):
+ case wxT('>'):
OutputString(stream, wxT(">"), NULL, NULL);
break;
- case wxT('&'):
+ case wxT('&'):
OutputString(stream, wxT("&"), NULL, NULL);
break;
default: break;
case wxXML_TEXT_NODE:
OutputStringEnt(stream, node->GetContent(), convMem, convFile);
break;
-
+
case wxXML_ELEMENT_NODE:
OutputString(stream, wxT("<"), NULL, NULL);
OutputString(stream, node->GetName(), NULL, NULL);
-
+
prop = node->GetProperties();
while (prop)
{
// FIXME - what if prop contains '"'?
prop = prop->GetNext();
}
-
+
if (node->GetChildren())
{
OutputString(stream, wxT(">"), NULL, NULL);
else
OutputString(stream, wxT("/>"), NULL, NULL);
break;
-
+
case wxXML_COMMENT_NODE:
OutputString(stream, wxT("<!--"), NULL, NULL);
OutputString(stream, node->GetContent(), convMem, convFile);
OutputString(stream, wxT("-->"), NULL, NULL);
break;
-
+
default:
wxFAIL_MSG(wxT("unsupported node type"));
}
{
if ( !IsOk() )
return FALSE;
-
+
wxString s;
-
+
wxMBConv *convMem = NULL, *convFile = NULL;
#if wxUSE_UNICODE
convFile = new wxCSConv(GetFileEncoding());
convMem = new wxCSConv(GetEncoding());
}
#endif
-
+
s.Printf(wxT("<?xml version=\"%s\" encoding=\"%s\"?>\n"),
GetVersion().c_str(), GetFileEncoding().c_str());
OutputString(stream, s, NULL, NULL);
-
+
OutputNode(stream, GetRoot(), 0, convMem, convFile);
OutputString(stream, wxT("\n"), NULL, NULL);
-
+
if ( convFile )
delete convFile;
if ( convMem )
delete convMem;
-
+
return TRUE;
}