📄 parser.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/parser/Parser.h"
SyncML* Parser::getSyncML(const char*xml) {
SyncBody* syncBody = NULL;
SyncHdr* syncHdr = NULL;
SyncML* syncML = NULL;
unsigned int pos = 0;
char* t = NULL;
t = XMLProcessor::copyElementContent(xml, SYNC_HDR, &pos);
syncHdr = getSyncHdr (t);
if (t) { delete [] t; t = NULL; }
t = XMLProcessor::copyElementContent(xml, SYNC_BODY, &pos);
syncBody = getSyncBody(t);
if (t) { delete [] t; t = NULL; }
syncML = new SyncML(syncHdr, syncBody);
deleteSyncHdr (&syncHdr);
deleteSyncBody(&syncBody);
return syncML;
}
SyncHdr* Parser::getSyncHdr(const char*xml) {
SessionID* sessionID = NULL;
VerDTD* verDTD = NULL;
VerProto* verProto = NULL;
Source* source = NULL;
Target* target = NULL;
Cred* cred = NULL;
char* respURI = NULL;
char* msgID = NULL;
BOOL noResp = NULL;
char* tmp = NULL;
Meta* meta = NULL;
SyncHdr* ret = NULL;
unsigned int pos = 0;
char* t = NULL;
t = XMLProcessor::copyElementContent(xml, SESSION_ID, NULL);
sessionID = getSessionID(t);
if (t) { delete [] t; t = NULL; }
t = XMLProcessor::copyElementContent (xml, VER_DTD, NULL);
verDTD = getVerDTD(t);
if (t) { delete [] t; t = NULL; }
t = XMLProcessor::copyElementContent (xml, VER_PROTO, NULL);
verProto = getVerProto(t);
if (t) { delete [] t; t = NULL; }
t = XMLProcessor::copyElementContent (xml, SOURCE, NULL);
source = getSource(t);
if (t) { delete [] t; t = NULL; }
t = XMLProcessor::copyElementContent (xml, TARGET, NULL);
target = getTarget(t);
if (t) { delete [] t; t = NULL; }
t = XMLProcessor::copyElementContent (xml, CRED, NULL);
cred = getCred(t);
if (t) { delete [] t; t = NULL; }
msgID = XMLProcessor::copyElementContent(xml, MSG_ID, NULL);
respURI = XMLProcessor::copyElementContent(xml, RESP_URI, NULL);
t = XMLProcessor::copyElementContentLevel(xml, META, NULL);
meta = getMeta(t);
if (t) { delete [] t; t = NULL; }
tmp = XMLProcessor::copyElementContent(xml, NO_RESP, NULL);
if (tmp) {
wcscmpIgnoreCase(tmp, "TRUE") ? noResp = TRUE : noResp = FALSE;
}
ret = new SyncHdr(verDTD, verProto, sessionID, msgID, target, source, respURI, noResp, cred, meta);
deleteVerDTD(&verDTD);
deleteVerProto(&verProto);
deleteSessionID(&sessionID);
deleteSource(&source );
deleteTarget(&target);
deleteCred(&cred);
deleteMeta(&meta);
safeDel(&respURI);
safeDel(&msgID);
safeDel(&tmp);
return ret;
}
Cred* Parser::getCred(const char*xml) {
Cred* ret = NULL;
Authentication* auth = NULL;
auth = getAuthentication(xml);
if (auth) {
ret = new Cred(auth);
}
deleteAuthentication(&auth);
return ret;
}
Authentication* Parser::getAuthentication(const char*xml) {
Authentication* ret = NULL;
char* data = NULL;
Meta* meta = NULL;
unsigned int pos = 0;
char* t = NULL;
data = XMLProcessor::copyElementContent (xml, DATA , NULL);
t = XMLProcessor::copyElementContentLevel (xml, META , NULL);
meta = getMeta(t);
if (t) {delete [] t; t = NULL;}
if (data || meta) {
ret = new Authentication(meta, data);
}
safeDel(&data);
deleteMeta(&meta);
return ret;
}
Meta* Parser::getMeta(const char*xml) {
Meta* ret = NULL;
MetInf* metInf = NULL;
metInf = getMetInf(xml);
if (metInf) {
ret = new Meta();
ret->setMetInf(metInf);
}
deleteMetInf(&metInf);
return ret;
}
MetInf* Parser::getMetInf(const char*xml) {
MetInf* ret = NULL;
char* format = NULL;
char* type = NULL;
char* mark = NULL;
Anchor* anchor = NULL;
char* version = NULL;
NextNonce* nextNonce = NULL;
long maxMsgSize = 0;
long maxObjSize = 0;
long size = 0;
ArrayList* emi = NULL;
Mem* mem = NULL;
char* maxMsgSizeW = NULL;
char* maxObjSizeW = NULL;
char* sizeW = NULL;
// get all the values
format = XMLProcessor::copyElementContent (xml, FORMAT , NULL);
type = XMLProcessor::copyElementContent (xml, TYPE , NULL);
mark = XMLProcessor::copyElementContent (xml, MARK , NULL);
anchor = getAnchor(xml);
version = XMLProcessor::copyElementContent (xml, VERSIONSTR , NULL);
nextNonce = getNextNonce(xml);
maxMsgSizeW = XMLProcessor::copyElementContent (xml, MAX_MESSAGE_SIZE , NULL);
maxObjSizeW = XMLProcessor::copyElementContent (xml, MAX_OBJ_SIZE , NULL);
sizeW = XMLProcessor::copyElementContent (xml, SIZE , NULL);
if (maxMsgSizeW) {
maxMsgSize = strtol(maxMsgSizeW, NULL, 10);
}
if (maxObjSizeW) {
maxObjSize = strtol(maxObjSizeW, NULL, 10);
}
if (sizeW) {
size = strtol(sizeW, NULL, 10);
}
emi = getEMI(xml);
mem = getMem(xml);
// check if someting is null, 0 or zero lenght
BOOL isToCreate = FALSE;
isToCreate = NotNullCheck(7, format, type, mark, version, maxMsgSizeW, maxObjSizeW, sizeW)
|| NotZeroArrayLenght(1, emi)
|| (mem)
|| (anchor)
|| (nextNonce);
if (isToCreate) {
ret = new MetInf(format, type, mark, size, anchor, version, nextNonce, maxMsgSize,
maxObjSize, emi, mem);
}
deleteAll(7, &format, &type, &mark, &version, &maxMsgSizeW, &maxObjSizeW, &sizeW);
deleteAnchor(&anchor);
deleteNextNonce(&nextNonce);
deleteArrayList(&emi);
deleteMem(&mem);
return ret;
}
ArrayList* Parser::getSources(const char*xml) {
Source* source = NULL;
SourceArray* sourceArray = NULL;
unsigned int pos = 0, previous = 0;
ArrayList* list = new ArrayList();
char* t = NULL;
t = XMLProcessor::copyElementContent(&xml[pos], SOURCE, &pos);
while ((source = getSource(t)) != NULL) {
if (source) {
sourceArray = new SourceArray(source);
list->add(*sourceArray); // in the ArrayList NULL element cannot be inserted
deleteSource(&source);
deleteSourceArray(&sourceArray);
}
pos += previous;
previous = pos;
if (t) { delete [] t; t = NULL; }
t = XMLProcessor::copyElementContent(&xml[pos], SOURCE, &pos);
}
if (t) { delete [] t; t = NULL;}
return list;
}
Source* Parser::getSource(const char*xml) {
Source* ret = NULL;
char* locURI = NULL;
char* locName = NULL;
locURI = XMLProcessor::copyElementContent (xml, LOC_URI, NULL);
locName = XMLProcessor::copyElementContent (xml, LOC_NAME, NULL);
if (NotNullCheck(2, locURI, locName)) {
ret = new Source(locURI, locName);
}
safeDel(&locURI);
safeDel(&locName);
return ret;
}
Target* Parser::getTarget(const char*xml) {
Target* ret = NULL;
char* locURI = NULL;
char* locName = NULL;
locURI = XMLProcessor::copyElementContent (xml, LOC_URI, NULL);
locName = XMLProcessor::copyElementContent (xml, LOC_NAME, NULL);
if (NotNullCheck(2, locURI, locName)) {
ret = new Target(locURI, locName);
}
safeDel(&locURI);
safeDel(&locName);
return ret;
}
Anchor* Parser::getAnchor(const char*xml) {
Anchor* ret = NULL;
char* last = NULL;
char* next = NULL;
last = XMLProcessor::copyElementContent (xml, LAST, NULL);
next = XMLProcessor::copyElementContent (xml, NEXT, NULL);
if (NotNullCheck(2, last, next)) {
ret = new Anchor(last, next);
}
safeDel(&next);
safeDel(&last);
return ret;
}
NextNonce* Parser::getNextNonce(const char*xml) {
NextNonce* ret = NULL;
char* value = NULL;
value = XMLProcessor::copyElementContent (xml, NEXT_NONCE, NULL);
if (NotNullCheck(1, value)) {
ret = new NextNonce(value);
}
safeDel(&value);
return ret;
}
Mem* Parser::getMem(const char*xml) {
Mem* ret = NULL;
char* freeMemW = NULL;
char* sharedMemW = NULL;
char* freeIDW = NULL;
BOOL sharedMem = NULL;
long freeMem = 0;
long freeID = 0;
BOOL isToCreate = FALSE;
freeMemW = XMLProcessor::copyElementContent (xml, FREE_MEM, NULL);
sharedMemW = XMLProcessor::copyElementContent (xml, SHARED_MEM, NULL);
freeIDW = XMLProcessor::copyElementContent (xml, FREE_ID, NULL);
isToCreate = NotNullCheck(3, freeMemW, sharedMemW, freeIDW);
if (freeMemW != NULL) {
freeMem = strtol(freeMemW, NULL, 10);
}
if (freeIDW != NULL) {
freeID = strtol(freeIDW, NULL, 10);
}
if (sharedMemW != NULL) {
sharedMem = strtol(sharedMemW, NULL, 10);
}
if (isToCreate) {
ret = new Mem(sharedMem, freeMem, freeID);
}
safeDel(&freeMemW);
safeDel(&freeIDW);
safeDel(&sharedMemW);
return ret;
}
SessionID* Parser::getSessionID(const char*content) {
SessionID* ret = NULL;
if (content) {
ret = new SessionID(content);
}
return ret;
}
VerDTD* Parser::getVerDTD(const char*content) {
VerDTD* ret = NULL;
if (content) {
ret = new VerDTD(content);
}
return ret;
}
VerProto* Parser::getVerProto(const char*content) {
VerProto* ret = NULL;
if (content) {
ret = new VerProto(content);
}
return ret;
}
SyncBody* Parser::getSyncBody(const char*xml) {
SyncBody* syncBody = NULL;
BOOL finalMsg = FALSE;
ArrayList* commands;
unsigned int pos = 0;
char* t = NULL;
commands = getCommands(xml);
t = XMLProcessor::copyElementContent(xml, FINAL_MSG, NULL);
finalMsg = getFinalMsg(t);
if (t) {delete [] t; t = NULL;}
syncBody = new SyncBody(commands, finalMsg);
deleteArrayList(&commands);
return syncBody;
}
/*
* The sequence tag can contains the common commands (Add, Replace, Delete, Copy) and
* Alert
* Exec
* Get
* Map
*
* Atomic
* Sync
*/
Sequence* Parser::getSequence(const char*xml) {
Sequence* ret = NULL;
Meta* meta = NULL;
BOOL noResp = NULL;
CmdID* cmdID = NULL;
ArrayList* commands = new ArrayList();
Sync* sync = NULL;
Atomic* atomic = NULL;
Alert* alert = NULL;
Map* map = NULL;
Get* get = NULL;
Exec* exec = NULL;
ArrayList* list = new ArrayList();
unsigned int pos = 0, previous = 0;
char* t = NULL;
t = XMLProcessor::copyElementContent(xml, CMD_ID, NULL);
cmdID = getCmdID(t);
if (t) {delete [] t; t = NULL;}
t = XMLProcessor::copyElementContentLevel (xml, META , NULL);
meta = getMeta(t);
if (t) {delete [] t; t = NULL;}
t = XMLProcessor::copyElementContent (xml, NO_RESP, NULL);
noResp = getNoResp(t);
if (t) {delete [] t; t = NULL;}
// list of commands that must not be leaf of Sync and Atomic
commands = getCommonCommandList(xml, "Atomic&Sync");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -