-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathattribute_record.go
More file actions
72 lines (59 loc) · 1.78 KB
/
Copy pathattribute_record.go
File metadata and controls
72 lines (59 loc) · 1.78 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
package binxml
import (
"encoding/xml"
"fmt"
dt "wcfproxy/datatype"
)
type AttributeRecord struct {
Record
Prefix string
Name string
Value ITextRecord
}
func NewAttributeRecord(prefix, name string, value ITextRecord) AttributeRecord {
return AttributeRecord{
Record: Record{Type: RTAttribute},
Prefix: prefix,
Name: name,
Value: value,
}
}
func (r *AttributeRecord) ReadFromBuffer(buf []byte) (int, error) {
n, buf, err := r.Record.ReadFromBufferExpectType(buf, RTAttribute)
if err != nil {
return 0, err
}
nGeneric, buf, err := readGeneric(buf, &r.Prefix, &r.Name)
if err != nil {
return 0, err
}
value, nValue, err := readTextRecord(buf)
if err != nil {
return 0, err
}
r.Value = value
return n + nGeneric + nValue, nil
}
func (r *AttributeRecord) WriteToBuffer(buf []byte) (int, error) {
rLen := r.ByteLength()
if rLen > len(buf) {
return 0, fmt.Errorf("cannot write %v bytes to buffer of size %v", rLen, len(buf))
}
n, _ := r.Record.WriteToBuffer(buf)
buf = buf[n:]
n, _ = dt.WriteStrToBuffer(r.Prefix, buf)
buf = buf[n:]
n, _ = dt.WriteStrToBuffer(r.Name, buf)
buf = buf[n:]
r.Value.WriteToBuffer(buf)
return rLen, nil
}
func (r *AttributeRecord) ToString(dict *StringDict, typehint bool) string {
return fmt.Sprintf(` %s:%s="%s"`, r.Prefix, r.Name, r.Value.ToString(dict, ContextAttribute, typehint)) // whitespace required by spec
}
func (r *AttributeRecord) ToXMLAttr(dict *StringDict, typehint bool) xml.Attr {
return xml.Attr{Name: xml.Name{Space: r.Prefix, Local: r.Name}, Value: r.Value.ToString(dict, ContextAttribute, typehint)}
}
func (r *AttributeRecord) ByteLength() int {
return r.Record.ByteLength() + dt.StrByteLengthSum(r.Prefix, r.Name) + r.Value.ByteLength()
}