2011-03-19 13:50:46 +00:00
|
|
|
// This work is subject to the CC0 1.0 Universal (CC0 1.0) Public Domain Dedication
|
|
|
|
// license. Its contents can be found at:
|
|
|
|
// http://creativecommons.org/publicdomain/zero/1.0/
|
2010-09-26 20:59:14 +00:00
|
|
|
|
2009-11-23 04:16:27 +00:00
|
|
|
package xmlx
|
|
|
|
|
2011-01-18 20:31:56 +00:00
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"xml"
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
)
|
2009-11-23 04:16:27 +00:00
|
|
|
|
|
|
|
const (
|
2010-08-22 03:07:38 +00:00
|
|
|
NT_ROOT = iota
|
|
|
|
NT_DIRECTIVE
|
|
|
|
NT_PROCINST
|
|
|
|
NT_COMMENT
|
|
|
|
NT_ELEMENT
|
2009-11-23 04:16:27 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Attr struct {
|
2010-05-06 03:36:48 +00:00
|
|
|
Name xml.Name
|
|
|
|
Value string
|
2009-11-23 04:16:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Node struct {
|
2010-05-06 03:36:48 +00:00
|
|
|
Type byte
|
|
|
|
Name xml.Name
|
|
|
|
Children []*Node
|
2010-08-22 03:07:38 +00:00
|
|
|
Attributes []*Attr
|
2010-05-06 03:36:48 +00:00
|
|
|
Parent *Node
|
|
|
|
Value string
|
|
|
|
Target string // procinst field
|
2009-11-23 04:16:27 +00:00
|
|
|
}
|
|
|
|
|
2010-08-22 03:07:38 +00:00
|
|
|
func NewNode(tid byte) *Node {
|
|
|
|
n := new(Node)
|
|
|
|
n.Type = tid
|
|
|
|
n.Children = make([]*Node, 0, 10)
|
|
|
|
n.Attributes = make([]*Attr, 0, 10)
|
|
|
|
return n
|
|
|
|
}
|
2009-11-23 04:16:27 +00:00
|
|
|
|
2010-05-06 03:36:48 +00:00
|
|
|
// This wraps the standard xml.Unmarshal function and supplies this particular
|
2009-12-02 18:38:35 +00:00
|
|
|
// node as the content to be unmarshalled.
|
|
|
|
func (this *Node) Unmarshal(obj interface{}) os.Error {
|
2010-05-06 03:36:48 +00:00
|
|
|
return xml.Unmarshal(strings.NewReader(this.String()), obj)
|
2009-12-02 18:38:35 +00:00
|
|
|
}
|
|
|
|
|
2009-11-23 05:40:57 +00:00
|
|
|
// Get node value as string
|
2010-12-19 20:45:42 +00:00
|
|
|
func (this *Node) S(namespace, name string) string {
|
|
|
|
if node := rec_SelectNode(this, namespace, name); node != nil {
|
|
|
|
return node.Value
|
2010-05-06 03:36:48 +00:00
|
|
|
}
|
2010-12-19 20:45:42 +00:00
|
|
|
return ""
|
2009-11-23 05:40:57 +00:00
|
|
|
}
|
|
|
|
|
2010-12-19 20:45:42 +00:00
|
|
|
// Deprecated - use Node.S()
|
|
|
|
func (this *Node) GetValue(namespace, name string) string { return this.S(namespace, name) }
|
|
|
|
|
|
|
|
|
2009-11-23 05:40:57 +00:00
|
|
|
// Get node value as int
|
2010-12-19 20:45:42 +00:00
|
|
|
func (this *Node) I(namespace, name string) int {
|
|
|
|
if node := rec_SelectNode(this, namespace, name); node != nil && node.Value != "" {
|
|
|
|
n, _ := strconv.Atoi(node.Value)
|
|
|
|
return n
|
2010-05-06 03:36:48 +00:00
|
|
|
}
|
2010-12-19 20:45:42 +00:00
|
|
|
return 0
|
2009-11-23 05:40:57 +00:00
|
|
|
}
|
|
|
|
|
2010-12-19 20:45:42 +00:00
|
|
|
// Deprecated - use Node.I()
|
|
|
|
func (this *Node) GetValuei(namespace, name string) int { return this.I(namespace, name) }
|
|
|
|
|
|
|
|
|
2009-11-23 05:40:57 +00:00
|
|
|
// Get node value as int64
|
2010-12-19 20:45:42 +00:00
|
|
|
func (this *Node) I64(namespace, name string) int64 {
|
|
|
|
if node := rec_SelectNode(this, namespace, name); node != nil && node.Value != "" {
|
|
|
|
n, _ := strconv.Atoi64(node.Value)
|
|
|
|
return n
|
2010-05-06 03:36:48 +00:00
|
|
|
}
|
2010-12-19 20:45:42 +00:00
|
|
|
return 0
|
2009-11-23 05:40:57 +00:00
|
|
|
}
|
|
|
|
|
2010-12-19 20:45:42 +00:00
|
|
|
// Deprecated - use Node.I64()
|
|
|
|
func (this *Node) GetValuei64(namespace, name string) int64 { return this.I64(namespace, name) }
|
|
|
|
|
|
|
|
|
2009-11-23 05:40:57 +00:00
|
|
|
// Get node value as uint
|
2010-12-19 20:45:42 +00:00
|
|
|
func (this *Node) U(namespace, name string) uint {
|
|
|
|
if node := rec_SelectNode(this, namespace, name); node != nil && node.Value != "" {
|
|
|
|
n, _ := strconv.Atoui(node.Value)
|
|
|
|
return n
|
2010-05-06 03:36:48 +00:00
|
|
|
}
|
2010-12-19 20:45:42 +00:00
|
|
|
return 0
|
2009-11-23 05:40:57 +00:00
|
|
|
}
|
|
|
|
|
2010-12-19 20:45:42 +00:00
|
|
|
// Deprecated - use Node.U()
|
|
|
|
func (this *Node) GetValueui(namespace, name string) uint { return this.U(namespace, name) }
|
|
|
|
|
|
|
|
|
2009-11-23 05:40:57 +00:00
|
|
|
// Get node value as uint64
|
2010-12-19 20:45:42 +00:00
|
|
|
func (this *Node) U64(namespace, name string) uint64 {
|
|
|
|
if node := rec_SelectNode(this, namespace, name); node != nil && node.Value != "" {
|
|
|
|
n, _ := strconv.Atoui64(node.Value)
|
|
|
|
return n
|
2010-05-06 03:36:48 +00:00
|
|
|
}
|
2010-12-19 20:45:42 +00:00
|
|
|
return 0
|
2009-11-23 05:40:57 +00:00
|
|
|
}
|
|
|
|
|
2010-12-19 20:45:42 +00:00
|
|
|
// Deprecated - use Node.U64()
|
|
|
|
func (this *Node) GetValueui64(namespace, name string) uint64 { return this.U64(namespace, name) }
|
|
|
|
|
2009-11-23 05:40:57 +00:00
|
|
|
// Get node value as float32
|
2010-12-19 20:45:42 +00:00
|
|
|
func (this *Node) F32(namespace, name string) float32 {
|
|
|
|
if node := rec_SelectNode(this, namespace, name); node != nil && node.Value != "" {
|
|
|
|
n, _ := strconv.Atof32(node.Value)
|
|
|
|
return n
|
2010-05-06 03:36:48 +00:00
|
|
|
}
|
2010-12-19 20:45:42 +00:00
|
|
|
return 0
|
2009-11-23 05:40:57 +00:00
|
|
|
}
|
|
|
|
|
2010-12-19 20:45:42 +00:00
|
|
|
// Deprecated - use Node.F32()
|
|
|
|
func (this *Node) GetValuef32(namespace, name string) float32 { return this.F32(namespace, name) }
|
|
|
|
|
|
|
|
|
2009-11-23 05:40:57 +00:00
|
|
|
// Get node value as float64
|
2010-12-19 20:45:42 +00:00
|
|
|
func (this *Node) F64(namespace, name string) float64 {
|
|
|
|
if node := rec_SelectNode(this, namespace, name); node != nil && node.Value != "" {
|
|
|
|
n, _ := strconv.Atof64(node.Value)
|
|
|
|
return n
|
2010-05-06 03:36:48 +00:00
|
|
|
}
|
2010-12-19 20:45:42 +00:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// Deprecated - use Node.F64()
|
|
|
|
func (this *Node) GetValuef64(namespace, name string) float64 { return this.F64(namespace, name) }
|
|
|
|
|
|
|
|
|
|
|
|
// Get node value as bool
|
|
|
|
func (this *Node) B(namespace, name string) bool {
|
|
|
|
if node := rec_SelectNode(this, namespace, name); node != nil && node.Value != "" {
|
|
|
|
n, _ := strconv.Atob(node.Value)
|
|
|
|
return n
|
|
|
|
}
|
|
|
|
return false
|
2009-11-23 05:40:57 +00:00
|
|
|
}
|
|
|
|
|
2009-11-23 05:15:40 +00:00
|
|
|
// Get attribute value as string
|
2010-12-19 20:45:42 +00:00
|
|
|
func (this *Node) As(namespace, name string) string {
|
2010-05-06 03:36:48 +00:00
|
|
|
for _, v := range this.Attributes {
|
2011-02-01 14:29:35 +00:00
|
|
|
if (namespace == "*" || namespace == v.Name.Space) && name == v.Name.Local {
|
2010-05-06 03:36:48 +00:00
|
|
|
return v.Value
|
2009-11-23 05:15:40 +00:00
|
|
|
}
|
|
|
|
}
|
2010-05-06 03:36:48 +00:00
|
|
|
return ""
|
2009-11-23 05:15:40 +00:00
|
|
|
}
|
|
|
|
|
2010-12-19 20:45:42 +00:00
|
|
|
// Deprecated - use Node.As()
|
|
|
|
func (this *Node) GetAttr(namespace, name string) string { return this.As(namespace, name) }
|
|
|
|
|
|
|
|
|
2009-11-23 05:15:40 +00:00
|
|
|
// Get attribute value as int
|
2010-12-19 20:45:42 +00:00
|
|
|
func (this *Node) Ai(namespace, name string) int {
|
|
|
|
if s := this.As(namespace, name); s != "" {
|
|
|
|
n, _ := strconv.Atoi(s)
|
|
|
|
return n
|
2010-05-06 03:36:48 +00:00
|
|
|
}
|
2010-12-19 20:45:42 +00:00
|
|
|
return 0
|
2009-11-23 05:15:40 +00:00
|
|
|
}
|
|
|
|
|
2010-12-19 20:45:42 +00:00
|
|
|
// Deprecated - use Node.Ai()
|
|
|
|
func (this *Node) GetAttri(namespace, name string) int { return this.Ai(namespace, name) }
|
|
|
|
|
|
|
|
|
2009-11-23 05:15:40 +00:00
|
|
|
// Get attribute value as uint
|
2010-12-19 20:45:42 +00:00
|
|
|
func (this *Node) Au(namespace, name string) uint {
|
|
|
|
if s := this.As(namespace, name); s != "" {
|
|
|
|
n, _ := strconv.Atoui(s)
|
|
|
|
return n
|
2010-05-06 03:36:48 +00:00
|
|
|
}
|
2010-12-19 20:45:42 +00:00
|
|
|
return 0
|
2009-11-23 05:15:40 +00:00
|
|
|
}
|
|
|
|
|
2010-12-19 20:45:42 +00:00
|
|
|
// Deprecated - use Node.Au()
|
|
|
|
func (this *Node) GetAttrui(namespace, name string) uint { return this.Au(namespace, name) }
|
|
|
|
|
|
|
|
|
2009-11-23 05:15:40 +00:00
|
|
|
// Get attribute value as uint64
|
2010-12-19 20:45:42 +00:00
|
|
|
func (this *Node) Au64(namespace, name string) uint64 {
|
|
|
|
if s := this.As(namespace, name); s != "" {
|
|
|
|
n, _ := strconv.Atoui64(s)
|
|
|
|
return n
|
2010-05-06 03:36:48 +00:00
|
|
|
}
|
2010-12-19 20:45:42 +00:00
|
|
|
return 0
|
2009-11-23 05:15:40 +00:00
|
|
|
}
|
|
|
|
|
2010-12-19 20:45:42 +00:00
|
|
|
// Deprecated - use Node.Au64()
|
|
|
|
func (this *Node) GetAttrui64(namespace, name string) uint64 { return this.Au64(namespace, name) }
|
|
|
|
|
|
|
|
|
2009-11-23 05:15:40 +00:00
|
|
|
// Get attribute value as int64
|
2010-12-19 20:45:42 +00:00
|
|
|
func (this *Node) Ai64(namespace, name string) int64 {
|
|
|
|
if s := this.As(namespace, name); s != "" {
|
|
|
|
n, _ := strconv.Atoi64(s)
|
|
|
|
return n
|
2010-05-06 03:36:48 +00:00
|
|
|
}
|
2010-12-19 20:45:42 +00:00
|
|
|
return 0
|
2009-11-23 05:15:40 +00:00
|
|
|
}
|
|
|
|
|
2010-12-19 20:45:42 +00:00
|
|
|
// Deprecated - use Node.Ai64()
|
|
|
|
func (this *Node) GetAttri64(namespace, name string) int64 { return this.Ai64(namespace, name) }
|
|
|
|
|
2009-11-23 05:15:40 +00:00
|
|
|
// Get attribute value as float32
|
2010-12-19 20:45:42 +00:00
|
|
|
func (this *Node) Af32(namespace, name string) float32 {
|
|
|
|
if s := this.As(namespace, name); s != "" {
|
|
|
|
n, _ := strconv.Atof32(s)
|
|
|
|
return n
|
2010-05-06 03:36:48 +00:00
|
|
|
}
|
2010-12-19 20:45:42 +00:00
|
|
|
return 0
|
2009-11-23 05:15:40 +00:00
|
|
|
}
|
|
|
|
|
2010-12-19 20:45:42 +00:00
|
|
|
// Deprecated - use Node.Af32()
|
|
|
|
func (this *Node) GetAttrf32(namespace, name string) float32 { return this.Af32(namespace, name) }
|
|
|
|
|
2009-11-23 05:15:40 +00:00
|
|
|
// Get attribute value as float64
|
2010-12-19 20:45:42 +00:00
|
|
|
func (this *Node) Af64(namespace, name string) float64 {
|
|
|
|
if s := this.As(namespace, name); s != "" {
|
|
|
|
n, _ := strconv.Atof64(s)
|
|
|
|
return n
|
2010-05-06 03:36:48 +00:00
|
|
|
}
|
2010-12-19 20:45:42 +00:00
|
|
|
return 0
|
2009-11-23 05:15:40 +00:00
|
|
|
}
|
|
|
|
|
2010-12-19 20:45:42 +00:00
|
|
|
// Deprecated - use Node.Af64()
|
|
|
|
func (this *Node) GetAttrf64(namespace, name string) float64 { return this.Af64(namespace, name) }
|
|
|
|
|
|
|
|
|
|
|
|
// Get attribute value as bool
|
|
|
|
func (this *Node) Ab(namespace, name string) bool {
|
|
|
|
if s := this.As(namespace, name); s != "" {
|
|
|
|
n, _ := strconv.Atob(s)
|
|
|
|
return n
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2009-11-23 05:15:40 +00:00
|
|
|
// Returns true if this node has the specified attribute. False otherwise.
|
|
|
|
func (this *Node) HasAttr(namespace, name string) bool {
|
2010-05-06 03:36:48 +00:00
|
|
|
for _, v := range this.Attributes {
|
2011-02-01 14:29:35 +00:00
|
|
|
if (namespace == "*" || namespace == v.Name.Space) && name == v.Name.Local {
|
2009-11-23 05:15:40 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Select single node by name
|
2009-11-23 04:16:27 +00:00
|
|
|
func (this *Node) SelectNode(namespace, name string) *Node {
|
2010-05-06 03:36:48 +00:00
|
|
|
return rec_SelectNode(this, namespace, name)
|
2009-11-23 04:16:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func rec_SelectNode(cn *Node, namespace, name string) *Node {
|
2011-02-01 14:29:35 +00:00
|
|
|
// Allow wildcard for namespace names. Meaning we will match any namespace
|
|
|
|
// name with a matching local name.
|
|
|
|
if (namespace == "*" || cn.Name.Space == namespace) && cn.Name.Local == name {
|
2010-05-06 03:36:48 +00:00
|
|
|
return cn
|
2009-11-23 04:16:27 +00:00
|
|
|
}
|
|
|
|
|
2010-08-22 03:07:38 +00:00
|
|
|
var tn *Node
|
2009-11-23 04:16:27 +00:00
|
|
|
for _, v := range cn.Children {
|
2010-08-22 03:07:38 +00:00
|
|
|
if tn = rec_SelectNode(v, namespace, name); tn != nil {
|
2010-05-06 03:36:48 +00:00
|
|
|
return tn
|
|
|
|
}
|
2009-11-23 04:16:27 +00:00
|
|
|
}
|
2010-05-06 03:36:48 +00:00
|
|
|
return nil
|
2009-11-23 04:16:27 +00:00
|
|
|
}
|
|
|
|
|
2009-11-23 05:15:40 +00:00
|
|
|
// Select multiple nodes by name
|
2009-11-23 04:16:27 +00:00
|
|
|
func (this *Node) SelectNodes(namespace, name string) []*Node {
|
2010-08-22 03:07:38 +00:00
|
|
|
list := make([]*Node, 0, 16)
|
2010-05-06 03:36:48 +00:00
|
|
|
rec_SelectNodes(this, namespace, name, &list)
|
|
|
|
return list
|
2009-11-23 04:16:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func rec_SelectNodes(cn *Node, namespace, name string, list *[]*Node) {
|
2011-02-01 14:29:35 +00:00
|
|
|
// Allow wildcard for namespace names. Meaning we will match any namespace
|
|
|
|
// name with a matching local name.
|
|
|
|
if (namespace == "*" || cn.Name.Space == namespace) && cn.Name.Local == name {
|
2010-11-05 00:26:35 +00:00
|
|
|
*list = append(*list, cn)
|
2009-11-23 04:16:27 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, v := range cn.Children {
|
2010-05-06 03:36:48 +00:00
|
|
|
rec_SelectNodes(v, namespace, name, list)
|
2009-11-23 04:16:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-11-23 05:15:40 +00:00
|
|
|
// Convert node to appropriate string representation based on it's @Type.
|
2010-05-06 03:36:48 +00:00
|
|
|
// Note that NT_ROOT is a special-case empty node used as the root for a
|
2009-11-23 05:15:40 +00:00
|
|
|
// Document. This one has no representation by itself. It merely forwards the
|
|
|
|
// String() call to it's child nodes.
|
2009-11-23 04:16:27 +00:00
|
|
|
func (this *Node) String() (s string) {
|
|
|
|
switch this.Type {
|
|
|
|
case NT_PROCINST:
|
|
|
|
s = this.printProcInst()
|
|
|
|
case NT_COMMENT:
|
|
|
|
s = this.printComment()
|
|
|
|
case NT_DIRECTIVE:
|
|
|
|
s = this.printDirective()
|
|
|
|
case NT_ELEMENT:
|
|
|
|
s = this.printElement()
|
|
|
|
case NT_ROOT:
|
|
|
|
s = this.printRoot()
|
|
|
|
}
|
2010-05-06 03:36:48 +00:00
|
|
|
return
|
2009-11-23 04:16:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (this *Node) printRoot() (s string) {
|
2010-08-22 03:07:38 +00:00
|
|
|
var data []byte
|
|
|
|
buf := bytes.NewBuffer(data)
|
2009-11-23 04:16:27 +00:00
|
|
|
for _, v := range this.Children {
|
2010-08-22 03:07:38 +00:00
|
|
|
buf.WriteString(v.String())
|
2009-11-23 04:16:27 +00:00
|
|
|
}
|
2010-08-22 03:07:38 +00:00
|
|
|
return buf.String()
|
2009-11-23 04:16:27 +00:00
|
|
|
}
|
|
|
|
|
2010-08-22 03:07:38 +00:00
|
|
|
func (this *Node) printProcInst() string {
|
|
|
|
return "<?" + this.Target + " " + this.Value + "?>"
|
2009-11-23 04:16:27 +00:00
|
|
|
}
|
|
|
|
|
2010-08-22 03:07:38 +00:00
|
|
|
func (this *Node) printComment() string {
|
|
|
|
return "<!-- " + this.Value + " -->"
|
2009-11-23 04:16:27 +00:00
|
|
|
}
|
|
|
|
|
2010-08-22 03:07:38 +00:00
|
|
|
func (this *Node) printDirective() string {
|
|
|
|
return "<!" + this.Value + "!>"
|
2009-11-23 04:16:27 +00:00
|
|
|
}
|
|
|
|
|
2010-08-22 03:07:38 +00:00
|
|
|
func (this *Node) printElement() string {
|
|
|
|
var data []byte
|
|
|
|
buf := bytes.NewBuffer(data)
|
|
|
|
|
2009-11-23 04:16:27 +00:00
|
|
|
if len(this.Name.Space) > 0 {
|
2010-08-22 03:07:38 +00:00
|
|
|
buf.WriteRune('<')
|
|
|
|
buf.WriteString(this.Name.Space)
|
|
|
|
buf.WriteRune(':')
|
|
|
|
buf.WriteString(this.Name.Local)
|
2009-11-23 04:16:27 +00:00
|
|
|
} else {
|
2010-08-22 03:07:38 +00:00
|
|
|
buf.WriteRune('<')
|
|
|
|
buf.WriteString(this.Name.Local)
|
2009-11-23 04:16:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, v := range this.Attributes {
|
|
|
|
if len(v.Name.Space) > 0 {
|
2010-08-22 03:07:38 +00:00
|
|
|
buf.WriteString(fmt.Sprintf(` %s:%s="%s"`, v.Name.Space, v.Name.Local, v.Value))
|
2009-11-23 04:16:27 +00:00
|
|
|
} else {
|
2010-08-22 03:07:38 +00:00
|
|
|
buf.WriteString(fmt.Sprintf(` %s="%s"`, v.Name.Local, v.Value))
|
2009-11-23 04:16:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(this.Children) == 0 && len(this.Value) == 0 {
|
2010-08-22 03:07:38 +00:00
|
|
|
buf.WriteString(" />")
|
|
|
|
return buf.String()
|
2009-11-23 04:16:27 +00:00
|
|
|
}
|
|
|
|
|
2010-08-22 03:07:38 +00:00
|
|
|
buf.WriteRune('>')
|
2009-11-23 04:16:27 +00:00
|
|
|
|
|
|
|
for _, v := range this.Children {
|
2010-08-22 03:07:38 +00:00
|
|
|
buf.WriteString(v.String())
|
2009-11-23 04:16:27 +00:00
|
|
|
}
|
|
|
|
|
2010-08-22 03:07:38 +00:00
|
|
|
buf.WriteString(this.Value)
|
2009-11-23 04:16:27 +00:00
|
|
|
if len(this.Name.Space) > 0 {
|
2010-08-22 03:07:38 +00:00
|
|
|
buf.WriteString("</")
|
|
|
|
buf.WriteString(this.Name.Space)
|
|
|
|
buf.WriteRune(':')
|
|
|
|
buf.WriteString(this.Name.Local)
|
|
|
|
buf.WriteRune('>')
|
2009-11-23 04:16:27 +00:00
|
|
|
} else {
|
2010-08-22 03:07:38 +00:00
|
|
|
buf.WriteString("</")
|
|
|
|
buf.WriteString(this.Name.Local)
|
|
|
|
buf.WriteRune('>')
|
2009-11-23 04:16:27 +00:00
|
|
|
}
|
2010-08-22 03:07:38 +00:00
|
|
|
|
|
|
|
return buf.String()
|
2009-11-23 04:16:27 +00:00
|
|
|
}
|
|
|
|
|
2009-11-23 05:15:40 +00:00
|
|
|
// Add a child node
|
2009-11-23 04:16:27 +00:00
|
|
|
func (this *Node) AddChild(t *Node) {
|
|
|
|
if t.Parent != nil {
|
|
|
|
t.Parent.RemoveChild(t)
|
|
|
|
}
|
2010-05-06 03:36:48 +00:00
|
|
|
t.Parent = this
|
2010-11-05 00:26:35 +00:00
|
|
|
this.Children = append(this.Children, t)
|
2009-11-23 04:16:27 +00:00
|
|
|
}
|
|
|
|
|
2009-11-23 05:15:40 +00:00
|
|
|
// Remove a child node
|
2009-11-23 04:16:27 +00:00
|
|
|
func (this *Node) RemoveChild(t *Node) {
|
2010-05-06 03:36:48 +00:00
|
|
|
p := -1
|
2009-11-23 04:16:27 +00:00
|
|
|
for i, v := range this.Children {
|
|
|
|
if v == t {
|
2010-05-06 03:36:48 +00:00
|
|
|
p = i
|
|
|
|
break
|
2009-11-23 04:16:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-06 03:36:48 +00:00
|
|
|
if p == -1 {
|
2009-11-23 04:16:27 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2010-08-22 03:07:38 +00:00
|
|
|
copy(this.Children[p:], this.Children[p+1:])
|
|
|
|
this.Children = this.Children[0 : len(this.Children)-1]
|
2009-11-23 04:16:27 +00:00
|
|
|
|
2010-05-06 03:36:48 +00:00
|
|
|
t.Parent = nil
|
2009-11-23 04:16:27 +00:00
|
|
|
}
|