Преглед на файлове

добавил запись таблицы

kpmy преди 10 години
родител
ревизия
4d4ca7e9ab
променени са 7 файла, в които са добавени 192 реда и са изтрити 11 реда
  1. 12 4
      mappers/fmt.go
  2. 114 0
      mappers/table.go
  3. 37 5
      model/stub/simple_riders.go
  4. 2 2
      model/tree.go
  5. 10 0
      odf_test.go
  6. 1 0
      xmlns/office/o.go
  7. 16 0
      xmlns/table/t.go

+ 12 - 4
mappers/fmt.go

@@ -8,6 +8,7 @@ import (
 	"odf/xmlns/text"
 	"reflect"
 	"ypk/assert"
+	"ypk/halt"
 )
 
 var New func(name model.LeafName) model.Leaf
@@ -17,7 +18,7 @@ type Formatter struct {
 	rider    model.Writer
 	MimeType xmlns.Mime
 	attr     *Attr
-	text     model.Node
+	root     model.Node //root of document content, not model
 	ready    bool
 }
 
@@ -40,8 +41,15 @@ func (f *Formatter) Init() {
 	wr.Write(f.attr.ffdc)
 	wr.Write(f.attr.asc)
 	wr.WritePos(New(office.Body))
-	f.text = wr.WritePos(New(office.Text)).(model.Node)
-	f.rider.Pos(f.text)
+	switch f.MimeType {
+	case xmlns.MimeText:
+		f.root = wr.WritePos(New(office.Text)).(model.Node)
+	case xmlns.MimeSpreadsheet:
+		f.root = wr.WritePos(New(office.Spreadsheet)).(model.Node)
+	default:
+		halt.As(100, f.MimeType)
+	}
+	f.rider.Pos(f.root)
 	f.ready = true
 }
 
