From b1b3c0d15158abc71267da5893de020f047c3872 Mon Sep 17 00:00:00 2001 From: Alex Geer Date: Thu, 19 Dec 2019 19:30:48 +0300 Subject: =?UTF-8?q?Fix=20#539=20Fixed=20error=20opening=20excel=20file=20c?= =?UTF-8?q?reated=20in=20encoding=20d=E2=80=A6=20(#540)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Fixed issue #539 Fixed error opening excel file created in encoding different from UTF-8, added logging of possible errors when decoding XML if the function does not provide exit with an error * Added test for CharsetReader * Fixed #discussion_r359397878 Discussion: https://github.com/360EntSecGroup-Skylar/excelize/pull/540#discussion_r359397878 * Fixed go fmt * go mod tidy and removed unused imports * The code has been refactored --- docProps.go | 66 ++++++++++++++++++++++++++++++++++++++----------------------- 1 file changed, 41 insertions(+), 25 deletions(-) (limited to 'docProps.go') diff --git a/docProps.go b/docProps.go index 166512f..884eb63 100644 --- a/docProps.go +++ b/docProps.go @@ -10,7 +10,10 @@ package excelize import ( + "bytes" "encoding/xml" + "fmt" + "io" "reflect" ) @@ -65,13 +68,23 @@ import ( // Version: "1.0.0", // }) // -func (f *File) SetDocProps(docProperties *DocProperties) error { - core := decodeCoreProperties{} - err := xml.Unmarshal(namespaceStrictToTransitional(f.readXML("docProps/core.xml")), &core) - if err != nil { - return err +func (f *File) SetDocProps(docProperties *DocProperties) (err error) { + var ( + core *decodeCoreProperties + newProps *xlsxCoreProperties + fields []string + output []byte + immutable, mutable reflect.Value + field, val string + ) + + core = new(decodeCoreProperties) + if err = f.xmlNewDecoder(bytes.NewReader(namespaceStrictToTransitional(f.readXML("docProps/core.xml")))). + Decode(core); err != nil && err != io.EOF { + err = fmt.Errorf("xml decode error: %s", err) + return } - newProps := xlsxCoreProperties{ + newProps, err = &xlsxCoreProperties{ Dc: NameSpaceDublinCore, Dcterms: NameSpaceDublinCoreTerms, Dcmitype: NameSpaceDublinCoreMetadataIntiative, @@ -88,18 +101,16 @@ func (f *File) SetDocProps(docProperties *DocProperties) error { ContentStatus: core.ContentStatus, Category: core.Category, Version: core.Version, + }, nil + newProps.Created.Text, newProps.Created.Type, newProps.Modified.Text, newProps.Modified.Type = + core.Created.Text, core.Created.Type, core.Modified.Text, core.Modified.Type + fields = []string{ + "Category", "ContentStatus", "Creator", "Description", "Identifier", "Keywords", + "LastModifiedBy", "Revision", "Subject", "Title", "Language", "Version", } - newProps.Created.Text = core.Created.Text - newProps.Created.Type = core.Created.Type - newProps.Modified.Text = core.Modified.Text - newProps.Modified.Type = core.Modified.Type - - fields := []string{"Category", "ContentStatus", "Creator", "Description", "Identifier", "Keywords", "LastModifiedBy", "Revision", "Subject", "Title", "Language", "Version"} - immutable := reflect.ValueOf(*docProperties) - mutable := reflect.ValueOf(&newProps).Elem() - for _, field := range fields { - val := immutable.FieldByName(field).String() - if val != "" { + immutable, mutable = reflect.ValueOf(*docProperties), reflect.ValueOf(newProps).Elem() + for _, field = range fields { + if val = immutable.FieldByName(field).String(); val != "" { mutable.FieldByName(field).SetString(val) } } @@ -109,19 +120,22 @@ func (f *File) SetDocProps(docProperties *DocProperties) error { if docProperties.Modified != "" { newProps.Modified.Text = docProperties.Modified } - output, err := xml.Marshal(&newProps) + output, err = xml.Marshal(newProps) f.saveFileList("docProps/core.xml", output) - return err + + return } // GetDocProps provides a function to get document core properties. -func (f *File) GetDocProps() (*DocProperties, error) { - core := decodeCoreProperties{} - err := xml.Unmarshal(namespaceStrictToTransitional(f.readXML("docProps/core.xml")), &core) - if err != nil { - return nil, err +func (f *File) GetDocProps() (ret *DocProperties, err error) { + var core = new(decodeCoreProperties) + + if err = f.xmlNewDecoder(bytes.NewReader(namespaceStrictToTransitional(f.readXML("docProps/core.xml")))). + Decode(core); err != nil && err != io.EOF { + err = fmt.Errorf("xml decode error: %s", err) + return } - return &DocProperties{ + ret, err = &DocProperties{ Category: core.Category, ContentStatus: core.ContentStatus, Created: core.Created.Text, @@ -137,4 +151,6 @@ func (f *File) GetDocProps() (*DocProperties, error) { Language: core.Language, Version: core.Version, }, nil + + return } -- cgit v1.2.1