simple_riders.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. package stub
  2. import (
  3. "github.com/kpmy/odf/model"
  4. "github.com/kpmy/odf/xmlns"
  5. "github.com/kpmy/ypk/assert"
  6. "github.com/kpmy/ypk/halt"
  7. "image/color"
  8. "reflect"
  9. )
  10. type sr struct {
  11. base *sm
  12. pos model.Leaf
  13. eol bool
  14. this model.Leaf
  15. }
  16. func (r *sr) Base() model.Model {
  17. return r.base
  18. }
  19. func (r *sr) InitFrom(old model.Reader) {
  20. panic(126)
  21. }
  22. func (r *sr) Pos(p ...model.Leaf) model.Leaf {
  23. if len(p) == 1 {
  24. r.pos = p[0]
  25. if n, ok := r.pos.(model.Node); ok {
  26. r.eol = n.NofChild() == 0
  27. } else {
  28. r.eol = true
  29. }
  30. }
  31. return r.pos
  32. }
  33. func (r *sr) Read() model.Leaf {
  34. assert.For(r.pos != nil && !r.eol, 20)
  35. n, ok := r.pos.(model.Node)
  36. assert.For(ok, 21)
  37. idx := 0
  38. if r.this != nil {
  39. idx = n.IndexOf(r.this)
  40. idx++
  41. }
  42. if idx < n.NofChild() {
  43. r.this = n.Child(idx)
  44. } else {
  45. r.eol = true
  46. }
  47. return r.this
  48. }
  49. func (r *sr) Eol() bool {
  50. return r.eol
  51. }
  52. type sw struct {
  53. base *sm
  54. pos model.Leaf
  55. }
  56. func (w *sw) Base() model.Model {
  57. return w.base
  58. }
  59. func (w *sw) InitFrom(old model.Writer) {
  60. if old != nil {
  61. w.Pos(old.Pos())
  62. }
  63. }
  64. func (w *sw) Pos(p ...model.Leaf) model.Leaf {
  65. if len(p) == 1 {
  66. w.pos = p[0]
  67. }
  68. return w.pos
  69. }
  70. func thisNode(l model.Leaf) model.Node {
  71. if _n, ok := l.(model.Node); ok {
  72. switch n := _n.(type) {
  73. case *sn:
  74. return n
  75. case *root:
  76. return n.inner
  77. default:
  78. halt.As(100, reflect.TypeOf(n))
  79. }
  80. }
  81. return nil
  82. }
  83. func (w *sw) Write(l model.Leaf, after ...model.Leaf) {
  84. assert.For(l != nil, 20)
  85. assert.For(w.pos != nil, 21)
  86. var splitter model.Leaf
  87. split := false
  88. if len(after) == 1 {
  89. splitter = after[0]
  90. split = true
  91. }
  92. add := func(source []model.Leaf, x model.Leaf) (ret []model.Leaf) {
  93. var (
  94. front []model.Leaf
  95. tail []model.Leaf
  96. )
  97. switch {
  98. case split && splitter == nil:
  99. tail = source
  100. case split && splitter != nil:
  101. found := false
  102. for _, i := range source {
  103. if !found {
  104. front = append(front, i)
  105. found = i == splitter
  106. } else {
  107. tail = append(tail, i)
  108. }
  109. }
  110. default:
  111. front = source
  112. }
  113. ret = append(ret, front...)
  114. ret = append(ret, x)
  115. ret = append(ret, tail...)
  116. return
  117. }
  118. if _n, ok := w.pos.(model.Node); ok {
  119. switch n := _n.(type) {
  120. case *sn:
  121. n.children = add(n.children, l)
  122. l.Parent(n)
  123. case *root:
  124. n.inner.children = add(n.inner.children, l)
  125. l.Parent(n.inner)
  126. default:
  127. halt.As(100, reflect.TypeOf(n))
  128. }
  129. }
  130. }
  131. func (w *sw) Delete(l model.Leaf) {
  132. del := func(l []model.Leaf, x model.Leaf) (ret []model.Leaf) {
  133. for _, i := range l {
  134. if i != x {
  135. ret = append(ret, i)
  136. }
  137. }
  138. return
  139. }
  140. assert.For(l != nil, 20)
  141. assert.For(l.Parent() == thisNode(w.pos), 21, l.Parent(), w.pos.(model.Node))
  142. switch n := thisNode(w.pos).(type) {
  143. case *sn:
  144. n.children = del(n.children, l)
  145. case *root:
  146. n.inner.children = del(n.inner.children, l)
  147. default:
  148. halt.As(100, reflect.TypeOf(n))
  149. }
  150. }
  151. func (w *sw) WritePos(l model.Leaf, after ...model.Leaf) model.Leaf {
  152. w.Write(l, after...)
  153. return w.Pos(l)
  154. }
  155. func validateAttr(n model.AttrName, val string) {
  156. values := xmlns.Enums[n]
  157. found := false
  158. for _, v := range values {
  159. if v == val {
  160. found = true
  161. }
  162. }
  163. assert.For(found, 60, n, val)
  164. }
  165. func castAttr(n model.AttrName, i interface{}) (ret model.Attribute) {
  166. if i == nil {
  167. return nil
  168. }
  169. typ := xmlns.Typed[n]
  170. switch typ {
  171. case xmlns.NONE, xmlns.STRING:
  172. ret = &StringAttr{Value: i.(string)}
  173. case xmlns.INT:
  174. ret = &IntAttr{Value: i.(int)}
  175. case xmlns.ENUM:
  176. validateAttr(n, i.(string))
  177. ret = &StringAttr{Value: i.(string)}
  178. case xmlns.MEASURE:
  179. ret = &MeasureAttr{Value: i.(float64)}
  180. case xmlns.COLOR:
  181. ret = &ColorAttr{Value: i.(color.Color)}
  182. case xmlns.BOOL:
  183. ret = &BoolAttr{Value: i.(bool)}
  184. default:
  185. halt.As(100, typ, reflect.TypeOf(i))
  186. }
  187. return ret
  188. }
  189. func (w *sw) Attr(n model.AttrName, val interface{}) model.Writer {
  190. w.pos.Attr(n, castAttr(n, val))
  191. return w
  192. }