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
|
/*++
Copyright (C) 2018 3MF Consortium
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Abstract:
NMR_XmlWriter_Native.cpp implements an XML Writer Class without dependencies.
--*/
#include "Common/Platform/NMR_XmlWriter_Native.h"
#include "Common/Platform/NMR_ExportStream.h"
#include "Common/NMR_Exception_Windows.h"
#include "Common/NMR_StringUtils.h"
#include <string.h>
#include <vector>
namespace NMR {
CXmlWriter_Native::CXmlWriter_Native(_In_ PExportStream pExportStream)
: CXmlWriter(pExportStream)
{
m_bIsFreshLine = true;
#ifndef __GNUC__
m_nLineEndingBuffer[0] = 0x0d;
m_nLineEndingBuffer[1] = 0x0a;
m_nLineEndingCharCount = 2;
#else
m_nLineEndingBuffer[0] = 0x0a;
m_nLineEndingBuffer[1] = 0x0a;
m_nLineEndingCharCount = 1;
#endif // __GNUC__
m_nSpacesPerLayer = 1;
m_nLayer = 0;
m_SpacingBuffer.fill(NATIVEXMLSPACING);
m_bElementIsOpen = false;
}
void CXmlWriter_Native::WriteStartDocument()
{
writeUTF8(NATIVEXMLENCODING, true);
}
void CXmlWriter_Native::WriteEndDocument()
{
}
void CXmlWriter_Native::Flush()
{
}
void CXmlWriter_Native::WriteAttributeString(_In_opt_ LPCSTR pszPrefix, _In_opt_ LPCSTR pszLocalName, _In_opt_ LPCSTR pszNamespaceUri, _In_opt_ LPCSTR pszValue)
{
if (m_bElementIsOpen) {
writeUTF8(" ", false);
if (pszPrefix != nullptr) {
if (*pszPrefix != 0) {
writeUTF8(pszPrefix, false);
writeUTF8(":", false);
}
}
writeUTF8(pszLocalName, false);
writeUTF8("=\"", false);
size_t nLength = strlen(pszValue);
if (nLength < NATIVEXMLFIXEDENCODINGLENGTH) {
// for small attribute lengths, use fixed size buffer
nfChar * pszAttribBuffer = &m_FixedEncodingBuffer[0];
escapeXMLString(pszValue, pszAttribBuffer);
writeUTF8(pszAttribBuffer, false);
}
else {
if (nLength > NATIVEXMLMAXSTRINGLENGTH)
throw CNMRException(NMR_ERROR_INVALIDBUFFERSIZE);
// for large attribute lengths, use dynamic buffer
std::vector<nfChar> Buffer;
Buffer.resize((nLength + 1) * 6);
nfChar * pszAttribBuffer = &Buffer[0];
escapeXMLString(pszValue, pszAttribBuffer);
}
writeUTF8("\"", false);
}
}
void CXmlWriter_Native::WriteStartElement(_In_opt_ LPCSTR pszPrefix, _In_ LPCSTR pszLocalName, _In_opt_ LPCSTR pszNamespaceUri)
{
closeCurrentElement(true);
writeSpaces(m_nLayer * m_nSpacesPerLayer);
writeUTF8("<", false);
m_bElementIsOpen = true;
std::string sNodePrefix;
if (pszPrefix != nullptr) {
if (*pszPrefix != 0) {
sNodePrefix = pszPrefix;
sNodePrefix += ":";
writeUTF8(sNodePrefix.c_str(), false);
}
}
writeUTF8(pszLocalName, false);
std::string sNodeName (pszLocalName);
m_NodeStack.push_back(sNodePrefix + sNodeName);
m_nLayer++;
if (pszNamespaceUri != nullptr) {
if (*pszNamespaceUri != 0) {
writeUTF8(" xmlns=\"", false);
writeUTF8(pszNamespaceUri, false);
writeUTF8("\"", false);
}
}
}
void CXmlWriter_Native::WriteEndElement()
{
if (m_bElementIsOpen) {
writeUTF8(" />", true);
m_bElementIsOpen = false;
if ((m_NodeStack.size() == 0) || (m_nLayer == 0))
throw CNMRException(NMR_ERROR_XMLWRITER_CLOSENODEERROR);
m_NodeStack.pop_back();
m_nLayer--;
}
else
WriteFullEndElement();
}
void CXmlWriter_Native::WriteFullEndElement()
{
closeCurrentElement(false);
if ((m_NodeStack.size() == 0) || (m_nLayer == 0))
throw CNMRException(NMR_ERROR_XMLWRITER_CLOSENODEERROR);
std::string sNodeName = m_NodeStack.back();
m_NodeStack.pop_back();
m_nLayer--;
if (m_bIsFreshLine)
writeSpaces(m_nLayer * m_nSpacesPerLayer);
writeUTF8("</", false);
writeUTF8(sNodeName.c_str(), false);
writeUTF8(">", true);
}
void CXmlWriter_Native::WriteText(_In_ const nfChar * pszContent, _In_ const nfUint32 cbLength)
{
if (pszContent == nullptr)
throw CNMRException(NMR_ERROR_INVALIDPARAM);
closeCurrentElement(false);
writeUTF8(pszContent, false);
}
void CXmlWriter_Native::writeData(_In_ const void * pData, _In_ nfUint32 cbLength)
{
if (pData == nullptr)
throw CNMRException(NMR_ERROR_INVALIDPARAM);
m_pExportStream->writeBuffer(pData, cbLength);
}
void CXmlWriter_Native::writeUTF8(_In_ const nfChar * pszString, _In_ nfBool bNewLine)
{
if (pszString == nullptr)
throw CNMRException(NMR_ERROR_INVALIDPARAM);
#ifdef __GNUC__
size_t cbSize = strlen(pszString);
#else
size_t cbSize = strnlen_s(pszString, NATIVEXMLMAXSTRINGLENGTH);
#endif // __GNUC__
if (cbSize > NATIVEXMLMAXSTRINGLENGTH)
throw CNMRException(NMR_ERROR_INSUFFICIENTBUFFERSIZE);
if (cbSize > 0) {
writeData((nfByte *)pszString, (nfUint32)cbSize);
m_bIsFreshLine = false;
}
if (bNewLine) {
writeData(m_nLineEndingBuffer, m_nLineEndingCharCount);
m_bIsFreshLine = true;
}
}
void CXmlWriter_Native::writeSpaces(_In_ nfUint32 cbCount)
{
while (cbCount > 0) {
if (cbCount > NATIVEXMLSPACINGBUFFERSIZE) {
writeData(&m_SpacingBuffer[0], NATIVEXMLSPACINGBUFFERSIZE);
cbCount -= NATIVEXMLSPACINGBUFFERSIZE;
}
else {
writeData(&m_SpacingBuffer[0], cbCount);
break;
}
}
}
void CXmlWriter_Native::writeUTF16(_In_ const nfWChar * pszString, _In_ nfBool bNewLine)
{
std::wstring sString(pszString);
std::string sUTF8String = fnUTF16toUTF8(sString);
writeUTF8(sUTF8String.c_str(), bNewLine);
}
void CXmlWriter_Native::closeCurrentElement(_In_ nfBool bNewLine)
{
if (m_bElementIsOpen) {
writeUTF8(">", bNewLine);
m_bElementIsOpen = false;
}
}
void CXmlWriter_Native::WriteRawLine(_In_ const nfChar * pszRawData, _In_ nfUint32 cbCount)
{
if (m_bElementIsOpen) {
closeCurrentElement(true);
}
writeSpaces(m_nLayer * m_nSpacesPerLayer);
writeData(pszRawData, cbCount);
writeData(m_nLineEndingBuffer, m_nLineEndingCharCount);
}
void CXmlWriter_Native::escapeXMLString(_In_z_ const nfChar * pszString, _Out_ nfChar * pszBuffer)
{
__NMRASSERT(pszString);
__NMRASSERT(pszBuffer);
const nfChar * pSrcChar = pszString;
nfChar * pDstChar = pszBuffer;
while (*pSrcChar) {
nfUint32 nReplaceLength = 0;
const nfChar * pszReplacement = nullptr;
switch (*pSrcChar) {
case 34: // "
pszReplacement = """;
nReplaceLength = 6;
break;
case 38: // &
pszReplacement = "&";
nReplaceLength = 5;
break;
case 39: // '
pszReplacement = "'";
nReplaceLength = 6;
break;
case 60: // <
pszReplacement = "<";
nReplaceLength = 4;
break;
case 62: // >
pszReplacement = ">";
nReplaceLength = 4;
break;
}
if (nReplaceLength > 0) {
nfUint32 j;
for (j = 0; j < nReplaceLength; j++) {
*pDstChar = *pszReplacement;
pDstChar++;
pszReplacement++;
}
}
else {
*pDstChar = *pSrcChar;
pDstChar++;
}
pSrcChar++;
}
*pDstChar = 0;
}
}
|