corinthia-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From pmke...@apache.org
Subject [57/92] [abbrv] incubator-corinthia git commit: WordPackage: Don't explicitly zip/unzip file
Date Wed, 17 Dec 2014 13:29:07 GMT
WordPackage: Don't explicitly zip/unzip file

In WordPackage and OPCPackage, assume that the contents of the DFPackage
object can be loaded and save directly, without the need to first unzip
or zip the data from a file. This allows us to work with packages
resresented solely in memory, e.g. in the test cases. If the data is to
actually be stored in a physical .docx file (which is just a zip file),
then the code that uses WordPackage must manage this.

This is a temporary measure until we have a DFPackage implementation
that can work directly with zip files.


Project: http://git-wip-us.apache.org/repos/asf/incubator-corinthia/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-corinthia/commit/ad3bfd8b
Tree: http://git-wip-us.apache.org/repos/asf/incubator-corinthia/tree/ad3bfd8b
Diff: http://git-wip-us.apache.org/repos/asf/incubator-corinthia/diff/ad3bfd8b

Branch: refs/heads/stable
Commit: ad3bfd8b9743c10295dcee9339af09ff83b16b2b
Parents: 84793fa
Author: Peter Kelly <peter@uxproductivity.com>
Authored: Mon Dec 1 22:48:28 2014 +0700
Committer: Peter Kelly <peter@uxproductivity.com>
Committed: Mon Dec 1 22:48:28 2014 +0700

----------------------------------------------------------------------
 DocFormats/api/src/Operations.c                 | 13 +++++++---
 DocFormats/filters/ooxml/src/common/OPC.c       | 17 +++----------
 DocFormats/filters/ooxml/src/common/OPC.h       |  4 +--
 DocFormats/filters/ooxml/src/word/Word.c        |  5 +++-
 DocFormats/filters/ooxml/src/word/WordPackage.c | 10 +++-----
 DocFormats/filters/ooxml/src/word/WordPackage.h |  4 +--
 consumers/dfutil/src/Commands.c                 | 26 ++++++++++++++------
 consumers/dfutil/src/Plain.c                    | 24 ++++++++----------
 consumers/dfutil/src/Plain.h                    |  2 +-
 consumers/dfutil/src/TestFunctions.c            | 21 +++++-----------
 10 files changed, 61 insertions(+), 65 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-corinthia/blob/ad3bfd8b/DocFormats/api/src/Operations.c
----------------------------------------------------------------------
diff --git a/DocFormats/api/src/Operations.c b/DocFormats/api/src/Operations.c
index 2bbbcfc..d6c51e8 100644
--- a/DocFormats/api/src/Operations.c
+++ b/DocFormats/api/src/Operations.c
@@ -20,6 +20,7 @@
 #include "DFHTML.h"
 #include "DFDOM.h"
 #include "DFXML.h"
+#include "DFZipFile.h"
 #include <stdlib.h>
 
 static int generateHTML(const char *packageFilename, const char *htmlFilename, DFError **error)
@@ -31,7 +32,9 @@ static int generateHTML(const char *packageFilename, const char *htmlFilename,
D
     DFBuffer *warnings = DFBufferNew();
     DFDocument *htmlDoc = NULL;
 
-    wordPackage = WordPackageOpenFrom(rawPackage,packageFilename,error);
+    if (!DFUnzip(packageFilename,rawPackage,error))
+        goto end;
+    wordPackage = WordPackageOpenFrom(rawPackage,error);
     if (wordPackage == NULL)
         goto end;
 
@@ -91,7 +94,9 @@ static int updateFrom(const char *packageFilename, const char *htmlFilename,
DFE
         HTMLBreakBDTRefs(htmlDoc->docNode,idPrefix);
     }
     else {
-        wordPackage = WordPackageOpenFrom(rawPackage,packageFilename,error);
+        if (!DFUnzip(packageFilename,rawPackage,error))
+            goto end;
+        wordPackage = WordPackageOpenFrom(rawPackage,error);
         if (wordPackage == NULL)
             goto end;
     }
@@ -104,7 +109,9 @@ static int updateFrom(const char *packageFilename, const char *htmlFilename,
DFE
         goto end;
     }
 
-    if (!WordPackageSaveTo(wordPackage,packageFilename,error))
+    if (!WordPackageSave(wordPackage,error))
+        goto end;
+    if (!DFZip(packageFilename,rawPackage,error))
         goto end;
 
     ok = 1;

http://git-wip-us.apache.org/repos/asf/incubator-corinthia/blob/ad3bfd8b/DocFormats/filters/ooxml/src/common/OPC.c
----------------------------------------------------------------------
diff --git a/DocFormats/filters/ooxml/src/common/OPC.c b/DocFormats/filters/ooxml/src/common/OPC.c
index b6da88b..94f7a27 100644
--- a/DocFormats/filters/ooxml/src/common/OPC.c
+++ b/DocFormats/filters/ooxml/src/common/OPC.c
@@ -521,14 +521,11 @@ OPCPackage *OPCPackageOpenNew(DFPackage *store, DFError **error)
     return OPCPackageNew(store);
 }
 
