Skip to content

Commit

Permalink
schema: allow multiple anchors
Browse files Browse the repository at this point in the history
Required for multiple charts in a single drawing.
  • Loading branch information
tbaliance committed Sep 5, 2017
1 parent e834046 commit b8bfe81
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ package chartDrawing

import (
"encoding/xml"
"fmt"
"log"
)

type CT_Drawing struct {
RelSizeAnchor *CT_RelSizeAnchor
AbsSizeAnchor *CT_AbsSizeAnchor
EG_Anchor []*EG_Anchor
}

func NewCT_Drawing() *CT_Drawing {
Expand All @@ -25,13 +25,10 @@ func NewCT_Drawing() *CT_Drawing {
func (m *CT_Drawing) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
start.Name.Local = "CT_Drawing"
e.EncodeToken(start)
if m.RelSizeAnchor != nil {
serelSizeAnchor := xml.StartElement{Name: xml.Name{Local: "relSizeAnchor"}}
e.EncodeElement(m.RelSizeAnchor, serelSizeAnchor)
}
if m.AbsSizeAnchor != nil {
seabsSizeAnchor := xml.StartElement{Name: xml.Name{Local: "absSizeAnchor"}}
e.EncodeElement(m.AbsSizeAnchor, seabsSizeAnchor)
if m.EG_Anchor != nil {
for _, c := range m.EG_Anchor {
c.MarshalXML(e, xml.StartElement{})
}
}
e.EncodeToken(xml.EndElement{Name: start.Name})
return nil
Expand All @@ -49,15 +46,19 @@ lCT_Drawing:
case xml.StartElement:
switch el.Name.Local {
case "relSizeAnchor":
m.RelSizeAnchor = NewCT_RelSizeAnchor()
if err := d.DecodeElement(m.RelSizeAnchor, &el); err != nil {
tmpanchor := NewEG_Anchor()
tmpanchor.RelSizeAnchor = NewCT_RelSizeAnchor()
if err := d.DecodeElement(tmpanchor.RelSizeAnchor, &el); err != nil {
return err
}
m.EG_Anchor = append(m.EG_Anchor, tmpanchor)
case "absSizeAnchor":
m.AbsSizeAnchor = NewCT_AbsSizeAnchor()
if err := d.DecodeElement(m.AbsSizeAnchor, &el); err != nil {
tmpanchor := NewEG_Anchor()
tmpanchor.AbsSizeAnchor = NewCT_AbsSizeAnchor()
if err := d.DecodeElement(tmpanchor.AbsSizeAnchor, &el); err != nil {
return err
}
m.EG_Anchor = append(m.EG_Anchor, tmpanchor)
default:
log.Printf("skipping unsupported element on CT_Drawing %v", el.Name)
if err := d.Skip(); err != nil {
Expand All @@ -79,13 +80,8 @@ func (m *CT_Drawing) Validate() error {

// ValidateWithPath validates the CT_Drawing and its children, prefixing error messages with path
func (m *CT_Drawing) ValidateWithPath(path string) error {
if m.RelSizeAnchor != nil {
if err := m.RelSizeAnchor.ValidateWithPath(path + "/RelSizeAnchor"); err != nil {
return err
}
}
if m.AbsSizeAnchor != nil {
if err := m.AbsSizeAnchor.ValidateWithPath(path + "/AbsSizeAnchor"); err != nil {
for i, v := range m.EG_Anchor {
if err := v.ValidateWithPath(fmt.Sprintf("%s/EG_Anchor[%d]", path, i)); err != nil {
return err
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,12 @@ package spreadsheetDrawing

import (
"encoding/xml"
"fmt"
"log"
)

type CT_Drawing struct {
TwoCellAnchor *CT_TwoCellAnchor
OneCellAnchor *CT_OneCellAnchor
AbsoluteAnchor *CT_AbsoluteAnchor
EG_Anchor []*EG_Anchor
}

func NewCT_Drawing() *CT_Drawing {
Expand All @@ -25,17 +24,10 @@ func NewCT_Drawing() *CT_Drawing {

func (m *CT_Drawing) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
e.EncodeToken(start)
if m.TwoCellAnchor != nil {
setwoCellAnchor := xml.StartElement{Name: xml.Name{Local: "xdr:twoCellAnchor"}}
e.EncodeElement(m.TwoCellAnchor, setwoCellAnchor)
}
if m.OneCellAnchor != nil {
seoneCellAnchor := xml.StartElement{Name: xml.Name{Local: "xdr:oneCellAnchor"}}
e.EncodeElement(m.OneCellAnchor, seoneCellAnchor)
}
if m.AbsoluteAnchor != nil {
seabsoluteAnchor := xml.StartElement{Name: xml.Name{Local: "xdr:absoluteAnchor"}}
e.EncodeElement(m.AbsoluteAnchor, seabsoluteAnchor)
if m.EG_Anchor != nil {
for _, c := range m.EG_Anchor {
c.MarshalXML(e, xml.StartElement{})
}
}
e.EncodeToken(xml.EndElement{Name: start.Name})
return nil
Expand All @@ -53,20 +45,26 @@ lCT_Drawing:
case xml.StartElement:
switch el.Name.Local {
case "twoCellAnchor":
m.TwoCellAnchor = NewCT_TwoCellAnchor()
if err := d.DecodeElement(m.TwoCellAnchor, &el); err != nil {
tmpanchor := NewEG_Anchor()
tmpanchor.TwoCellAnchor = NewCT_TwoCellAnchor()
if err := d.DecodeElement(tmpanchor.TwoCellAnchor, &el); err != nil {
return err
}
m.EG_Anchor = append(m.EG_Anchor, tmpanchor)
case "oneCellAnchor":
m.OneCellAnchor = NewCT_OneCellAnchor()
if err := d.DecodeElement(m.OneCellAnchor, &el); err != nil {
tmpanchor := NewEG_Anchor()
tmpanchor.OneCellAnchor = NewCT_OneCellAnchor()
if err := d.DecodeElement(tmpanchor.OneCellAnchor, &el); err != nil {
return err
}
m.EG_Anchor = append(m.EG_Anchor, tmpanchor)
case "absoluteAnchor":
m.AbsoluteAnchor = NewCT_AbsoluteAnchor()
if err := d.DecodeElement(m.AbsoluteAnchor, &el); err != nil {
tmpanchor := NewEG_Anchor()
tmpanchor.AbsoluteAnchor = NewCT_AbsoluteAnchor()
if err := d.DecodeElement(tmpanchor.AbsoluteAnchor, &el); err != nil {
return err
}
m.EG_Anchor = append(m.EG_Anchor, tmpanchor)
default:
log.Printf("skipping unsupported element on CT_Drawing %v", el.Name)
if err := d.Skip(); err != nil {
Expand All @@ -88,18 +86,8 @@ func (m *CT_Drawing) Validate() error {

// ValidateWithPath validates the CT_Drawing and its children, prefixing error messages with path
func (m *CT_Drawing) ValidateWithPath(path string) error {
if m.TwoCellAnchor != nil {
if err := m.TwoCellAnchor.ValidateWithPath(path + "/TwoCellAnchor"); err != nil {
return err
}
}
if m.OneCellAnchor != nil {
if err := m.OneCellAnchor.ValidateWithPath(path + "/OneCellAnchor"); err != nil {
return err
}
}
if m.AbsoluteAnchor != nil {
if err := m.AbsoluteAnchor.ValidateWithPath(path + "/AbsoluteAnchor"); err != nil {
for i, v := range m.EG_Anchor {
if err := v.ValidateWithPath(fmt.Sprintf("%s/EG_Anchor[%d]", path, i)); err != nil {
return err
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,26 @@ lWsDr:
case xml.StartElement:
switch el.Name.Local {
case "twoCellAnchor":
m.TwoCellAnchor = NewCT_TwoCellAnchor()
if err := d.DecodeElement(m.TwoCellAnchor, &el); err != nil {
tmpanchor := NewEG_Anchor()
tmpanchor.TwoCellAnchor = NewCT_TwoCellAnchor()
if err := d.DecodeElement(tmpanchor.TwoCellAnchor, &el); err != nil {
return err
}
m.EG_Anchor = append(m.EG_Anchor, tmpanchor)
case "oneCellAnchor":
m.OneCellAnchor = NewCT_OneCellAnchor()
if err := d.DecodeElement(m.OneCellAnchor, &el); err != nil {
tmpanchor := NewEG_Anchor()
tmpanchor.OneCellAnchor = NewCT_OneCellAnchor()
if err := d.DecodeElement(tmpanchor.OneCellAnchor, &el); err != nil {
return err
}
m.EG_Anchor = append(m.EG_Anchor, tmpanchor)
case "absoluteAnchor":
m.AbsoluteAnchor = NewCT_AbsoluteAnchor()
if err := d.DecodeElement(m.AbsoluteAnchor, &el); err != nil {
tmpanchor := NewEG_Anchor()
tmpanchor.AbsoluteAnchor = NewCT_AbsoluteAnchor()
if err := d.DecodeElement(tmpanchor.AbsoluteAnchor, &el); err != nil {
return err
}
m.EG_Anchor = append(m.EG_Anchor, tmpanchor)
default:
log.Printf("skipping unsupported element on WsDr %v", el.Name)
if err := d.Skip(); err != nil {
Expand Down

0 comments on commit b8bfe81

Please sign in to comment.