1
0
kpmy 10 жил өмнө
parent
commit
2d2c01dbbf

+ 13 - 0
LICENSE

@@ -0,0 +1,13 @@
+           DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
+                   Version 2, December 2004
+
+Copyright (C) 2004 Sam Hocevar <sam@hocevar.net>
+
+Everyone is permitted to copy and distribute verbatim or modified
+copies of this license document, and changing it is allowed as long
+as the name is changed.
+
+           DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
+  TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
+
+ 0. You just DO WHAT THE FUCK YOU WANT TO.

+ 19 - 0
mappers/attr.go

@@ -0,0 +1,19 @@
+package mappers
+
+import (
+	"odf/model"
+	"odf/xmlns/office"
+)
+
+type Attr struct {
+	doc model.Model
+	ds  model.Leaf //document styles
+}
+
+func (a *Attr) Init(m model.Model) {
+	a.doc = m
+	wr := a.doc.NewWriter()
+	wr.Pos(a.doc.Root())
+	a.ds = wr.WritePos(New(office.DocumentStyles))
+	wr.Attr(office.Version, "1.0")
+}

+ 34 - 0
mappers/fmt.go

@@ -0,0 +1,34 @@
+package mappers
+
+import (
+	"odf/model"
+	"odf/xmlns"
+	"ypk/assert"
+)
+
+var New func(name model.LeafName) model.Leaf
+
+type Formatter struct {
+	m        model.Model
+	inner    model.Writer
+	MimeType xmlns.Mime
+	attr     *Attr
+}
+
+func (f *Formatter) ConnectTo(m model.Model) {
+	assert.For(m.Root().NofChild() == 0, 20, "only new documents for now")
+	f.m = m
+	f.inner = f.m.NewWriter()
+	f.attr = &Attr{}
+}
+
+func (f *Formatter) Init() {
+	assert.For(f.MimeType != "", 20)
+	wr := f.m.NewWriter()
+	wr.Pos(f.m.Root())
+	f.attr.Init(f.m)
+}
+
+func init() {
+	New = model.LeafFactory
+}

+ 6 - 0
model/c.go

@@ -0,0 +1,6 @@
+package model
+
+type AttrName string
+type LeafName string
+
+var LeafFactory func(LeafName) Leaf

+ 9 - 0
model/stub/simple_attrs.go

@@ -0,0 +1,9 @@
+package stub
+
+type StringAttr struct {
+	Value string
+}
+
+func (a *StringAttr) String() string {
+	return a.Value
+}

+ 23 - 0
model/stub/simple_nodes.go

@@ -0,0 +1,23 @@
+package stub
+
+import (
+	"odf/model"
+	"ypk/assert"
+)
+
+func lf() func(x model.LeafName) model.Leaf {
+	return func(x model.LeafName) model.Leaf {
+		var ret *sn
+		switch x {
+		default:
+			ret = &sn{name: x}
+			ret.init()
+		}
+		assert.For(ret != nil, 60)
+		return ret
+	}
+}
+
+func init() {
+	model.LeafFactory = lf()
+}

+ 61 - 0
model/stub/simple_riders.go

@@ -0,0 +1,61 @@
+package stub
+
+import (
+	"odf/model"
+	"odf/xmlns"
+	"reflect"
+	"ypk/assert"
+	"ypk/halt"
+)
+
+type sw struct {
+	base *sm
+	pos  model.Leaf
+}
+
+func (w *sw) Base() model.Model {
+	return w.base
+}
+
+func (w *sw) InitFrom(old model.Writer) {
+	panic(126)
+}
+
+func (w *sw) Pos(p ...model.Leaf) model.Leaf {
+	if len(p) == 1 {
+		w.pos = p[0]
+	}
+	return w.pos
+}
+
+func (w *sw) Write(l model.Leaf) {
+	assert.For(l != nil, 20)
+	assert.For(w.pos != nil, 21)
+	if _n, ok := w.pos.(model.Node); ok {
+		if n, da := _n.(*sn); da {
+			n.children = append(n.children, l)
+		} else {
+			halt.As(100, reflect.TypeOf(n))
+		}
+	}
+}
+
+func (w *sw) WritePos(l model.Leaf) model.Leaf {
+	w.Write(l)
+	return w.Pos(l)
+}
+
+func castAttr(n model.AttrName, i interface{}) (ret model.Attribute) {
+	typ := xmlns.Typed[n]
+	switch typ {
+	case xmlns.NONE, xmlns.STRING:
+		ret = &StringAttr{Value: i.(string)}
+	default:
+		halt.As(100, typ, reflect.TypeOf(i))
+	}
+	return ret
+}
+
+func (w *sw) Attr(n model.AttrName, val interface{}) {
+	w.pos.Attr(n, castAttr(n, val))
+}

