-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcontent.go
82 lines (67 loc) · 2.12 KB
/
content.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
// Copyright (c) 2017 Andrey Gayvoronsky <[email protected]>
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package ooxml
import (
"github.com/plandem/ooxml/ml"
)
//ContentTypes is helper object that implements some functionality for content types that is a required part of any OOXML document
type ContentTypes struct {
ml ml.ContentTypes
pkg *PackageInfo
file *PackageFile
}
//newContentTypes creates and returns content types information
func newContentTypes(f interface{}, pkg *PackageInfo) *ContentTypes {
content := &ContentTypes{
pkg: pkg,
}
content.file = NewPackageFile(pkg, f, &content.ml, nil)
content.file.LoadIfRequired(nil)
return content
}
//RegisterType adds information about a new type of content if there is no such type already
func (ct *ContentTypes) RegisterType(extension string, contentType ml.ContentType) {
//check if there is type with such extension already, and if it's here then ignore
for _, def := range ct.ml.Defaults {
if def.Extension == extension {
return
}
}
ct.ml.Defaults = append(ct.ml.Defaults, &ml.TypeDefault{
Extension: extension,
ContentType: contentType,
})
ct.file.MarkAsUpdated()
}
//RegisterContent adds information about a new content with fileName of contentType
func (ct *ContentTypes) RegisterContent(fileName string, contentType ml.ContentType) {
if fileName[0] != '/' {
fileName = "/" + fileName
}
ct.ml.Overrides = append(ct.ml.Overrides, &ml.TypeOverride{
PartName: fileName,
ContentType: contentType,
})
ct.file.MarkAsUpdated()
}
//RemoveContent removes information about a content with fileName
func (ct *ContentTypes) RemoveContent(fileName string) {
for i, part := range ct.ml.Overrides {
if part.PartName == fileName {
ct.ml.Overrides = append(ct.ml.Overrides[:i], ct.ml.Overrides[i+1:]...)
ct.file.MarkAsUpdated()
return
}
}
}
//CountTypes returns total number of contentType items
func (ct *ContentTypes) CountTypes(contentType ml.ContentType) int {
total := 0
for _, part := range ct.ml.Overrides {
if part.ContentType == contentType {
total++
}
}
return total
}