Esempio n. 1
0
static OPCRelationship *addImageRelationship(WordConverter *converter, const char *src, DFError **error)
{
    const char *mediaDir = DFAppendPathComponent(converter->concretePath,"word/media");

    if (!DFFileExists(mediaDir) && !DFCreateDirectory(mediaDir,1,error))
        return NULL;

    char *ext = DFPathExtension(src);
    char *filename = genImageFilename(mediaDir,ext,error);
    free(ext);
    if (filename == NULL)
        return NULL;

    char *abstractPathSlash = DFFormatString("%s/",converter->abstractPath);
    char *unescapedSrc = DFRemovePercentEncoding(src);
    char *srcPath = DFPathResolveAbsolute(abstractPathSlash,unescapedSrc);
    char *destPath = DFAppendPathComponent(mediaDir,filename);

    OPCRelationship *result = NULL;
    if (DFCopyFile(srcPath,destPath,error)) {
        OPCRelationshipSet *rels = converter->package->documentPart->relationships;
        char *relPath = DFFormatString("/word/media/%s",filename);
        result = OPCRelationshipSetAddType(rels,WORDREL_IMAGE,relPath,0);
        free(relPath);
    }

    free(filename);
    free(abstractPathSlash);
    free(unescapedSrc);
    free(srcPath);
    free(destPath);
    return result;
}
Esempio n. 2
0
static int fromPlain2(const char *inStr, const char *inPath, const char *outFilename, DFError **error)
{
    char *outExtension = DFPathExtension(outFilename);
    int isDocx = DFStringEqualsCI(outExtension,"docx");
    int ok = 0;

    if (!isDocx) {
        DFErrorFormat(error,"%s: Unknown extension",outFilename);
        goto end;
    }

    DFStorage *storage = NULL;

    storage = Word_fromPlain(inStr,inPath,error);
    if (storage == NULL)
        goto end;

    ok = DFZip(outFilename,storage,error);
    DFStorageRelease(storage);

    return ok;
end:
    free(outExtension);
    return ok;
}
Esempio n. 3
0
static int checkContentType(WordConverter *converter, const char *htmlSrc)
{
    char *extension = DFPathExtension(htmlSrc);
    const char *contentType = DFHashTableLookup(converter->supportedContentTypes,extension);
    if (contentType == NULL) {
        WordConverterWarning(converter,"Unsupported image type: %s",extension);
        free(extension);
        return 0;
    }
    else {
        OPCContentTypesSetDefault(converter->package->opc->contentTypes,extension,contentType);
        free(extension);
        return 1;
    }
}
Esempio n. 4
0
int prettyPrintFile(const char *filename, DFError **error)
{
    int ok;
    char *extension = DFPathExtension(filename);
    if (DFStringEqualsCI(extension,"xml"))
        ok = prettyPrintXMLFile(filename,0,error);
    else if (DFStringEqualsCI(extension,"html") || DFStringEqualsCI(extension,"htm"))
        ok = prettyPrintXMLFile(filename,1,error);
    else if (DFStringEqualsCI(extension,"docx"))
        ok = prettyPrintWordFile(filename,error);
    else if (DFStringEqualsCI(extension,"odt"))
        ok = prettyPrintODFFile(filename,error);
    else {
        DFErrorFormat(error,"Unknown file type");
        ok = 0;
    }
    free(extension);
    return ok;
}
Esempio n. 5
0
DFNode *WordDrawingGet(WordGetData *get, DFNode *concrete)
{
    ImageInfo *info = getImageInfo(concrete);
    if (info == NULL)
        return createAbstractPlaceholder(get,"[Unsupported object]",concrete);

    int validFileType = 0;

    const char *filename = WordPackageTargetForDocumentRel(get->conv->package,info->rId);
    if (filename != NULL) {
        char *origExtension = DFPathExtension(filename);
        char *lowerExtension = DFLowerCase(origExtension);
        if (DFHashTableLookup(get->conv->supportedContentTypes,lowerExtension) != NULL)
            validFileType = 1;
        free(origExtension);
        free(lowerExtension);
    }

    DFNode *result = NULL;
    if (!validFileType) {
        if (info->progId != NULL) {
            char *message = DFFormatString("[Unsupported object: %s]",info->progId);
            DFNode *placeholder = createAbstractPlaceholder(get,message,concrete);
            free(message);
            result = placeholder;
        }
        else {
            result = createAbstractPlaceholder(get,"[Unsupported object]",concrete);
        }

    }

    if (result == NULL)
        result = imageWithFilename(get,filename,info->widthPts,concrete);

    ImageInfoFree(info);
    return result;
}
Esempio n. 6
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;
}