+ 85 - 0
model/stub/simple_tree.go

@@ -0,0 +1,85 @@
+package stub
+
+/*
+	простая реализация модели документа
+*/
+
+import (
+	"odf/model"
+	"ypk/assert"
+)
+
+type sn struct {
+	name     model.LeafName
+	attr     map[model.AttrName]model.Attribute
+	children []model.Leaf
+}
+
+func (n *sn) Attr(name model.AttrName, val ...model.Attribute) model.Attribute {
+	assert.For(len(val) <= 1, 20, "only one attribute accepted")
+	assert.For(name != "", 21)
+	if len(val) == 1 {
+		n.attr[name] = val[0]
+	}
+	return n.attr[name]
+}
+
+func (n *sn) Child(i int) model.Leaf {
+	assert.For(i < len(n.children), 20)
+	return n.children[i]
+}
+
+func (n *sn) IndexOf(l model.Leaf) (ret int) {
+	ret = -1
+	for i := 0; i < len(n.children) && ret == -1; i++ {
+		if l == n.children[i] {
+			ret = i
+		}
+	}
+	return
+}
+
+func (n *sn) NofChild() int {
+	return len(n.children)
+}
+
+func (n *sn) Name() model.LeafName {
+	return n.name
+}
+
+func (n *sn) init() {
+	n.attr = make(map[model.AttrName]model.Attribute)
+	n.children = make([]model.Leaf, 0)
+}
+
+type sm struct {
+	root *sn
+}
+
+func (m *sm) NewReader(...model.Reader) model.Reader {
+	return nil
+}
+
+func (m *sm) NewWriter(old ...model.Writer) model.Writer {
+	w := &sw{base: m}
+	if len(old) == 1 {
+		w.InitFrom(old[0])
+	}
+	return w
+}
+
+func (m *sm) Root() model.Node {
+	return m.root
+}
+
+func nf() func() model.Model {
+	return func() model.Model {
+		r := &sn{}
+		r.init()
+		return &sm{root: r}
+	}
+}
+
+func init() {
+	model.ModelFactory = nf()
+}

+ 39 - 0
model/tree.go

@@ -0,0 +1,39 @@
+package model
+
+type Attribute interface {
+	String() string
+}
+
+type Leaf interface {
+	Name() LeafName
+	Attr(AttrName, ...Attribute) Attribute
+}
+
+type Node interface {
+	Leaf
+	Child(int) Leaf
+	IndexOf(Leaf) int
+	NofChild() int
+}
+
+type Model interface {
+	Root() Node
+	NewReader(...Reader) Reader
+	NewWriter(...Writer) Writer
+}
+
+type Reader interface {
+	InitFrom(Reader)
+	Base() Model
+}
+
+type Writer interface {
+	InitFrom(Writer)
+	Base() Model
+	Pos(...Leaf) Leaf
+	Write(Leaf)
+	WritePos(Leaf) Leaf
+	Attr(AttrName, interface{})
+}
+
+var ModelFactory func() Model

+ 28 - 0
odf_test.go

@@ -0,0 +1,28 @@
+package odf
+
+import (
+	"odf/mappers"
+	"odf/model"
+	_ "odf/model/stub"
+	"odf/xmlns"
+	"testing"
+)
+
+func TestModel(t *testing.T) {
+	m := model.ModelFactory()
+	if m == nil {
+		t.Error("model is nil")
+	}
+	w := m.NewWriter()
+	if w == nil {
+		t.Error("writer is nil")
+	}
+}
+
+func TestMappers(t *testing.T) {
+	m := model.ModelFactory()
+	fm := &mappers.Formatter{}
+	fm.ConnectTo(m)
+	fm.MimeType = xmlns.MimeText
+	fm.Init()
+}

+ 13 - 0
xmlns/office/office.go

@@ -0,0 +1,13 @@
+package office
+
+import (
+	"odf/model"
+)
+
+const (
+	DocumentStyles model.LeafName = "office:document-styles"
+)
+
+const (
+	Version model.AttrName = "office:version"
+)

+ 24 - 0
xmlns/x.go

@@ -0,0 +1,24 @@
+package xmlns
+
+import (
+	"odf/model"
+)
+
+type AttrType int
+
+const (
+	NONE AttrType = iota
+	STRING
+)
+
+type Mime string
+
+const (
+	MimeText Mime = "application/vnd.oasis.opendocument.text"
+)
+
+var Typed map[model.AttrName]AttrType
+
+func init() {
+	Typed = make(map[model.AttrName]AttrType)
+}