-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathparser_document.go
More file actions
326 lines (289 loc) · 7.89 KB
/
parser_document.go
File metadata and controls
326 lines (289 loc) · 7.89 KB
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
package helium
import (
"bytes"
"context"
"errors"
"github.com/lestrrat-go/helium/internal/encoding"
"github.com/lestrrat-go/helium/internal/strcursor"
"github.com/lestrrat-go/helium/sax"
"github.com/lestrrat-go/pdebug"
)
func (pctx *parserCtx) parseDocument(ctx context.Context) error {
if pdebug.Enabled {
g := pdebug.IPrintf("START parseDocument")
defer g.IRelease("END parseDocument")
}
// Store pctx in the context so SAX callbacks (e.g. TreeBuilder) can
// retrieve it via getParserCtx. Also store the document locator and
// stop function so helium.StopParser works.
ctx = withParserCtx(ctx, pctx)
ctx = sax.WithDocumentLocator(ctx, pctx)
ctx = context.WithValue(ctx, stopFuncKey{}, pctx.stop)
if s := pctx.sax; s != nil {
switch err := s.SetDocumentLocator(ctx, pctx); err {
case nil, sax.ErrHandlerUnspecified:
// no op
default:
return pctx.error(ctx, err)
}
}
// see if we can find the preliminary encoding
if pctx.encoding == "" {
if enc, err := pctx.detectEncoding(); err == nil {
pctx.detectedEncoding = enc
}
}
// At this stage we MUST be using a ByteCursor, as we
// don't know what the encoding is.
bcur := pctx.getByteCursor()
if bcur == nil {
return pctx.error(ctx, ErrByteCursorRequired)
}
// nothing left? eek
if bcur.Done() {
return pctx.error(ctx, errors.New("empty document"))
}
// For UTF-16 detected encodings, we must switch encoding FIRST
// because the XML declaration is encoded in UTF-16, not ASCII.
switch pctx.detectedEncoding {
case encUTF16LE, encUTF16BE:
// For UTF-16 detected encodings, we must switch encoding FIRST
// because the XML declaration is encoded in UTF-16, not ASCII.
if err := pctx.switchEncoding(); err != nil {
return pctx.error(ctx, err)
}
cur := pctx.getCursor()
if cur != nil && cur.HasPrefixString("<?xml") {
if err := pctx.parseXMLDeclFromCursor(ctx); err != nil {
return pctx.error(ctx, err)
}
}
case encEBCDIC:
// EBCDIC bytes are not ASCII-compatible, so we cannot parse the
// XML declaration at byte level. Instead, scan the raw bytes
// using the EBCDIC invariant character set (shared across all
// EBCDIC Latin code pages) to extract the encoding name.
if pctx.rawInput != nil {
if encName := encoding.ExtractEBCDICEncoding(pctx.rawInput); encName != "" {
pctx.encoding = encName
}
}
// Fall back to IBM-037 (US EBCDIC) if no encoding was declared.
if pctx.encoding == "" {
pctx.encoding = "ibm037"
}
// Reset the byte cursor from the raw input so the decoder
// reads from the beginning of the document.
pctx.popInput()
pctx.pushInput(strcursor.NewByteCursor(bytes.NewReader(pctx.rawInput)))
if err := pctx.switchEncoding(); err != nil {
return pctx.error(ctx, err)
}
// Parse the XML declaration from the decoded rune cursor.
cur := pctx.getCursor()
if cur != nil && looksLikeXMLDeclString(cur) {
if err := pctx.parseXMLDeclFromCursor(ctx); err != nil {
return pctx.error(ctx, err)
}
}
default:
// XML prolog (byte-level for ASCII-compatible encodings)
if looksLikeXMLDecl(bcur) {
if err := pctx.parseXMLDecl(ctx); err != nil {
return pctx.error(ctx, err)
}
}
// At this point we know the encoding, so switch the encoding
// of the source.
if err := pctx.switchEncoding(); err != nil {
return pctx.error(ctx, err)
}
}
if pctx.treeBuilder != nil {
pctx.fastStartDocument()
} else if s := pctx.sax; s != nil {
switch err := s.StartDocument(ctx); err {
case nil, sax.ErrHandlerUnspecified:
// no op
default:
return pctx.error(ctx, err)
}
}
if pctx.stopped {
return errParserStopped
}
// Misc part of the prolog
if err := pctx.parseMisc(ctx); err != nil {
return pctx.error(ctx, err)
}
cur := pctx.getCursor()
if cur == nil {
return pctx.error(ctx, errNoCursor)
}
// Doctype declarations and more misc
if cur.HasPrefixString("<!DOCTYPE") {
pctx.inSubset = inInternalSubset
if err := pctx.parseDocTypeDecl(ctx); err != nil {
return pctx.error(ctx, err)
}
if cur.HasPrefixString("[") {
pctx.instate = psDTD
if err := pctx.parseInternalSubset(ctx); err != nil {
return pctx.error(ctx, err)
}
}
// Query SAX callbacks for subset/standalone status.
// These mirror libxml2's calls after internal subset parsing.
if s := pctx.sax; s != nil {
if has, err := s.HasInternalSubset(ctx); err == nil {
_ = has // informational; handler may use for validation decisions
}
if has, err := s.HasExternalSubset(ctx); err == nil {
_ = has
}
}
pctx.inSubset = inExternalSubset
if s := pctx.sax; s != nil {
switch err := s.ExternalSubset(ctx, pctx.intSubName, pctx.extSubSystem, pctx.extSubURI); err {
case nil, sax.ErrHandlerUnspecified:
// no op
default:
return pctx.error(ctx, err)
}
}
if pctx.instate == psEOF {
return pctx.error(ctx, errors.New("unexpected EOF"))
}
pctx.inSubset = notInSubset
pctx.cleanSpecialAttributes()
pctx.instate = psPrologue
if err := pctx.parseMisc(ctx); err != nil {
return pctx.error(ctx, err)
}
}
pctx.skipBlanks(ctx)
if cur.Peek() != '<' {
return pctx.error(ctx, ErrEmptyDocument)
}
pctx.instate = psContent
if err := pctx.parseElement(ctx); err != nil {
return pctx.error(ctx, err)
}
if pctx.stopped {
return errParserStopped
}
pctx.instate = psEpilogue
if err := pctx.parseMisc(ctx); err != nil {
return pctx.error(ctx, err)
}
if !cur.Done() {
return pctx.error(ctx, ErrDocumentEnd)
}
pctx.instate = psEOF
// All done
if pctx.treeBuilder != nil {
pctx.fastEndDocument()
} else if s := pctx.sax; s != nil {
switch err := s.EndDocument(ctx); err {
case nil, sax.ErrHandlerUnspecified:
// no op
default:
return pctx.error(ctx, err)
}
}
return nil
}
func (pctx *parserCtx) parseContent(ctx context.Context) error {
if pdebug.Enabled {
g := pdebug.IPrintf("START parseContent")
defer g.IRelease("END parseContent")
}
pctx.instate = psContent
cur := pctx.getCursor()
if cur == nil {
return pctx.error(ctx, errNoCursor)
}
doRecover := pctx.options.IsSet(parseRecover)
for !cur.Done() && !pctx.stopped {
if cur.Peek() == '<' && cur.PeekAt(1) == '/' {
break
}
var err error
switch cur.Peek() {
case '<':
switch cur.PeekAt(1) {
case '?':
err = pctx.parsePI(ctx)
case '!':
switch {
case cur.PeekAt(2) == '[' &&
cur.PeekAt(3) == 'C' &&
cur.PeekAt(4) == 'D' &&
cur.PeekAt(5) == 'A' &&
cur.PeekAt(6) == 'T' &&
cur.PeekAt(7) == 'A' &&
cur.PeekAt(8) == '[':
err = pctx.parseCDSect(ctx)
case cur.PeekAt(2) == '-' && cur.PeekAt(3) == '-':
err = pctx.parseComment(ctx)
default:
err = pctx.parseElement(ctx)
}
default:
err = pctx.parseElement(ctx)
}
case '&':
err = pctx.parseReference(ctx)
default:
if err := pctx.parseCharData(ctx, false); err != nil {
if !doRecover || errors.Is(err, errParserStopped) {
return err
}
if pctx.recoverErr == nil {
pctx.recoverErr = err
}
pctx.disableSAX = true
pctx.wellFormed = false
pctx.skipToRecoverPoint()
}
continue
}
if err != nil {
if !doRecover || errors.Is(err, errParserStopped) {
return pctx.error(ctx, err)
}
if pctx.recoverErr == nil {
pctx.recoverErr = err
}
pctx.disableSAX = true
pctx.wellFormed = false
prevLine, prevCol := cur.LineNumber(), cur.Column()
pctx.skipToRecoverPoint()
if !cur.Done() && cur.LineNumber() == prevLine && cur.Column() == prevCol {
_ = cur.Advance(1)
}
continue
}
}
if pctx.stopped {
return errParserStopped
}
if pctx.recoverErr != nil {
return pctx.recoverErr
}
return nil
}
// skipToRecoverPoint advances the cursor past unrecoverable content to the
// next '<' character or EOF, for re-synchronization in parseRecover mode.
func (ctx *parserCtx) skipToRecoverPoint() {
cur := ctx.getCursor()
if cur == nil {
return
}
for !cur.Done() {
if cur.Peek() == '<' {
return
}
_ = cur.Advance(1)
}
}