📄 formatter.cpp
字号:
/*
* Copyright (C) 2003-2007 Funambol, Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY, TITLE, NONINFRINGEMENT 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 this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
* 02111-1307 USA
*/
#include "syncml/formatter/Formatter.h"
#include "base/Log.h"
#define EMPTY_VALUE "__EMPTY__"
/*
* Returns a StringBuffer giving the tag and the value as long. To use for generic simple value
*/
StringBuffer* Formatter::getValue(const char* tagName, long value, const char *params) {
if (!value)
return NULL;
char* t1 = new char[strlen(tagName) + 3 + (params ? strlen(params) + 1 : 0)]; // < > 0 plus optional parameters
char* t2 = new char[strlen(tagName) + 5]; // </ > \n 0
sprintf(t1, "<%s%s%s>", tagName, params ? " " : "", params ? params : "");
sprintf(t2, "</%s>\n", tagName);
StringBuffer* s = new StringBuffer();
s->append(t1);
s->append(value);
s->append(t2);
safeDel(&t1);
safeDel(&t2);
return s;
}
/*
* Returns a StringBuffer giving the tag and the value as BOOL. If true return only the tag, nothing otherwise
*/
StringBuffer* Formatter::getValue(const char* tagName, BOOL value, const char *params) {
if (!value)
return NULL;
char* t1 = new char[strlen(tagName) + 4 + (params ? strlen(params) + 1 : 0)]; // < /> plus optional parameters
sprintf(t1, "<%s%s%s/>", tagName, params ? " " : "", params ? params : "");
StringBuffer* s = new StringBuffer();
s->append(t1);
safeDel(&t1);
return s;
}
/*
* Returns a StringBuffer giving the tag and the value.
* Returns NULL if value's length is 0.
*/
StringBuffer* Formatter::getValueNotEmpty(const char* tagName, const char* value, const char *params) {
if (!strlen(value)) {
return NULL;
}
return getValue(tagName, value, params);
}
/*
* Returns a StringBuffer giving the tag and the value as wchar.
* To use for generic simple value
*/
StringBuffer* Formatter::getValue(const char* tagName, const char* value, const char *params) {
if (!value)
return NULL;
char* t1 = new char[strlen(tagName) + 3 + (params ? 1 + strlen(params) : 0)]; // < > 0
char* t2 = new char[strlen(tagName) + 5]; // </ > \n 0
sprintf(t1, "<%s%s%s>", tagName, params ? " " : "", params ? params : "");
sprintf(t2, "</%s>\n", tagName);
StringBuffer* s = new StringBuffer(t1);
if (strcmp(value, EMPTY_VALUE) != 0)
s->append(value);
s->append(t2);
safeDel(&t1);
safeDel(&t2);
return s;
}
/*
* Returns a StringBuffer giving the tag and the value as StringBuffer. To use to include other stuffs
*/
StringBuffer* Formatter::getValue(const char* tagName, StringBuffer* value, const char *params) {
if (!value)
return NULL;
char* t1 = new char[strlen(tagName) + 3 + (params ? strlen(params) + 1 : 0)]; // < > 0 plus optional parameters
char* t2 = new char[strlen(tagName) + 5]; // </ > \n 0
sprintf(t1, "<%s%s%s>", tagName, params ? " " : "", params ? params : "");
sprintf(t2, "</%s>\n", tagName);
StringBuffer* s = new StringBuffer();
s->append(t1);
s->append(value);
s->append(t2);
safeDel(&t1);
safeDel(&t2);
return s;
}
StringBuffer* Formatter::getSyncML(SyncML* syncML) {
StringBuffer* sBody = NULL;
StringBuffer* sHdr = NULL;
StringBuffer* sML = NULL;
if (syncML == NULL) {
return NULL;
}
sHdr = getSyncHdr (syncML->getSyncHdr ());
sBody = getSyncBody(syncML->getSyncBody());
sML = new StringBuffer("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
sML->append("<SyncML>\n");
sML->append(sHdr);
sML->append(sBody);
sML->append("</SyncML>");
deleteAllStringBuffer(2,&sHdr, &sBody);
return sML;
}
StringBuffer* Formatter::getSyncHdr(SyncHdr* syncHdr) {
StringBuffer* ret = NULL;
StringBuffer* s = NULL;
StringBuffer* sessionID = NULL;
StringBuffer* verDTD = NULL;
StringBuffer* verProto = NULL;
StringBuffer* source = NULL;
StringBuffer* target = NULL;
StringBuffer* cred = NULL;
StringBuffer* msgID = NULL;
StringBuffer* respURI = NULL;
StringBuffer* meta = NULL;
sessionID = getSessionID(syncHdr->getSessionID());
verDTD = getVerDTD (syncHdr->getVerDTD());
verProto = getVerProto (syncHdr->getVerProto());
source = getSource (syncHdr->getSource());
target = getTarget (syncHdr->getTarget());
cred = getCred (syncHdr->getCred());
msgID = getValue (MSG_ID, syncHdr->getMsgID());
respURI = getValue (RESP_URI, syncHdr->getRespURI());
meta = getMeta (syncHdr->getMeta());
if (NotZeroStringBufferLenght(9, sessionID, verDTD, verProto, source, target, cred, msgID, respURI, meta)) {
s = new StringBuffer();
s->append(verDTD);
s->append(verProto);
s->append(sessionID);
s->append(msgID);
s->append(target);
s->append(source);
s->append(respURI);
s->append(cred);
s->append(meta);
}
ret = getValue(SYNC_HDR, s);
deleteAllStringBuffer(10, &s, &sessionID, &verDTD, &verProto, &msgID, &respURI, &target, &source, &cred, &meta);
return ret;
}
StringBuffer* Formatter::getCred(Cred* cred) {
if (!cred)
return NULL;
StringBuffer* ret = NULL;
StringBuffer* auth = NULL;
auth = getAuthentication(cred->getAuthentication());
if (auth) {
//ret = new StringBuffer();
ret = getValue(CRED, auth);
}
deleteStringBuffer(&auth);
return ret;
}
StringBuffer* Formatter::getAuthentication(Authentication* auth) {
if (!auth)
return NULL;
StringBuffer* ret = NULL;
StringBuffer* data = NULL;
StringBuffer* meta = NULL;
data = getValue(DATA, auth->getData());
meta = getMeta(auth->getMeta());
if (NotZeroStringBufferLenght(2, data, meta)) {
ret = new StringBuffer();
ret->append(meta);
ret->append(data);
}
deleteAllStringBuffer(2, &meta, &data);
return ret;
}
StringBuffer* Formatter::getMeta(Meta* meta) {
if (!meta)
return NULL;
StringBuffer* ret = NULL;
StringBuffer* metInf = NULL;
metInf = getMetInf(meta->getMetInf());
if (NotZeroStringBufferLenght(1, metInf)) {
ret = getValue(META, metInf);
}
deleteStringBuffer(&metInf);
return ret;
}
StringBuffer* Formatter::getMetInf(MetInf* metInf) {
if (!metInf)
return NULL;
StringBuffer* ret = NULL;
StringBuffer* format = NULL;
StringBuffer* type = NULL;
StringBuffer* mark = NULL;
StringBuffer* anchor = NULL;
StringBuffer* version = NULL;
StringBuffer* nextNonce = NULL;
StringBuffer* maxMsgSize = NULL;
StringBuffer* maxObjSize = NULL;
StringBuffer* size = NULL;
//ArrayList* emi = NULL;
StringBuffer* mem = NULL;
// get all the values
format = getValue(FORMAT, metInf->getFormat(), METINFO);
type = getValue(TYPE, metInf->getType(), METINFO);
mark = getValue(MARK, metInf->getMark());
anchor = getAnchor(metInf->getAnchor());
version = getValue(VERSIONSTR, metInf->getVersion());
nextNonce = getNextNonce(metInf->getNextNonce());
maxMsgSize = getValue(MAX_MESSAGE_SIZE, metInf->getMaxMsgSize(), METINFO);
maxObjSize = getValue(MAX_OBJ_SIZE, metInf->getMaxObjSize(), METINFO);
size = getValue(SIZE, metInf->getSize(), METINFO);
//emi = getEMI(xml);
mem = getMem(metInf->getMem());
if (NotZeroStringBufferLenght(8, format, type, mark, size, anchor, version, maxMsgSize, maxObjSize)) {
ret = new StringBuffer();
ret ->append(format);
ret ->append(type);
ret ->append(mark);
ret ->append(size);
ret ->append(anchor);
ret ->append(version);
ret ->append(nextNonce);
ret ->append(maxMsgSize);
ret ->append(maxObjSize);
ret ->append(mem);
}
deleteAllStringBuffer(10, &format, &type, &mark, &version, &maxMsgSize, &maxObjSize, &size, &nextNonce, &mem, &anchor);
return ret;
}
StringBuffer* Formatter::getMem(Mem* mem) {
if (!mem)
return NULL;
StringBuffer* ret = new StringBuffer();
StringBuffer* tmp = NULL;
tmp = getValue(SHARED_MEM, mem->getSharedMem());
ret->append(tmp);
if (tmp) { delete tmp; tmp = NULL; }
tmp = getValue(FREE_MEM, mem->getFreeMem());
ret->append(tmp);
if (tmp) { delete tmp; tmp = NULL; }
tmp = getValue(FREE_ID, mem->getFreeID());
ret->append(tmp);
if (tmp) { delete tmp; tmp = NULL; }
return ret;
}
StringBuffer* Formatter::getNextNonce(NextNonce* nextNonce) {
if (!nextNonce)
return NULL;
StringBuffer* ret = new StringBuffer();
ret = getValue(NEXT_NONCE, nextNonce->getValueAsBase64());
return ret;
}
StringBuffer* Formatter::getAnchor(Anchor* anchor) {
if (!anchor)
return NULL;
StringBuffer* ret = NULL;
StringBuffer* buf = new StringBuffer();
StringBuffer* tmp = NULL;
tmp = getValue(LAST, anchor->getLast());
buf->append(tmp);
if (tmp) { delete tmp; tmp = NULL; }
tmp = getValue(NEXT, anchor->getNext());
buf->append(tmp);
if (tmp) { delete tmp; tmp = NULL; }
ret = getValue(ANCHOR, (char *)buf->c_str(), METINFO);
if (buf) {delete buf; buf = NULL; }
return ret;
}
/*
* Returns a series of
* <Source> ... </Source>
* <Source> ... </Source>
* <Source> ... </Source>
*
* use a SourceArray class type
*/
StringBuffer* Formatter::getSources(ArrayList* sources) {
if (!sources || !NotZeroArrayLenght(1, sources))
return NULL;
StringBuffer* ret = new StringBuffer();
StringBuffer* tmp = NULL;
for (int i = 0; i < sources->size(); i++) {
tmp = getSourceArray(((SourceArray*)sources->get(i)));
ret->append(tmp);
if (tmp) { delete tmp; tmp = NULL; }
}
return ret;
}
StringBuffer* Formatter::getSourceArray(SourceArray* sourceArray) {
if (!sourceArray)
return NULL;
StringBuffer* ret = new StringBuffer();
StringBuffer* s = new StringBuffer();
StringBuffer* tmp = NULL;
tmp = getValue(LOC_URI, sourceArray->getSource()->getLocURI());
s->append(tmp);
if (tmp) { delete tmp; tmp = NULL; }
tmp = getValue(LOC_NAME, sourceArray->getSource()->getLocName());
s->append(tmp);
if (tmp) { delete tmp; tmp = NULL; }
if (NotZeroStringBufferLenght(1, s)) {
ret = getValue(SOURCE, s);
}
deleteAllStringBuffer(1, &s);
return ret;
}
StringBuffer* Formatter::getSource(Source* source) {
if (!source)
return NULL;
StringBuffer* ret = new StringBuffer();
StringBuffer* s = new StringBuffer();
StringBuffer* tmp = NULL;
tmp = getValue(LOC_URI, source->getLocURI());
s->append(tmp);
if (tmp) { delete tmp; tmp = NULL; }
tmp = getValue(LOC_NAME, source->getLocName());
s->append(tmp);
if (tmp) { delete tmp; tmp = NULL; }
if (NotZeroStringBufferLenght(1, s)) {
delete ret; ret = NULL;
ret = getValue(SOURCE, s);
}
deleteStringBuffer(&s);
return ret;
}
StringBuffer* Formatter::getTarget(Target* target) {
if (!target)
return NULL;
StringBuffer* ret = new StringBuffer();
StringBuffer* s = new StringBuffer();
StringBuffer* filter = new StringBuffer();
StringBuffer* tmp = NULL;
tmp = getValue(LOC_URI, target->getLocURI());
s->append(tmp);
if (tmp) { delete tmp; tmp = NULL; }
tmp = getValue(LOC_NAME, target->getLocName());
s->append(tmp);
if (tmp) { delete tmp; tmp = NULL; }
//
// And now the filter (if any)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -