1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
|
// Copyright ©2018 The Gonum Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package gexf12 implements marshaling and unmarshaling of GEXF1.2 documents.
//
// For details of GEXF see https://gephi.org/gexf/format/.
package gexf12 // import "gonum.org/v1/gonum/graph/formats/gexf12"
import (
"bytes"
"encoding/xml"
"time"
)
// BUG(kortschak): The namespace for GEFX1.2 is 1.2draft, though it has
// already been deprecated. There is no specification for 1.3, although
// it is being used in the wild.
// Content holds a GEFX graph and metadata.
type Content struct {
XMLName xml.Name `xml:"http://www.gexf.net/1.2draft gexf"`
Meta *Meta `xml:"meta,omitempty"`
Graph Graph `xml:"graph"`
// Version must be "1.2".
Version string `xml:"version,attr"`
Variant string `xml:"variant,attr,omitempty"`
}
// Meta holds optional metadata associated with the graph.
type Meta struct {
Creator string `xml:"creator,omitempty"`
Keywords string `xml:"keywords,omitempty"`
Description string `xml:"description,omitempty"`
LastModified time.Time `xml:"lastmodifieddate,attr,omitempty"`
}
// MarshalXML implements the xml.Marshaler interface.
func (t *Meta) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
type T Meta
var layout struct {
*T
LastModified *xsdDate `xml:"lastmodifieddate,attr,omitempty"`
}
layout.T = (*T)(t)
layout.LastModified = (*xsdDate)(&layout.T.LastModified)
return e.EncodeElement(layout, start)
}
// UnmarshalXML implements the xml.Unmarshaler interface.
func (t *Meta) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
type T Meta
var overlay struct {
*T
LastModified *xsdDate `xml:"lastmodifieddate,attr,omitempty"`
}
overlay.T = (*T)(t)
overlay.LastModified = (*xsdDate)(&overlay.T.LastModified)
return d.DecodeElement(&overlay, &start)
}
// Graph stores the graph nodes, edges, dynamics and visualization data.
type Graph struct {
Attributes []Attributes `xml:"attributes"`
Nodes Nodes `xml:"nodes"`
Edges Edges `xml:"edges"`
// TimeFormat may be one of "integer", "double", "date" or "dateTime".
TimeFormat string `xml:"timeformat,attr,omitempty"`
Start string `xml:"start,attr,omitempty"`
StartOpen string `xml:"startopen,attr,omitempty"`
End string `xml:"end,attr,omitempty"`
EndOpen string `xml:"endopen,attr,omitempty"`
// DefaultEdgeType may be one of "directed", "undirected" or "mutual".
DefaultEdgeType string `xml:"defaultedgetype,attr,omitempty"`
// IDType may be one of "integer" or "string".
IDType string `xml:"idtype,attr,omitempty"`
// Mode may be "static" or "dynamic".
Mode string `xml:"mode,attr,omitempty"`
}
// Attributes holds a collection of potentially dynamic attributes
// associated with a graph.
type Attributes struct {
Attributes []Attribute `xml:"attribute,omitempty"`
// Class be one of "node" or "edge".
Class string `xml:"class,attr"`
// Mode may be "static" or "dynamic".
Mode string `xml:"mode,attr,omitempty"`
Start string `xml:"start,attr,omitempty"`
StartOpen string `xml:"startopen,attr,omitempty"`
End string `xml:"end,attr,omitempty"`
EndOpen string `xml:"endopen,attr,omitempty"`
}
// Attribute holds a single graph attribute.
type Attribute struct {
ID string `xml:"id,attr"`
Title string `xml:"title,attr"`
// Type may be one of "integer", "long", "double", "float",
// "boolean", "liststring", "string", or "anyURI".
Type string `xml:"type,attr"`
Default string `xml:"default,omitempty"`
Options string `xml:"options,omitempty"`
}
// Nodes holds a collection of nodes constituting a graph or subgraph.
type Nodes struct {
Count int `xml:"count,attr,omitempty"`
Nodes []Node `xml:"node,omitempty"`
}
// Node is a single node and its associated attributes.
type Node struct {
ID string `xml:"id,attr,omitempty"`
Label string `xml:"label,attr,omitempty"`
AttValues *AttValues `xml:"attvalues"`
Spells *Spells `xml:"spells"`
Nodes *Nodes `xml:"nodes"`
Edges *Edges `xml:"edges"`
ParentID string `xml:"pid,attr,omitempty"`
Parents *Parents `xml:"parents"`
Color *Color `xml:"http://www.gexf.net/1.2draft/viz color"`
Position *Position `xml:"http://www.gexf.net/1.2draft/viz position"`
Size *Size `xml:"http://www.gexf.net/1.2draft/viz size"`
Shape *NodeShape `xml:"http://www.gexf.net/1.2draft/viz shape"`
Start string `xml:"start,attr,omitempty"`
StartOpen string `xml:"startopen,attr,omitempty"`
End string `xml:"end,attr,omitempty"`
EndOpen string `xml:"endopen,attr,omitempty"`
}
// NodeShape holds the visual representation of a node with associated
// dynamics.
type NodeShape struct {
Spells *Spells `xml:"spells,omitempty"`
// Value be one of "disc", "square", "triangle",
// "diamond" or "image".
Shape string `xml:"value,attr"`
URI string `xml:"uri,attr,omitempty"`
Start string `xml:"start,attr,omitempty"`
StartOpen string `xml:"startopen,attr,omitempty"`
End string `xml:"end,attr,omitempty"`
EndOpen string `xml:"endopen,attr,omitempty"`
}
// Color represents a node or edge color and its associated dynamics.
type Color struct {
Spells *Spells `xml:"spells,omitempty"`
R byte `xml:"r,attr"`
G byte `xml:"g,attr"`
B byte `xml:"b,attr"`
A float64 `xml:"a,attr,omitempty"`
Start string `xml:"start,attr,omitempty"`
StartOpen string `xml:"startopen,attr,omitempty"`
End string `xml:"end,attr,omitempty"`
EndOpen string `xml:"endopen,attr,omitempty"`
}
// Edges holds a collection of edges constituting a graph or subgraph.
type Edges struct {
Count int `xml:"count,attr,omitempty"`
Edges []Edge `xml:"edge,omitempty"`
}
// Edge is a single edge and its associated attributes.
type Edge struct {
ID string `xml:"id,attr,omitempty"`
AttValues *AttValues `xml:"attvalues"`
Spells *Spells `xml:"spells"`
Color *Color `xml:"http://www.gexf.net/1.2draft/viz color"`
Thickness *Thickness `xml:"http://www.gexf.net/1.2draft/viz thickness"`
Shape *Edgeshape `xml:"http://www.gexf.net/1.2draft/viz shape"`
Start string `xml:"start,attr,omitempty"`
StartOpen string `xml:"startopen,attr,omitempty"`
End string `xml:"end,attr,omitempty"`
EndOpen string `xml:"endopen,attr,omitempty"`
// Type may be one of directed, undirected, mutual
Type string `xml:"type,attr,omitempty"`
Label string `xml:"label,attr,omitempty"`
Source string `xml:"source,attr"`
Target string `xml:"target,attr"`
Weight float64 `xml:"weight,attr,omitempty"`
}
// AttValues holds a collection of attribute values.
type AttValues struct {
AttValues []AttValue `xml:"attvalue,omitempty"`
}
// AttValue holds a single attribute value and its associated dynamics.
type AttValue struct {
For string `xml:"for,attr"`
Value string `xml:"value,attr"`
Start string `xml:"start,attr,omitempty"`
StartOpen string `xml:"startopen,attr,omitempty"`
End string `xml:"end,attr,omitempty"`
EndOpen string `xml:"endopen,attr,omitempty"`
}
// Edgeshape holds the visual representation of an edge with associated
// dynamics.
type Edgeshape struct {
// Shape be one of solid, dotted, dashed, double
Shape string `xml:"value,attr"`
Spells *Spells `xml:"spells,omitempty"`
Start string `xml:"start,attr,omitempty"`
StartOpen string `xml:"startopen,attr,omitempty"`
End string `xml:"end,attr,omitempty"`
EndOpen string `xml:"endopen,attr,omitempty"`
}
// Parents holds parent relationships between nodes in a hierarchical
// graph.
type Parents struct {
Parents []Parent `xml:"parent,omitempty"`
}
// Parent is a single parent relationship.
type Parent struct {
For string `xml:"for,attr"`
}
// Position hold the spatial position of a node and its dynamics.
type Position struct {
X float64 `xml:"x,attr"`
Y float64 `xml:"y,attr"`
Z float64 `xml:"z,attr"`
Spells *Spells `xml:"spells,omitempty"`
Start string `xml:"start,attr,omitempty"`
StartOpen string `xml:"startopen,attr,omitempty"`
End string `xml:"end,attr,omitempty"`
EndOpen string `xml:"endopen,attr,omitempty"`
}
// Size hold the visual size of a node and its dynamics.
type Size struct {
Value float64 `xml:"value,attr"`
Spells *Spells `xml:"http://www.gexf.net/1.2draft/viz spells,omitempty"`
Start string `xml:"start,attr,omitempty"`
StartOpen string `xml:"startopen,attr,omitempty"`
End string `xml:"end,attr,omitempty"`
EndOpen string `xml:"endopen,attr,omitempty"`
}
// Thickness hold the visual thickness of an edge and its dynamics.
type Thickness struct {
Value float64 `xml:"value,attr"`
Spells *Spells `xml:"http://www.gexf.net/1.2draft/viz spells,omitempty"`
Start string `xml:"start,attr,omitempty"`
StartOpen string `xml:"startopen,attr,omitempty"`
End string `xml:"end,attr,omitempty"`
EndOpen string `xml:"endopen,attr,omitempty"`
}
// Spells holds a collection of time dynamics for a graph entity.
type Spells struct {
Spells []Spell `xml:"spell"`
}
// Spell is a time interval.
type Spell struct {
Start string `xml:"start,attr,omitempty"`
StartOpen string `xml:"startopen,attr,omitempty"`
End string `xml:"end,attr,omitempty"`
EndOpen string `xml:"endopen,attr,omitempty"`
}
type xsdDate time.Time
func (t *xsdDate) UnmarshalText(text []byte) error {
return _unmarshalTime(text, (*time.Time)(t), "2006-01-02")
}
func (t xsdDate) MarshalText() ([]byte, error) {
return []byte((time.Time)(t).Format("2006-01-02")), nil
}
func (t xsdDate) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
if (time.Time)(t).IsZero() {
return nil
}
m, err := t.MarshalText()
if err != nil {
return err
}
return e.EncodeElement(m, start)
}
func (t xsdDate) MarshalXMLAttr(name xml.Name) (xml.Attr, error) {
if (time.Time)(t).IsZero() {
return xml.Attr{}, nil
}
m, err := t.MarshalText()
return xml.Attr{Name: name, Value: string(m)}, err
}
func _unmarshalTime(text []byte, t *time.Time, format string) (err error) {
s := string(bytes.TrimSpace(text))
*t, err = time.Parse(format, s)
if _, ok := err.(*time.ParseError); ok {
*t, err = time.Parse(format+"Z07:00", s)
}
return err
}
|