Beispiel #1
0
void ODFManifestRelease(ODFManifest *manifest)
{
    if ((manifest == NULL) || (--manifest->retainCount > 0))
        return;

    DFDocumentRelease(manifest->doc);
    DFHashTableRelease(manifest->entriesByPath);
    free(manifest);
}
Beispiel #2
0
void DFSAXParserFree(DFSAXParser *parser)
{
    DFDocumentRelease(parser->document);
    DFBufferRelease(parser->warnings);
    DFBufferRelease(parser->errors);
    DFBufferRelease(parser->fatalErrors);
    DFMarkupCompatibilityFree(parser->compatibility);
    free(parser);
}
int parseHTMLFile(const char *filename, DFError **error)
{
    DFDocument *doc = DFParseHTMLFile(filename,0,error);
    if (doc == NULL)
        return 0;
    char *result = DFSerializeXMLString(doc,0,0);
    printf("%s",result);
    free(result);
    DFDocumentRelease(doc);
    return 1;
}
Beispiel #4
0
static int saveStrippedXMLText(DFStorage *storage, const char *filename,
                               const char *input, NamespaceID defaultNS, DFError **error)
{
    DFDocument *doc = DFParseXMLString(input,error);
    if (doc == NULL)
        return 0;
    DFStripWhitespace(doc->docNode);
    int ok = saveXMLDocument(storage,filename,doc,defaultNS,error);
    DFDocumentRelease(doc);
    return ok;
}
int diffFiles(const char *filename1, const char *filename2, DFError **error)
{
    DFDocument *doc1 = DFParseHTMLFile(filename1,0,error);
    if (doc1 == NULL) {
        DFErrorFormat(error,"%s: %s",filename1,DFErrorMessage(error));
        return 0;
    }

    DFDocument *doc2 = DFParseHTMLFile(filename1,0,error);
    if (doc2 == NULL) {
        DFErrorFormat(error,"%s: %s",filename2,DFErrorMessage(error));
        DFDocumentRelease(doc1);
        return 0;
    }

    DFComputeChanges(doc1->root,doc2->root,HTML_ID);
    char *changesStr = DFChangesToString(doc1->root);
    printf("%s",changesStr);
    free(changesStr);

    DFDocumentRelease(doc1);
    DFDocumentRelease(doc2);
    return 1;
}
int normalizeFile(const char *filename, DFError **error)
{
    DFDocument *doc = DFParseHTMLFile(filename,0,error);
    if (doc == NULL) {
        DFErrorFormat(error,"%s: %s",filename,DFErrorMessage(error));
        return 0;
    }

    HTML_normalizeDocument(doc);
    HTML_safeIndent(doc->docNode,0);
    char *str = DFSerializeXMLString(doc,0,0);
    printf("%s",str);
    free(str);
    DFDocumentRelease(doc);
    return 1;
}
static int prettyPrintXMLFile(const char *filename, int html, DFError **error)
{
    DFError *err = NULL;
    DFDocument *doc;
    if (html)
        doc = DFParseHTMLFile(filename,0,&err);
    else
        doc = DFParseXMLFile(filename,&err);
    if (doc == NULL)
        return 0;

    char *str = DFSerializeXMLString(doc,0,1);
    printf("%s",str);
    free(str);
    DFDocumentRelease(doc);
    return 1;
}
static void test_create(void)
{
    DFError *error = NULL;
    DFStorage *htmlStorage = DFStorageNewMemory(DFFileFormatHTML);
    DFDocument *htmlDoc = TestCaseGetHTML(htmlStorage,&error);
    DFStorageRelease(htmlStorage);
    if (htmlDoc == NULL) {
        DFBufferFormat(utgetoutput(),"%s\n",DFErrorMessage(&error));
        DFErrorRelease(error);
        return;
    }

    HTML_normalizeDocument(htmlDoc);
    char *latex = HTMLToLaTeX(htmlDoc);
    DFBufferFormat(utgetoutput(),"%s",latex);
    free(latex);
    DFDocumentRelease(htmlDoc);
}
Beispiel #9
0
static char *Word_toPlainFromDir(DFStorage *storage, DFHashTable *parts, DFError **error)
{
    char *documentPath = NULL;
    DFHashTable *rels = DFHashTableNew((DFCopyFunction)xstrdup,(DFFreeFunction)free);
    DFBuffer *output = DFBufferNew();
    char *relsPathRel = NULL;
    DFDocument *relsDoc = NULL;
    int ok = 0;


    documentPath = findDocumentPath(storage,error);
    if (documentPath == NULL) {
        DFErrorFormat(error,"findDocumentPath: %s",DFErrorMessage(error));
        goto end;
    }

    relsPathRel = computeDocumentRelsPath(documentPath);
    if (DFStorageExists(storage,relsPathRel) && ((relsDoc = DFParseXMLStorage(storage,relsPathRel,error)) == NULL)) {
        DFErrorFormat(error,"%s: %s",relsPathRel,DFErrorMessage(error));
        goto end;
    }

    parseDocumentRels(documentPath,relsDoc,rels,error);

    if (!processParts(parts,documentPath,relsDoc,rels,output,storage,error))
        goto end;

    ok = 1;

end:
    free(relsPathRel);
    free(documentPath);
    DFHashTableRelease(rels);
    DFDocumentRelease(relsDoc);
    if (!ok) {
        DFBufferRelease(output);
        return NULL;
    }
    else {
        char *result = xstrdup(output->data);
        DFBufferRelease(output);
        return result;
    }
}
Beispiel #10
0
void WordConverterFree(WordConverter *converter)
{
    DFDocumentRelease(converter->html);
    free(converter->abstractPath);
    free(converter->concretePath);
    free(converter->idPrefix);
    WordSheetFree(converter->styles);
    WordNumberingFree(converter->numbering);
    WordThemeFree(converter->theme);
    WordSectionFree(converter->mainSection);
    WordObjectsFree(converter->objects);
    WordNoteGroupRelease(converter->footnotes);
    WordNoteGroupRelease(converter->endnotes);
    DFHashTableRelease(converter->supportedContentTypes);
    DFBufferRelease(converter->warnings);
    CSSSheetRelease(converter->styleSheet);
    WordPackageRelease(converter->package);
    free(converter);
}
Beispiel #11
0
static int addRelatedDoc(DFHashTable *parts, DFHashTable *documentRels, const char *relName, const char *filename,
                         DFBuffer *output, DFHashTable *includeTypes, DFStorage *storage, DFError **error)
{
    const char *relPath = DFHashTableLookup(documentRels,relName);
    if (relPath == NULL)
        return 1;;

    DFDocument *doc = DFParseXMLStorage(storage,relPath,error);
    if (doc == NULL) {
        DFErrorFormat(error,"%s: %s",relPath,DFErrorMessage(error));
        return 0;
    }

    if (doc->root->first != NULL) {
        addStrippedSerializedDoc(output,doc,filename);
        DFHashTableAdd(includeTypes,relName,"");
    }

    DFDocumentRelease(doc);
    return 1;
}
Beispiel #12
0
static char *findDocumentPath(DFStorage *storage, DFError **error)
{
    int ok = 0;
    DFDocument *relsDoc = NULL;
    char *result = NULL;

    relsDoc = DFParseXMLStorage(storage,"/_rels/.rels",error);
    if (relsDoc == NULL) {
        DFErrorFormat(error,"_rels/.rels: %s",DFErrorMessage(error));
        goto end;
    }

    for (DFNode *child = relsDoc->root->first; child != NULL; child = child->next) {
        if (child->tag != REL_RELATIONSHIP)
            continue;

        const char *type = DFGetAttribute(child,NULL_Type);
        const char *target = DFGetAttribute(child,NULL_TARGET);
        if ((type == NULL) || (target == NULL))
            continue;

        if (strcmp(type,"http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument"))
            continue;

        result = xstrdup(target);
        ok = 1;
        break;
    }

end:
    DFDocumentRelease(relsDoc);
    if (ok)
        return result;
    free(result);
    return NULL;
}
Beispiel #13
0
static int processParts(DFHashTable *parts, const char *documentPath, DFDocument *relsDoc,
                        DFHashTable *documentRels,
                        DFBuffer *output, DFStorage *storage, DFError **error)
{
    int ok = 0;
    DFHashTable *includeTypes = DFHashTableNew((DFCopyFunction)xstrdup,free);
    DFHashTableAdd(includeTypes,WORDREL_HYPERLINK,"");
    DFHashTableAdd(includeTypes,WORDREL_IMAGE,"");

    if ((parts == NULL) || (DFHashTableLookup(parts,"document") != NULL)) {
        DFDocument *doc = DFParseXMLStorage(storage,documentPath,error);
        if (doc == NULL)
            goto end;
        addStrippedSerializedDoc(output,doc,"document.xml");
        DFDocumentRelease(doc);
    }

    if ((parts == NULL) || (DFHashTableLookup(parts,"styles") != NULL)) {
        if (!addRelatedDoc(parts,documentRels,WORDREL_STYLES,"styles.xml",output,includeTypes,storage,error))
            goto end;
    }
    if ((parts == NULL) || (DFHashTableLookup(parts,"numbering") != NULL)) {
        if (!addRelatedDoc(parts,documentRels,WORDREL_NUMBERING,"numbering.xml",output,includeTypes,storage,error))
            goto end;
    }
    if ((parts == NULL) || (DFHashTableLookup(parts,"footnotes") != NULL)) {
        if (!addRelatedDoc(parts,documentRels,WORDREL_FOOTNOTES,"footnotes.xml",output,includeTypes,storage,error))
            goto end;
    }
    if ((parts == NULL) || (DFHashTableLookup(parts,"endnotes") != NULL)) {
        if (!addRelatedDoc(parts,documentRels,WORDREL_ENDNOTES,"endnotes.xml",output,includeTypes,storage,error))
            goto end;
    }
    if ((parts != NULL) && (DFHashTableLookup(parts,"settings") != NULL)) {
        if (!addRelatedDoc(parts,documentRels,WORDREL_SETTINGS,"settings.xml",output,includeTypes,storage,error))
            goto end;
    }
    if ((parts != NULL) && (DFHashTableLookup(parts,"theme") != NULL)) {
        if (!addRelatedDoc(parts,documentRels,WORDREL_THEME,"theme.xml",output,includeTypes,storage,error))
            goto end;
    }

    if ((DFHashTableLookup(documentRels,WORDREL_HYPERLINK) != NULL) ||
        (DFHashTableLookup(documentRels,WORDREL_IMAGE) != NULL) ||
        ((parts != NULL) && (DFHashTableLookup(parts,"documentRels") != NULL))) {
        if (relsDoc == NULL) {
            DFErrorFormat(error,"document.xml.rels does not exist");
            goto end;
        }
        DFNode *next;
        for (DFNode *child = relsDoc->root->first; child != NULL; child = next) {
            next = child->next;
            if (child->tag != REL_RELATIONSHIP)
                continue;
            const char *type = DFGetAttribute(child,NULL_Type);
            if ((type != NULL) && (DFHashTableLookup(includeTypes,type) == NULL)) {
                DFRemoveNode(child);
            }
        }
        addSerializedDoc(output,relsDoc,"document.xml.rels");
    }

    const char **entries = DFStorageList(storage,NULL);
    if (entries != NULL) { // FIXME: Should really report an error if this is not the case
        for (int i = 0; entries[i]; i++) {
            const char *filename = entries[i];
            char *extension = DFPathExtension(filename);
            if (DFStringEqualsCI(extension,"png") || DFStringEqualsCI(extension,"jpg")) {
                char *absFilename;
                if (!DFStringHasSuffix(filename,"/"))
                    absFilename = DFFormatString("/%s",filename);
                else
                    absFilename = xstrdup(filename);
                DFBuffer *data = DFBufferReadFromStorage(storage,absFilename,NULL);
                addSerializedBinary(output,data,absFilename);
                DFBufferRelease(data);
                free(absFilename);
            }
            free(extension);
        }
    }
    free(entries);
    DFHashTableRelease(includeTypes);

    ok = 1;

end:
    return ok;
}