-
Notifications
You must be signed in to change notification settings - Fork 4
/
ofd_reader.go
161 lines (147 loc) · 3.35 KB
/
ofd_reader.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
package ofd
import (
"archive/zip"
"encoding/xml"
"errors"
"fmt"
"strings"
)
const (
OFD_PATH = "OFD.xml"
)
type OFDReader struct {
validator Validator
filepath string
rc *zip.ReadCloser
ofd *OFD
}
type Option func(opt *Options)
type Options struct {
validator Validator
}
var defaultOption = Options{
validator: &CommonValidator{},
}
func WithValidator(validator Validator) Option {
return func(opt *Options) {
opt.validator = validator
}
}
func NewOFDReader(path string, opts ...Option) (*OFDReader, error) {
opt := defaultOption
for _, o := range opts {
o(&opt)
}
rc, err := zip.OpenReader(path)
if err != nil {
return nil, err
}
return &OFDReader{
rc: rc,
validator: opt.validator,
filepath: path,
}, nil
}
func (ofdReader *OFDReader) Close() error {
if ofdReader.rc != nil {
return ofdReader.rc.Close()
}
return nil
}
func (ofdReader *OFDReader) OFD() (*OFD, error) {
if ofdReader.ofd == nil {
content, err := ofdReader.GetFileContent(OFD_PATH)
if err != nil {
return nil, err
}
var ofd OFD
if err := xml.Unmarshal(content, &ofd); err != nil {
return nil, err
} else {
ofdReader.ofd = &ofd
return &ofd, nil
}
} else {
return ofdReader.ofd, nil
}
}
type Resource int
const (
DOCUMENT Resource = iota
SIGNATURES
)
func (ofdReader *OFDReader) getResourceById(docId string, resType Resource) (interface{}, error) {
if docId == "" {
return nil, errors.New("docId is empty")
}
ofd, err := ofdReader.OFD()
if err != nil {
return nil, err
}
docs := ofd.DocBody
var path string
for _, doc := range docs {
if !strings.EqualFold(doc.DocInfo.DocID.Text, docId) {
continue
}
switch resType {
case DOCUMENT:
path = doc.DocRoot.Text
case SIGNATURES:
path = doc.Signatures.Text
default:
return nil, fmt.Errorf("resType[%v] is invalid", resType)
}
if path[0] == '/' {
path = path[1:]
}
pos := strings.LastIndexByte(path, '/')
pwd := path[0:pos]
content, err := ofdReader.GetFileContent(path)
if err != nil {
return nil, err
}
switch resType {
case DOCUMENT:
var doc Document
if err := xml.Unmarshal(content, &doc); err != nil {
return nil, err
} else {
doc.pwd = pwd
doc.rc = ofdReader.rc
}
return &doc, nil
case SIGNATURES:
var signatures Signatures
if err := xml.Unmarshal(content, &signatures); err != nil {
return nil, err
} else {
signatures.pwd = pwd
signatures.rc = ofdReader.rc
}
return &signatures, nil
default:
return nil, fmt.Errorf("resType[%v] is invalid", resType)
}
}
return nil, fmt.Errorf("docId [%v] not found", docId)
}
func (ofdReader *OFDReader) GetDocumentById(docId string) (*Document, error) {
res, err := ofdReader.getResourceById(docId, DOCUMENT)
if err != nil {
return nil, err
}
return res.(*Document), nil
}
func (ofdReader *OFDReader) GetSignaturesById(docId string) (*Signatures, error) {
res, err := ofdReader.getResourceById(docId, SIGNATURES)
if err != nil {
return nil, err
}
signatures := res.(*Signatures)
signatures.Validator = ofdReader.validator
return signatures, nil
}
func (ofdReader *OFDReader) GetFileContent(path string) ([]byte, error) {
return LoadZipFileContent(ofdReader.rc, path)
}