-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathdecoder.go
410 lines (323 loc) · 10.7 KB
/
decoder.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
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
// Decoding a Document
//
// Decoding a GEDCOM stream:
//
// ged := "0 HEAD\n1 CHAR UTF-8"
//
// decoder := gedcom.NewDecoder(strings.NewReader(ged))
// document, err := decoder.Decode()
// if err != nil {
// panic(err)
// }
//
// If you are reading from a file you can use NewDocumentFromGEDCOMFile:
//
// document, err := gedcom.NewDocumentFromGEDCOMFile("family.ged")
// if err != nil {
// panic(err)
// }
//
package gedcom
import (
"bufio"
"bytes"
"fmt"
"io"
"regexp"
"strconv"
"strings"
)
// See Decoder.consumeOptionalBOM().
var byteOrderMark = []byte{0xef, 0xbb, 0xbf}
// Decoder represents a GEDCOM decoder.
type Decoder struct {
r *bufio.Reader
// It is not valid for GEDCOM values to contain new lines or carriage
// returns. However, some application dump data without correctly using the
// CONT tags.
//
// Strictly speaking we should bail out with an error but there are too many
// cases that are difficult to clean up for consumers so we offer and option
// to permit it.
//
// When enabled any line than cannot be parsed will be considered an
// extension of the previous line (including the new line character).
AllowMultiLine bool
// AllowInvalidIndents allows a child node to have an indent greater than +1
// of the parent. AllowInvalidIndents is disabled by default because if this
// happens the GEDCOM file is broken in some possibly serious way and
// certainly not a valid GEDCOM file.
//
// The biggest problem with having the indents wrongly aligned is that nodes
// that are expected to be a certain depth (such as NPFX inside a NAME) will
// probably break or interfere with a traversal algorithm that is not
// expecting the node to be there/at that level.
//
// Another important thing to note is that the incorrect indent level will
// not be retained when writing the Document back to a GEDCOM.
AllowInvalidIndents bool
}
// Create a new decoder to parse a reader that contain GEDCOM data.
func NewDecoder(r io.Reader) *Decoder {
return &Decoder{
r: bufio.NewReader(r),
}
}
// Decode will parse the entire GEDCOM stream (until EOF is reached) and return
// a Document. If the GEDCOM stream is not valid then the document node will
// be nil and the error is returned.
//
// A blank GEDCOM or a GEDCOM that only contains empty lines is valid and a
// Document will be returned with zero nodes.
func (dec *Decoder) Decode() (*Document, error) {
document := NewDocument()
indents := Nodes{}
var family *FamilyNode
document.HasBOM = dec.consumeOptionalBOM()
finished := false
lineNumber := 0
// Only used when AllowMultiLine is enabled.
var previousNode Node
for !finished {
lineNumber++
line, err := dec.readLine()
if err != nil {
if err != io.EOF {
return nil, err
}
finished = true
}
// Skip blank lines.
if line == "" {
if dec.AllowMultiLine && previousNode != nil {
previousNode.RawSimpleNode().value += "\n"
}
continue
}
node, indent, err := parseLine(line, document, family)
if err != nil {
if dec.AllowMultiLine && previousNode != nil {
previousNode.RawSimpleNode().value += "\n" + line
continue
}
return nil, fmt.Errorf("line %d: %s", lineNumber, err)
}
// Families cannot be nested so any children that appear after this node
// will be attached to the most recently seen family. We do not need to
// set this back to nil after we exit the family node.
if f, ok := node.(*FamilyNode); ok {
family = f
}
// Add a root node to the document.
if indent == 0 {
dec.trimNodeValue(previousNode)
document.AddNode(node)
previousNode = node
// There can be multiple root nodes so make sure we always reset all
// indent pointers.
indents = Nodes{node}
continue
}
if indent-1 >= len(indents) {
// This means the file is not valid. I have seen it in very rare
// cases. See full explanation in AllowInvalidIndents.
if dec.AllowInvalidIndents {
indent = len(indents)
} else {
panic(fmt.Sprintf(
"indent is too large - missing parent? at line %d: %s",
lineNumber, line))
}
}
i := indents[indent-1]
switch {
case indent >= len(indents):
// Descending one level. It is not valid for a child to have an
// indent that is more than one greater than the parent. This would
// be a corrupt GEDCOM and lead to a panic.
indents = append(indents, node)
case indent < len(indents)-1:
// Moving back to a parent. It is possible for this leap to be
// greater than one so trim the indent levels back as many times as
// needed to represent the new indent level.
indents = indents[:indent+1]
indents[indent] = node
default:
// This case would be "indent == len(indents)-1" (the indent does
// not change from the previous line). However, since it is the only
// other logical possibility there is no need to evaluate it for the
// case condition.
//
// Make sure we update the current indent with the new node so that
// children get place on this node and not the previous one.
indents[indent] = node
}
dec.trimNodeValue(previousNode)
i.AddNode(node)
previousNode = node
}
dec.trimNodeValue(previousNode)
// Build the cache once.
document.buildPointerCache()
return document, nil
}
func (dec *Decoder) trimNodeValue(previousNode Node) {
// When AllowMultiLine is enabled we have to be careful to trim the
// surrounding spaces off the value so it can be interpreted correct.
//
// Another solution would be to ignore blank lines entirely, but then we
// would miss the paragraph separators in multiline text.
if !IsNil(previousNode) {
newValue := strings.TrimSpace(previousNode.RawSimpleNode().value)
previousNode.RawSimpleNode().value = newValue
}
}
func (dec *Decoder) readLine() (string, error) {
buf := new(bytes.Buffer)
for {
b, err := dec.r.ReadByte()
if err != nil {
return string(buf.Bytes()), err
}
// The line endings in the GEDCOM files can be different. A newline and
// carriage return are both considered to be the end of the line and
// empty lines are ignored so we can treat both of these characters as
// independent line terminators.
if b == '\n' || b == '\r' {
break
}
buf.WriteByte(b)
}
return string(buf.Bytes()), nil
}
var lineRegexp = regexp.MustCompile(`^(\d) +(@[^@]+@ )?(\w+) ?(.*)?$`)
func parseLine(line string, document *Document, family *FamilyNode) (Node, int, error) {
parts := lineRegexp.FindStringSubmatch(line)
if len(parts) == 0 {
return nil, 0, fmt.Errorf("could not parse: %s", line)
}
// Indent (required).
indent, _ := strconv.Atoi(parts[1])
// Pointer (optional).
pointer := ""
if parts[2] != "" {
// Trim off the surrounding '@'.
pointer = parts[2][1 : len(parts[2])-2]
}
// Tag (required).
tag := TagFromString(parts[3])
// Value (optional).
value := parts[4]
return newNode(document, family, tag, value, pointer), indent, nil
}
// NewNode creates a node with no children. It is also the correct way to
// create a shallow copy of a node.
//
// If the node tag is recognised as a more specific type, such as *DateNode then
// that will be returned. Otherwise a *SimpleNode will be used.
func NewNode(tag Tag, value, pointer string, children ...Node) Node {
return newNodeWithChildren(nil, nil, tag, value, pointer, children)
}
func newNode(document *Document, family *FamilyNode, tag Tag, value, pointer string) Node {
return newNodeWithChildren(document, family, tag, value, pointer, nil)
}
func newNodeWithChildren(document *Document, family *FamilyNode, tag Tag, value, pointer string, children Nodes) Node {
var node Node
switch tag {
case TagBaptism:
node = NewBaptismNode(value, children...)
case TagBirth:
node = NewBirthNode(value, children...)
case TagBurial:
node = NewBurialNode(value, children...)
case TagChild:
needsFamily(family, tag)
node = newChildNode(family, value, children...)
case TagDate:
node = NewDateNode(value, children...)
case TagDeath:
node = NewDeathNode(value, children...)
case TagEvent:
node = NewEventNode(value, children...)
case TagFamily:
needsDocument(document, tag)
node = newFamilyNode(document, pointer, children...)
case UnofficialTagFamilySearchID1, UnofficialTagFamilySearchID2:
node = NewFamilySearchIDNode(tag, value, children...)
case TagFormat:
node = NewFormatNode(value, children...)
case TagHusband:
needsFamily(family, tag)
node = newHusbandNode(family, value, children...)
case TagIndividual:
needsDocument(document, tag)
node = newIndividualNode(document, pointer, children...)
case TagLatitude:
node = NewLatitudeNode(value, children...)
case TagLongitude:
node = NewLongitudeNode(value, children...)
case TagMap:
node = NewMapNode(value, children...)
case TagName:
node = NewNameNode(value, children...)
case TagNickname:
node = NewNicknameNode(value, children...)
case TagNote:
node = NewNoteNode(value, children...)
case TagPhonetic:
node = NewPhoneticVariationNode(value, children...)
case TagPlace:
node = NewPlaceNode(value, children...)
case TagResidence:
node = NewResidenceNode(value, children...)
case TagRomanized:
node = NewRomanizedVariationNode(value, children...)
case TagSex:
node = NewSexNode(value)
case TagSource:
node = NewSourceNode(value, pointer, children...)
case TagType:
node = NewTypeNode(value, children...)
case UnofficialTagUniqueID:
node = NewUniqueIDNode(value, children...)
case TagWife:
needsFamily(family, tag)
node = newWifeNode(family, value, children...)
}
if IsNil(node) {
node = newSimpleNode(tag, value, pointer, children...)
} else {
simpleNode := node.RawSimpleNode()
simpleNode.pointer = pointer
}
return node
}
func needsDocument(document *Document, tag Tag) {
if document == nil {
panic(fmt.Sprintf("cannot create %s without a document", tag))
}
}
func needsFamily(family *FamilyNode, tag Tag) {
if family == nil {
panic(fmt.Sprintf("cannot create %s without a family", tag))
}
}
// consumeOptionalBOM will test and discard the Byte Order Mark at the start of
// the stream.
//
// In order to keep the original stream as intact as possible when encoding the
// BOM will be written back if it existed originally.
//
// Use of a BOM is neither required nor recommended for UTF-8, but may be
// encountered in contexts where UTF-8 data is converted from other encoding
// forms that use a BOM or where the BOM is used as a UTF-8 signature. See the
// “Byte Order Mark” subsection in Section 16.8, Specials, for more information.
// - 2.6 Encoding Schemes, http://www.unicode.org/versions/Unicode5.0.0/ch02.pdf
func (dec *Decoder) consumeOptionalBOM() bool {
possibleBOM, _ := dec.r.Peek(3)
hasBOM := bytes.Compare(possibleBOM, byteOrderMark) == 0
if hasBOM {
dec.r.Discard(3)
}
return hasBOM
}