simple_nodes.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. package stub
  2. import (
  3. "encoding/xml"
  4. "github.com/kpmy/odf/model"
  5. "github.com/kpmy/odf/xmlns"
  6. "github.com/kpmy/odf/xmlns/office"
  7. "github.com/kpmy/odf/xmlns/urn"
  8. "github.com/kpmy/ypk/assert"
  9. )
  10. type root struct {
  11. inner *sn
  12. }
  13. func (r *root) Name() model.LeafName {
  14. return r.inner.Name()
  15. }
  16. func (r *root) Attr(n model.AttrName, a ...model.Attribute) model.Attribute {
  17. return r.inner.Attr(n, a...)
  18. }
  19. func (r *root) Child(i int) model.Leaf {
  20. return r.inner.Child(i)
  21. }
  22. func (r *root) IndexOf(l model.Leaf) int {
  23. return r.inner.IndexOf(l)
  24. }
  25. func (r *root) NofChild() int {
  26. return r.inner.NofChild()
  27. }
  28. func (r *root) Parent(...model.Node) model.Node { return nil }
  29. type text struct {
  30. data string
  31. parent model.Node
  32. }
  33. func (t *text) Name() model.LeafName { panic(100) }
  34. func (t *text) Attr(model.AttrName, ...model.Attribute) model.Attribute { panic(100) }
  35. func (t *text) Parent(p ...model.Node) model.Node {
  36. if len(p) == 1 {
  37. assert.For(t.parent == nil, 20)
  38. t.parent = p[0]
  39. }
  40. return t.parent
  41. }
  42. func (r *root) MarshalXML(e *xml.Encoder, start xml.StartElement) (err error) {
  43. start.Name.Local = string(r.inner.Name())
  44. start.Attr = append(start.Attr, xml.Attr{Name: xml.Name{Local: xmlns.NSanim}, Value: urn.Anim})
  45. start.Attr = append(start.Attr, xml.Attr{Name: xml.Name{Local: xmlns.NSchart}, Value: urn.Chart})
  46. start.Attr = append(start.Attr, xml.Attr{Name: xml.Name{Local: xmlns.NSconfig}, Value: urn.Config})
  47. start.Attr = append(start.Attr, xml.Attr{Name: xml.Name{Local: xmlns.NSdc}, Value: urn.Dc})
  48. start.Attr = append(start.Attr, xml.Attr{Name: xml.Name{Local: xmlns.NSdr3d}, Value: urn.Dr3d})
  49. start.Attr = append(start.Attr, xml.Attr{Name: xml.Name{Local: xmlns.NSdraw}, Value: urn.Draw})
  50. start.Attr = append(start.Attr, xml.Attr{Name: xml.Name{Local: xmlns.NSfo}, Value: urn.Fo})
  51. start.Attr = append(start.Attr, xml.Attr{Name: xml.Name{Local: xmlns.NSform}, Value: urn.Form})
  52. start.Attr = append(start.Attr, xml.Attr{Name: xml.Name{Local: xmlns.NSmath}, Value: urn.Math})
  53. start.Attr = append(start.Attr, xml.Attr{Name: xml.Name{Local: xmlns.NSmeta}, Value: urn.Meta})
  54. start.Attr = append(start.Attr, xml.Attr{Name: xml.Name{Local: xmlns.NSnumber}, Value: urn.Number})
  55. start.Attr = append(start.Attr, xml.Attr{Name: xml.Name{Local: xmlns.NSoffice}, Value: urn.Office})
  56. start.Attr = append(start.Attr, xml.Attr{Name: xml.Name{Local: xmlns.NSpresentation}, Value: urn.Presentation})
  57. start.Attr = append(start.Attr, xml.Attr{Name: xml.Name{Local: xmlns.NSscript}, Value: urn.Script})
  58. start.Attr = append(start.Attr, xml.Attr{Name: xml.Name{Local: xmlns.NSsmil}, Value: urn.Smil})
  59. start.Attr = append(start.Attr, xml.Attr{Name: xml.Name{Local: xmlns.NSstyle}, Value: urn.Style})
  60. start.Attr = append(start.Attr, xml.Attr{Name: xml.Name{Local: xmlns.NSsvg}, Value: urn.Svg})
  61. start.Attr = append(start.Attr, xml.Attr{Name: xml.Name{Local: xmlns.NStable}, Value: urn.Table})
  62. start.Attr = append(start.Attr, xml.Attr{Name: xml.Name{Local: xmlns.NStext}, Value: urn.Text})
  63. start.Attr = append(start.Attr, xml.Attr{Name: xml.Name{Local: xmlns.NSxforms}, Value: urn.Xforms})
  64. start.Attr = append(start.Attr, xml.Attr{Name: xml.Name{Local: xmlns.NSxlink}, Value: urn.Xlink})
  65. err = e.EncodeElement(r.inner, start)
  66. return err
  67. }
  68. func lf() func(x model.LeafName) model.Leaf {
  69. return func(x model.LeafName) (ret model.Leaf) {
  70. switch x {
  71. case office.DocumentContent, office.DocumentMeta, office.DocumentStyles:
  72. r := &root{}
  73. r.inner = &sn{name: x}
  74. r.inner.init()
  75. ret = r
  76. default:
  77. r := &sn{name: x}
  78. r.init()
  79. ret = r
  80. }
  81. assert.For(ret != nil, 60)
  82. return ret
  83. }
  84. }
  85. func tf() func(s string) model.Leaf {
  86. return func(s string) model.Leaf {
  87. return &text{data: s}
  88. }
  89. }
  90. func init() {
  91. model.LeafFactory = lf()
  92. model.Text = tf()
  93. }