commit c98a5e822c5097481fb88dc86b72f9284c917096 Author: huangsimin Date: Fri Nov 29 18:30:20 2019 +0800 123 diff --git a/1.xml b/1.xml new file mode 100644 index 0000000..daa4792 --- /dev/null +++ b/1.xml @@ -0,0 +1,755 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..708f270 --- /dev/null +++ b/go.mod @@ -0,0 +1,15 @@ +module ggraph + +go 1.13 + +require ( + github.com/474420502/focus v0.7.0 + github.com/antchfx/xpath v1.1.2 + github.com/davecgh/go-spew v1.1.1 + github.com/google/uuid v1.1.1 // indirect + github.com/lestrrat-go/libxml2 v0.0.0-20191008001243-8ad9bf187c39 + github.com/lithammer/shortuuid v3.0.0+incompatible // indirect + github.com/pkg/errors v0.8.1 + github.com/stretchr/testify v1.4.0 // indirect + gopkg.in/xmlpath.v1 v1.0.0-20140413065638-a146725ea6e7 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..2ae8d76 --- /dev/null +++ b/go.sum @@ -0,0 +1,27 @@ +github.com/474420502/focus v0.7.0 h1:X0BMIcUY/x6Y/Tpdf8ak72u402fImKoxbV1atUhN68o= +github.com/474420502/focus v0.7.0/go.mod h1:Rnsdajih5yVIPvDg0tpQjVdEYz+zCZRjlzGfk+Sx3q8= +github.com/Pallinder/go-randomdata v1.1.0/go.mod h1:yHmJgulpD2Nfrm0cR9tI/+oAgRqCQQixsA8HyRZfV9Y= +github.com/antchfx/xpath v1.1.2 h1:YziPrtM0gEJBnhdUGxYcIVYXZ8FXbtbovxOi+UW/yWQ= +github.com/antchfx/xpath v1.1.2/go.mod h1:Yee4kTMuNiPYJ7nSNorELQMr1J33uOpXDMByNYhvtNk= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/emirpasic/gods v1.12.0/go.mod h1:YfzfFFoVP/catgzJb4IKIqXjX78Ha8FMSDh3ymbK86o= +github.com/google/uuid v1.1.1 h1:Gkbcsh/GbpXz7lPftLA3P6TYMwjCLYm83jiFQZF/3gY= +github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/lestrrat-go/libxml2 v0.0.0-20191008001243-8ad9bf187c39 h1:1WgaDmERN0G8DJlrvb8Q2/CGs4wS90YkcpWiyzXU7pg= +github.com/lestrrat-go/libxml2 v0.0.0-20191008001243-8ad9bf187c39/go.mod h1:fy/ZVbgyB83mtricxwSW3zqIRXWOVpKG2PvdUDFeC58= +github.com/lithammer/shortuuid v3.0.0+incompatible h1:NcD0xWW/MZYXEHa6ITy6kaXN5nwm/V115vj2YXfhS0w= +github.com/lithammer/shortuuid v3.0.0+incompatible/go.mod h1:FR74pbAuElzOUuenUHTK2Tciko1/vKuIKS9dSkDrA4w= +github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I= +github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk= +github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/xmlpath.v1 v1.0.0-20140413065638-a146725ea6e7 h1:zibSPXbkfB1Dwl76rJgLa68xcdHu42qmFTe6vAnU4wA= +gopkg.in/xmlpath.v1 v1.0.0-20140413065638-a146725ea6e7/go.mod h1:wo0SW5T6XqIKCCAge330Cd5sm+7VI6v85OrQHIk50KM= +gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= +gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/graph.go b/graph.go new file mode 100644 index 0000000..50b04c9 --- /dev/null +++ b/graph.go @@ -0,0 +1,151 @@ +package graph + +import ( + "errors" + "log" +) + +var attributeNameLaw map[rune]struct{} = make(map[rune]struct{}) + +func init() { + for _, c := range "abcdefghijklmnopqrstuvwsyzABCDEFGHIJKLMNOPQRSTUVWSYZ-_" { + attributeNameLaw[c] = struct{}{} + } +} + +type Range struct { + Start int + End int +} + +type ElementTag struct { + Range +} + +type Element struct { + Range + Tag *ElementTag + Attributes []*Attribute + Source []rune + Children []*Element +} + +type Attribute struct { + Range + Key Range + Value Range +} + +func (e *Element) GetTagName() string { + return string(e.Source[e.Tag.Start:e.Tag.End]) +} + +func GetTag(source []rune, cur int) (*ElementTag, int) { + + for i := cur; i < len(source); i++ { + c := source[i] + if c == ' ' { + return &ElementTag{Range{cur, i - 1}}, i + } + } + + return nil, -1 +} + +func SkipAttribute(source []rune, cur int) int { + for i := cur; i < len(source); i++ { + c := source[i] + if c == ' ' || c == '>' { + return i - 1 + } + } + panic(errors.New("over the range of the next attribute")) +} + +func GetAttribute(source []rune, cur int) *Range { + +} + +func GetAttributes(source []rune, cur int) []*Attribute { + + var attrs []*Attribute + +LOOP_TOP: + for i := cur; i < len(source); i++ { + + c := source[i] + switch c { + case '>': + break LOOP_TOP + case '/': + if source[i+1] == '>' { + break LOOP_TOP + } + case ' ': + continue + default: + + attr := &Attribute{} + attr.Range.Start = i + + for ; i < len(source); i++ { + c = source[i] + if _, ok := attributeNameLaw[c]; !ok { + i = SkipAttribute(source, i) + break + } else { + if c == ' ' || c == '>' { + attr.Range.End = i + break + } + } + } + + } + + } + + return attrs +} + +func ParseChild(parent *Element, cur int) *Element { + + start := cur + end := start + + child := &Element{} + child.Tag, cur = GetTag(parent.Source, start) + GetAttributes(parent.Source, cur) + + for i := cur; i < len(parent.Source); i++ { + + c := parent.Source[i] + if c == '>' { + end = i - 1 + return child + } + + } + + return nil +} + +func GetChildren(parent *Element) { + // GetChild + for i := parent.Range.Start; i < parent.Range.End; i++ { + c := parent.Source[i] + + if c == '<' { + tag := ParseChild(parent, i) + log.Println(tag) + } + } +} + +func ParseXML(data []byte) *Element { + source := []rune(string(data)) + + root := &Element{tag: nil, Source: source, Pos: Range{0, len(source)}} + + return root +} diff --git a/graph_test.go b/graph_test.go new file mode 100644 index 0000000..b9d7e58 --- /dev/null +++ b/graph_test.go @@ -0,0 +1,19 @@ +package graph + +import ( + "io/ioutil" + "os" + "testing" +) + +func Test1(t *testing.T) { + f, err := os.Open("1.xml") + if err != nil { + panic(err) + } + data, err := ioutil.ReadAll(f) + if err != nil { + panic(err) + } + ParseXML(data) +} diff --git a/xml_test.go b/xml_test.go new file mode 100644 index 0000000..3042760 --- /dev/null +++ b/xml_test.go @@ -0,0 +1,111 @@ +package graph + +import ( + "io/ioutil" + "log" + "os" + "testing" + + "github.com/lestrrat-go/libxml2" + "github.com/lestrrat-go/libxml2/types" + "github.com/lestrrat-go/libxml2/xpath" + "github.com/pkg/errors" +) + +func checkRune(c rune, i int) { + +} + +func TestLibxml(t *testing.T) { + f, err := os.Open("1.xml") + if err != nil { + panic(err) + } + data, err := ioutil.ReadAll(f) + if err != nil { + panic(err) + } + doc, err := libxml2.Parse(data) + if err != nil { + panic(err) + } + + root, err := doc.DocumentElement() + if err != nil { + panic(err) + } + cxt, err := xpath.NewContext(root) + if err != nil { + panic(err) + } + result, err := cxt.Find("//diagram") + if err != nil { + panic(err) + } + + diagrams := result.NodeIter() + + var mdiagram types.Node + + for diagrams.Next() { + + diagram := diagrams.Node() + result, err = diagram.Find("./mxGraphModel/root") + if err != nil { + panic(err) + } + + rootiter := result.NodeIter() + if rootiter.Next() { + root := rootiter.Node() + // id 冲突 + if mdiagram == nil { + mdiagram = root + } else { + + cells, err := mdiagram.ChildNodes() + errors.Cause(err) + // t.Error(cells) + + first := cells.First() + for first != nil { + + err = mdiagram.AddChild(first) + + if err != nil { + log.Println(err) + } + + first, err = first.NextSibling() + if err != nil { + log.Println(err) + } + + } + + } + } + + } + + t.Error(mdiagram) + + // ndoc := dom.CreateDocument() + // doc.CreateElement("") + // t.Error(spew.Sprint(result.NodeList())) +} + +func TestXML(t *testing.T) { + // f, err := os.Open("1.xml") + // if err != nil { + // panic(err) + // } + // buf, err := ioutil.ReadAll(f) + + // if err != nil { + // panic(err) + // } + + // data := []rune(string(buf)) + +}