-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrammar.go
337 lines (305 loc) · 9.25 KB
/
grammar.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
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
// Copyright 2015 Walter Schulze
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package relaxng
import (
"bytes"
"encoding/xml"
"fmt"
"reflect"
)
//Parses simplified RelaxNG XML into a Grammar structure.
func ParseGrammar(buf []byte) (*Grammar, error) {
g := &Grammar{}
err := xml.Unmarshal(buf, g)
return g, err
}
func (g *Grammar) String() string {
data, err := xml.MarshalIndent(g, "", "\t")
if err != nil {
panic(err)
}
return string(data)
}
/*
The simplified RelaxNG Grammar as specified in
http://relaxng.org/spec-20011203.html
grammar ::= <grammar> <start> top </start> define* </grammar>
define ::= <define name="NCName"> <element> nameClass top </element> </define>
top ::= <notAllowed/>
| Pattern
Pattern ::= <empty/>
| Pattern
Pattern ::= <text/>
| <data type="NCName" datatypeLibrary="anyURI"> param* [exceptPattern] </data>
| <value datatypeLibrary="anyURI" type="NCName" ns="string"> string </value>
| <list> Pattern </list>
| <attribute> nameClass Pattern </attribute>
| <ref name="NCName"/>
| <oneOrMore> Pattern </oneOrMore>
| <choice> Pattern Pattern </choice>
| <group> Pattern Pattern </group>
| <interleave> Pattern Pattern </interleave>
param ::= <param name="NCName"> string </param>
exceptPattern ::= <except> Pattern </except>
nameClass ::= <anyName> [exceptNameClass] </anyName>
| <nsName ns="string"> [exceptNameClass] </nsName>
| <name ns="string"> NCName </name>
| <choice> nameClass nameClass </choice>
exceptNameClass ::= <except> nameClass </except>
*/
type Grammar struct {
XMLName xml.Name `xml:"grammar"`
Start *NameOrPattern `xml:"start"`
Define []Define `xml:"define"`
}
//The define RelaxNG grammar element
type Define struct {
Name string `xml:"name,attr"`
//Left is Name and Right is Pattern
Element Pair `xml:"element"`
}
//One of the name or pattern RelaxNG grammar elements
type NameOrPattern struct {
NotAllowed *NotAllowed `xml:"notAllowed"`
Empty *Empty `xml:"empty"`
Text *Text `xml:"text"`
Data *Data `xml:"data"`
Value *Value `xml:"value"`
List *List `xml:"list"`
//Attribute does not care about order, left is Name and Right is Pattern
Attribute *Pair `xml:"attribute"`
Ref *Ref `xml:"ref"`
OneOrMore *OneOrMore `xml:"oneOrMore"`
Choice *Pair `xml:"choice"`
//http://books.xmlschemata.org/relaxng/relax-CHP-6-SECT-1.html
// Because the order of attributes isn't considered significant by the XML 1.0 specification,
// the meaning of the group compositor is slightly less straightforward than it appears at first.
// Here's the semantic quirk: the group compositor says,
// "Check that the patterns included in this compositor appear in the specified order,
// except for attributes, which are allowed to appear in any order in the start tag."
Group *Pair `xml:"group"`
Interleave *Pair `xml:"interleave"`
AnyName *AnyNameClass `xml:"anyName"`
NsName *NsNameClass `xml:"nsName"`
Name *NameNameClass `xml:"name"`
}
func (this *NameOrPattern) IsPattern() bool {
return !this.IsNameClass()
}
func (this *NameOrPattern) IsNameClass() bool {
if this.AnyName != nil || this.NsName != nil || this.Name != nil {
return true
}
if this.Choice != nil {
return this.Choice.Left.IsNameClass()
}
return false
}
func (this *NameOrPattern) String() string {
buf := bytes.NewBuffer(nil)
enc := xml.NewEncoder(buf)
err := this.marshalXML(enc, xml.StartElement{})
if err != nil {
panic(err)
}
enc.Flush()
return string(buf.Bytes())
}
func (this *NameOrPattern) unmarshalXML(d *xml.Decoder, start xml.StartElement) error {
t := reflect.TypeOf(this).Elem()
numFields := t.NumField()
v := reflect.ValueOf(this).Elem()
for i := 0; i < numFields; i++ {
f := t.Field(i)
xmlTag := f.Tag.Get("xml")
if xmlTag == start.Name.Local {
n := reflect.New(f.Type)
err := d.DecodeElement(n.Interface(), &start)
if err != nil {
return err
}
v.Field(i).Set(n.Elem())
return nil
}
}
return fmt.Errorf("unknown pattern " + start.Name.Local)
}
func (this *NameOrPattern) marshalXML(e *xml.Encoder, start xml.StartElement) error {
v := reflect.ValueOf(this).Elem()
t := reflect.TypeOf(this).Elem()
numFields := v.NumField()
for i := 0; i < numFields; i++ {
if !v.Field(i).IsNil() {
newStart := xml.StartElement{
Name: xml.Name{
Local: t.Field(i).Tag.Get("xml"),
},
}
return e.EncodeElement(v.Field(i).Interface(), newStart)
}
}
return fmt.Errorf("unset pattern")
}
//The notAllowed RelaxNG grammar element.
type NotAllowed struct {
XMLName xml.Name `xml:"notAllowed"`
}
//The empty RelaxNG grammar element.
type Empty struct {
XMLName xml.Name `xml:"empty"`
}
//The text RelaxNG grammar element.
type Text struct {
XMLName xml.Name `xml:"text"`
}
//The data RelaxNG grammar element which is described here:
//http://books.xmlschemata.org/relaxng/ch17-77040.html
//http://books.xmlschemata.org/relaxng/relax-CHP-8-SECT-1.html
//Even though katydid could easily support more types,
//only Type string and token are currently supported.
//This also means that Param is not currently supported.
//DatatypeLibrary is not supported.
type Data struct {
XMLName xml.Name `xml:"data"`
Type string `xml:"type,attr"`
DatatypeLibrary string `xml:"datatypeLibrary,attr"`
Param []Param `xml:"param"`
Except *NameOrPattern `xml:"except"`
}
//Returns whether this data type is a string type.
//Only type string and type token are supported.
//An empty Type value implies a default value of token.
func (this *Data) IsString() bool {
return this.Type == "string"
}
//The value RelaxNG grammar element which is described here:
//http://books.xmlschemata.org/relaxng/ch17-77225.html
//Match a value in a text node.
//DatatypeLibrary and Ns fields are not supported.
type Value struct {
XMLName xml.Name `xml:"value"`
DatatypeLibrary string `xml:"datatypeLibrary,attr"`
Type string `xml:"type,attr"`
Ns string `xml:"ns,attr"`
Text string `xml:",chardata"`
}
//Returns whether this value type is a string type.
//http://books.xmlschemata.org/relaxng/relax-CHP-7-SECT-4.html
//Only type string and type token are supported.
//An empty Type value implies a default value of token.
func (this *Value) IsString() bool {
return this.Type == "string"
}
//The list RelaxNG grammar element which is described here:
//http://books.xmlschemata.org/relaxng/relax-CHP-7-SECT-9.html
//http://books.xmlschemata.org/relaxng/ch17-77136.html
type List struct {
XMLName xml.Name `xml:"list"`
*NameOrPattern
}
//The oneOrMore RelaxNG grammar element.
type OneOrMore struct {
XMLName xml.Name `xml:"oneOrMore"`
*NameOrPattern
}
//The ref RelaxNG grammar element.
type Ref struct {
XMLName xml.Name `xml:"ref"`
Name string `xml:"name,attr"`
}
//A pair of RelaxNG grammar elements.
type Pair struct {
Left *NameOrPattern
Right *NameOrPattern
}
func skipToStart(d *xml.Decoder) (*xml.StartElement, error) {
for {
t, err := d.Token()
if err != nil {
return nil, err
}
s, ok := t.(xml.StartElement)
if ok {
return &s, nil
}
}
}
func (this *Pair) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
s, err := skipToStart(d)
if err != nil {
return err
}
this.Left = &NameOrPattern{}
if err := this.Left.unmarshalXML(d, *s); err != nil {
return err
}
s, err = skipToStart(d)
if err != nil {
return err
}
this.Right = &NameOrPattern{}
if err := this.Right.unmarshalXML(d, *s); err != nil {
return err
}
for {
t, err := d.Token()
if err != nil {
return err
}
e, ok := t.(xml.EndElement)
if ok && e.Name.Local == start.Name.Local {
break
}
}
return nil
}
func (this *Pair) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
if err := e.EncodeToken(start); err != nil {
return err
}
if err := this.Left.marshalXML(e, start); err != nil {
return err
}
if err := this.Right.marshalXML(e, start); err != nil {
return err
}
if err := e.EncodeToken(start.End()); err != nil {
return err
}
return nil
}
//The param RelaxNG grammar element.
type Param struct {
Name string `xml:",attr"`
Text string `xml:",chardata"`
}
//The anyNameClass RelaxNG grammar element.
type AnyNameClass struct {
XMLName xml.Name `xml:"anyName"`
Except *NameOrPattern `xml:"except"`
}
//The nsName RelaxNG grammar element.
//This element is not supported.
type NsNameClass struct {
XMLName xml.Name `xml:"nsName"`
Ns string `xml:"ns,attr"`
Except *NameOrPattern `xml:"except"`
}
//The name RelaxNG grammar element.
//Ns is not supported.
type NameNameClass struct {
XMLName xml.Name `xml:"name"`
Ns string `xml:"ns,attr"`
Text string `xml:",chardata"`
}