Repository navigation
Expand file tree
/
Copy pathparser_document.go
More file actions
432 lines (392 loc) · 12.2 KB
/
Copy pathparser_document.go
File metadata and controls
432 lines (392 loc) · 12.2 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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
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"
)
func (pctx *parserCtx) parseDocument(ctx context.Context) error {
// Store pctx in the context so SAX callbacks (e.g. TreeBuilder) and
// helium.StopParser can retrieve it via getParserCtx. Also store the
// document locator.
ctx = withParserCtx(ctx, pctx)
ctx = sax.WithDocumentLocator(ctx, pctx)
// Honor a context that is already cancelled before any parsing work
// (or blocking reads) begins.
if err := ctx.Err(); err != nil {
return err
}
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() {
// An apparently empty document may instead be a cancelled read: the
// push parser's stream returns the context error from its blocking
// wait, which the byte cursor records and Done() then masks. Surface
// cancellation (and any other sticky read error) instead of the
// misleading "empty document".
if err := ctx.Err(); err != nil {
return err
}
if err := bcur.Err(); err != nil {
return pctx.error(ctx, err)
}
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, encUCS4BE, encUCS4LE, encUCS42143, encUCS43412:
// For UTF-16 and UCS-4 detected encodings, we must switch encoding
// FIRST because the XML declaration is encoded in that fixed-width
// encoding, not ASCII. These encodings are not ASCII-compatible, so
// the byte-level XML declaration parser cannot read them. The UCS-4
// detection patterns are the encoded first '<' character (peeked, not
// consumed), so the decoded cursor still begins at the document start.
if err := pctx.switchEncoding(); err != nil {
return pctx.error(ctx, err)
}
cur := pctx.getCursor()
if cur != nil && looksLikeXMLDeclString(cur) {
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"
}
// For the byte-slice path rawInput holds the whole document, so reset the
// byte cursor from it to read the decoder from the document start.
// For the streaming path (ebcdicStream) rawInput is only a bounded sniff
// prefix; the live cursor already sits at the document start (detection
// only peeks) over the full prefix+remainder stream, so decode it in
// place — the parser's per-node content caps then bound memory as the
// stream is consumed, with no whole-document buffer.
if !pctx.ebcdicStream {
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)
}
}
// A leading byte-order mark asserts the entity's encoding; a contradicting
// encoding declaration is a fatal error (XML §4.3.3).
if err := pctx.checkBOMEncodingConflict(); 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
// The whole DTD is parsed now, so the entity tables are complete. Re-check
// every attribute default value's WFCs to catch a nested external/unparsed
// (or '<') entity reached through a FORWARD-referenced entity that was not
// yet declared when the default was parsed (SubstituteEntities(false)).
if err := pctx.validateAttributeDefaultsWFC(ctx); err != nil {
return err
}
pctx.cleanSpecialAttributes()
pctx.instate = psPrologue
if err := pctx.parseMisc(ctx); err != nil {
return pctx.error(ctx, err)
}
}
pctx.skipBlanks(ctx)
if pctx.blankRunErr != nil {
return pctx.error(ctx, pctx.blankRunErr)
}
if cur.Peek() != '<' {
// A cancelled or failed read can leave the cursor at an apparent end
// of input; report the underlying cause, in place of a misleading
// "start tag expected".
if err := ctx.Err(); err != nil {
return err
}
if err := pctx.cursorDecodeErr(); err != nil {
return pctx.error(ctx, err)
}
return pctx.error(ctx, ErrEmptyDocument)
}
u8, err := pctx.contentCursor()
if err != nil {
return pctx.error(ctx, err)
}
pctx.instate = psContent
pctx.armBodyPoll(ctx)
err = pctx.parseElement(ctx, u8)
pctx.bodyNeverDone = false
if 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)
}
// A clean Done() may mask a transcoding/decode error (e.g. an unpaired
// UTF-16 surrogate the decoder replaced with U+FFFD). Surface it as a fatal
// error, so truncated or malformed encoded input is never accepted.
if err := pctx.cursorDecodeErr(); err != nil {
return pctx.error(ctx, err)
}
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
}
// contentCursor returns the input cursor that element content is parsed from.
// Every parse installs a UTF-8 cursor (switchEncoding) before it reaches
// element content, so the element-content functions take the concrete cursor
// as a parameter: its methods are called directly, and inlined where small,
// instead of through strcursor.Cursor, and no level re-fetches it from the
// input stack.
func (pctx *parserCtx) contentCursor() (*strcursor.UTF8Cursor, error) {
cur := pctx.getCursor()
if cur == nil {
return nil, errNoCursor
}
u8, ok := cur.(*strcursor.UTF8Cursor)
if !ok {
return nil, errContentCursor
}
return u8, nil
}
// parseContentInput parses element content from the current input: the entry
// point of a sub-parse (entity replacement text, a node-context fragment).
func (pctx *parserCtx) parseContentInput(ctx context.Context) error {
cur, err := pctx.contentCursor()
if err != nil {
return pctx.error(ctx, err)
}
return pctx.parseContent(ctx, cur)
}
// parseContent parses element content from cur, the cursor contentCursor
// returned. A reference is expanded in its own parser context, so the input
// stack, and with it cur, does not change while the loop runs.
func (pctx *parserCtx) parseContent(ctx context.Context, cur *strcursor.UTF8Cursor) error {
pctx.instate = psContent
doRecover := pctx.options.IsSet(parseRecover)
for {
// Check the context BEFORE cur.Done(), which may refill the cursor
// from an io.Reader and block; this lets a cancelled context be
// observed between reads, ahead of any blocking refill.
if err := pctx.pollErr(ctx); err != nil {
return err
}
if cur.Done() || pctx.stopped {
break
}
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, cur)
}
default:
err = pctx.parseElement(ctx, cur)
}
case '&':
err = pctx.parseReference(ctx)
default:
if err := pctx.parseCharDataContent(ctx, cur); err != nil {
if !doRecover || isParseAbort(err) {
return err
}
if pctx.recoverErr == nil {
pctx.recoverErr = err
}
pctx.disableSAX = true
pctx.wellFormed = false
if err := pctx.skipToRecoverPoint(ctx); err != nil {
return err
}
}
continue
}
if err != nil {
if !doRecover || isParseAbort(err) {
return pctx.error(ctx, err)
}
if pctx.recoverErr == nil {
pctx.recoverErr = err
}
pctx.disableSAX = true
pctx.wellFormed = false
prevLine, prevCol := cur.LineNumber(), cur.Column()
if err := pctx.skipToRecoverPoint(ctx); err != nil {
return err
}
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.
// It returns the context error if the context is cancelled so that recovery
// cannot run (or block on a cursor refill) after cancellation.
func (pctx *parserCtx) skipToRecoverPoint(ctx context.Context) error {
cur := pctx.getCursor()
if cur == nil {
return nil
}
for {
// Check the context BEFORE cur.Done(), which may refill the cursor
// from an io.Reader and block; this lets a cancelled context abort
// recovery, with no spin and no block.
if err := ctx.Err(); err != nil {
return err
}
if cur.Done() {
return nil
}
if cur.Peek() == '<' {
return nil
}
_ = cur.Advance(1)
}
}