-OPCPackage *OPCPackageOpenFrom(DFPackage *store, const char *filename, DFError **error)
+OPCPackage *OPCPackageOpenFrom(DFPackage *store, DFError **error)
 {
     int ok = 0;
     OPCPackage *pkg = OPCPackageNew(store);
 
-    if (!DFUnzip(filename,pkg->store,error))
-        goto end;
-
     if (!OPCContentTypesLoadFromFile(pkg->contentTypes,pkg->store,"[Content_Types].xml",error))
         goto end;
 
@@ -579,19 +576,11 @@ int OPCPackageSaveToDir(OPCPackage *pkg)
     return (pkg->errors->len == 0);
 }
 
-int OPCPackageSaveTo(OPCPackage *pkg, const char *filename)
+int OPCPackageSave(OPCPackage *pkg, DFError **error)
 {
     if (!OPCPackageSaveToDir(pkg))
-        return 0;;
-    DFError *dferror = NULL;
-    // Build zip file
-    if (!DFZip(filename,pkg->store,&dferror)) {
-        OPCPackageError(pkg,"%s",DFErrorMessage(&dferror));
-        DFErrorRelease(dferror);
         return 0;
-    }
-
-    return 1;
+    return DFPackageSave(pkg->store,error);
 }
 
 OPCPart *OPCPackagePartWithURI(OPCPackage *pkg, const char *URI)

http://git-wip-us.apache.org/repos/asf/incubator-corinthia/blob/ad3bfd8b/DocFormats/filters/ooxml/src/common/OPC.h
----------------------------------------------------------------------
diff --git a/DocFormats/filters/ooxml/src/common/OPC.h b/DocFormats/filters/ooxml/src/common/OPC.h
index 2cb567d..883e174 100644
--- a/DocFormats/filters/ooxml/src/common/OPC.h
+++ b/DocFormats/filters/ooxml/src/common/OPC.h
@@ -108,10 +108,10 @@ struct OPCPackage {
 };
 
 OPCPackage *OPCPackageOpenNew(DFPackage *store, DFError **error);
-OPCPackage *OPCPackageOpenFrom(DFPackage *store, const char *filename, DFError **error);
+OPCPackage *OPCPackageOpenFrom(DFPackage *store, DFError **error);
 void OPCPackageFree(OPCPackage *pkg);
 int OPCPackageSaveToDir(OPCPackage *pkg);
-int OPCPackageSaveTo(OPCPackage *pkg, const char *filename);
+int OPCPackageSave(OPCPackage *pkg, DFError **error);
 
 OPCPart *OPCPackagePartWithURI(OPCPackage *pkg, const char *URI);
 OPCPart *OPCPackageAddRelatedPart(OPCPackage *pkg, const char *URI, const char *contentType,

http://git-wip-us.apache.org/repos/asf/incubator-corinthia/blob/ad3bfd8b/DocFormats/filters/ooxml/src/word/Word.c
----------------------------------------------------------------------
diff --git a/DocFormats/filters/ooxml/src/word/Word.c b/DocFormats/filters/ooxml/src/word/Word.c
index 876d9d7..2436af8 100644
--- a/DocFormats/filters/ooxml/src/word/Word.c
+++ b/DocFormats/filters/ooxml/src/word/Word.c
@@ -19,6 +19,7 @@
 #include "CSSProperties.h"
 #include "DFString.h"
 #include "DFCommon.h"
+#include "DFZipFile.h"
 #include <stdlib.h>
 
 int DFHTMLToWord(const char *sourcePath, const char *destPath, DFError **error)
@@ -51,7 +52,9 @@ int DFHTMLToWord(const char *sourcePath, const char *destPath, DFError **error)
         goto end;
     }
 
