-
Notifications
You must be signed in to change notification settings - Fork 10
/
decoder.go
792 lines (724 loc) · 20.4 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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
// © Copyright 2021 HP Development Company, L.P.
// SPDX-License Identifier: BSD-2-Clause
package go3mf
import (
"bytes"
"encoding/xml"
"strconv"
"strings"
"unsafe"
specerr "github.com/hpinc/go3mf/errors"
"github.com/hpinc/go3mf/spec"
)
type modelDecoder struct {
baseDecoder
model *Model
isRoot bool
path string
}
func (d *modelDecoder) Child(name xml.Name) (i int, child spec.ElementDecoder) {
if name.Space == Namespace {
switch name.Local {
case attrResources:
resources, _ := d.model.FindResources(d.path)
child = &resourceDecoder{resources: resources, model: d.model}
i = -1
case attrBuild:
if d.isRoot {
child = &buildDecoder{build: &d.model.Build, model: d.model}
i = -1
}
case attrMetadata:
if d.isRoot {
child = &metadataDecoder{metadatas: &d.model.Metadata, model: d.model}
i = len(d.model.Metadata)
}
}
} else {
dec := spec.NewElementDecoder(name)
child = dec
if dec != nil {
d.model.Any = append(d.model.Any, dec.Element().(spec.Marshaler))
}
i = -1
}
return
}
func (d *modelDecoder) Start(attrs []spec.XMLAttr) (err error) {
if !d.isRoot {
return
}
var requiredExts []string
for _, a := range attrs {
if a.Name.Space == "" {
switch a.Name.Local {
case attrUnit:
var ok bool
if d.model.Units, ok = newUnits(string(a.Value)); !ok {
err = specerr.Append(err, specerr.NewParseAttrError(a.Name.Local, false))
}
case attrThumbnail:
d.model.Thumbnail = string(a.Value)
case attrReqExt:
requiredExts = strings.Fields(string(a.Value))
}
} else {
err = specerr.Append(err, d.noCoreAttribute(a))
}
}
for _, local := range requiredExts {
for i := range d.model.Extensions {
ext := &d.model.Extensions[i]
if ext.LocalName == local {
ext.IsRequired = true
break
}
}
}
return
}
func (d *modelDecoder) noCoreAttribute(a spec.XMLAttr) (err error) {
switch a.Name.Space {
case nsXML:
if a.Name.Local == attrLang {
d.model.Language = string(a.Value)
}
case attrXmlns:
d.model.Extensions = append(d.model.Extensions, Extension{
Namespace: string(a.Value),
LocalName: a.Name.Local,
IsRequired: false,
})
default:
var attr spec.AttrGroup
if attr = d.model.AnyAttr.Get(a.Name.Space); attr == nil {
attr = spec.NewAttrGroup(a.Name.Space, xml.Name{Space: Namespace, Local: attrModel})
d.model.AnyAttr = append(d.model.AnyAttr, attr)
}
err = specerr.Append(err, attr.Unmarshal3MFAttr(a))
}
return
}
type metadataGroupDecoder struct {
baseDecoder
metadatas *MetadataGroup
model *Model
}
func (d *metadataGroupDecoder) Child(name xml.Name) (i int, child spec.ElementDecoder) {
if name.Space == Namespace && name.Local == attrMetadata {
child = &metadataDecoder{metadatas: &d.metadatas.Metadata, model: d.model}
i = len(d.metadatas.Metadata)
}
return
}
func (d *metadataGroupDecoder) Start(attrs []spec.XMLAttr) error {
var errs error
for _, a := range attrs {
var attr spec.AttrGroup
if attr = d.metadatas.AnyAttr.Get(a.Name.Space); attr == nil {
attr = spec.NewAttrGroup(a.Name.Space, xml.Name{Space: Namespace, Local: attrMetadataGroup})
d.metadatas.AnyAttr = append(d.metadatas.AnyAttr, attr)
}
errs = specerr.Append(errs, attr.Unmarshal3MFAttr(a))
}
return errs
}
type metadataDecoder struct {
baseDecoder
model *Model
metadatas *[]Metadata
metadata Metadata
}
func (d *metadataDecoder) namespace(local string) (string, bool) {
for _, ext := range d.model.Extensions {
if ext.LocalName == local {
return ext.Namespace, true
}
}
return "", false
}
func (d *metadataDecoder) Start(attrs []spec.XMLAttr) error {
for _, a := range attrs {
if a.Name.Space != "" {
continue
}
switch a.Name.Local {
case attrName:
d.metadata.Name = a.Name
i := bytes.IndexByte(a.Value, ':')
if i < 0 {
d.metadata.Name.Local = string(a.Value)
} else if _, ok := d.namespace(string(a.Value[0:i])); ok {
d.metadata.Name.Space = string(a.Value[0:i])
d.metadata.Name.Local = string(a.Value[i+1:])
} else {
d.metadata.Name.Local = string(a.Value)
}
case attrType:
d.metadata.Type = string(a.Value)
case attrPreserve:
d.metadata.Preserve, _ = strconv.ParseBool(string(a.Value))
}
}
return nil
}
func (d *metadataDecoder) CharData(txt []byte) {
d.metadata.Value = string(txt)
}
func (d *metadataDecoder) End() {
*d.metadatas = append(*d.metadatas, d.metadata)
}
type buildDecoder struct {
baseDecoder
model *Model
build *Build
}
func (d *buildDecoder) Child(name xml.Name) (i int, child spec.ElementDecoder) {
if name.Space == Namespace && name.Local == attrItem {
child = &buildItemDecoder{build: d.build, model: d.model}
i = len(d.build.Items)
}
return
}
func (d *buildDecoder) Start(attrs []spec.XMLAttr) error {
var errs error
for _, a := range attrs {
var attr spec.AttrGroup
if attr = d.build.AnyAttr.Get(a.Name.Space); attr == nil {
attr = spec.NewAttrGroup(a.Name.Space, xml.Name{Space: Namespace, Local: attrBuild})
d.build.AnyAttr = append(d.build.AnyAttr, attr)
}
errs = specerr.Append(errs, attr.Unmarshal3MFAttr(a))
}
return errs
}
type buildItemDecoder struct {
baseDecoder
model *Model
build *Build
item Item
}
func (d *buildItemDecoder) End() {
d.build.Items = append(d.build.Items, &d.item)
}
func (d *buildItemDecoder) Child(name xml.Name) (i int, child spec.ElementDecoder) {
if name.Space == Namespace && name.Local == attrMetadataGroup {
child = &metadataGroupDecoder{metadatas: &d.item.Metadata, model: d.model}
i = -1
}
return
}
func (d *buildItemDecoder) Start(attrs []spec.XMLAttr) error {
var errs error
for _, a := range attrs {
if a.Name.Space == "" {
errs = specerr.Append(errs, d.parseCoreAttr(a))
} else {
var attr spec.AttrGroup
if attr = d.item.AnyAttr.Get(a.Name.Space); attr == nil {
attr = spec.NewAttrGroup(a.Name.Space, xml.Name{Space: Namespace, Local: attrItem})
d.item.AnyAttr = append(d.item.AnyAttr, attr)
}
errs = specerr.Append(errs, attr.Unmarshal3MFAttr(a))
}
}
return errs
}
func (d *buildItemDecoder) parseCoreAttr(a spec.XMLAttr) (errs error) {
switch a.Name.Local {
case attrObjectID:
val, err := strconv.ParseUint(string(a.Value), 10, 32)
if err != nil {
errs = specerr.Append(errs, specerr.NewParseAttrError(a.Name.Local, true))
}
d.item.ObjectID = uint32(val)
case attrPartNumber:
d.item.PartNumber = string(a.Value)
case attrTransform:
var ok bool
d.item.Transform, ok = spec.ParseMatrix(string(a.Value))
if !ok {
errs = specerr.Append(errs, specerr.NewParseAttrError(a.Name.Local, false))
}
}
return
}
type resourceDecoder struct {
baseDecoder
model *Model
resources *Resources
}
func (d *resourceDecoder) Start(attrs []spec.XMLAttr) error {
var errs error
for _, a := range attrs {
var attr spec.AttrGroup
if attr = d.resources.AnyAttr.Get(a.Name.Space); attr == nil {
attr = spec.NewAttrGroup(a.Name.Space, xml.Name{Space: Namespace, Local: attrResources})
d.resources.AnyAttr = append(d.resources.AnyAttr, attr)
}
errs = specerr.Append(errs, attr.Unmarshal3MFAttr(a))
}
return errs
}
func (d *resourceDecoder) Child(name xml.Name) (i int, child spec.ElementDecoder) {
if name.Space == Namespace {
switch name.Local {
case attrObject:
child = &objectDecoder{resources: d.resources, model: d.model}
i = len(d.resources.Objects)
case attrBaseMaterials:
child = &baseMaterialsDecoder{resources: d.resources}
i = len(d.resources.Assets)
}
} else if ext, ok := spec.Load(name.Space); ok {
dec := ext.NewElementDecoder(name)
i = len(d.resources.Assets)
child = dec
if dec != nil {
d.resources.Assets = append(d.resources.Assets, dec.Element().(Asset))
}
} else {
child = &unknownAssetDecoder{UnknownTokensDecoder: *spec.NewUnknownDecoder(name), resources: d.resources}
i = len(d.resources.Assets)
}
return
}
type baseMaterialsDecoder struct {
baseDecoder
resources *Resources
resource BaseMaterials
baseMaterialDecoder baseMaterialDecoder
}
func (d *baseMaterialsDecoder) End() {
d.resources.Assets = append(d.resources.Assets, &d.resource)
}
func (d *baseMaterialsDecoder) Child(name xml.Name) (i int, child spec.ElementDecoder) {
if name.Space == Namespace && name.Local == attrBase {
child = &d.baseMaterialDecoder
i = len(d.resource.Materials)
}
return
}
func (d *baseMaterialsDecoder) Start(attrs []spec.XMLAttr) error {
var errs error
d.baseMaterialDecoder.resource = &d.resource
for _, a := range attrs {
if a.Name.Space == "" {
if a.Name.Local == attrID {
id, err := strconv.ParseUint(string(a.Value), 10, 32)
if err != nil {
errs = specerr.Append(errs, specerr.NewParseAttrError(a.Name.Local, true))
}
d.resource.ID = uint32(id)
}
} else {
var attr spec.AttrGroup
if attr = d.resource.AnyAttr.Get(a.Name.Space); attr == nil {
attr = spec.NewAttrGroup(a.Name.Space, xml.Name{Space: Namespace, Local: attrBaseMaterials})
d.resource.AnyAttr = append(d.resource.AnyAttr, attr)
}
errs = specerr.Append(errs, attr.Unmarshal3MFAttr(a))
}
}
return errs
}
type baseMaterialDecoder struct {
baseDecoder
resource *BaseMaterials
}
func (d *baseMaterialDecoder) Start(attrs []spec.XMLAttr) error {
var (
base Base
errs error
)
for _, a := range attrs {
if a.Name.Space == "" {
switch a.Name.Local {
case attrName:
base.Name = string(a.Value)
case attrDisplayColor:
var err error
base.Color, err = spec.ParseRGBA(string(a.Value))
if err != nil {
errs = specerr.Append(errs, specerr.NewParseAttrError(a.Name.Local, true))
}
}
} else {
var attr spec.AttrGroup
if attr = base.AnyAttr.Get(a.Name.Space); attr == nil {
attr = spec.NewAttrGroup(a.Name.Space, xml.Name{Space: Namespace, Local: attrBase})
base.AnyAttr = append(base.AnyAttr, attr)
}
errs = specerr.Append(errs, attr.Unmarshal3MFAttr(a))
}
}
d.resource.Materials = append(d.resource.Materials, base)
return errs
}
type meshDecoder struct {
baseDecoder
resource *Object
}
func (d *meshDecoder) Start(attrs []spec.XMLAttr) error {
d.resource.Mesh = new(Mesh)
var errs error
for _, a := range attrs {
var attr spec.AttrGroup
if attr = d.resource.Mesh.AnyAttr.Get(a.Name.Space); attr == nil {
attr = spec.NewAttrGroup(a.Name.Space, xml.Name{Space: Namespace, Local: attrMesh})
d.resource.Mesh.AnyAttr = append(d.resource.Mesh.AnyAttr, attr)
}
errs = specerr.Append(errs, attr.Unmarshal3MFAttr(a))
}
return errs
}
func (d *meshDecoder) Child(name xml.Name) (i int, child spec.ElementDecoder) {
if name.Space == Namespace {
if name.Local == attrVertices {
child = &verticesDecoder{mesh: d.resource.Mesh}
i = -1
} else if name.Local == attrTriangles {
child = &trianglesDecoder{resource: d.resource}
i = -1
}
} else {
dec := spec.NewElementDecoder(name)
child = dec
if dec != nil {
d.resource.Mesh.Any = append(d.resource.Mesh.Any, dec.Element().(spec.Marshaler))
}
i = -1
}
return
}
type verticesDecoder struct {
baseDecoder
mesh *Mesh
vertexDecoder vertexDecoder
}
func (d *verticesDecoder) Start(attrs []spec.XMLAttr) error {
d.vertexDecoder.mesh = d.mesh
var errs error
for _, a := range attrs {
var attr spec.AttrGroup
if attr = d.mesh.Vertices.AnyAttr.Get(a.Name.Space); attr == nil {
attr = spec.NewAttrGroup(a.Name.Space, xml.Name{Space: Namespace, Local: attrVertices})
d.mesh.Vertices.AnyAttr = append(d.mesh.Vertices.AnyAttr, attr)
}
errs = specerr.Append(errs, attr.Unmarshal3MFAttr(a))
}
return errs
}
func (d *verticesDecoder) Child(name xml.Name) (i int, child spec.ElementDecoder) {
if name.Space == Namespace && name.Local == attrVertex {
child = &d.vertexDecoder
i = len(d.mesh.Vertices.Vertex)
}
return
}
type vertexDecoder struct {
baseDecoder
mesh *Mesh
}
func (d *vertexDecoder) Start(attrs []spec.XMLAttr) error {
var (
x, y, z float32
errs error
)
for _, a := range attrs {
if a.Name.Space != "" {
continue
}
val, err := strconv.ParseFloat(*(*string)(unsafe.Pointer(&a.Value)), 32)
if err != nil {
errs = specerr.Append(errs, specerr.NewParseAttrError(a.Name.Local, true))
}
switch a.Name.Local {
case attrX:
x = float32(val)
case attrY:
y = float32(val)
case attrZ:
z = float32(val)
}
}
d.mesh.Vertices.Vertex = append(d.mesh.Vertices.Vertex, Point3D{x, y, z})
return errs
}
type trianglesDecoder struct {
baseDecoder
resource *Object
triangleDecoder triangleDecoder
}
func (d *trianglesDecoder) Start(attrs []spec.XMLAttr) error {
d.triangleDecoder.mesh = d.resource.Mesh
d.triangleDecoder.defaultPropertyID = d.resource.PID
d.triangleDecoder.defaultPropertyIndex = d.resource.PIndex
if len(d.resource.Mesh.Triangles.Triangle) == 0 && len(d.resource.Mesh.Vertices.Vertex) > 0 {
d.resource.Mesh.Triangles.Triangle = make([]Triangle, 0, len(d.resource.Mesh.Vertices.Vertex)*2)
}
var errs error
for _, a := range attrs {
var attr spec.AttrGroup
if attr = d.resource.Mesh.Triangles.AnyAttr.Get(a.Name.Space); attr == nil {
attr = spec.NewAttrGroup(a.Name.Space, xml.Name{Space: Namespace, Local: attrTriangles})
d.resource.Mesh.Triangles.AnyAttr = append(d.resource.Mesh.Triangles.AnyAttr, attr)
}
errs = specerr.Append(errs, attr.Unmarshal3MFAttr(a))
}
return errs
}
func (d *trianglesDecoder) Child(name xml.Name) (i int, child spec.ElementDecoder) {
if name.Space == Namespace && name.Local == attrTriangle {
child = &d.triangleDecoder
i = len(d.resource.Mesh.Triangles.Triangle)
}
return
}
type triangleDecoder struct {
baseDecoder
mesh *Mesh
defaultPropertyIndex, defaultPropertyID uint32
}
func (d *triangleDecoder) Start(attrs []spec.XMLAttr) error {
var (
t Triangle
pid, p1, p2, p3 uint32
hasPID, hasP1, hasP2, hasP3 bool
errs error
)
for _, a := range attrs {
if a.Name.Space == "" {
required := true
val, err := strconv.ParseUint(string(a.Value), 10, 32)
switch a.Name.Local {
case attrV1:
t.V1 = uint32(val)
case attrV2:
t.V2 = uint32(val)
case attrV3:
t.V3 = uint32(val)
case attrPID:
pid = uint32(val)
hasPID = true
required = false
case attrP1:
p1 = uint32(val)
hasP1 = true
required = false
case attrP2:
p2 = uint32(val)
hasP2 = true
required = false
case attrP3:
p3 = uint32(val)
hasP3 = true
required = false
}
if err != nil {
errs = specerr.Append(errs, specerr.NewParseAttrError(a.Name.Local, required))
}
} else {
var attr spec.AttrGroup
if attr = t.AnyAttr.Get(a.Name.Space); attr == nil {
attr = spec.NewAttrGroup(a.Name.Space, xml.Name{Space: Namespace, Local: attrTriangle})
t.AnyAttr = append(t.AnyAttr, attr)
}
errs = specerr.Append(errs, attr.Unmarshal3MFAttr(a))
}
}
p1 = applyDefault(p1, d.defaultPropertyIndex, hasP1)
p2 = applyDefault(p2, p1, hasP2)
p3 = applyDefault(p3, p1, hasP3)
pid = applyDefault(pid, d.defaultPropertyID, hasPID)
t.PID = pid
t.P1, t.P2, t.P3 = p1, p2, p3
d.mesh.Triangles.Triangle = append(d.mesh.Triangles.Triangle, t)
return errs
}
func applyDefault(val, defVal uint32, noDef bool) uint32 {
if noDef {
return val
}
return defVal
}
type objectDecoder struct {
baseDecoder
model *Model
resources *Resources
resource Object
}
func (d *objectDecoder) End() {
d.resources.Objects = append(d.resources.Objects, &d.resource)
}
func (d *objectDecoder) Start(attrs []spec.XMLAttr) error {
var errs error
for _, a := range attrs {
if a.Name.Space == "" {
errs = specerr.Append(errs, d.parseCoreAttr(a))
} else {
var attr spec.AttrGroup
if attr = d.resource.AnyAttr.Get(a.Name.Space); attr == nil {
attr = spec.NewAttrGroup(a.Name.Space, xml.Name{Space: Namespace, Local: attrObject})
d.resource.AnyAttr = append(d.resource.AnyAttr, attr)
}
errs = specerr.Append(errs, attr.Unmarshal3MFAttr(a))
}
}
return errs
}
func (d *objectDecoder) Child(name xml.Name) (i int, child spec.ElementDecoder) {
if name.Space == Namespace {
if name.Local == attrMesh {
child = &meshDecoder{resource: &d.resource}
i = -1
} else if name.Local == attrComponents {
child = &componentsDecoder{resource: &d.resource}
i = -1
} else if name.Local == attrMetadataGroup {
child = &metadataGroupDecoder{metadatas: &d.resource.Metadata, model: d.model}
i = -1
}
}
return
}
func (d *objectDecoder) parseCoreAttr(a spec.XMLAttr) (errs error) {
switch a.Name.Local {
case attrID:
id, err := strconv.ParseUint(string(a.Value), 10, 32)
if err != nil {
errs = specerr.Append(errs, specerr.NewParseAttrError(a.Name.Local, true))
}
d.resource.ID = uint32(id)
case attrType:
var ok bool
d.resource.Type, ok = newObjectType(string(a.Value))
if !ok {
errs = specerr.Append(errs, specerr.NewParseAttrError(a.Name.Local, false))
}
case attrThumbnail:
d.resource.Thumbnail = string(a.Value)
case attrName:
d.resource.Name = string(a.Value)
case attrPartNumber:
d.resource.PartNumber = string(a.Value)
case attrPID:
val, err := strconv.ParseUint(string(a.Value), 10, 32)
if err != nil {
errs = specerr.Append(errs, specerr.NewParseAttrError(a.Name.Local, false))
}
d.resource.PID = uint32(val)
case attrPIndex:
val, err := strconv.ParseUint(string(a.Value), 10, 32)
if err != nil {
errs = specerr.Append(errs, specerr.NewParseAttrError(a.Name.Local, false))
}
d.resource.PIndex = uint32(val)
}
return
}
type componentsDecoder struct {
baseDecoder
resource *Object
componentDecoder componentDecoder
}
func (d *componentsDecoder) Start(attrs []spec.XMLAttr) error {
var errs error
components := new(Components)
d.componentDecoder.resource = d.resource
for _, a := range attrs {
var attr spec.AttrGroup
if attr = components.AnyAttr.Get(a.Name.Space); attr == nil {
attr = spec.NewAttrGroup(a.Name.Space, xml.Name{Space: Namespace, Local: attrComponents})
components.AnyAttr = append(components.AnyAttr, attr)
}
errs = specerr.Append(errs, attr.Unmarshal3MFAttr(a))
}
d.resource.Components = components
return errs
}
func (d *componentsDecoder) Child(name xml.Name) (i int, child spec.ElementDecoder) {
if name.Space == Namespace && name.Local == attrComponent {
child = &d.componentDecoder
i = len(d.resource.Components.Component)
}
return
}
type componentDecoder struct {
baseDecoder
resource *Object
}
func (d *componentDecoder) Start(attrs []spec.XMLAttr) error {
var (
component Component
errs error
)
for _, a := range attrs {
if a.Name.Space == "" {
if a.Name.Local == attrObjectID {
val, err := strconv.ParseUint(string(a.Value), 10, 32)
if err != nil {
errs = specerr.Append(errs, specerr.NewParseAttrError(a.Name.Local, true))
}
component.ObjectID = uint32(val)
} else if a.Name.Local == attrTransform {
var ok bool
component.Transform, ok = spec.ParseMatrix(string(a.Value))
if !ok {
errs = specerr.Append(errs, specerr.NewParseAttrError(a.Name.Local, false))
}
}
} else {
var attr spec.AttrGroup
if attr = component.AnyAttr.Get(a.Name.Space); attr == nil {
attr = spec.NewAttrGroup(a.Name.Space, xml.Name{Space: Namespace, Local: attrComponent})
component.AnyAttr = append(component.AnyAttr, attr)
}
errs = specerr.Append(errs, attr.Unmarshal3MFAttr(a))
}
}
d.resource.Components.Component = append(d.resource.Components.Component, &component)
return errs
}
type baseDecoder struct {
}
func (d *baseDecoder) Start([]spec.XMLAttr) error { return nil }
func (d *baseDecoder) End() {}
type topLevelDecoder struct {
baseDecoder
model *Model
isRoot bool
path string
}
func (d *topLevelDecoder) Child(name xml.Name) (i int, child spec.ElementDecoder) {
modelName := xml.Name{Space: Namespace, Local: attrModel}
if name == modelName {
child = &modelDecoder{model: d.model, isRoot: d.isRoot, path: d.path}
i = -1
}
return
}
type unknownAssetDecoder struct {
spec.UnknownTokensDecoder
resources *Resources
resource UnknownAsset
}
func (d *unknownAssetDecoder) Start(attrs []spec.XMLAttr) (errs error) {
d.UnknownTokensDecoder.Start(attrs)
for _, a := range attrs {
if a.Name.Space == "" && a.Name.Local == attrID {
id, err := strconv.ParseUint(string(a.Value), 10, 32)
if err != nil {
errs = specerr.Append(errs, specerr.NewParseAttrError(a.Name.Local, true))
}
d.resource.id = uint32(id)
break
}
}
return errs
}
func (d *unknownAssetDecoder) End() {
d.UnknownTokensDecoder.End()
d.resource.UnknownTokens = d.UnknownTokensDecoder.Tokens()
d.resources.Assets = append(d.resources.Assets, &d.resource)
}