@@ -55,7 +63,7 @@ func (f *Formatter) WritePara(s string) {
 	assert.For(f.ready, 20)
 	assert.For(f.MimeType == xmlns.MimeText, 21)
 	if pos := f.rider.Pos(); pos.Name() != office.Text || pos.Name() == text.P {
-		f.rider.Pos(f.text)
+		f.rider.Pos(f.root)
 	}
 	f.rider.WritePos(New(text.P))
 	f.writeAttr()

+ 114 - 0
mappers/table.go

@@ -0,0 +1,114 @@
+package mappers
+
+import (
+	"odf/model"
+	"odf/xmlns/table"
+	"ypk/assert"
+)
+
+type Table struct {
+	Rows                int
+	Columns             int
+	Root                model.Leaf
+	rowCache, colsCache []model.Leaf
+	cellCache           [][]model.Leaf
+}
+
+type TableMapper struct {
+	List map[string]*Table
+	fm   *Formatter
+}
+
+func (t *TableMapper) Ready() bool {
+	return t.fm != nil && t.fm.ready
+}
+
+func (t *TableMapper) rider() model.Writer {
+	return t.fm.rider
+}
+
+func (t *TableMapper) newWriter(old ...model.Writer) model.Writer {
+	return t.fm.m.NewWriter(old...)
+}
+
+func (t *TableMapper) ConnectTo(fm *Formatter) {
+	t.fm = fm
+	t.List = make(map[string]*Table)
+}
+
+func (t *TableMapper) Write(name string, rows, cols int) {
+	assert.For(t.Ready(), 20)
+	assert.For(name != "" && t.List[name] == nil, 21)
+	t.fm.writeAttr()
+	this := &Table{Rows: rows, Columns: cols}
+	t.List[name] = this
+	wr := t.newWriter(t.rider())
+	this.Root = wr.WritePos(New(table.Table))
+	wr.Attr(table.Name, name)
+	for i := 0; i < this.Columns; i++ {
+		col := New(table.TableColumn)
+		this.colsCache = append(this.colsCache, col)
+		this.cellCache = append(this.cellCache, make([]model.Leaf, 0))
+		wr.Write(col)
+	}
+	for i := 0; i < this.Rows; i++ {
+		rwr := t.newWriter(wr)
+		row := rwr.WritePos(New(table.TableRow))
+		this.rowCache = append(this.rowCache, row)
+		for j := 0; j < this.Columns; j++ {
+			cell := New(table.TableCell)
+			this.cellCache[j] = append(this.cellCache[j], cell)
+			rwr.Write(cell)
+		}
+	}
+}
+
+func (t *TableMapper) WriteRows(this *Table, rows int) {
+	assert.For(t.Ready(), 20)
+	t.fm.writeAttr()
+	wr := t.newWriter()
+	for i := 0; i < rows; i++ {
+		wr.Pos(this.Root)
+		row := wr.WritePos(New(table.TableRow))
+		this.rowCache = append(this.rowCache, row)
+		for j := 0; j < this.Columns; j++ {
+			cell := New(table.TableCell)
+			this.cellCache[j] = append(this.cellCache[j], cell)
+			wr.Write(cell)
+		}
+		this.Rows++
+	}
+}
+
+func (t *TableMapper) WriteColumns(this *Table, cols int) {
+	assert.For(t.Ready(), 20)
+	t.fm.writeAttr()
+	wr := t.newWriter()
+	var last model.Leaf
+	if this.Columns > 0 {
+		last = this.colsCache[this.Columns-1]
+	}
+	for i := 0; i < cols; i++ {
+		wr.Pos(this.Root)
+		col := wr.WritePos(New(table.TableColumn), last)
+		this.colsCache = append(this.colsCache, col)
+		this.cellCache = append(this.cellCache, make([]model.Leaf, 0))
+		this.Columns++
+		for j := 0; j < this.Rows; j++ {
+			t.WriteCells(this, j, 1)
+		}
+	}
+}
+
+func (t *TableMapper) WriteCells(this *Table, _row int, cells int) {
+	assert.For(t.Ready(), 20)
+	t.fm.writeAttr()
+	wr := t.newWriter()
+	row := this.rowCache[_row]
+	wr.Pos(row)
+	for i := 0; i < cells; i++ {
+		cell := New(table.TableCell)
+		this.cellCache[i] = append(this.cellCache[i], cell)
+		wr.Write(cell)
+	}
+}

+ 37 - 5
model/stub/simple_riders.go

@@ -92,16 +92,48 @@ func thisNode(l model.Leaf) model.Node {
 	return nil
 }
 
-func (w *sw) Write(l model.Leaf) {
+func (w *sw) Write(l model.Leaf, after ...model.Leaf) {
 	assert.For(l != nil, 20)
 	assert.For(w.pos != nil, 21)
+	var splitter model.Leaf
+	split := false
+	if len(after) == 1 {
+		splitter = after[0]
+		split = true
+	}
+	add := func(source []model.Leaf, x model.Leaf) (ret []model.Leaf) {
+		var (
+			front []model.Leaf
+			tail  []model.Leaf
+		)
+		switch {
+		case split && splitter == nil:
+			tail = source
+		case split && splitter != nil:
+			found := false
+			for _, i := range source {
+				if !found {
+					front = append(front, i)
+					found = i == splitter
+				} else {
+					tail = append(tail, i)
+				}
+			}
+		default:
+			front = source
+		}
+		ret = append(ret, front...)
+		ret = append(ret, x)
+		ret = append(ret, tail...)
+		return
+	}
 	if _n, ok := w.pos.(model.Node); ok {
 		switch n := _n.(type) {
 		case *sn:
-			n.children = append(n.children, l)
+			n.children = add(n.children, l)
 			l.Parent(n)
 		case *root:
-			n.inner.children = append(n.inner.children, l)
+			n.inner.children = add(n.inner.children, l)
 			l.Parent(n.inner)
 		default:
 			halt.As(100, reflect.TypeOf(n))
@@ -131,8 +163,8 @@ func (w *sw) Delete(l model.Leaf) {
 
 }
 
-func (w *sw) WritePos(l model.Leaf) model.Leaf {
-	w.Write(l)
+func (w *sw) WritePos(l model.Leaf, after ...model.Leaf) model.Leaf {
+	w.Write(l, after...)
 	return w.Pos(l)
 }
 

+ 2 - 2
model/tree.go

@@ -35,8 +35,8 @@ type Writer interface {
 	InitFrom(Writer)
 	Base() Model
 	Pos(...Leaf) Leaf
-	Write(Leaf)
-	WritePos(Leaf) Leaf
+	Write(Leaf, ...Leaf)
+	WritePos(Leaf, ...Leaf) Leaf
 	Attr(AttrName, interface{})
 	Delete(Leaf)
 }

+ 10 - 0
odf_test.go

@@ -81,6 +81,14 @@ func TestStylesMechanism(t *testing.T) {
 }
 
 func TestTables(t *testing.T) {
+	table := func(fm *mappers.Formatter) {
+		tm := &mappers.TableMapper{}
+		tm.ConnectTo(fm)
+		tm.Write("test", 5, 10)
+		tt := tm.List["test"]
+		tm.WriteColumns(tt, 4)
+		tm.WriteRows(tt, 3)
+	}
 	{
 		output, _ := os.OpenFile("test3.odf", os.O_CREATE|os.O_WRONLY, 0666)
 		m := model.ModelFactory()
@@ -88,6 +96,7 @@ func TestTables(t *testing.T) {
 		fm.ConnectTo(m)
 		fm.MimeType = xmlns.MimeText
 		fm.Init()
+		table(fm)
 		generators.Generate(m, output, fm.MimeType)
 		assert.For(output.Close() == nil, 20)
 	}
@@ -98,6 +107,7 @@ func TestTables(t *testing.T) {
 		fm.ConnectTo(m)
 		fm.MimeType = xmlns.MimeSpreadsheet
 		fm.Init()
+		table(fm)
 		generators.Generate(m, output, fm.MimeType)
 		assert.For(output.Close() == nil, 20)
 	}

+ 1 - 0
xmlns/office/o.go

@@ -13,6 +13,7 @@ const (
 	DocumentContent model.LeafName = "office:document-content"
 	Body            model.LeafName = "office:body"
 	Text            model.LeafName = "office:text"
+	Spreadsheet     model.LeafName = "office:spreadsheet"
 	Styles          model.LeafName = "office:styles"
 )
 

+ 16 - 0
xmlns/table/t.go

@@ -0,0 +1,16 @@
+package table
+
+import (
+	"odf/model"
+)
+
+const (
+	Table       model.LeafName = "table:table"
+	TableColumn model.LeafName = "table:table-column"
+	TableRow    model.LeafName = "table:table-row"
+	TableCell   model.LeafName = "table:table-cell"
+)
+
+const (
+	Name model.AttrName = "table:name"
+)