-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconverter.go
84 lines (60 loc) · 1.42 KB
/
converter.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
/**
* converts RTF to text or html based on the RTF source
**/
package rtfconverter
import (
"errors"
"io/ioutil"
// "fmt"
)
type RtfInterpreter interface {
Parse(rtfObj RtfStructure) ([]byte, error)
}
type rtfConverter struct {
rtfObj RtfStructure
}
/**
* create a new convertor
*/
func NewConverter() (rtfConverter) {
c := rtfConverter{}
return c;
}
func (c *rtfConverter) LoadFile(sourceFile string) {
c.rtfObj = RtfStructure{}
// decompose RTF into structure based on words, symbols, etc
c.rtfObj.ParseFile(sourceFile)
}
func (c *rtfConverter) SetBytes(content []byte) {
c.rtfObj = RtfStructure{}
// decompose RTF into structure based on words, symbols, etc
c.rtfObj.ParseBytes(content)
}
func (c *rtfConverter) SaveFile(content []byte, path string) (error) {
err := ioutil.WriteFile(path, content , 0644)
return err
}
func (c *rtfConverter) Convert(exportType string) (result []byte, err error) {
var (
parser RtfInterpreter
)
parser, err = c.getInterpreter(exportType)
if err != nil {
return result, err
}
result, err = parser.Parse(c.rtfObj)
if err != nil {
return nil, err
}
return result, nil
}
func (c *rtfConverter) getInterpreter(interpreterType string) (RtfInterpreter, error) {
switch interpreterType {
case "html":
return &rtfHtmlInterpreter{}, nil
case "text":
return &rtfTextInterpreter{}, nil
default:
return nil, errors.New("Parser for conversion do not exists.")
}
}