12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256 |
- /*##############################################################################
- HPCC SYSTEMS software Copyright (C) 2012 HPCC Systems.
- Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License.
- You may obtain a copy of the License at
- http://www.apache.org/licenses/LICENSE-2.0
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
- ############################################################################## */
- #include "platform.h"
- #include "jlib.hpp"
- #include "thorxmlwrite.hpp"
- #include "eclrtl.hpp"
- #include "rtlkey.hpp"
- #include "eclhelper.hpp"
- #include "deftype.hpp"
- #include "rtlbcd.hpp"
- CommonXmlWriter::CommonXmlWriter(unsigned _flags, unsigned initialIndent, IXmlStreamFlusher *_flusher)
- {
- flusher = _flusher;
- flags = _flags;
- indent = initialIndent;
- nestLimit = flags & XWFnoindent ? (unsigned) -1 : 0;
- tagClosed = true;
- }
- CommonXmlWriter::~CommonXmlWriter()
- {
- flush(true);
- }
- CommonXmlWriter & CommonXmlWriter::clear()
- {
- out.clear();
- indent = 0;
- nestLimit = flags & XWFnoindent ? (unsigned) -1 : 0;
- tagClosed = true;
- return *this;
- }
- bool CommonXmlWriter::checkForAttribute(const char * fieldname)
- {
- if (!tagClosed)
- {
- if (fieldname && (fieldname[0] == '@'))
- return true;
- closeTag();
- }
- return false;
- }
- void CommonXmlWriter::closeTag()
- {
- if (!tagClosed)
- {
- out.append(">");
- if (!nestLimit)
- out.newline();
- tagClosed = true;
- }
- flush(false);
- }
- void CommonXmlWriter::outputQuoted(const char *text)
- {
- out.append(text);
- }
- void CommonXmlWriter::outputString(unsigned len, const char *field, const char *fieldname)
- {
- if (flags & XWFtrim)
- len = rtlTrimStrLen(len, field);
- if ((flags & XWFopt) && (rtlTrimStrLen(len, field) == 0))
- return;
- if (checkForAttribute(fieldname))
- outputXmlAttrString(len, field, fieldname+1, out);
- else
- {
- if (!nestLimit)
- out.pad(indent);
- outputXmlString(len, field, fieldname, out);
- if (!nestLimit)
- out.newline();
- }
- }
- void CommonXmlWriter::outputQString(unsigned len, const char *field, const char *fieldname)
- {
- MemoryAttr tempBuffer;
- char * temp;
- if (len <= 100)
- temp = (char *)alloca(len);
- else
- temp = (char *)tempBuffer.allocate(len);
- rtlQStrToStr(len, temp, len, field);
- outputString(len, temp, fieldname);
- }
- void CommonXmlWriter::outputBool(bool field, const char *fieldname)
- {
- if (checkForAttribute(fieldname))
- outputXmlAttrBool(field, fieldname+1, out);
- else
- {
- if (!nestLimit)
- out.pad(indent);
- outputXmlBool(field, fieldname, out);
- if (!nestLimit)
- out.newline();
- }
- }
- void CommonXmlWriter::outputData(unsigned len, const void *field, const char *fieldname)
- {
- if (checkForAttribute(fieldname))
- outputXmlAttrData(len, field, fieldname+1, out);
- else
- {
- if (!nestLimit)
- out.pad(indent);
- outputXmlData(len, field, fieldname, out);
- if (!nestLimit)
- out.newline();
- }
- }
- void CommonXmlWriter::outputInt(__int64 field, const char *fieldname)
- {
- if (checkForAttribute(fieldname))
- outputXmlAttrInt(field, fieldname+1, out);
- else
- {
- if (!nestLimit)
- out.pad(indent);
- outputXmlInt(field, fieldname, out);
- if (!nestLimit)
- out.newline();
- }
- }
- void CommonXmlWriter::outputUInt(unsigned __int64 field, const char *fieldname)
- {
- if (checkForAttribute(fieldname))
- outputXmlAttrUInt(field, fieldname+1, out);
- else
- {
- if (!nestLimit)
- out.pad(indent);
- outputXmlUInt(field, fieldname, out);
- if (!nestLimit)
- out.newline();
- }
- }
- void CommonXmlWriter::outputReal(double field, const char *fieldname)
- {
- if (checkForAttribute(fieldname))
- outputXmlAttrReal(field, fieldname+1, out);
- else
- {
- if (!nestLimit)
- out.pad(indent);
- outputXmlReal(field, fieldname, out);
- if (!nestLimit)
- out.newline();
- }
- }
- void CommonXmlWriter::outputDecimal(const void *field, unsigned size, unsigned precision, const char *fieldname)
- {
- if (checkForAttribute(fieldname))
- outputXmlAttrDecimal(field, size, precision, fieldname+1, out);
- else
- {
- if (!nestLimit)
- out.pad(indent);
- outputXmlDecimal(field, size, precision, fieldname, out);
- if (!nestLimit)
- out.newline();
- }
- }
- void CommonXmlWriter::outputUDecimal(const void *field, unsigned size, unsigned precision, const char *fieldname)
- {
- if (checkForAttribute(fieldname))
- outputXmlAttrUDecimal(field, size, precision, fieldname+1, out);
- else
- {
- if (!nestLimit)
- out.pad(indent);
- outputXmlUDecimal(field, size, precision, fieldname, out);
- if (!nestLimit)
- out.newline();
- }
- }
- void CommonXmlWriter::outputUnicode(unsigned len, const UChar *field, const char *fieldname)
- {
- if (flags & XWFtrim)
- len = rtlTrimUnicodeStrLen(len, field);
- if ((flags & XWFopt) && (rtlTrimUnicodeStrLen(len, field) == 0))
- return;
- if (checkForAttribute(fieldname))
- outputXmlAttrUnicode(len, field, fieldname+1, out);
- else
- {
- if (!nestLimit)
- out.pad(indent);
- outputXmlUnicode(len, field, fieldname, out);
- if (!nestLimit)
- out.newline();
- }
- }
- void CommonXmlWriter::outputUtf8(unsigned len, const char *field, const char *fieldname)
- {
- if (flags & XWFtrim)
- len = rtlTrimUtf8StrLen(len, field);
- if ((flags & XWFopt) && (rtlTrimUtf8StrLen(len, field) == 0))
- return;
- if (checkForAttribute(fieldname))
- outputXmlAttrUtf8(len, field, fieldname+1, out);
- else
- {
- if (!nestLimit)
- out.pad(indent);
- outputXmlUtf8(len, field, fieldname, out);
- if (!nestLimit)
- out.newline();
- }
- }
- void CommonXmlWriter::outputBeginNested(const char *fieldname, bool nestChildren)
- {
- const char * sep = strchr(fieldname, '/');
- if (sep)
- {
- StringAttr leading(fieldname, sep-fieldname);
- outputBeginNested(leading, nestChildren);
- outputBeginNested(sep+1, nestChildren);
- return;
- }
- closeTag();
- if (!nestLimit)
- out.pad(indent);
- out.append('<').append(fieldname);
- indent += 1;
- if (!nestChildren && !nestLimit)
- nestLimit = indent;
- tagClosed = false;
- }
- void CommonXmlWriter::outputEndNested(const char *fieldname)
- {
- const char * sep = strchr(fieldname, '/');
- if (sep)
- {
- StringAttr leading(fieldname, sep-fieldname);
- outputEndNested(sep+1);
- outputEndNested(leading);
- return;
- }
- if (flags & XWFexpandempty)
- closeTag();
- if (!tagClosed)
- {
- out.append("/>");
- tagClosed = true;
- }
- else
- {
- if (!nestLimit)
- out.pad(indent-1);
- out.append("</").append(fieldname).append('>');
- }
- if (indent==nestLimit)
- nestLimit = 0;
- indent -= 1;
- if (!nestLimit)
- out.newline();
- }
- void CommonXmlWriter::outputSetAll()
- {
- closeTag();
- if (!nestLimit)
- out.pad(indent);
- outputXmlSetAll(out);
- if (!nestLimit)
- out.newline();
- }
- //=====================================================================================
- CommonJsonWriter::CommonJsonWriter(unsigned _flags, unsigned initialIndent, IXmlStreamFlusher *_flusher)
- {
- flusher = _flusher;
- flags = _flags;
- indent = initialIndent;
- nestLimit = flags & XWFnoindent ? (unsigned) -1 : 0;
- needDelimiter = false;
- }
- CommonJsonWriter::~CommonJsonWriter()
- {
- flush(true);
- }
- CommonJsonWriter & CommonJsonWriter::clear()
- {
- out.clear();
- indent = 0;
- nestLimit = flags & XWFnoindent ? (unsigned) -1 : 0;
- return *this;
- }
- void CommonJsonWriter::checkFormat(bool doDelimit, bool delimitNext, int inc)
- {
- if (doDelimit)
- {
- if (needDelimiter)
- {
- if (!out.length()) //new block
- out.append(',');
- else
- delimitJSON(out);
- }
- if (!nestLimit)
- out.append('\n').pad(indent);
- }
- indent+=inc;
- needDelimiter = delimitNext;
- }
- void CommonJsonWriter::checkDelimit(int inc)
- {
- checkFormat(true, true, inc);
- }
- const char *CommonJsonWriter::checkItemName(CJsonWriterItem *item, const char *name)
- {
- if (item && item->depth==0 && strieq(item->name, name))
- return NULL;
- return name;
- }
- const char *CommonJsonWriter::checkItemName(const char *name)
- {
- CJsonWriterItem *item = (arrays.length()) ? &arrays.tos() : NULL;
- return checkItemName(item, name);
- }
- const char *CommonJsonWriter::checkItemNameInc(const char *name)
- {
- CJsonWriterItem *item = (arrays.length()) ? &arrays.tos() : NULL;
- name = checkItemName(item, name);
- if (item)
- item->depth++;
- return name;
- }
- const char *CommonJsonWriter::checkItemNameDec(const char *name)
- {
- CJsonWriterItem *item = (arrays.length()) ? &arrays.tos() : NULL;
- if (item)
- item->depth--;
- return checkItemName(item, name);
- }
- void CommonJsonWriter::outputQuoted(const char *text)
- {
- checkDelimit();
- appendJSONValue(out, NULL, text);
- }
- void CommonJsonWriter::outputString(unsigned len, const char *field, const char *fieldname)
- {
- if (flags & XWFtrim)
- len = rtlTrimStrLen(len, field);
- if ((flags & XWFopt) && (rtlTrimStrLen(len, field) == 0))
- return;
- checkDelimit();
- appendJSONValue(out, checkItemName(fieldname), len, field);
- }
- void CommonJsonWriter::outputQString(unsigned len, const char *field, const char *fieldname)
- {
- MemoryAttr tempBuffer;
- char * temp;
- if (len <= 100)
- temp = (char *)alloca(len);
- else
- temp = (char *)tempBuffer.allocate(len);
- rtlQStrToStr(len, temp, len, field);
- outputString(len, temp, fieldname);
- }
- void CommonJsonWriter::outputBool(bool field, const char *fieldname)
- {
- checkDelimit();
- appendJSONValue(out, checkItemName(fieldname), field);
- }
- void CommonJsonWriter::outputData(unsigned len, const void *field, const char *fieldname)
- {
- checkDelimit();
- appendJSONValue(out, checkItemName(fieldname), len, field);
- }
- void CommonJsonWriter::outputInt(__int64 field, const char *fieldname)
- {
- checkDelimit();
- appendJSONValue(out, checkItemName(fieldname), field);
- }
- void CommonJsonWriter::outputUInt(unsigned __int64 field, const char *fieldname)
- {
- checkDelimit();
- appendJSONValue(out, checkItemName(fieldname), field);
- }
- void CommonJsonWriter::outputReal(double field, const char *fieldname)
- {
- checkDelimit();
- appendJSONValue(out, checkItemName(fieldname), field);
- }
- void CommonJsonWriter::outputDecimal(const void *field, unsigned size, unsigned precision, const char *fieldname)
- {
- checkDelimit();
- outputJsonDecimal(field, size, precision, checkItemName(fieldname), out);
- }
- void CommonJsonWriter::outputUDecimal(const void *field, unsigned size, unsigned precision, const char *fieldname)
- {
- checkDelimit();
- outputJsonUDecimal(field, size, precision, checkItemName(fieldname), out);
- }
- void CommonJsonWriter::outputUnicode(unsigned len, const UChar *field, const char *fieldname)
- {
- if (flags & XWFtrim)
- len = rtlTrimUnicodeStrLen(len, field);
- if ((flags & XWFopt) && (rtlTrimUnicodeStrLen(len, field) == 0))
- return;
- checkDelimit();
- outputJsonUnicode(len, field, checkItemName(fieldname), out);
- }
- void CommonJsonWriter::outputUtf8(unsigned len, const char *field, const char *fieldname)
- {
- if (flags & XWFtrim)
- len = rtlTrimUtf8StrLen(len, field);
- if ((flags & XWFopt) && (rtlTrimUtf8StrLen(len, field) == 0))
- return;
- checkDelimit();
- appendJSONValue(out, checkItemName(fieldname), len, field);
- }
- void CommonJsonWriter::outputBeginArray(const char *fieldname)
- {
- arrays.append(*new CJsonWriterItem(fieldname));
- const char * sep = strchr(fieldname, '/');
- while (sep)
- {
- StringAttr leading(fieldname, sep-fieldname);
- appendJSONName(out, leading).append(" {");
- fieldname = sep+1;
- sep = strchr(fieldname, '/');
- }
- checkFormat(false, false, 1);
- appendJSONName(out, fieldname).append('[');
- }
- void CommonJsonWriter::outputEndArray(const char *fieldname)
- {
- arrays.pop();
- checkFormat(false, true, -1);
- out.append(']');
- const char * sep = (fieldname) ? strchr(fieldname, '/') : NULL;
- while (sep)
- {
- out.append('}');
- sep = strchr(sep+1, '/');
- }
- }
- void CommonJsonWriter::outputBeginNested(const char *fieldname, bool nestChildren)
- {
- flush(false);
- checkFormat(true, false, 1);
- fieldname = checkItemNameInc(fieldname);
- if (fieldname)
- {
- const char * sep = (fieldname) ? strchr(fieldname, '/') : NULL;
- while (sep)
- {
- StringAttr leading(fieldname, sep-fieldname);
- appendJSONName(out, leading).append("{");
- fieldname = sep+1;
- sep = strchr(fieldname, '/');
- }
- appendJSONName(out, fieldname);
- }
- out.append("{");
- if (!nestChildren && !nestLimit)
- nestLimit = indent;
- }
- void CommonJsonWriter::outputEndNested(const char *fieldname)
- {
- flush(false);
- checkFormat(false, true, -1);
- fieldname = checkItemNameDec(fieldname);
- if (fieldname)
- {
- const char * sep = (fieldname) ? strchr(fieldname, '/') : NULL;
- while (sep)
- {
- out.append('}');
- sep = strchr(sep+1, '/');
- }
- }
- out.append("}");
- if (indent==nestLimit)
- nestLimit = 0;
- }
- void CommonJsonWriter::outputSetAll()
- {
- flush(false);
- checkDelimit();
- appendJSONValue(out, NULL, "All");
- }
- //=====================================================================================
- inline void outputEncodedXmlString(unsigned len, const char *field, const char *fieldname, StringBuffer &out)
- {
- if (fieldname)
- out.append('<').append(fieldname).append(" xsi:type=\"xsd:string\">");
- encodeXML(field, out, 0, len);
- if (fieldname)
- out.append("</").append(fieldname).append('>');
- }
- inline void outputEncodedXmlBool(bool field, const char *fieldname, StringBuffer &out)
- {
- const char * text = field ? "true" : "false";
- if (fieldname)
- out.append('<').append(fieldname).append(" xsi:type=\"xsd:boolean\">").append(text).append("</").append(fieldname).append('>');
- else
- out.append(text);
- }
- static char thorHelperhexchar[] = "0123456789ABCDEF";
- inline void outputEncodedXmlData(unsigned len, const void *_field, const char *fieldname, StringBuffer &out)
- {
- const unsigned char *field = (const unsigned char *) _field;
- if (fieldname)
- out.append('<').append(fieldname).append(" xsi:type=\"xsd:hexBinary\">");
- for (unsigned int i = 0; i < len; i++)
- {
- out.append(thorHelperhexchar[field[i] >> 4]).append(thorHelperhexchar[field[i] & 0x0f]);
- }
- if (fieldname)
- out.append("</").append(fieldname).append('>');
- }
- inline void outputEncoded64XmlData(unsigned len, const void *_field, const char *fieldname, StringBuffer &out)
- {
- if (fieldname)
- out.append('<').append(fieldname).append(" xsi:type=\"xsd:base64Binary\">");
- JBASE64_Encode(_field, len, out, false);
- if (fieldname)
- out.append("</").append(fieldname).append('>');
- }
- inline void outputEncodedXmlInt(__int64 field, const char *fieldname, StringBuffer &out)
- {
- if (fieldname)
- out.append('<').append(fieldname).append(" xsi:type=\"xsd:integer\">").append(field).append("</").append(fieldname).append('>');
- else
- out.append(field);
- }
- inline void outputEncodedXmlUInt(unsigned __int64 field, const char *fieldname, StringBuffer &out)
- {
- if (fieldname)
- out.append('<').append(fieldname).append(" xsi:type=\"xsd:nonNegativeInteger\">").append(field).append("</").append(fieldname).append('>');
- else
- out.append(field);
- }
- inline void outputEncodedXmlReal(double field, const char *fieldname, StringBuffer &out)
- {
- if (fieldname)
- out.append('<').append(fieldname).append(" xsi:type=\"xsd:double\">").append(field).append("</").append(fieldname).append('>');
- else
- out.append(field);
- }
- inline void outputEncodedXmlDecimal(const void *field, unsigned size, unsigned precision, const char *fieldname, StringBuffer &out)
- {
- char dec[50];
- if (fieldname)
- out.append('<').append(fieldname).append(" xsi:type=\"xsd:decimal\">");
- DecLock();
- if (DecValid(true, size*2-1, field))
- {
- DecPushDecimal(field, size, precision);
- DecPopCString(sizeof(dec), dec);
- const char *finger = dec;
- while(isspace(*finger)) finger++;
- out.append(finger);
- }
- else
- out.append("####");
- DecUnlock();
- if (fieldname)
- out.append("</").append(fieldname).append('>');
- }
- inline void outputEncodedXmlUDecimal(const void *field, unsigned size, unsigned precision, const char *fieldname, StringBuffer &out)
- {
- char dec[50];
- if (fieldname)
- out.append('<').append(fieldname).append(" xsi:type=\"xsd:decimal\">");
- DecLock();
- if (DecValid(false, size*2, field))
- {
- DecPushUDecimal(field, size, precision);
- DecPopCString(sizeof(dec), dec);
- const char *finger = dec;
- while(isspace(*finger)) finger++;
- out.append(finger);
- }
- else
- out.append("####");
- DecUnlock();
- if (fieldname)
- out.append("</").append(fieldname).append('>');
- }
- inline void outputEncodedXmlUnicode(unsigned len, const UChar *field, const char *fieldname, StringBuffer &out)
- {
- char * buff = 0;
- unsigned bufflen = 0;
- rtlUnicodeToCodepageX(bufflen, buff, len, field, "utf-8");
- if (fieldname)
- out.append('<').append(fieldname).append(" xsi:type=\"xsd:string\">");
- encodeXML(buff, out, 0, bufflen, true); // output as UTF-8
- if (fieldname)
- out.append("</").append(fieldname).append('>');
- rtlFree(buff);
- }
- inline void outputEncodedXmlUtf8(unsigned len, const char *field, const char *fieldname, StringBuffer &out)
- {
- if (fieldname)
- out.append('<').append(fieldname).append(" xsi:type=\"xsd:string\">");
- encodeXML(field, out, 0, rtlUtf8Size(len, field), true); // output as UTF-8
- if (fieldname)
- out.append("</").append(fieldname).append('>');
- }
- //=====================================================================================
- CommonEncodedXmlWriter::CommonEncodedXmlWriter(unsigned _flags, unsigned initialIndent, IXmlStreamFlusher *_flusher)
- : CommonXmlWriter(_flags, initialIndent, _flusher)
- {
- }
- void CommonEncodedXmlWriter::outputString(unsigned len, const char *field, const char *fieldname)
- {
- if (flags & XWFtrim)
- len = rtlTrimStrLen(len, field);
- if ((flags & XWFopt) && (rtlTrimStrLen(len, field) == 0))
- return;
- if (checkForAttribute(fieldname))
- outputXmlAttrString(len, field, fieldname+1, out);
- else
- {
- if (!nestLimit)
- out.pad(indent);
- outputEncodedXmlString(len, field, fieldname, out);
- if (!nestLimit)
- out.newline();
- }
- }
- void CommonEncodedXmlWriter::outputBool(bool field, const char *fieldname)
- {
- if (checkForAttribute(fieldname))
- outputXmlAttrBool(field, fieldname+1, out);
- else
- {
- if (!nestLimit)
- out.pad(indent);
- outputEncodedXmlBool(field, fieldname, out);
- if (!nestLimit)
- out.newline();
- }
- }
- void CommonEncodedXmlWriter::outputData(unsigned len, const void *field, const char *fieldname)
- {
- if (checkForAttribute(fieldname))
- outputXmlAttrData(len, field, fieldname+1, out);
- else
- {
- if (!nestLimit)
- out.pad(indent);
- outputEncodedXmlData(len, field, fieldname, out);
- if (!nestLimit)
- out.newline();
- }
- }
- void CommonEncodedXmlWriter::outputInt(__int64 field, const char *fieldname)
- {
- if (checkForAttribute(fieldname))
- outputXmlAttrInt(field, fieldname+1, out);
- else
- {
- if (!nestLimit)
- out.pad(indent);
- outputEncodedXmlInt(field, fieldname, out);
- if (!nestLimit)
- out.newline();
- }
- }
- void CommonEncodedXmlWriter::outputUInt(unsigned __int64 field, const char *fieldname)
- {
- if (checkForAttribute(fieldname))
- outputXmlAttrUInt(field, fieldname+1, out);
- else
- {
- if (!nestLimit)
- out.pad(indent);
- outputEncodedXmlUInt(field, fieldname, out);
- if (!nestLimit)
- out.newline();
- }
- }
- void CommonEncodedXmlWriter::outputReal(double field, const char *fieldname)
- {
- if (checkForAttribute(fieldname))
- outputXmlAttrReal(field, fieldname+1, out);
- else
- {
- if (!nestLimit)
- out.pad(indent);
- outputEncodedXmlReal(field, fieldname, out);
- if (!nestLimit)
- out.newline();
- }
- }
- void CommonEncodedXmlWriter::outputDecimal(const void *field, unsigned size, unsigned precision, const char *fieldname)
- {
- if (checkForAttribute(fieldname))
- outputXmlAttrDecimal(field, size, precision, fieldname+1, out);
- else
- {
- if (!nestLimit)
- out.pad(indent);
- outputEncodedXmlDecimal(field, size, precision, fieldname, out);
- if (!nestLimit)
- out.newline();
- }
- }
- void CommonEncodedXmlWriter::outputUDecimal(const void *field, unsigned size, unsigned precision, const char *fieldname)
- {
- if (checkForAttribute(fieldname))
- outputXmlAttrUDecimal(field, size, precision, fieldname+1, out);
- else
- {
- if (!nestLimit)
- out.pad(indent);
- outputEncodedXmlUDecimal(field, size, precision, fieldname, out);
- if (!nestLimit)
- out.newline();
- }
- }
- void CommonEncodedXmlWriter::outputUnicode(unsigned len, const UChar *field, const char *fieldname)
- {
- if (flags & XWFtrim)
- len = rtlTrimUnicodeStrLen(len, field);
- if ((flags & XWFopt) && (rtlTrimUnicodeStrLen(len, field) == 0))
- return;
- if (checkForAttribute(fieldname))
- outputXmlAttrUnicode(len, field, fieldname+1, out);
- else
- {
- if (!nestLimit)
- out.pad(indent);
- outputEncodedXmlUnicode(len, field, fieldname, out);
- if (!nestLimit)
- out.newline();
- }
- }
- void CommonEncodedXmlWriter::outputUtf8(unsigned len, const char *field, const char *fieldname)
- {
- if (flags & XWFtrim)
- len = rtlTrimUtf8StrLen(len, field);
- if ((flags & XWFopt) && (rtlTrimUtf8StrLen(len, field) == 0))
- return;
- if (checkForAttribute(fieldname))
- outputXmlAttrUtf8(len, field, fieldname+1, out);
- else
- {
- if (!nestLimit)
- out.pad(indent);
- outputEncodedXmlUtf8(len, field, fieldname, out);
- if (!nestLimit)
- out.newline();
- }
- }
- //=====================================================================================
- CommonEncoded64XmlWriter::CommonEncoded64XmlWriter(unsigned _flags, unsigned initialIndent, IXmlStreamFlusher *_flusher)
- : CommonEncodedXmlWriter(_flags, initialIndent, _flusher)
- {
- }
- void CommonEncoded64XmlWriter::outputData(unsigned len, const void *field, const char *fieldname)
- {
- if (checkForAttribute(fieldname))
- outputXmlAttrData(len, field, fieldname+1, out);
- else
- {
- if (!nestLimit)
- out.pad(indent);
- outputEncoded64XmlData(len, field, fieldname, out);
- if (!nestLimit)
- out.newline();
- }
- }
- //=====================================================================================
- CommonXmlWriter * CreateCommonXmlWriter(unsigned _flags, unsigned _initialIndent, IXmlStreamFlusher *_flusher, XMLWriterType xmlType)
- {
- switch (xmlType)
- {
- case WTStandard:
- return new CommonXmlWriter(_flags, _initialIndent, _flusher);//standard XML writer
- case WTEncodingData64:
- return new CommonEncoded64XmlWriter(_flags, _initialIndent, _flusher);//writes xsd type attributes, and all data as base64binary
- case WTEncoding:
- return new CommonEncodedXmlWriter(_flags, _initialIndent, _flusher);//writes xsd type attributes, and all data as hexBinary
- default:
- assertex(false);
- return NULL;
- }
- }
- //=====================================================================================
- IXmlWriter * createIXmlWriter(unsigned _flags, unsigned _initialIndent, IXmlStreamFlusher *_flusher, XMLWriterType xmlType)
- {
- if (xmlType==WTJSON)
- return new CommonJsonWriter(_flags, _initialIndent, _flusher);
- return CreateCommonXmlWriter(_flags, _initialIndent, _flusher, xmlType);
- }
- //=====================================================================================
- SimpleOutputWriter::SimpleOutputWriter()
- {
- separatorNeeded = false;
- }
- void SimpleOutputWriter::outputFieldSeparator()
- {
- if (separatorNeeded)
- out.append(',');
- separatorNeeded = true;
- }
- SimpleOutputWriter & SimpleOutputWriter::clear()
- {
- out.clear();
- separatorNeeded = false;
- return *this;
- }
- void SimpleOutputWriter::outputQuoted(const char *text)
- {
- out.append(text);
- }
- void SimpleOutputWriter::outputString(unsigned len, const char *field, const char *)
- {
- outputFieldSeparator();
- out.append(len, field);
- }
- void SimpleOutputWriter::outputQString(unsigned len, const char *field, const char *fieldname)
- {
- MemoryAttr tempBuffer;
- char * temp;
- if (len <= 100)
- temp = (char *)alloca(len);
- else
- temp = (char *)tempBuffer.allocate(len);
- rtlQStrToStr(len, temp, len, field);
- outputString(len, temp, fieldname);
- }
- void SimpleOutputWriter::outputBool(bool field, const char *)
- {
- outputFieldSeparator();
- outputXmlBool(field, NULL, out);
- }
- void SimpleOutputWriter::outputData(unsigned len, const void *field, const char *)
- {
- outputFieldSeparator();
- outputXmlData(len, field, NULL, out);
- }
- void SimpleOutputWriter::outputInt(__int64 field, const char *)
- {
- outputFieldSeparator();
- outputXmlInt(field, NULL, out);
- }
- void SimpleOutputWriter::outputUInt(unsigned __int64 field, const char *)
- {
- outputFieldSeparator();
- outputXmlUInt(field, NULL, out);
- }
- void SimpleOutputWriter::outputReal(double field, const char *)
- {
- outputFieldSeparator();
- outputXmlReal(field, NULL, out);
- }
- void SimpleOutputWriter::outputDecimal(const void *field, unsigned size, unsigned precision, const char *)
- {
- outputFieldSeparator();
- outputXmlDecimal(field, size, precision, NULL, out);
- }
- void SimpleOutputWriter::outputUDecimal(const void *field, unsigned size, unsigned precision, const char *)
- {
- outputFieldSeparator();
- outputXmlUDecimal(field, size, precision, NULL, out);
- }
- void SimpleOutputWriter::outputUnicode(unsigned len, const UChar *field, const char *)
- {
- outputFieldSeparator();
- outputXmlUnicode(len, field, NULL, out);
- }
- void SimpleOutputWriter::outputUtf8(unsigned len, const char *field, const char *)
- {
- outputFieldSeparator();
- outputXmlUtf8(len, field, NULL, out);
- }
- void SimpleOutputWriter::outputBeginNested(const char *s, bool)
- {
- if (!s || !*s)
- return;
- outputFieldSeparator();
- out.append('[');
- separatorNeeded = false;
- }
- void SimpleOutputWriter::outputEndNested(const char *s)
- {
- if (!s || !*s)
- return;
- out.append(']');
- separatorNeeded = true;
- }
- void SimpleOutputWriter::outputSetAll()
- {
- out.append('*');
- }
- void SimpleOutputWriter::newline()
- {
- out.append('\n');
- }
- //=====================================================================================
- CommonFieldProcessor::CommonFieldProcessor(StringBuffer &_result, bool _trim) : result(_result), trim(_trim)
- {
- }
- void CommonFieldProcessor::processString(unsigned len, const char *value, const RtlFieldInfo * field)
- {
- if (trim)
- len = rtlTrimStrLen(len, value);
- result.append("'");
- outputXmlString(len, value, NULL, result);
- result.append("'");
- }
- void CommonFieldProcessor::processBool(bool value, const RtlFieldInfo * field)
- {
- outputXmlBool(value, NULL, result);
- }
- void CommonFieldProcessor::processData(unsigned len, const void *value, const RtlFieldInfo * field)
- {
- outputXmlData(len, value, NULL, result);
- }
- void CommonFieldProcessor::processInt(__int64 value, const RtlFieldInfo * field)
- {
- outputXmlInt(value, NULL, result);
- }
- void CommonFieldProcessor::processUInt(unsigned __int64 value, const RtlFieldInfo * field)
- {
- outputXmlUInt(value, NULL, result);
- }
- void CommonFieldProcessor::processReal(double value, const RtlFieldInfo * field)
- {
- outputXmlReal(value, NULL, result);
- }
- void CommonFieldProcessor::processDecimal(const void *value, unsigned digits, unsigned precision, const RtlFieldInfo * field)
- {
- outputXmlDecimal(value, digits, precision, NULL, result);
- }
- void CommonFieldProcessor::processUDecimal(const void *value, unsigned digits, unsigned precision, const RtlFieldInfo * field)
- {
- outputXmlUDecimal(value, digits, precision, NULL, result);
- }
- void CommonFieldProcessor::processUnicode(unsigned len, const UChar *value, const RtlFieldInfo * field)
- {
- if (trim)
- len = rtlTrimUnicodeStrLen(len, value);
- outputXmlUnicode(len, value, NULL, result);
- }
- void CommonFieldProcessor::processQString(unsigned len, const char *value, const RtlFieldInfo * field)
- {
- MemoryAttr tempBuffer;
- char * temp;
- if (len <= 100)
- temp = (char *)alloca(len);
- else
- temp = (char *)tempBuffer.allocate(len);
- rtlQStrToStr(len, temp, len, value);
- processString(len, temp, field);
- }
- void CommonFieldProcessor::processSetAll(const RtlFieldInfo * field)
- {
- result.append("ALL");
- }
- void CommonFieldProcessor::processUtf8(unsigned len, const char *value, const RtlFieldInfo * field)
- {
- if (trim)
- len = rtlTrimUtf8StrLen(len, value);
- outputXmlUtf8(len, value, NULL, result);
- }
- bool CommonFieldProcessor::processBeginSet(const RtlFieldInfo * field)
- {
- result.append('[');
- return true;
- }
- bool CommonFieldProcessor::processBeginDataset(const RtlFieldInfo * field)
- {
- result.append('[');
- return true;
- }
- bool CommonFieldProcessor::processBeginRow(const RtlFieldInfo * field)
- {
- result.append('{');
- return true;
- }
- void CommonFieldProcessor::processEndSet(const RtlFieldInfo * field)
- {
- result.append(']');
- }
- void CommonFieldProcessor::processEndDataset(const RtlFieldInfo * field)
- {
- result.append(']');
- }
- void CommonFieldProcessor::processEndRow(const RtlFieldInfo * field)
- {
- result.append('}');
- }
- //=============================================================================================
- void printKeyedValues(StringBuffer &out, IIndexReadContext *segs, IOutputMetaData *rowMeta)
- {
- unsigned totalKeyedSize = 0;
- unsigned numSegs = segs->ordinality();
- while (numSegs)
- {
- IKeySegmentMonitor &seg = *segs->item(numSegs-1);
- if (!seg.isWild())
- {
- totalKeyedSize = seg.getOffset() + seg.getSize();
- break;
- }
- numSegs--;
- }
- if (numSegs)
- {
- byte *tempRow = (byte *) alloca(totalKeyedSize);
- byte *savedRow = (byte *) alloca(totalKeyedSize);
- const RtlFieldInfo * const *fields = rowMeta->queryTypeInfo()->queryFields();
- unsigned fieldOffset = 0;
- bool inKeyed = false;
- bool inWild = false;
- for (unsigned segNo = 0; segNo < numSegs; segNo++)
- {
- IKeySegmentMonitor &seg = *segs->item(segNo);
- unsigned segOffset = seg.getOffset();
- unsigned segSize = seg.getSize();
- while (fieldOffset < segOffset + segSize) // This is trying to cope with the combined case but not sure it completely does
- {
- assertex(fields[0]->type->isFixedSize());
- unsigned curFieldSize = fields[0]->type->size(NULL, NULL);
- if (seg.isWild())
- {
- if (!inWild)
- {
- if (inKeyed)
- {
- out.append("),");
- inKeyed = false;
- }
- out.append("WILD(");
- inWild = true;
- }
- else
- out.append(',');
- out.append(fields[0]->name);
- }
- else
- {
- StringBuffer setValues;
- CommonFieldProcessor setProcessor(setValues, true);
- unsigned numValues = 0;
- unsigned subStringLength = 0;
- if (!seg.isEmpty())
- {
- seg.setLow(tempRow);
- loop
- {
- if (numValues)
- setValues.append(",");
- memcpy(savedRow+segOffset, tempRow+segOffset, segSize);
- seg.endRange(tempRow);
- if (memcmp(savedRow+segOffset, tempRow+segOffset, segSize) != 0)
- {
- // Special case - if they differ only in trailing values that are 0 vs 0xff, then it's a substring match...
- if (numValues==0 && (fields[0]->type->fieldType & (RFTMkind | RFTMebcdic)) == type_string)
- {
- unsigned pos;
- for (pos = 0; pos < segSize; pos++)
- {
- if (savedRow[segOffset+pos] != tempRow[segOffset+pos])
- break;
- }
- subStringLength = pos;
- for (; pos < segSize; pos++)
- {
- if (savedRow[segOffset+pos] != 0 || tempRow[segOffset+pos] != 0xff)
- {
- subStringLength = 0;
- break;
- }
- }
- }
- fields[0]->process(savedRow + fieldOffset, tempRow, setProcessor);
- setValues.append("..");
- fields[0]->process(tempRow + fieldOffset, tempRow, setProcessor);
- numValues+=2;
- }
- else
- {
- fields[0]->process(tempRow + fieldOffset, tempRow, setProcessor);
- numValues++;
- }
- if (!seg.increment(tempRow))
- break;
- }
- }
- if (!inKeyed)
- {
- if (inWild)
- {
- out.append("),");
- inWild = false;
- }
- out.append("KEYED(");
- inKeyed = true;
- }
- else
- out.append(',');
- out.append(fields[0]->name);
- if (numValues==1)
- out.append("=").append(setValues);
- else if (subStringLength)
- out.appendf("[1..%d]='", subStringLength).append(subStringLength, (char *) savedRow+fieldOffset).append("'");
- else
- out.append(" IN [").append(setValues).append("]");
- }
- fieldOffset += curFieldSize;
- fields++;
- if (!fields[0])
- break;
- }
- }
- if (inKeyed || inWild)
- out.append(")");
- }
- else
- out.append("UNKEYED");
- }
- extern thorhelper_decl void convertRowToXML(size32_t & lenResult, char * & result, IOutputMetaData & info, const void * row, unsigned flags)
- {
- const byte * self = (const byte *)row;
- if (flags == (unsigned)-1)
- flags = XWFtrim|XWFopt|XWFnoindent;
- CommonXmlWriter writer(flags);
- info.toXML(self, writer);
- //could use detach...
- unsigned sizeResult;
- rtlStrToStrX(sizeResult, result, writer.length(), writer.str());
- lenResult = rtlUtf8Length(sizeResult, result);
- }
|