-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathdocument.go
212 lines (181 loc) · 6.37 KB
/
document.go
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
// 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/
/*
This package wraps the standard XML library and uses it to build a node tree of
any document you load. This allows you to look up nodes forwards and backwards,
as well as perform simple search queries.
Nodes now simply become collections and don't require you to read them in the
order in which the xml.Parser finds them.
The Document currently implements 2 search functions which allow you to
look for specific nodes.
*xmlx.Document.SelectNode(namespace, name string) *Node;
*xmlx.Document.SelectNodes(namespace, name string) []*Node;
SelectNode() returns the first, single node it finds matching the given name
and namespace. SelectNodes() returns a slice containing all the matching nodes.
Note that these search functions can be invoked on individual nodes as well.
This allows you to search only a subset of the entire document.
*/
package xmlx
import (
"os"
"io"
"io/ioutil"
"strings"
"bytes"
"xml"
"fmt"
"http"
"go-charset.googlecode.com/hg/charset"
)
// represents a single XML document.
type Document struct {
Version string // XML version
Encoding string // Encoding found in document. If absent, assumes UTF-8.
StandAlone string // Value of XML doctype's 'standalone' attribute.
SaveDocType bool // Whether not to include the XML doctype in saves.
Root *Node // The document's root node.
Entity map[string]string // Mapping of custom entity conversions.
Verbose bool // [depracated] Not actually used anymore.
}
// Create a new, empty XML document instance.
func New() *Document {
return &Document{
Version: "1.0",
Encoding: "utf-8",
StandAlone: "yes",
SaveDocType: true,
Entity: make(map[string]string),
}
}
// This loads a rather massive table of non-conventional xml escape sequences.
// Needed to make the parser map them to characters properly. It is advised to
// set only those entities needed manually using the document.Entity map, but
// if need be, this method can be called to fill the map with the entire set
// defined on http://www.w3.org/TR/html4/sgml/entities.html
func (this *Document) LoadExtendedEntityMap() { loadNonStandardEntities(this.Entity) }
// Select a single node with the given namespace and name. Returns nil if no
// matching node was found.
func (this *Document) SelectNode(namespace, name string) *Node {
return this.Root.SelectNode(namespace, name)
}
// Select all nodes with the given namespace and name. Returns an empty slice
// if no matches were found.
func (this *Document) SelectNodes(namespace, name string) []*Node {
return this.Root.SelectNodes(namespace, name)
}
// Load the contents of this document from the supplied reader.
func (this *Document) LoadStream(r io.Reader) (err os.Error) {
xp := xml.NewParser(r)
xp.Entity = this.Entity
xp.CharsetReader = func(enc string, input io.Reader) (io.Reader, os.Error) {
return charset.NewReader(enc, input)
}
this.Root = NewNode(NT_ROOT)
ct := this.Root
var tok xml.Token
var t *Node
var doctype string
for {
if tok, err = xp.Token(); err != nil {
if err == os.EOF {
return nil
}
return err
}
switch tt := tok.(type) {
case xml.SyntaxError:
return os.NewError(tt.String())
case xml.CharData:
ct.Value = strings.TrimSpace(string([]byte(tt)))
case xml.Comment:
t := NewNode(NT_COMMENT)
t.Value = strings.TrimSpace(string([]byte(tt)))
ct.AddChild(t)
case xml.Directive:
t = NewNode(NT_DIRECTIVE)
t.Value = strings.TrimSpace(string([]byte(tt)))
ct.AddChild(t)
case xml.StartElement:
t = NewNode(NT_ELEMENT)
t.Name = tt.Name
t.Attributes = make([]*Attr, len(tt.Attr))
for i, v := range tt.Attr {
t.Attributes[i] = new(Attr)
t.Attributes[i].Name = v.Name
t.Attributes[i].Value = v.Value
}
ct.AddChild(t)
ct = t
case xml.ProcInst:
if tt.Target == "xml" { // xml doctype
doctype = strings.TrimSpace(string(tt.Inst))
if i := strings.Index(doctype, `standalone="`); i > -1 {
this.StandAlone = doctype[i+len(`standalone="`) : len(doctype)]
i = strings.Index(this.StandAlone, `"`)
this.StandAlone = this.StandAlone[0:i]
}
} else {
t = NewNode(NT_PROCINST)
t.Target = strings.TrimSpace(tt.Target)
t.Value = strings.TrimSpace(string(tt.Inst))
ct.AddChild(t)
}
case xml.EndElement:
if ct = ct.Parent; ct == nil {
return
}
}
}
return
}
// Load the contents of this document from the supplied byte slice.
func (this *Document) LoadBytes(d []byte) (err os.Error) {
return this.LoadStream(bytes.NewBuffer(d))
}
// Load the contents of this document from the supplied string.
func (this *Document) LoadString(s string) (err os.Error) {
return this.LoadStream(strings.NewReader(s))
}
// Load the contents of this document from the supplied file.
func (this *Document) LoadFile(filename string) (err os.Error) {
var fd *os.File
if fd, err = os.Open(filename); err != nil {
return
}
defer fd.Close()
return this.LoadStream(fd)
}
// Load the contents of this document from the supplied uri.
func (this *Document) LoadUri(uri string) (err os.Error) {
var r *http.Response
if r, _, err = http.Get(uri); err != nil {
return
}
defer r.Body.Close()
return this.LoadStream(r.Body)
}
// Save the contents of this document to the supplied file.
func (this *Document) SaveFile(path string) os.Error {
return ioutil.WriteFile(path, this.SaveBytes(), 0600)
}
// Save the contents of this document as a byte slice.
func (this *Document) SaveBytes() []byte {
var b bytes.Buffer
if this.SaveDocType {
b.WriteString(fmt.Sprintf(`<?xml version="%s" encoding="%s" standalone="%s"?>`,
this.Version, this.Encoding, this.StandAlone))
}
b.Write(this.Root.Bytes())
return b.Bytes()
}
// Save the contents of this document as a string.
func (this *Document) SaveString() string { return string(this.SaveBytes()) }
// Alias for Document.SaveString(). This one is invoked by anything looking for
// the standard String() method (eg: fmt.Printf("%s\n", mydoc).
func (this *Document) String() string { return string(this.SaveBytes()) }
// Save the contents of this document to the supplied writer.
func (this *Document) SaveStream(w io.Writer) (err os.Error) {
_, err = w.Write(this.SaveBytes())
return
}