2017-08-28 20:56:18 -05:00
|
|
|
// Copyright 2017 Baliance. All rights reserved.
|
|
|
|
//
|
|
|
|
// Use of this source code is governed by the terms of the Affero GNU General
|
|
|
|
// Public License version 3.0 as published by the Free Software Foundation and
|
|
|
|
// appearing in the file LICENSE included in the packaging of this file. A
|
|
|
|
// commercial license can be purchased by contacting sales@baliance.com.
|
|
|
|
|
|
|
|
package chart
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/xml"
|
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
|
2017-09-04 16:55:00 -05:00
|
|
|
"baliance.com/gooxml"
|
2017-08-28 20:56:18 -05:00
|
|
|
"baliance.com/gooxml/schema/schemas.openxmlformats.org/officeDocument/2006/sharedTypes"
|
|
|
|
)
|
|
|
|
|
|
|
|
type CT_PageSetup struct {
|
|
|
|
PaperSizeAttr *uint32
|
|
|
|
PaperHeightAttr *string
|
|
|
|
PaperWidthAttr *string
|
|
|
|
FirstPageNumberAttr *uint32
|
|
|
|
OrientationAttr ST_PageSetupOrientation
|
|
|
|
BlackAndWhiteAttr *bool
|
|
|
|
DraftAttr *bool
|
|
|
|
UseFirstPageNumberAttr *bool
|
|
|
|
HorizontalDpiAttr *int32
|
|
|
|
VerticalDpiAttr *int32
|
|
|
|
CopiesAttr *uint32
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewCT_PageSetup() *CT_PageSetup {
|
|
|
|
ret := &CT_PageSetup{}
|
2017-09-04 16:55:00 -05:00
|
|
|
ret.PaperSizeAttr = gooxml.Uint32(1)
|
|
|
|
ret.FirstPageNumberAttr = gooxml.Uint32(1)
|
|
|
|
ret.OrientationAttr = ST_PageSetupOrientationDefault
|
|
|
|
ret.BlackAndWhiteAttr = gooxml.Bool(false)
|
|
|
|
ret.DraftAttr = gooxml.Bool(false)
|
|
|
|
ret.UseFirstPageNumberAttr = gooxml.Bool(false)
|
|
|
|
ret.CopiesAttr = gooxml.Uint32(1)
|
2017-08-28 20:56:18 -05:00
|
|
|
return ret
|
|
|
|
}
|
2017-09-01 15:19:23 -05:00
|
|
|
|
2017-08-28 20:56:18 -05:00
|
|
|
func (m *CT_PageSetup) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
|
|
|
|
if m.PaperSizeAttr != nil {
|
|
|
|
start.Attr = append(start.Attr, xml.Attr{Name: xml.Name{Local: "paperSize"},
|
|
|
|
Value: fmt.Sprintf("%v", *m.PaperSizeAttr)})
|
|
|
|
}
|
|
|
|
if m.PaperHeightAttr != nil {
|
|
|
|
start.Attr = append(start.Attr, xml.Attr{Name: xml.Name{Local: "paperHeight"},
|
|
|
|
Value: fmt.Sprintf("%v", *m.PaperHeightAttr)})
|
|
|
|
}
|
|
|
|
if m.PaperWidthAttr != nil {
|
|
|
|
start.Attr = append(start.Attr, xml.Attr{Name: xml.Name{Local: "paperWidth"},
|
|
|
|
Value: fmt.Sprintf("%v", *m.PaperWidthAttr)})
|
|
|
|
}
|
|
|
|
if m.FirstPageNumberAttr != nil {
|
|
|
|
start.Attr = append(start.Attr, xml.Attr{Name: xml.Name{Local: "firstPageNumber"},
|
|
|
|
Value: fmt.Sprintf("%v", *m.FirstPageNumberAttr)})
|
|
|
|
}
|
|
|
|
if m.OrientationAttr != ST_PageSetupOrientationUnset {
|
|
|
|
attr, err := m.OrientationAttr.MarshalXMLAttr(xml.Name{Local: "orientation"})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
start.Attr = append(start.Attr, attr)
|
|
|
|
}
|
|
|
|
if m.BlackAndWhiteAttr != nil {
|
|
|
|
start.Attr = append(start.Attr, xml.Attr{Name: xml.Name{Local: "blackAndWhite"},
|
2017-09-03 21:00:35 -05:00
|
|
|
Value: fmt.Sprintf("%d", b2i(*m.BlackAndWhiteAttr))})
|
2017-08-28 20:56:18 -05:00
|
|
|
}
|
|
|
|
if m.DraftAttr != nil {
|
|
|
|
start.Attr = append(start.Attr, xml.Attr{Name: xml.Name{Local: "draft"},
|
2017-09-03 21:00:35 -05:00
|
|
|
Value: fmt.Sprintf("%d", b2i(*m.DraftAttr))})
|
2017-08-28 20:56:18 -05:00
|
|
|
}
|
|
|
|
if m.UseFirstPageNumberAttr != nil {
|
|
|
|
start.Attr = append(start.Attr, xml.Attr{Name: xml.Name{Local: "useFirstPageNumber"},
|
2017-09-03 21:00:35 -05:00
|
|
|
Value: fmt.Sprintf("%d", b2i(*m.UseFirstPageNumberAttr))})
|
2017-08-28 20:56:18 -05:00
|
|
|
}
|
|
|
|
if m.HorizontalDpiAttr != nil {
|
|
|
|
start.Attr = append(start.Attr, xml.Attr{Name: xml.Name{Local: "horizontalDpi"},
|
|
|
|
Value: fmt.Sprintf("%v", *m.HorizontalDpiAttr)})
|
|
|
|
}
|
|
|
|
if m.VerticalDpiAttr != nil {
|
|
|
|
start.Attr = append(start.Attr, xml.Attr{Name: xml.Name{Local: "verticalDpi"},
|
|
|
|
Value: fmt.Sprintf("%v", *m.VerticalDpiAttr)})
|
|
|
|
}
|
|
|
|
if m.CopiesAttr != nil {
|
|
|
|
start.Attr = append(start.Attr, xml.Attr{Name: xml.Name{Local: "copies"},
|
|
|
|
Value: fmt.Sprintf("%v", *m.CopiesAttr)})
|
|
|
|
}
|
|
|
|
e.EncodeToken(start)
|
|
|
|
e.EncodeToken(xml.EndElement{Name: start.Name})
|
|
|
|
return nil
|
|
|
|
}
|
2017-09-01 15:19:23 -05:00
|
|
|
|
2017-08-28 20:56:18 -05:00
|
|
|
func (m *CT_PageSetup) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
|
|
|
|
// initialize to default
|
2017-09-04 16:55:00 -05:00
|
|
|
m.PaperSizeAttr = gooxml.Uint32(1)
|
|
|
|
m.FirstPageNumberAttr = gooxml.Uint32(1)
|
|
|
|
m.OrientationAttr = ST_PageSetupOrientationDefault
|
|
|
|
m.BlackAndWhiteAttr = gooxml.Bool(false)
|
|
|
|
m.DraftAttr = gooxml.Bool(false)
|
|
|
|
m.UseFirstPageNumberAttr = gooxml.Bool(false)
|
|
|
|
m.CopiesAttr = gooxml.Uint32(1)
|
2017-08-28 20:56:18 -05:00
|
|
|
for _, attr := range start.Attr {
|
|
|
|
if attr.Name.Local == "paperSize" {
|
|
|
|
parsed, err := strconv.ParseUint(attr.Value, 10, 32)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
pt := uint32(parsed)
|
|
|
|
m.PaperSizeAttr = &pt
|
|
|
|
}
|
|
|
|
if attr.Name.Local == "paperHeight" {
|
|
|
|
parsed, err := attr.Value, error(nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
m.PaperHeightAttr = &parsed
|
|
|
|
}
|
|
|
|
if attr.Name.Local == "paperWidth" {
|
|
|
|
parsed, err := attr.Value, error(nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
m.PaperWidthAttr = &parsed
|
|
|
|
}
|
|
|
|
if attr.Name.Local == "firstPageNumber" {
|
|
|
|
parsed, err := strconv.ParseUint(attr.Value, 10, 32)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
pt := uint32(parsed)
|
|
|
|
m.FirstPageNumberAttr = &pt
|
|
|
|
}
|
|
|
|
if attr.Name.Local == "orientation" {
|
|
|
|
m.OrientationAttr.UnmarshalXMLAttr(attr)
|
|
|
|
}
|
|
|
|
if attr.Name.Local == "blackAndWhite" {
|
|
|
|
parsed, err := strconv.ParseBool(attr.Value)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
m.BlackAndWhiteAttr = &parsed
|
|
|
|
}
|
|
|
|
if attr.Name.Local == "draft" {
|
|
|
|
parsed, err := strconv.ParseBool(attr.Value)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
m.DraftAttr = &parsed
|
|
|
|
}
|
|
|
|
if attr.Name.Local == "useFirstPageNumber" {
|
|
|
|
parsed, err := strconv.ParseBool(attr.Value)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
m.UseFirstPageNumberAttr = &parsed
|
|
|
|
}
|
|
|
|
if attr.Name.Local == "horizontalDpi" {
|
|
|
|
parsed, err := strconv.ParseInt(attr.Value, 10, 32)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
pt := int32(parsed)
|
|
|
|
m.HorizontalDpiAttr = &pt
|
|
|
|
}
|
|
|
|
if attr.Name.Local == "verticalDpi" {
|
|
|
|
parsed, err := strconv.ParseInt(attr.Value, 10, 32)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
pt := int32(parsed)
|
|
|
|
m.VerticalDpiAttr = &pt
|
|
|
|
}
|
|
|
|
if attr.Name.Local == "copies" {
|
|
|
|
parsed, err := strconv.ParseUint(attr.Value, 10, 32)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
pt := uint32(parsed)
|
|
|
|
m.CopiesAttr = &pt
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// skip any extensions we may find, but don't support
|
|
|
|
for {
|
|
|
|
tok, err := d.Token()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("parsing CT_PageSetup: %s", err)
|
|
|
|
}
|
|
|
|
if el, ok := tok.(xml.EndElement); ok && el.Name == start.Name {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2017-09-01 15:19:23 -05:00
|
|
|
|
|
|
|
// Validate validates the CT_PageSetup and its children
|
2017-08-28 20:56:18 -05:00
|
|
|
func (m *CT_PageSetup) Validate() error {
|
|
|
|
return m.ValidateWithPath("CT_PageSetup")
|
|
|
|
}
|
2017-09-01 15:19:23 -05:00
|
|
|
|
|
|
|
// ValidateWithPath validates the CT_PageSetup and its children, prefixing error messages with path
|
2017-08-28 20:56:18 -05:00
|
|
|
func (m *CT_PageSetup) ValidateWithPath(path string) error {
|
|
|
|
if m.PaperHeightAttr != nil {
|
|
|
|
if !sharedTypes.ST_PositiveUniversalMeasurePatternRe.MatchString(*m.PaperHeightAttr) {
|
|
|
|
return fmt.Errorf(`%s/m.PaperHeightAttr must match '%s' (have %v)`, path, sharedTypes.ST_PositiveUniversalMeasurePatternRe, *m.PaperHeightAttr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if m.PaperHeightAttr != nil {
|
|
|
|
if !sharedTypes.ST_UniversalMeasurePatternRe.MatchString(*m.PaperHeightAttr) {
|
|
|
|
return fmt.Errorf(`%s/m.PaperHeightAttr must match '%s' (have %v)`, path, sharedTypes.ST_UniversalMeasurePatternRe, *m.PaperHeightAttr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if m.PaperWidthAttr != nil {
|
|
|
|
if !sharedTypes.ST_PositiveUniversalMeasurePatternRe.MatchString(*m.PaperWidthAttr) {
|
|
|
|
return fmt.Errorf(`%s/m.PaperWidthAttr must match '%s' (have %v)`, path, sharedTypes.ST_PositiveUniversalMeasurePatternRe, *m.PaperWidthAttr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if m.PaperWidthAttr != nil {
|
|
|
|
if !sharedTypes.ST_UniversalMeasurePatternRe.MatchString(*m.PaperWidthAttr) {
|
|
|
|
return fmt.Errorf(`%s/m.PaperWidthAttr must match '%s' (have %v)`, path, sharedTypes.ST_UniversalMeasurePatternRe, *m.PaperWidthAttr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err := m.OrientationAttr.ValidateWithPath(path + "/OrientationAttr"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|