-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathxmlwriter.functions
118 lines (100 loc) · 3.15 KB
/
xmlwriter.functions
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
# This file is part of shellfire xmlwriter. It is subject to the licence terms in the COPYRIGHT file found in the top-level directory of this distribution and at https://raw.githubusercontent.com/shellfire-dev/xmlwriter/master/COPYRIGHT. No part of shellfire xmlwriter, including this file, may be copied, modified, propagated, or distributed except according to the terms contained in the COPYRIGHT file.
# Copyright © 2014-2015 The developers of shellfire xmlwriter. See the COPYRIGHT file in the top-level directory of this distribution and at https://raw.githubusercontent.com/shellfire-dev/xmlwriter/master/COPYRIGHT.
core_usesIn core variable
xmlwriter_declaration()
{
local version="$1"
local encoding="$2"
local standalone="$3"
if core_variable_isTrue "$standalone"; then
printf '<?xml version="%s" encoding="%s" standlone="yes"?>' "$version" "$encoding"
else
printf '<?xml version="%s" encoding="%s"?>' "$version" "$encoding"
fi
}
xmlwriter_dtd()
{
local name="$1"
local path="$2"
printf '<!DOCTYPE %s PUBLIC "%s" "%s.dtd">' "$name" "$path" "$name"
}
xmlwriter_open()
{
local nodeName="$1"
shift 1
printf '<%s' "$nodeName"
local attributeName
local attributeValue
while [ $# -gt 1 ]
do
attributeName="$1"
attributeValue="$2"
shift 2
printf ' %s="' "$attributeName"
xmlwriter_escape_attributeValue "$attributeValue"
printf '"'
done
printf '>'
}
xmlwriter_close()
{
printf '</%s>' "$1"
}
xmlwriter_leaf()
{
local nodeName="$1"
shift 1
printf '<%s' "$nodeName"
local attributeName
local attributeValue
while [ $# -gt 1 ]
do
attributeName="$1"
attributeValue="$2"
shift 2
printf ' %s="' "$attributeName"
xmlwriter_escape_attributeValue "$attributeValue"
printf '"'
done
if [ $# -eq 1 ]; then
printf '>'
xmlwriter_escape_value "$1"
printf '</%s>' "$nodeName"
else
printf '/>'
fi
}
core_dependency_requires '*' sed
xmlwriter_escape_attributeValue()
{
local value="$1"
# We do all replacements as capturing the value again causes line feeds to be lost
if [ "$value" != "${value%*&*}" ]; then
printf '%s' "$value" | sed -e 's/\&/\&/g' -e 's/</\</g' -e 's/>/\>/g' -e "s/'/\'/g" -e 's/"/\"/g'
elif [ "$value" != "${value%*<*}" ]; then
printf '%s' "$value" | sed -e 's/</\</g' -e 's/>/\>/g' -e "s/'/\'/g" -e 's/"/\"/g'
elif [ "$value" != "${value%*>*}" ]; then
printf '%s' "$value" | sed -e 's/>/\>/g' -e "s/'/\'/g" -e 's/"/\"/g'
elif [ "$value" != "${value%*\'*}" ]; then
printf '%s' "$value" | sed -e "s/'/\'/g" -e 's/"/\"/g'
elif [ "$value" != "${value%*\"*}" ]; then
printf '%s' "$value" | sed -e 's/"/\"/g'
else
printf '%s' "$1"
fi
}
core_dependency_requires '*' sed
xmlwriter_escape_value()
{
local value="$1"
# We do all replacements as capturing the value again causes line feeds to be lost
if [ "$value" != "${value%*&*}" ]; then
printf '%s' "$value" | sed -e 's/\&/\&/g' -e 's/</\</g' -e 's/>/\>/g'
elif [ "$value" != "${value%*<*}" ]; then
printf '%s' "$value" | sed -e 's/</\</g' -e 's/>/\>/g'
elif [ "$value" != "${value%*>*}" ]; then
printf '%s' "$value" | sed -e 's/>/\>/g'
else
printf '%s' "$1"
fi
}