⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xmlprocessor.cpp

📁 funambol windows mobile plugin source code, the source code is taken from the funambol site
💻 CPP
📖 第 1 页 / 共 2 页
字号:
    if (countTag > 0) {
        validElement = new int[countTag];
        for (l = 0; l < countTag; l++) {
            validElement[l] = 1;
        }
    }

    char* internal = stringdup(except);
    char* p1, *p2;
    p1 = p2 = internal;
    int i = 0, k = 0, len = 0;

    while (strstr(p2, "&") != NULL) {
        len = strlen(p2);
        for (k = 0; k < len; k++) {
            if (*p1 == 0) {
                break;
            }
            else if (*p1 == '&') {
                *p1 = 0;
                array[i] = stringdup(p2);
                p1 = p1 + 1;
                p2 = p1;
                i++;
                break;
            }
            p1 = p1 + 1;
        }
    }

    if (i == 0 || k < len) {
        if (array[i]) { delete [] array[i]; array[i] = NULL; }
        array[i] = stringdup(p2);
        i++;
    }

    for (int s = 0; s < count; s ++) {
        i = 0;

        do {
            safeDel(&ret);
            k = 0;

            pos = 0, previous = 0;
            while ((ret = copyElementContent(&xml[pos], tag, &pos)) != NULL) {

                if (validElement && validElement[k] == 1) {
                    pos += previous;
                    position = 0;
                    previousPosition = 0;
                    startPos = 0;
                    endPos   = 0;
                    while ((found = getElementContent(&xml[position], array[i], &position, &startPos, &endPos)) != NULL ) {

                        startPos += previousPosition;
                        endPos   += previousPosition;
                        position += previousPosition;
                        if (startPos < pos && pos < endPos) {
                            notValid = TRUE;
                            break;
                        }
                        previousPosition = position;

                    }

                    if (notValid) {
                        notValid = FALSE;
                        safeDel(&ret);
                        validElement[k] = 0;
                    } else {
                        if (post) {
                            *post = pos;
                        }
                        break;
                    }
                    previous = pos;
                    k++;
                } else {
                    pos += previous;
                    previous = pos;
                    k++;
                    safeDel(&ret); // if (ret) { delete [] ret; ret = NULL; }
                }
            }
            i++;

        } while(array[i] != NULL);

        if (count > 1) {
            char* tmp = stringdup(array[0]);

            for (int m = 0; m < count - 1; m++) {
                if (array[m]) { delete [] array[m]; array[m] = NULL; }
                //safeDel(&array[m]);
                array[m] = stringdup(array[m+1]);
            }
            //safeDel(&array[count-1]);
            if (array[count-1]) { delete [] array[count-1]; array[count-1] = NULL; }
            array[count-1] = stringdup(tmp);
            safeDel(&tmp);
        }
    }

    if (notValid) {
        if (ret) {
            safeDel(&ret);
        }
        if (post) {
            *post = 0;
        }
    } else {
        ;
    }

    safeDel(&internal);
    if (validElement) {
        delete [] validElement; validElement = NULL;
    }

    for (l = 0; l <= count; l++) {
        if (array[l]) { delete [] array[l]; array[l] = NULL; }
    }
    delete [] array; array = NULL;
    //safeDelete(array);

    return ret;
}

/*
* It returns getElementContent value but it depends on the level that is specified.
* It return only ... to keep ... independently on the tag in which it is contained if lev is zero.
*
*   <Sync>
*     <Add>... to avoid ...</Add>
*   </Sync>
*   <Add>... to keep ...</Add>
*   <Sync>
*     <Add>... to avoid ...</Add>
*   </Sync>
*    <Atomic>
*     <Add>... to avoid ...</Add>
*   </Atomic>
*
* The parameters are:
* xml: the xml to analize
* tag: the name of the tag to use
* lev: the inner level in which discover the tag
* startLevel: the starting level from which start the search
* pos: the position index search end
*
* The follow piece of code explain how get the value tag "... to keep ..." that is in the root (0) level.
* Note the startLevel declaration and initialization to -1 value
*
*
*    char* p = NULL;
*    unsigned int pos = 0, previous = 0;
*    int startLevel = -1;
*    while ((p = XMLProcessor::getElementContentLevel(&xml[pos], "Add", 0, &startLevel, &pos)) != NULL) {
*        pos += previous;
*        previous = pos;
*    }
*
*/

