-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathnamespaces.go
99 lines (77 loc) · 3.11 KB
/
namespaces.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
// 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 ml
import (
"encoding/xml"
"fmt"
)
const (
//NamespaceXML is a XML namespace
NamespaceXML = "http://www.w3.org/XML/1998/namespace"
//NamespaceRelationships is a namespace for OOXML relationships
NamespaceRelationships = "http://schemas.openxmlformats.org/officeDocument/2006/relationships"
//NamespaceVML is general VML drawings namespace
NamespaceVML = "urn:schemas-microsoft-com:vml"
//NamespaceVMLOffice is general VML Office drawings namespace
NamespaceVMLOffice = "urn:schemas-microsoft-com:office:office"
//NamespaceVMLExcel is Excel related VML drawing namespace
NamespaceVMLExcel = "urn:schemas-microsoft-com:office:excel"
//NamespaceVMLWord is Word related VML drawing namespace
NamespaceVMLWord = "urn:schemas-microsoft-com:office:word"
//NamespaceVMLPowerPoint is PowerPoint related VML drawing namespace
NamespaceVMLPowerPoint = "urn:schemas-microsoft-com:office:powerpoint"
//NamespaceDrawing is general DrawingML namespace
NamespaceDrawing = "http://schemas.openxmlformats.org/drawingml/2006/main"
//NamespaceDrawingExcel is a namespace for Excel DrawingML definition
NamespaceDrawingExcel = "http://schemas.openxmlformats.org/drawingml/2006/spreadsheetDrawing"
//NamespaceDrawingWord is a namespace for Word DrawingML definition
NamespaceDrawingWord = "http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing"
//NamespaceDrawingChart is a namespace for Chart DrawingML definition
NamespaceDrawingChart = "http://schemas.openxmlformats.org/drawingml/2006/chart"
)
var (
namespacePrefixes map[string]string
)
func init() {
namespacePrefixes = map[string]string{
NamespaceRelationships: "r",
NamespaceVML: "v",
NamespaceVMLOffice: "o",
NamespaceVMLExcel: "x",
NamespaceVMLWord: "w",
NamespaceVMLPowerPoint: "p",
NamespaceDrawing: "a",
NamespaceDrawingExcel: "xdr",
NamespaceDrawingWord: "wp",
NamespaceDrawingChart: "c",
}
}
//ErrorNamespace returns error for unknown namespace
func ErrorNamespace(namespace string) error {
return fmt.Errorf("can't resolve prefix for: %s", namespace)
}
//ResolveNamespacePrefix is helper function that tries to resolve prefix for known namespace
func ResolveNamespacePrefix(namespace string) (prefix string, ok bool) {
prefix, ok = namespacePrefixes[namespace]
return
}
//ApplyNamespacePrefix adds namespace prefix to Local name and drops Space name
func ApplyNamespacePrefix(namespace string, name xml.Name) xml.Name {
if prefix, ok := ResolveNamespacePrefix(namespace); ok {
return xml.Name{
Local: prefix + ":" + name.Local,
}
}
return name
}
//Namespaces transform list of namespaces into list of related attributes
func Namespaces(namespaces ...string) []xml.Attr {
attrs := make([]xml.Attr, 0, len(namespaces))
for _, namespace := range namespaces {
if prefix, ok := ResolveNamespacePrefix(namespace); ok {
attrs = append(attrs, xml.Attr{Name: xml.Name{Local: "xmlns:" + prefix}, Value: namespace})
}
}
return attrs
}