-
Notifications
You must be signed in to change notification settings - Fork 3
/
relationships.go
147 lines (121 loc) · 3.52 KB
/
relationships.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
// Copyright (c) 2017 Andrey Gayvoronsky <plandem@gmail.com>
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package ooxml
import (
"fmt"
"github.com/plandem/ooxml/ml"
"math"
"regexp"
"strconv"
"strings"
)
//Relationships is a higher level object that wraps OOXML relationships with functionality
type Relationships struct {
ml ml.Relationships
pkg *PackageInfo
file *PackageFile
}
//NewRelationships creates and returns relationships
func NewRelationships(f interface{}, pkg *PackageInfo) *Relationships {
rels := &Relationships{
pkg: pkg,
}
rels.file = NewPackageFile(pkg, f, &rels.ml, nil)
rels.file.LoadIfRequired(nil)
if rels.file.IsNew() {
pkg.ContentTypes().RegisterContent(rels.file.FileName(), ContentTypeRelationships)
rels.file.MarkAsUpdated()
}
return rels
}
//FileName returns file name of relations
func (rels *Relationships) FileName() string {
return rels.file.fileName
}
//Total returns total number of relationships
func (rels *Relationships) Total() int {
return len(rels.ml.Relationships)
}
// GetTargetById returns target of relation for provided id
func (rels *Relationships) GetTargetById(id string) string {
for _, r := range rels.ml.Relationships {
if r.ID == id {
return string(r.Target)
}
}
return ""
}
// GetTargetByType returns target of first relation for provided type
func (rels *Relationships) GetTargetByType(t ml.RelationType) string {
for _, r := range rels.ml.Relationships {
if r.Type == t {
return r.Target
}
}
return ""
}
// GetIdByTarget returns id of first relation for provided target
func (rels *Relationships) GetIdByTarget(target string) ml.RID {
for _, r := range rels.ml.Relationships {
rTarget := r.Target
switch r.TargetMode {
case ml.TargetModeInternal:
//is weird case when link is related?
if rTarget[0] != '/' {
rTarget = "/xl/" + rTarget
}
if strings.Contains(string(rTarget), target) {
return ml.RID(r.ID)
}
case ml.TargetModeExternal:
if rTarget == target {
return ml.RID(r.ID)
}
}
}
return ""
}
//AddLink adds a new relation of type t to external target - e.g.: url
func (rels *Relationships) AddLink(t ml.RelationType, target string) (int, ml.RID) {
return rels.add(t, target, ml.TargetModeExternal)
}
//AddFile adds a new relation of type t to internal target, i.e. file inside of package. For simplicity - use absolute paths.
func (rels *Relationships) AddFile(t ml.RelationType, target string) (int, ml.RID) {
if target[0] != '/' {
target = "/" + target
}
return rels.add(t, target, ml.TargetModeInternal)
}
func (rels *Relationships) add(t ml.RelationType, target string, mode ml.TargetMode) (int, ml.RID) {
var id int
regID := regexp.MustCompile(`[\d]+`)
for _, rel := range rels.ml.Relationships {
if rid := regID.FindString(rel.ID); rid != "" {
if rid, err := strconv.ParseUint(rid, 10, 32); err == nil {
id = int(math.Max(float64(rid), float64(id)))
}
}
}
rid := fmt.Sprintf("rId%d", id+1)
relation := ml.Relation{
ID: rid,
Type: t,
Target: target,
TargetMode: mode,
}
rels.ml.Relationships = append(rels.ml.Relationships, relation)
rels.file.MarkAsUpdated()
return id, ml.RID(rid)
}
//Remove removes relation with provided rid
func (rels *Relationships) Remove(rid ml.RID) {
//remove relation
for i, r := range rels.ml.Relationships {
if ml.RID(r.ID) == rid {
rels.ml.Relationships = append(rels.ml.Relationships[:i], rels.ml.Relationships[i+1:]...)
break
}
}
rels.file.MarkAsUpdated()
}