char* XMLProcessor::copyElementContentLevel(const char*xml   ,
                                            const char*tag   ,
                                            unsigned int* pos,
                                            int           lev ,
                                            int*          startLevel)  {

    const char* p1 = NULL;
    const char* p2 = NULL;
    char* ret = NULL;
    BOOL openBracket  = FALSE;  // <
    BOOL closeBracket = FALSE;  // >
    BOOL aloneBracket = FALSE;  // </
    BOOL preCloseBracket = FALSE;  //<.../
    BOOL openTag      = FALSE;
    BOOL closeTag     = FALSE;

    char tagNameFound[40];

    int level               = -1;
    unsigned int xmlLength  = (unsigned int)-1;
    unsigned int l          = (unsigned int)-1;
    unsigned int previousIndex = (unsigned int)-1;
    unsigned int i          =  0;

    if (xml == NULL) {
        goto finally;
    }

    if (lev < 0) {
        return copyElementContent(xml, tag, pos);
    }

    xmlLength = strlen(xml);
    l = strlen(tag);

    if (pos != NULL) {
        *pos = 0;
    }
    if (startLevel != NULL) {
       level = *startLevel;
    }

    p1 = p2 = xml;

    for (i = 0; i < xmlLength; i ++) {
        if (!strncmp(p1 + i, "<![CDATA[", strlen("<![CDATA["))) {
            // skip over content
            while(p1[i]) {
                i++;
                if (!strcmp(p1 + i, "]]>")) {
                    i += strlen("]]>");
                    break;
                }
            }
        }
        if (p1[i] == '<') {
            openBracket = TRUE;
            previousIndex = i;
            p2 = &p1[i];

        } else if (p1[i] == '/') {
            if (previousIndex == (i - 1)) {
                // </...>
                preCloseBracket = TRUE;
            } else {
                // might be <.../>, which will be checked below
                // with p1[i - 1] == '/'
            }

        } else if (p1[i] == '>') {

            if (openBracket == FALSE) {
                closeBracket = FALSE;
                preCloseBracket = FALSE;
            } else {
                if (preCloseBracket) {
                    closeTag = TRUE;
                }
                else if (openBracket && p1[i - 1] == '/') {
                    // <.../>: do not change levels or open tag,
                    // it has been closed already
                } else {
                    openTag = TRUE;
                }
                closeBracket = TRUE;

                if (closeTag) {
                    level--;
                    openBracket  = FALSE;
                    closeBracket = FALSE;
                    preCloseBracket = FALSE;
                    openTag      = FALSE;
                    closeTag     = FALSE;

                } else if (openTag) {
                    level++;
                } else {
                    openBracket  = FALSE;
                    closeBracket = FALSE;
                    preCloseBracket = FALSE;
                    openTag      = FALSE;
                    closeTag     = FALSE;

                }
            }
        }
          if (openTag && openBracket && closeBracket) {
            int n = (&p1[i] - p2 - 1);
            strncpy(tagNameFound, p2 + 1, n);
            tagNameFound[n] = 0;
            if (strcmp(tagNameFound, tag) == 0 && (level == lev)) {
                unsigned int internalPos;
                ret = copyElementContent(p2, tag, &internalPos);
                if (pos) {
                    *pos = p2 - xml + internalPos;
                }
                if (startLevel) {
                    *startLevel = level - 1;
                }
                break;
            }
            openBracket  = FALSE;
            closeBracket = FALSE;
        }
    }

finally:
    openBracket  = FALSE;
    closeBracket = FALSE;
    preCloseBracket = FALSE;
    openTag      = FALSE;
    closeTag     = FALSE;
    return ret;

}

/**
 * Get the attribute list of the forst element 'tag', returning a pointer
 * to the beginning of the string in the original buffer 'xml', and the
 * starting and ending position of the substring.
 *
 * @param xml - the XML document to process.
 * @param tag - the tag name to find
 * @param startPos - return value - the start pos of the attribute list
 * @param endPos - return value - the end position of the attribute list
 */
const char* XMLProcessor::getElementAttributes(const char* xml,
                                          const char* tag,
                                          unsigned int* startPos,
                                          unsigned int* endPos,
                                          bool escaped) {

    const char* p1 = NULL;
    const char* p2 = NULL;
    BOOL charFound  = FALSE;
    unsigned int l = strlen(tag);

    // example ot tag with attribute list
    // <body enc="base64">
    char *openTag = 0; //<tag

    if (!xml) {
        goto finally;
    }

    if(strcmp(tag, "CDATA") == 0) {
        goto finally;
    }
    else {
        openTag = new char[l+10];
        if (escaped){
            sprintf(openTag, "&lt;%s ", tag);
        }
        else{
            sprintf(openTag, "<%s ", tag);
        }
    }

    p1 = strstr(xml, openTag);

    if (!p1) {
        LOG.info("XMLProcessor: tag %s not found", tag);
        goto finally;
    }
    // move to the beginning of the attribute list
    p1 += strlen(openTag);

    // find the end of the tag
    for (p2 = p1; *p2 != '>'; p2++) {
        if(*p2 == 0 || *p2 == '<'){
            LOG.info("XMLProcessor: incomplete tag");
            goto finally;
        }
    }
    // set the return parameters
    if (startPos != NULL) {
        *startPos = p1 - xml;
    }
    if (endPos != NULL) {
        *endPos = p2 - xml ;
    }

    finally:

    if (openTag)
        delete [] openTag;

    return p1;

}


StringBuffer XMLProcessor::makeElement(const char* tag, const char* val, const char* attr)
{
    StringBuffer s;

    if (!val)
        return s;
    if (!val[0])
        return s;

    size_t len = strlen(tag);
    char* t1 = new char[len + 4]; // <  >  0, whitout closing >
    char* t2 = new char[len + 6]; // </ > \n 0

    sprintf(t1, "<%s", tag);
    sprintf(t2, "</%s>\n", tag);

    s = t1;
    if (attr != NULL)
    {
        s += " ";
        s += attr;
    }
    s += ">";
    s += val; s += t2;

    delete [] t1;
    delete [] t2;

    return s;
}


StringBuffer XMLProcessor::makeElement(const char* tag,
                                    const char* val,
                                    ArrayList attrList) {

    StringBuffer s;

    for (int i = 0; i < attrList.size(); i++)
    {
        KeyValuePair* item = (KeyValuePair*)attrList[i];
        if (i > 0)
            s += " ";
        s += item->getKey();
        s += "=\"";
        s += item->getValue();
        s += "\"";
    }
    s = makeElement(tag, val, s.c_str());

    return s;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -