diff --git a/cmd/main/main.go b/cmd/main/main.go index 81b9576..198bb6a 100644 --- a/cmd/main/main.go +++ b/cmd/main/main.go @@ -5,6 +5,7 @@ import ( "flag" "fmt" "os" + "strings" "github.com/fumiama/docxlib" ) @@ -150,7 +151,9 @@ func main() { } if *unm { - f, err := os.Create("unmarshal_" + *fileLocation) + i := strings.LastIndex(*fileLocation, "/") + name := (*fileLocation)[:i+1] + "unmarshal_" + (*fileLocation)[i+1:] + f, err := os.Create(name) if err != nil { panic(err) } @@ -174,7 +177,7 @@ func main() { fmt.Printf("[%d] ", x) for y, c := range r.TableCells { if len(c.Paragraphs) > 0 && len(c.Paragraphs[0].Children) > 0 { - fmt.Printf("<%d> %s\t", y, c.Paragraphs[0].Children[0].(*docxlib.Run).Text.Text) + fmt.Printf("<%d> %v\t", y, &c.Paragraphs[0]) } else { fmt.Print("\t") } diff --git a/empty.go b/empty.go index afb2734..f21cff0 100644 --- a/empty.go +++ b/empty.go @@ -64,7 +64,7 @@ func newEmptyA4File() *Docx { "word/webSettings.xml", "[Content_Types].xml", }, - buf: bytes.NewBuffer(make([]byte, 0, 1024*1024*4)), + buf: bytes.NewBuffer(make([]byte, 0, 1024*1024)), } docx.Document.Body.file = docx return docx diff --git a/structdrawing.go b/structdrawing.go index f29fcd3..74cac5a 100644 --- a/structdrawing.go +++ b/structdrawing.go @@ -128,31 +128,33 @@ func (r *WPInline) UnmarshalXML(d *xml.Decoder, start xml.StartElement) (err err switch tt.Name.Local { case "extent": r.Extent = new(WPExtent) - r.Extent.CX, err = strconv.ParseInt(getAtt(tt.Attr, "cx"), 10, 64) - if err != nil { - return err - } - r.Extent.CY, err = strconv.ParseInt(getAtt(tt.Attr, "cy"), 10, 64) - if err != nil { - return err + for _, v := range tt.Attr { + switch v.Name.Local { + case "cx": + r.Extent.CX, err = strconv.ParseInt(v.Value, 10, 64) + case "cy": + r.Extent.CY, err = strconv.ParseInt(v.Value, 10, 64) + } + if err != nil { + return err + } } case "effectExtent": r.EffectExtent = new(WPEffectExtent) - r.EffectExtent.L, err = strconv.ParseInt(getAtt(tt.Attr, "l"), 10, 64) - if err != nil { - return err - } - r.EffectExtent.T, err = strconv.ParseInt(getAtt(tt.Attr, "t"), 10, 64) - if err != nil { - return err - } - r.EffectExtent.R, err = strconv.ParseInt(getAtt(tt.Attr, "r"), 10, 64) - if err != nil { - return err - } - r.EffectExtent.B, err = strconv.ParseInt(getAtt(tt.Attr, "b"), 10, 64) - if err != nil { - return err + for _, v := range tt.Attr { + switch v.Name.Local { + case "l": + r.EffectExtent.L, err = strconv.ParseInt(v.Value, 10, 64) + case "t": + r.EffectExtent.T, err = strconv.ParseInt(v.Value, 10, 64) + case "r": + r.EffectExtent.R, err = strconv.ParseInt(v.Value, 10, 64) + case "b": + r.EffectExtent.B, err = strconv.ParseInt(v.Value, 10, 64) + } + if err != nil { + return err + } } case "docPr": r.DocPr = new(WPDocPr) @@ -321,7 +323,11 @@ func (w *WPCNvGraphicFramePr) UnmarshalXML(d *xml.Decoder, start xml.StartElemen if err != nil && !strings.HasPrefix(err.Error(), "expected") { return err } - value.NoChangeAspect, err = strconv.Atoi(getAtt(tt.Attr, "noChangeAspect")) + v := getAtt(tt.Attr, "noChangeAspect") + if v == "" { + continue + } + value.NoChangeAspect, err = strconv.Atoi(v) if err != nil { return err } @@ -550,7 +556,11 @@ func (p *PicCNvPicPr) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error switch tt.Name.Local { case "picLocks": var value APicLocks - value.NoChangeAspect, err = strconv.Atoi(getAtt(tt.Attr, "noChangeAspect")) + v := getAtt(tt.Attr, "noChangeAspect") + if v == "" { + continue + } + value.NoChangeAspect, err = strconv.Atoi(v) if err != nil { return err } @@ -655,7 +665,11 @@ func (a *ABlip) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error { switch tt.Name.Local { case "alphaModFix": var value AAlphaModFix - value.Amount, err = strconv.Atoi(getAtt(tt.Attr, "amt")) + v := getAtt(tt.Attr, "amt") + if v == "" { + continue + } + value.Amount, err = strconv.Atoi(v) if err != nil { return err } @@ -779,22 +793,28 @@ func (a *AXfrm) UnmarshalXML(d *xml.Decoder, start xml.StartElement) (err error) if tt, ok := t.(xml.StartElement); ok { switch tt.Name.Local { case "off": - a.Off.X, err = strconv.ParseInt(getAtt(tt.Attr, "x"), 10, 64) - if err != nil { - return err - } - a.Off.Y, err = strconv.ParseInt(getAtt(tt.Attr, "y"), 10, 64) - if err != nil { - return err + for _, v := range tt.Attr { + switch v.Name.Local { + case "x": + a.Off.X, err = strconv.ParseInt(v.Value, 10, 64) + case "y": + a.Off.Y, err = strconv.ParseInt(v.Value, 10, 64) + } + if err != nil { + return err + } } case "ext": - a.Ext.CX, err = strconv.ParseInt(getAtt(tt.Attr, "cx"), 10, 64) - if err != nil { - return err - } - a.Ext.CY, err = strconv.ParseInt(getAtt(tt.Attr, "cy"), 10, 64) - if err != nil { - return err + for _, v := range tt.Attr { + switch v.Name.Local { + case "cx": + a.Ext.CX, err = strconv.ParseInt(v.Value, 10, 64) + case "cy": + a.Ext.CY, err = strconv.ParseInt(v.Value, 10, 64) + } + if err != nil { + return err + } } default: err = d.Skip() // skip unsupported tags @@ -976,13 +996,16 @@ func (r *WPAnchor) UnmarshalXML(d *xml.Decoder, start xml.StartElement) (err err switch tt.Name.Local { case "simplePos": r.SimplePosXY = new(WPSimplePos) - r.SimplePosXY.X, err = strconv.ParseInt(getAtt(tt.Attr, "x"), 10, 64) - if err != nil { - return err - } - r.SimplePosXY.Y, err = strconv.ParseInt(getAtt(tt.Attr, "y"), 10, 64) - if err != nil { - return err + for _, v := range tt.Attr { + switch v.Name.Local { + case "x": + r.SimplePosXY.X, err = strconv.ParseInt(v.Value, 10, 64) + case "y": + r.SimplePosXY.Y, err = strconv.ParseInt(v.Value, 10, 64) + } + if err != nil { + return err + } } case "positionH": r.PositionH = new(WPPositionH) diff --git a/structtable.go b/structtable.go index 8060370..324f38c 100644 --- a/structtable.go +++ b/structtable.go @@ -484,14 +484,22 @@ func (r *WTableCellProperties) UnmarshalXML(d *xml.Decoder, start xml.StartEleme switch tt.Name.Local { case "tcW": r.TableCellWidth = new(WTableCellWidth) - r.TableCellWidth.W, err = strconv.ParseInt(getAtt(tt.Attr, "w"), 10, 64) + v := getAtt(tt.Attr, "w") + if v == "" { + continue + } + r.TableCellWidth.W, err = strconv.ParseInt(v, 10, 64) if err != nil { return err } r.TableCellWidth.Type = getAtt(tt.Attr, "type") case "gridSpan": r.GridSpan = new(WGridSpan) - r.GridSpan.Val, err = strconv.Atoi(getAtt(tt.Attr, "val")) + v := getAtt(tt.Attr, "val") + if v == "" { + continue + } + r.GridSpan.Val, err = strconv.Atoi(v) if err != nil { return err } diff --git a/unpack.go b/unpack.go index 5dcdcae..a8b50ef 100644 --- a/unpack.go +++ b/unpack.go @@ -43,7 +43,7 @@ func unpack(zipReader *zip.Reader) (docx *Docx, err error) { // fill remaining files into tmpfslst docx.tmpfslst = append(docx.tmpfslst, f.Name) } - docx.buf = bytes.NewBuffer(make([]byte, 0, 1024*1024*4)) + docx.buf = bytes.NewBuffer(make([]byte, 0, 1024*1024)) return }