📄 xmlwriter.cpp
字号:
//%2006//////////////////////////////////////////////////////////////////////////// Copyright (c) 2000, 2001, 2002 BMC Software; Hewlett-Packard Development// Company, L.P.; IBM Corp.; The Open Group; Tivoli Systems.// Copyright (c) 2003 BMC Software; Hewlett-Packard Development Company, L.P.;// IBM Corp.; EMC Corporation, The Open Group.// Copyright (c) 2004 BMC Software; Hewlett-Packard Development Company, L.P.;// IBM Corp.; EMC Corporation; VERITAS Software Corporation; The Open Group.// Copyright (c) 2005 Hewlett-Packard Development Company, L.P.; IBM Corp.;// EMC Corporation; VERITAS Software Corporation; The Open Group.// Copyright (c) 2006 Hewlett-Packard Development Company, L.P.; IBM Corp.;// EMC Corporation; Symantec Corporation; The Open Group.//// Permission is hereby granted, free of charge, to any person obtaining a copy// of this software and associated documentation files (the "Software"), to// deal in the Software without restriction, including without limitation the// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or// sell copies of the Software, and to permit persons to whom the Software is// furnished to do so, subject to the following conditions:// // THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN// ALL COPIES OR SUBSTANTIAL PORTIONS OF THE SOFTWARE. THE SOFTWARE IS PROVIDED// "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT// LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN// ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.////==============================================================================////%/////////////////////////////////////////////////////////////////////////////#include <Pegasus/Common/Config.h>#include <cstdlib>#include <cstdio>#include "Constants.h"#include "CIMClass.h"#include "CIMClassRep.h"#include "CIMInstance.h"#include "CIMInstanceRep.h"#include "CIMProperty.h"#include "CIMPropertyRep.h"#include "CIMMethod.h"#include "CIMMethodRep.h"#include "CIMParameter.h"#include "CIMParameterRep.h"#include "CIMParamValue.h"#include "CIMParamValueRep.h"#include "CIMQualifier.h"#include "CIMQualifierRep.h"#include "CIMQualifierDecl.h"#include "CIMQualifierDeclRep.h"#include "CIMValue.h"#include "XmlWriter.h"#include "XmlParser.h"#include "Tracer.h"#include <Pegasus/Common/StatisticalData.h>#include "CommonUTF.h"#include "Buffer.h"#include "StrLit.h"#include "LanguageParser.h"#include "IDFactory.h"PEGASUS_NAMESPACE_BEGIN// This is a shortcut macro for outputing content length. This// pads the output number to the max characters representing a Uint32 number// so that it can be overwritten easily with a transfer encoding line later// on in HTTPConnection if required. This is strictly for performance since// messages can be very large. This overwriting shortcut allows us to NOT have// to repackage a large message later.#define OUTPUT_CONTENTLENGTH \{ \ char contentLengthP[11]; \ int n = sprintf(contentLengthP,"%.10u", contentLength); \ out << STRLIT("content-length: "); \ out.append(contentLengthP, n); \ out << STRLIT("\r\n"); \}//////////////////////////////////////////////////////////////////////////////////// SpecialChar and table.//////////////////////////////////////////////////////////////////////////////////// Note: we cannot use StrLit here since it has a constructur (forbits// structure initialization).struct SpecialChar{ const char* str; size_t size;};// Defines encodings of special characters. Just use a 7-bit ASCII character// as an index into this array to retrieve its string encoding and encoding// length in bytes.static const SpecialChar _specialChars[] ={ {STRLIT_ARGS("�")}, {STRLIT_ARGS("")}, {STRLIT_ARGS("")}, {STRLIT_ARGS("")}, {STRLIT_ARGS("")}, {STRLIT_ARGS("")}, {STRLIT_ARGS("")}, {STRLIT_ARGS("")}, {STRLIT_ARGS("")}, {STRLIT_ARGS("	")}, {STRLIT_ARGS(" ")}, {STRLIT_ARGS("")}, {STRLIT_ARGS("")}, {STRLIT_ARGS(" ")}, {STRLIT_ARGS("")}, {STRLIT_ARGS("")}, {STRLIT_ARGS("")}, {STRLIT_ARGS("")}, {STRLIT_ARGS("")}, {STRLIT_ARGS("")}, {STRLIT_ARGS("")}, {STRLIT_ARGS("")}, {STRLIT_ARGS("")}, {STRLIT_ARGS("")}, {STRLIT_ARGS("")}, {STRLIT_ARGS("")}, {STRLIT_ARGS("")}, {STRLIT_ARGS("")}, {STRLIT_ARGS("")}, {STRLIT_ARGS("")}, {STRLIT_ARGS("")}, {STRLIT_ARGS("")}, {STRLIT_ARGS(" ")}, {STRLIT_ARGS("!")}, {STRLIT_ARGS(""")}, {STRLIT_ARGS("#")}, {STRLIT_ARGS("$")}, {STRLIT_ARGS("%")}, {STRLIT_ARGS("&")}, {STRLIT_ARGS("'")}, {STRLIT_ARGS("(")}, {STRLIT_ARGS(")")}, {STRLIT_ARGS("*")}, {STRLIT_ARGS("+")}, {STRLIT_ARGS(",")}, {STRLIT_ARGS("-")}, {STRLIT_ARGS(".")}, {STRLIT_ARGS("/")}, {STRLIT_ARGS("0")}, {STRLIT_ARGS("1")}, {STRLIT_ARGS("2")}, {STRLIT_ARGS("3")}, {STRLIT_ARGS("4")}, {STRLIT_ARGS("5")}, {STRLIT_ARGS("6")}, {STRLIT_ARGS("7")}, {STRLIT_ARGS("8")}, {STRLIT_ARGS("9")}, {STRLIT_ARGS(":")}, {STRLIT_ARGS(";")}, {STRLIT_ARGS("<")}, {STRLIT_ARGS("=")}, {STRLIT_ARGS(">")}, {STRLIT_ARGS("?")}, {STRLIT_ARGS("@")}, {STRLIT_ARGS("A")}, {STRLIT_ARGS("B")}, {STRLIT_ARGS("C")}, {STRLIT_ARGS("D")}, {STRLIT_ARGS("E")}, {STRLIT_ARGS("F")}, {STRLIT_ARGS("G")}, {STRLIT_ARGS("H")}, {STRLIT_ARGS("I")}, {STRLIT_ARGS("J")}, {STRLIT_ARGS("K")}, {STRLIT_ARGS("L")}, {STRLIT_ARGS("M")}, {STRLIT_ARGS("N")}, {STRLIT_ARGS("O")}, {STRLIT_ARGS("P")}, {STRLIT_ARGS("Q")}, {STRLIT_ARGS("R")}, {STRLIT_ARGS("S")}, {STRLIT_ARGS("T")}, {STRLIT_ARGS("U")}, {STRLIT_ARGS("V")}, {STRLIT_ARGS("W")}, {STRLIT_ARGS("X")}, {STRLIT_ARGS("Y")}, {STRLIT_ARGS("Z")}, {STRLIT_ARGS("[")}, {STRLIT_ARGS("\\")}, {STRLIT_ARGS("]")}, {STRLIT_ARGS("^")}, {STRLIT_ARGS("_")}, {STRLIT_ARGS("`")}, {STRLIT_ARGS("a")}, {STRLIT_ARGS("b")}, {STRLIT_ARGS("c")}, {STRLIT_ARGS("d")}, {STRLIT_ARGS("e")}, {STRLIT_ARGS("f")}, {STRLIT_ARGS("g")}, {STRLIT_ARGS("h")}, {STRLIT_ARGS("i")}, {STRLIT_ARGS("j")}, {STRLIT_ARGS("k")}, {STRLIT_ARGS("l")}, {STRLIT_ARGS("m")}, {STRLIT_ARGS("n")}, {STRLIT_ARGS("o")}, {STRLIT_ARGS("p")}, {STRLIT_ARGS("q")}, {STRLIT_ARGS("r")}, {STRLIT_ARGS("s")}, {STRLIT_ARGS("t")}, {STRLIT_ARGS("u")}, {STRLIT_ARGS("v")}, {STRLIT_ARGS("w")}, {STRLIT_ARGS("x")}, {STRLIT_ARGS("y")}, {STRLIT_ARGS("z")}, {STRLIT_ARGS("{")}, {STRLIT_ARGS("|")}, {STRLIT_ARGS("}")}, {STRLIT_ARGS("~")}, {STRLIT_ARGS("")},};// If _isSpecialChar7[ch] is true, then ch is a special character, which must// have a special encoding in XML. But only use 7-bit ASCII characters to// index this array.static const int _isSpecialChar7[] ={ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,1,0,0, 0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,};////////////////////////////////////////////////////////////////////////////////Buffer& operator<<(Buffer& out, const Char16& x){ XmlWriter::append(out, x); return out;}Buffer& operator<<(Buffer& out, const String& x){ XmlWriter::append(out, x); return out;}Buffer& operator<<(Buffer& out, const Indentor& x){ XmlWriter::append(out, x); return out;}Buffer& operator<<(Buffer& out, const Buffer& x){ out.append(x.getData(), x.size()); return out;}Buffer& operator<<(Buffer& out, Uint32 x){ XmlWriter::append(out, x); return out;}Buffer& operator<<(Buffer& out, const CIMName& name){ XmlWriter::append(out, name.getString ()); return out;}Buffer& operator<<(Buffer& out, const AcceptLanguageList& al){ XmlWriter::append(out, LanguageParser::buildAcceptLanguageHeader(al)); return out;}Buffer& operator<<(Buffer& out, const ContentLanguageList& cl){ XmlWriter::append(out, LanguageParser::buildContentLanguageHeader(cl)); return out;}PEGASUS_STD(ostream)& operator<<(PEGASUS_STD(ostream)& os, const CIMDateTime& x){ return os << x.toString();}PEGASUS_STD(ostream)& operator<<(PEGASUS_STD(ostream)& os, const CIMName& name){ os << name.getString(); return os;}PEGASUS_STD(ostream)& operator<<(PEGASUS_STD(ostream)& os, const CIMNamespaceName& name){ os << name.getString(); return os;}static void _xmlWritter_appendChar(Buffer& out, const Char16& c){ // We need to convert the Char16 to UTF8 then append the UTF8 // character into the array. // NOTE: The UTF8 character could be several bytes long. // WARNING: This function will put in replacement character for // all characters that have surogate pairs. char str[6]; memset(str,0x00,sizeof(str)); Uint8* charIN = (Uint8 *)&c; const Uint16 *strsrc = (Uint16 *)charIN; Uint16 *endsrc = (Uint16 *)&charIN[1]; Uint8 *strtgt = (Uint8 *)str; Uint8 *endtgt = (Uint8 *)&str[5]; UTF16toUTF8( &strsrc, endsrc, &strtgt, endtgt); out.append(str, UTF_8_COUNT_TRAIL_BYTES(str[0]) + 1);}inline void _appendSpecialChar7(Buffer& out, char c){ if (_isSpecialChar7[int(c)]) out.append(_specialChars[int(c)].str, _specialChars[int(c)].size); else out.append(c);}inline void _xmlWritter_appendSpecialChar(Buffer& out, const Char16& c){ if (c < 128) _appendSpecialChar7(out, char(c)); else _xmlWritter_appendChar(out, c);}static void _xmlWritter_appendSpecialChar(PEGASUS_STD(ostream)& os, char c){ if ( ((c < 0x20) && (c >= 0)) || (c == 0x7f) ) { char charref[7]; sprintf(charref, "&#%u;", static_cast<Uint8>(c)); os << charref; } else { switch (c) { case '&': os << "&"; break; case '<': os << "<"; break; case '>': os << ">"; break; case '"': os << """; break; case '\'': os << "'"; break; default: os << c; } }}void _xmlWritter_appendSurrogatePair(Buffer& out, Uint16 high, Uint16 low){ char str[6]; Uint8 charIN[5]; memset(str,0x00,sizeof(str)); memcpy(&charIN,&high,2); memcpy(&charIN[2],&low,2); const Uint16 *strsrc = (Uint16 *)charIN; Uint16 *endsrc = (Uint16 *)&charIN[3]; Uint8 *strtgt = (Uint8 *)str; Uint8 *endtgt = (Uint8 *)&str[5]; UTF16toUTF8( &strsrc, endsrc, &strtgt, endtgt); Uint32 number1 = UTF_8_COUNT_TRAIL_BYTES(str[0]) + 1; out.append(str,number1);}inline void _xmlWritter_appendSpecial(PEGASUS_STD(ostream)& os, const char* str){ while (*str) _xmlWritter_appendSpecialChar(os, *str++);}void XmlWriter::append(Buffer& out, const Char16& x){ _xmlWritter_appendChar(out, x);}void XmlWriter::append(Buffer& out, Boolean x){ append(out, (x ? "TRUE" : "FALSE"));}void XmlWriter::append(Buffer& out, Uint32 x){ char buffer[32]; sprintf(buffer, "%u", x); append(out, buffer);}void XmlWriter::append(Buffer& out, Sint32 x){ char buffer[32]; sprintf(buffer, "%d", x); append(out, buffer);}void XmlWriter::append(Buffer& out, Uint64 x){ char buffer[32]; // Should need 21 chars max sprintf(buffer, "%" PEGASUS_64BIT_CONVERSION_WIDTH "u", x); append(out, buffer);}void XmlWriter::append(Buffer& out, Sint64 x){ char buffer[32]; // Should need 21 chars max sprintf(buffer, "%" PEGASUS_64BIT_CONVERSION_WIDTH "d", x); append(out, buffer);}void XmlWriter::append(Buffer& out, Real32 x){ char buffer[128]; // %.7e gives '[-]m.ddddddde+/-xx', which seems compatible with the format // given in the CIM/XML spec, and the precision required by the CIM 2.2 spec // (4 byte IEEE floating point) sprintf(buffer, "%.7e", x);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -