Пример #1
0
static void writeElement(Serialization *serialization, DFNode *element, int depth)
{
    const TagDecl *tagDecl = DFNameMapNameForTag(serialization->doc->map,element->tag);
    assert(tagDecl != NULL);
    const NamespaceDecl *nsDecl = DFNameMapNamespaceForID(serialization->doc->map,tagDecl->namespaceID);
    assert(nsDecl != NULL);

    const xmlChar *prefix = (const xmlChar *)nsDecl->prefix;
    const xmlChar *localName = (const xmlChar *)tagDecl->localName;

    if (serialization->indent && (element->parent != element->doc->docNode))
        xmlTextWriterWriteRawLen(serialization->writer,INDENT,1+depth);

    if (serialization->html || (tagDecl->namespaceID == serialization->defaultNS))
        xmlTextWriterStartElement(serialization->writer,localName);
    else
        xmlTextWriterStartElementNS(serialization->writer,prefix,localName,NULL);

    if ((element->parent == serialization->doc->docNode) && !serialization->html)
        writeNamespaceDeclarations(serialization,element);

    writeAttributes(serialization,element);

    // Check if all children are text nodes. If this is true; we should treat them as if they are a single text
    // node, and not do any indentation.
    int allChildrenText = 1;
    for (DFNode *child = element->first; child != NULL; child = child->next) {
        if (child->tag != DOM_TEXT)
            allChildrenText = 0;
    }

    if (allChildrenText) {
        int oldIndent = serialization->indent;
        serialization->indent = 0;
        for (DFNode *child = element->first; child != NULL; child = child->next)
            writeNode(serialization,child,depth+2);
        serialization->indent = oldIndent;
    }
    else {
        for (DFNode *child = element->first; child != NULL; child = child->next)
            writeNode(serialization,child,depth+2);
    }

    if (serialization->indent && (element->first != NULL) && !allChildrenText) {
        if ((element->first != element->last) ||
            (element->first->tag != DOM_TEXT))
        xmlTextWriterWriteRawLen(serialization->writer,INDENT,1+depth);
    }

    if (serialization->html && (element->first == NULL) && HTML_requiresCloseTag(element->tag)) {
        xmlTextWriterWriteString(serialization->writer,(xmlChar *)"");
    }

    xmlTextWriterEndElement(serialization->writer);
}
static bool saveArchiveResourceField(xmlTextWriterPtr writer, const xmlChar* tag, const char* data, int size)
{
    int result = xmlTextWriterStartElement(writer, tag);
    if (result < 0) {
        LOGD("saveArchiveResourceField: Failed to start element.");
        return false;
    }

    if (size > 0) {
        Vector<char> base64Data;
        base64Encode(data, size, base64Data, false);
        if (base64Data.isEmpty()) {
            LOGD("saveArchiveResourceField: Failed to base64 encode data.");
            return false;
        }

        result = xmlTextWriterWriteRawLen(writer, BAD_CAST base64Data.data(), base64Data.size());
        if (result < 0) {
            LOGD("saveArchiveResourceField: Failed to write data.");
            return false;
        }
    }

    result = xmlTextWriterEndElement(writer);
    if (result < 0) {
        LOGD("saveArchiveResourceField: Failed to end element.");
        return false;
    }

    return true;
}
Пример #3
0
static void writeNode(Serialization *serialization, DFNode *node, int depth)
{
    switch (node->tag) {
        case DOM_DOCUMENT: {
            if (!serialization->html)
                xmlTextWriterStartDocument(serialization->writer,"1.0","UTF-8","yes");
            if (serialization->html)
                xmlTextWriterWriteDTD(serialization->writer,(xmlChar *)"html",NULL,NULL,NULL);
//            xmlTextWriterWriteDTD(writer,
//                                  (xmlChar *)"html",
//                                  (xmlChar *)"-//W3C//DTD XHTML 1.0 Strict//EN",
//                                  (xmlChar *)"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd",
//                                  NULL);
            for (DFNode *child = node->first; child != NULL; child = child->next)
                writeNode(serialization,child,0);
            xmlTextWriterEndDocument(serialization->writer);
            break;
        }
        case DOM_TEXT: {
            if (serialization->indent && ((node->prev != NULL) || (node->next != NULL)))
                xmlTextWriterWriteRawLen(serialization->writer,INDENT,1+depth);
            if (serialization->html && (node->parent != NULL) && (node->parent->tag == HTML_STYLE)) {
                xmlTextWriterWriteRaw(serialization->writer,(const xmlChar *)node->value);
            }
            else {
                xmlTextWriterWriteString(serialization->writer,(const xmlChar *)node->value);
            }
            break;
        }
        case DOM_COMMENT: {
            xmlTextWriterWriteComment(serialization->writer,(const xmlChar *)node->value);
            break;
        }
        case DOM_CDATA: {
            xmlTextWriterWriteCDATA(serialization->writer,(const xmlChar *)node->value);
            break;
        }
        case DOM_PROCESSING_INSTRUCTION: {
            xmlTextWriterWritePI(serialization->writer,
                                 (const xmlChar *)node->target,
                                 (const xmlChar *)node->value);
            break;
        }
        default: {
            if (node->parent == serialization->doc->docNode)
                writeElement(serialization,node,0);
            else
                writeElement(serialization,node,depth);
            break;
        }
    }
}
Пример #4
0
static void writeElement(Serialization *serialization, DFNode *element, int depth)
{
    const TagDecl *tagDecl = DFNameMapNameForTag(serialization->doc->map,element->tag);
    assert(tagDecl != NULL);
    const NamespaceDecl *nsDecl = DFNameMapNamespaceForID(serialization->doc->map,tagDecl->namespaceID);
    assert(nsDecl != NULL);

    const xmlChar *prefix = (const xmlChar *)nsDecl->prefix;
    const xmlChar *localName = (const xmlChar *)tagDecl->localName;

    if (serialization->indent && (element->parent != element->doc->docNode))
        xmlTextWriterWriteRawLen(serialization->writer,INDENT,1+depth);

    if (serialization->html || (tagDecl->namespaceID == serialization->defaultNS))
        xmlTextWriterStartElement(serialization->writer,localName);
    else
        xmlTextWriterStartElementNS(serialization->writer,prefix,localName,NULL);

    if ((element->parent == serialization->doc->docNode) && !serialization->html)
        writeNamespaceDeclarations(serialization,element);

    writeAttributes(serialization,element);

    for (DFNode *child = element->first; child != NULL; child = child->next)
        writeNode(serialization,child,depth+2);

    if (serialization->indent && (element->first != NULL)) {
        if ((element->first != element->last) ||
            (element->first->tag != DOM_TEXT))
        xmlTextWriterWriteRawLen(serialization->writer,INDENT,1+depth);
    }

    if (serialization->html && (element->first == NULL) && HTML_requiresCloseTag(element->tag)) {
        xmlTextWriterWriteString(serialization->writer,(xmlChar *)"");
    }

    xmlTextWriterEndElement(serialization->writer);
}
Пример #5
0
static void my_characters(void* user_data, const xmlChar* ch, int len) {
    MwsHarvest_SaxUserData* data = (MwsHarvest_SaxUserData*)user_data;

    if (data->state == MWSHARVESTSTATE_IN_MWS_EXPR &&  // Valid state
        data->currentToken != nullptr) {               // Valid token
        data->currentToken->appendTextContent((char*)ch, len);
    }

    if (data->state == MWSHARVESTSTATE_IN_MWS_DATA) {
        string inputChars((const char*)ch, len);
        xmlChar* encodedChars =
            xmlEncodeSpecialChars(nullptr, BAD_CAST inputChars.c_str());
        assert(encodedChars != nullptr);
        xmlTextWriterWriteRawLen(data->stringWriter, encodedChars,
                                 strlen((const char*)encodedChars));
        xmlFree(encodedChars);
    }
}