-    if (!WordPackageSaveTo(wordPackage,destPath,error))
+    if (!WordPackageSave(wordPackage,error))
+        goto end;
+    if (!DFZip(destPath,rawPackage,error))
         goto end;
 
     ok = 1;

http://git-wip-us.apache.org/repos/asf/incubator-corinthia/blob/ad3bfd8b/DocFormats/filters/ooxml/src/word/WordPackage.c
----------------------------------------------------------------------
diff --git a/DocFormats/filters/ooxml/src/word/WordPackage.c b/DocFormats/filters/ooxml/src/word/WordPackage.c
index c11cb78..866ec05 100644
--- a/DocFormats/filters/ooxml/src/word/WordPackage.c
+++ b/DocFormats/filters/ooxml/src/word/WordPackage.c
@@ -208,9 +208,9 @@ WordPackage *WordPackageOpenNew(DFPackage *store, DFError **error)
     return NULL;
 }
 
-WordPackage *WordPackageOpenFrom(DFPackage *store, const char *filename, DFError **error)
+WordPackage *WordPackageOpenFrom(DFPackage *store, DFError **error)
 {
-    OPCPackage *opc = OPCPackageOpenFrom(store,filename,error);
+    OPCPackage *opc = OPCPackageOpenFrom(store,error);
     if (opc == NULL)
         return NULL;
 
@@ -331,7 +331,7 @@ static int savePart(WordPackage *package, DFDocument *document,
     return 1;
 }
 
-int WordPackageSaveTo(WordPackage *package, const char *filename, DFError **error)
+int WordPackageSave(WordPackage *package, DFError **error)
 {
     // Document
     assert(package->document != NULL);
@@ -364,10 +364,8 @@ int WordPackageSaveTo(WordPackage *package, const char *filename, DFError
**erro
         return 0;
 
     // Build .docx zip archive, if requested
-    if ((filename != NULL) && !OPCPackageSaveTo(package->opc,filename)) {
-        DFErrorFormat(error,"%s",package->opc->errors->data);
+    if (!OPCPackageSave(package->opc,error))
         return 0;
-    }
 
     return 1;
 }

http://git-wip-us.apache.org/repos/asf/incubator-corinthia/blob/ad3bfd8b/DocFormats/filters/ooxml/src/word/WordPackage.h
----------------------------------------------------------------------
diff --git a/DocFormats/filters/ooxml/src/word/WordPackage.h b/DocFormats/filters/ooxml/src/word/WordPackage.h
index a141650..5881844 100644
--- a/DocFormats/filters/ooxml/src/word/WordPackage.h
+++ b/DocFormats/filters/ooxml/src/word/WordPackage.h
@@ -57,8 +57,8 @@ void WordPackageCollapseBookmarks(WordPackage *package);
 void WordPackageExpandBookmarks(WordPackage *package);
 
 WordPackage *WordPackageOpenNew(DFPackage *store, DFError **error);
-WordPackage *WordPackageOpenFrom(DFPackage *store, const char *filename, DFError **error);
-int WordPackageSaveTo(WordPackage *package, const char *filename, DFError **error);
+WordPackage *WordPackageOpenFrom(DFPackage *store, DFError **error);
+int WordPackageSave(WordPackage *package, DFError **error);
 DFDocument *WordPackageGenerateHTML(WordPackage *package, const char *path, const char *idPrefix,
                                     DFError **error, DFBuffer *warnings);
 int WordPackageUpdateFromHTML(WordPackage *package, DFDocument *input, const char *path,

http://git-wip-us.apache.org/repos/asf/incubator-corinthia/blob/ad3bfd8b/consumers/dfutil/src/Commands.c
----------------------------------------------------------------------
diff --git a/consumers/dfutil/src/Commands.c b/consumers/dfutil/src/Commands.c
index df75e2a..7c4d813 100644
--- a/consumers/dfutil/src/Commands.c
+++ b/consumers/dfutil/src/Commands.c
@@ -28,6 +28,7 @@
 #include "CSS.h"
 #include "HTMLToLaTeX.h"
 #include "DFCommon.h"
+#include "DFZipFile.h"
 #include <DocFormats/DocFormats.h>
 #include <errno.h>
 #include <stdio.h>
@@ -108,7 +109,6 @@ static int prettyPrintWordFile(const char *filename, DFError **error)
         return 0;;
     int ok = 0;
     char *wordTempPath = DFAppendPathComponent(tempPath,"word");
-    char *plainTempPath = DFAppendPathComponent(tempPath,"plain");
     char *plain = NULL;
     WordPackage *wordPackage = NULL;
     DFPackage *rawPackage = DFPackageNewMemory();
@@ -116,12 +116,14 @@ static int prettyPrintWordFile(const char *filename, DFError **error)
     if (!DFEmptyDirectory(wordTempPath,error))
         goto end;
 
-    wordPackage = WordPackageOpenFrom(rawPackage,filename,error);
+    if (!DFUnzip(filename,rawPackage,error))
+        goto end;
+    wordPackage = WordPackageOpenFrom(rawPackage,error);
     if (wordPackage == NULL)
         goto end;
 
     WordPackageRemovePointlessElements(wordPackage);
-    plain = Word_toPlain(wordPackage,rawPackage,NULL,plainTempPath);
+    plain = Word_toPlain(wordPackage,rawPackage,NULL);
     printf("%s",plain);
 
     ok = 1;
@@ -129,7 +131,6 @@ static int prettyPrintWordFile(const char *filename, DFError **error)
 end:
     free(tempPath);
     free(wordTempPath);
-    free(plainTempPath);
     free(plain);
     DFPackageRelease(rawPackage);
     WordPackageRelease(wordPackage);
@@ -174,7 +175,9 @@ static int fromPlain2(const char *tempPath, const char *inStr, const char
*inPat
     if (!Word_fromPlain(inStr,inPath,zipPath,&wordPackage,&rawPackage,error))
         goto end;
 
-    ok = WordPackageSaveTo(wordPackage,outFilename,error);
+    ok = WordPackageSave(wordPackage,error);
+    if (!DFZip(outFilename,rawPackage,error))
+        ok = 0;
     WordPackageRelease(wordPackage);
     DFPackageRelease(rawPackage);
 
@@ -327,7 +330,11 @@ int simplifyFields(const char *inFilename, const char *outFilename, DFError
**er
     }
 
     DFPackage *rawPackage = DFPackageNewMemory();
-    WordPackage *wordPackage = WordPackageOpenFrom(rawPackage,inFilename,error);
+    if (!DFUnzip(inFilename,rawPackage,error)) {
+        DFErrorFormat(error,"%s: %s",inFilename,DFErrorMessage(error));
+        return 0;
+    }
+    WordPackage *wordPackage = WordPackageOpenFrom(rawPackage,error);
     DFPackageRelease(rawPackage);
     if (wordPackage == NULL) {
         DFErrorFormat(error,"%s: %s",inFilename,DFErrorMessage(error));
@@ -336,7 +343,12 @@ int simplifyFields(const char *inFilename, const char *outFilename, DFError
**er
 
     WordPackageSimplifyFields(wordPackage);
 
-    if (!WordPackageSaveTo(wordPackage,outFilename,error)) {
+    if (!WordPackageSave(wordPackage,error)) {
+        DFErrorFormat(error,"%s: %s",outFilename,DFErrorMessage(error));
+        WordPackageRelease(wordPackage);
+        return 0;
+    }
+    if (!DFZip(outFilename,rawPackage,error)) {
         DFErrorFormat(error,"%s: %s",outFilename,DFErrorMessage(error));
         WordPackageRelease(wordPackage);
         return 0;

http://git-wip-us.apache.org/repos/asf/incubator-corinthia/blob/ad3bfd8b/consumers/dfutil/src/Plain.c
----------------------------------------------------------------------
diff --git a/consumers/dfutil/src/Plain.c b/consumers/dfutil/src/Plain.c
index 14c3542..9167920 100644
--- a/consumers/dfutil/src/Plain.c
+++ b/consumers/dfutil/src/Plain.c
@@ -280,20 +280,13 @@ end:
     }
 }
 
-static char *Word_toPlainOrError(WordPackage *wordPackage, DFPackage *rawPackage,
-                                 DFHashTable *parts, const char *tempPath, DFError **error)
+static char *Word_toPlainOrError(WordPackage *wordPackage, DFPackage *rawPackage, DFHashTable
*parts, DFError **error)
 {
-    char *docxPath = DFAppendPathComponent(tempPath,"file.docx");
     char *result = NULL;
     int ok = 0;
 
-    if (!DFEmptyDirectory(tempPath,error)) {
-        DFErrorFormat(error,"%s: %s",tempPath,DFErrorMessage(error));
-        goto end;
-    }
-
-    if (!WordPackageSaveTo(wordPackage,docxPath,error)) {
-        DFErrorFormat(error,"WordPackageSaveTo: %s",DFErrorMessage(error));
+    if (!WordPackageSave(wordPackage,error)) {
+        DFErrorFormat(error,"WordPackageSave: %s",DFErrorMessage(error));
         goto end;
     }
 
@@ -301,17 +294,16 @@ static char *Word_toPlainOrError(WordPackage *wordPackage, DFPackage
*rawPackage
     ok = 1;
 
 end:
-    free(docxPath);
     if (ok)
         return result;
     free(result);
     return 0;
 }
 
-char *Word_toPlain(WordPackage *wordPackage, DFPackage *rawPackage, DFHashTable *parts, const
char *tempPath)
+char *Word_toPlain(WordPackage *wordPackage, DFPackage *rawPackage, DFHashTable *parts)
 {
     DFError *error = NULL;
-    char *result = Word_toPlainOrError(wordPackage,rawPackage,parts,tempPath,&error);
+    char *result = Word_toPlainOrError(wordPackage,rawPackage,parts,&error);
     if (result == NULL) {
         result = DFFormatString("%s\n",DFErrorMessage(&error));
         DFErrorRelease(error);
@@ -621,7 +613,11 @@ int Word_fromPlain(const char *plain, const char *plainPath, const char
*zipTemp
 
     // Now we have a .docx file; access it using what will be the new way (this API will
change so we just say
     // "open a word document from here", without having to separately create the package
object first.
-    wp = WordPackageOpenFrom(secondStore,docxPath,error);
+    if (!DFUnzip(docxPath,secondStore,error)) {
+        DFErrorFormat(error,"DFUnzip %s: %s\n",docxPath,DFErrorMessage(error));
+        goto end;
+    }
+    wp = WordPackageOpenFrom(secondStore,error);
     if (wp == NULL) {
         DFErrorFormat(error,"WordPackageStartFrom %s: %s",docxPath,DFErrorMessage(error));
         goto end;

http://git-wip-us.apache.org/repos/asf/incubator-corinthia/blob/ad3bfd8b/consumers/dfutil/src/Plain.h
----------------------------------------------------------------------
diff --git a/consumers/dfutil/src/Plain.h b/consumers/dfutil/src/Plain.h
index bde964b..edb6de5 100644
--- a/consumers/dfutil/src/Plain.h
+++ b/consumers/dfutil/src/Plain.h
@@ -20,7 +20,7 @@
 #include "DFDOM.h"
 #include "WordPackage.h"
 
-char *Word_toPlain(WordPackage *wordPackage, DFPackage *rawPackage, DFHashTable *parts, const
char *tempPath);
+char *Word_toPlain(WordPackage *wordPackage, DFPackage *rawPackage, DFHashTable *parts);
 int Word_fromPlain(const char *plain, const char *plainPath, const char *zipTempPath,
                    WordPackage **outWordPackage, DFPackage **outRawPackage, DFError **error);
 char *HTML_toPlain(DFDocument *doc, const char *imagePath, DFError **error);

http://git-wip-us.apache.org/repos/asf/incubator-corinthia/blob/ad3bfd8b/consumers/dfutil/src/TestFunctions.c
----------------------------------------------------------------------
diff --git a/consumers/dfutil/src/TestFunctions.c b/consumers/dfutil/src/TestFunctions.c
index e78df4a..a9ff3dd 100644
--- a/consumers/dfutil/src/TestFunctions.c
+++ b/consumers/dfutil/src/TestFunctions.c
@@ -105,9 +105,7 @@ static void Word_testCollapseBookmarks(TestCase *script, int argc, const
char **
     DFHashTableAdd(parts,"document","");
 
     // Output the docx file
-    char *plainTempPath = DFAppendPathComponent(script->tempPath,"plain");
-    char *plain = Word_toPlain(wordPackage,rawPackage,parts,plainTempPath);
-    free(plainTempPath);
+    char *plain = Word_toPlain(wordPackage,rawPackage,parts);
     DFBufferFormat(script->output,"%s",plain);
     free(plain);
     DFHashTableRelease(parts);
@@ -133,9 +131,7 @@ static void Word_testExpandBookmarks(TestCase *script, int argc, const
char **ar
     DFHashTableAdd(parts,"document","");
 
     // Output the docx file
-    char *plainTempPath = DFAppendPathComponent(script->tempPath,"plain");
-    char *plain = Word_toPlain(wordPackage,rawPackage,parts,plainTempPath);
-    free(plainTempPath);
+    char *plain = Word_toPlain(wordPackage,rawPackage,parts);
     DFBufferFormat(script->output,"%s",plain);
     free(plain);
     DFHashTableRelease(parts);
@@ -218,7 +214,6 @@ static void Word_testCreate(TestCase *script, int argc, const char **argv)
 
     DFDocument *htmlDoc = NULL;
     DFHashTable *parts = NULL;
-    char *plainTempPath = NULL;
     char *plain = NULL;
     DFError *error = NULL;
 
@@ -248,17 +243,15 @@ static void Word_testCreate(TestCase *script, int argc, const char **argv)
     DFBufferRelease(warnings);
 
     // We don't actually "save" the package as such; this is just to ensure the missing OPC
parts are added
-    WordPackageSaveTo(wordPackage,NULL,NULL);
+    WordPackageSave(wordPackage,NULL);
 
     // Output the docx file
     parts = getFlags(argc,argv);
-    plainTempPath = DFAppendPathComponent(script->tempPath,"plain");
-    plain = Word_toPlain(wordPackage,rawPackage,parts,plainTempPath);
+    plain = Word_toPlain(wordPackage,rawPackage,parts);
     DFBufferFormat(script->output,"%s",plain);
 
 end:
     DFDocumentRelease(htmlDoc);
-    free(plainTempPath);
     free(plain);
     DFHashTableRelease(parts);
     DFErrorRelease(error);
@@ -282,13 +275,11 @@ static void Word_testUpdate2(TestCase *script, WordPackage *wordPackage,
DFPacka
     }
 
     // We don't actually "save" the package as such; this is just to ensure the missing OPC
parts are added
-    WordPackageSaveTo(wordPackage,NULL,NULL);
+    WordPackageSave(wordPackage,NULL);
 
     // Output the updated docx file
     DFHashTable *parts = getFlags(argc,argv);
-    char *plainTempPath = DFAppendPathComponent(script->tempPath,"plain");
-    char *plain = Word_toPlain(wordPackage,rawPackage,parts,plainTempPath);
-    free(plainTempPath);
+    char *plain = Word_toPlain(wordPackage,rawPackage,parts);
     DFBufferFormat(script->output,"%s",plain);
     free(plain);
     DFHashTableRelease(parts);


Mime
View raw message