?? xmlprocessor.cpp
字號(hào):
/*
* 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 <stdlib.h>
#include "base/util/utils.h"
#include "base/util/XMLProcessor.h"
#include "base/util/StringBuffer.h"
#include "base/Log.h"
//--------------------------------------------------------------- Static functions
static const char *findElementContent(const char *xml,
const char *openTag, const char *closeTag,
unsigned int* pos ,
unsigned int* startPos,
unsigned int* endPos )
{
const char *p1, *p2, *xmlptr = xml;
if (pos) {
*pos = 0;
}
do {
p1 = strstr(xmlptr, openTag);
p2 = NULL;
if (!p1) {
// Tag not found
//LOG.debug("XMLProcessor: tag %s not found", openTag);
return 0;
}
p1 += strlen(openTag); // move to end of tag
// Check the tag type
switch( *p1 ){
case ' ': // <tag attr=xxx>
// Find the end of the tag (TODO: should check for invalid chars?)
for (p1++; *p1 != '>'; p1++) {
if(*p1 == 0 || *p1 == '<'){
LOG.info("XMLProcessor: incomplete tag");
return 0;
}
}
if(*(p1-1) == '/'){ // <tag attr=xxx />
// Like case '/' below:
p1++;
p2=p1;
closeTag=0;
break;
}
// The break is not missing!
// After the for, we are in the same case of the tag
// without attributes
case '>': // <tag>
p1++; // this is the beginning of content
if (!p1[0]) {
LOG.info("XMLProcessor: tag at end of file");
return 0;
}
// Find the closing tag
p2 = strstr(p1, closeTag);
break;
case '/':
p1++;
if(*p1 != '>'){
LOG.info("XMLProcessor: invalid empty tag");
return 0;
}
p1++;
// The tag is already closed, no content: make end = start
p2=p1;
// Invalidate closeTag
closeTag=0;
break;
case '\n':
p1++;
p2 = strstr(p1, closeTag);
break;
default:
// This is not the searched tag, search again
//LOG.debug("XMLProcessor: this is not tag %s", openTag);
xmlptr = p1;
p1 = 0;
}
}
while (!p1); // If p1 is null and we are here, it means that the 'default'
// case was hit.
// Closing tag not found
if (!p2) {
//
// This is abc<tag>xyz\0
//
p1 = NULL;
return 0;
}
// Okay, if we are here, the tag content has been found
if (startPos) {
*startPos = p1 - xml;
}
if (endPos) {
*endPos = p2 - xml ;
}
if (pos) {
*pos = p2-xml;
if (closeTag){
*pos += strlen(closeTag);
}
}
return p1;
}
const char* XMLProcessor::getElementContent(const char* xml,
const char* tag,
unsigned int* pos,
unsigned int* startPos,
unsigned int* endPos )
{
char *openTag = 0;
char *closeTag = 0;
if (!xml) {
return 0;
}
size_t l = strlen(tag);
if(strcmp(tag, "CDATA") == 0) {
openTag = stringdup("<![CDATA[");
closeTag = stringdup("]]>");
}
else {
openTag = new char[l+10];
closeTag = new char[l+10];
sprintf(openTag, "<%s", tag);
sprintf(closeTag, "</%s>", tag);
}
const char *ret = findElementContent(xml, openTag, closeTag, pos, startPos, endPos);
if (openTag)
delete [] openTag;
if (closeTag)
delete [] closeTag;
return ret;
}
char* XMLProcessor::copyContent(const char* xml,
unsigned int startPos,
unsigned int endPos ) {
char * ret = NULL;
if (!xml) {
return 0;
}
if (endPos < startPos) {
return 0;
}
if (strlen(xml) < endPos - startPos) {
return 0;
}
// figure out whether the text that we are about to copy
// contains further elements; if not, treat it as a leaf
// element and decode entities
BOOL isLeaf = TRUE;
unsigned int pos = startPos;
while (pos < endPos) {
if (xml[pos] == '<') {
isLeaf = FALSE;
break;
}
pos++;
}
const char cdataStart[] = "<![CDATA[";
const int cdataStartLen = sizeof(cdataStart) - 1;
const char cdataEnd[] = "]]>";
const int cdataEndLen = sizeof(cdataEnd) - 1;
// strip CDATA markers at start and end?
if (!isLeaf &&
endPos - pos > cdataStartLen + cdataEndLen &&
!strncmp(xml + pos, cdataStart, cdataStartLen)) {
// yep, copy content verbatim;
// search real end of data first
pos += cdataStartLen;
unsigned int cdataEndPos = endPos;
while (cdataEndPos - cdataEndLen > pos) {
if (!strncmp(xml + cdataEndPos - cdataEndLen,
cdataEnd,
cdataEndLen)) {
// found "]]>"
cdataEndPos -= cdataEndLen;
break;
}
cdataEndPos--;
}
ret = new char[cdataEndPos - pos + 1];
strncpy(ret, xml + pos, cdataEndPos - pos);
ret[cdataEndPos - pos] = 0;
} else if (isLeaf) {
// Decode content of final element:
// might contain escaped special characters.
//
// This must _not_ be done for tags which contain other
// tags because then we might destroy the content of e.g.
// <Add><Data><![CDATA[ literal entity & ]]></Data></Add>
//
StringBuffer tmp(xml+startPos, endPos - startPos);
tmp.replaceAll("&", "&");
tmp.replaceAll("<", "<");
ret = stringdup(tmp.c_str());
} else {
size_t len = endPos - startPos;
ret = new char [len + 1];
memcpy( ret, xml + startPos, len * sizeof(char));
ret[len] = 0;
}
return ret;
}
char* XMLProcessor::copyElementContent(const char* xml,
const char* tag,
unsigned int* pos)
{
unsigned int start, end;
if( getElementContent (xml, tag, pos, &start, &end) ) {
return copyContent(xml, start, end);
}
return 0;
}
/*
* It returns the number of the tag in the xml string
*/
int XMLProcessor::countElementTag(const char* xml, const char* tag) {
unsigned int count = 0, pos = 0, previous = 0;
while (getElementContent(&xml[pos], tag , &pos, NULL, NULL) != NULL) {
pos += previous;
previous = pos;
count ++;
}
return count;
}
/*
* Returns the next tag found in the xml string. It looks at the < and > tags to retrieve
* the name of the token.
* If <tag xmlns...> it returns "tag"
* The "pos" argument will contain the position of the close <tag/>
* The return value is a new char* and must be fred by the caller. If no tag is found, NULL is returned
*/
const char* XMLProcessor::getNextTag(const char*xml, int* pos) {
const char* p1, *p2, *p4, *p3 = NULL;
char* ret = NULL;
p1 = p2 = p4 = xml;
int i = 0, k = 0, len = 0;
BOOL found = FALSE;
len = strlen(xml);
for (i = 0; i < len; i++) {
if (found) {
if (p4[i] != '/' && p4[i] != '!' && p4[i] != '-' ) {
break; // the element found is right!
} else {
found = FALSE;
}
}
if (p4[i] == '<') {
p1 = &p4[i];
found = TRUE;
}
}
if (found) {
p2 = p1;
for (k = 0; k < len; k++) {
if (*p1 == 0) {
break;
}
else if (*p1 == ' ') {
p3 = p1;
}
else if (*p1 == '>') {
*pos = p1 -xml + 1;
if (p3) {
p1 = p3;
}
ret = new char[(p1)-(p2)];
strncpy(ret, p2+1, (p1)-(p2+1));
ret[(p1)-(p2+1)] = 0;
return ret;
break;
}
p1 = p1 + 1;
}
}
return ret;
}
/*
* count the number of "&" (passed as a string) in the token.
*/
int XMLProcessor::countAnd(const char* token) {
return countChar(token, "&");
}
int XMLProcessor::countChar(const char* token, const char* element) {
const char* p1, *p2;
p1 = p2 = token;
int i = 0, k = 0, len = 0;
while (strstr(p1, element) != NULL) {
len = strlen(p2);
for (k = 0; k < len; k++) {
if (*p1 == 0) {
break;
}
else if (*p1 == '&') {
p1 = p1 + 1;
i++;
break;
}
p1 = p1 + 1;
}
}
return i;
}
/*
* it's as getElementContent but it doesn't get the content of a tag if the parent match except.
* The parent can be more than one. They have to be separated by &
* i.e.
*
* getElementContentExcept(xmlPtr, "Add", "Sync&Atomic", &post)
*
* The function returns "... to keep ... " content only
*
* <SyncBody>
<Sync>
<Add>... to avoid ...</Add>
</Sync>
<Add>... to keep ...</Add>
<Sync>
<Add>... to avoid ...</Add>
</Sync>
<Atomic>
<Add>... to avoid ...</Add>
</Atomic>
</SyncBody>
*/
char* XMLProcessor::copyElementContentExcept(const char*xmlPtr ,
const char*tag ,
const char*except ,
unsigned int* post) {
char* ret = NULL;
const char* found = NULL;
const char* xml = NULL;
char** array = NULL;
int* validElement = NULL;
int count = 0, countTag = 0;
BOOL notValid = FALSE;
unsigned int pos = 0, previous = 0,
position = 0, previousPosition = 0,
startPos = 0, endPos = 0;
xml = xmlPtr;
if (xml == NULL) {
return NULL;
}
if (except == NULL) {
ret = copyElementContent(xml, tag, &pos);
if (post) {
*post = pos;
}
return ret;
}
count = countAnd(except);
count++;
array = new char*[count + 1];
int l;
for (l = 0; l <= count; l++) {
array[l] = NULL;
}
// represent a element found that can be used properly
countTag = countElementTag(xml, tag);
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -