blob: fe7783b21cbf6a0d85a82744588fc80f01a49482 (
plain)
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
|
#!/usr/bin/ksh93
#
# CDDL HEADER START
#
# The contents of this file are subject to the terms of the
# Common Development and Distribution License (the "License").
# You may not use this file except in compliance with the License.
#
# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
# or http://www.opensolaris.org/os/licensing.
# See the License for the specific language governing permissions
# and limitations under the License.
#
# When distributing Covered Code, include this CDDL HEADER in each
# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
# If applicable, add the following below this CDDL HEADER, with the
# fields enclosed by brackets "[]" replaced with your own identifying
# information: Portions Copyright [yyyy] [name of copyright owner]
#
# CDDL HEADER END
#
#
# Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved.
#
# Solaris needs /usr/xpg6/bin:/usr/xpg4/bin because the tools in /usr/bin are not POSIX-conformant
export PATH=/usr/xpg6/bin:/usr/xpg4/bin:/bin:/usr/bin
function fatal_error
{
print -u 2 "${progname}: $*"
exit 1
}
function attrstrtoattrarray
{
#set -o xtrace
typeset s="$1"
nameref aa=$2 # attribute array
integer aa_count=0
integer aa_count=0
typeset nextattr
integer currattrlen=0
typeset tagstr
typeset tagval
while (( ${#s} > 0 )) ; do
# skip whitespaces
while [[ "${s:currattrlen:1}" == ~(E)[[:blank:][:space:]] ]] ; do
(( currattrlen++ ))
done
s="${s:currattrlen:${#s}}"
# anything left ?
(( ${#s} == 0 )) && break
# Pattern tests:
#x="foo=bar huz=123" ; print "${x##~(E)[[:alnum:]_-:]*=[^[:blank:]\"]*}"
#x='foo="ba=r o" huz=123' ; print "${x##~(E)[[:alnum:]_-:]*=\"[^\"]*\"}"
#x="foo='ba=r o' huz=123" ; print "${x##~(E)[[:alnum:]_-:]*=\'[^\"]*\'}"
#x="foox huz=123" ; print "${x##~(E)[[:alnum:]_-:]*}"
# All pattern combined via eregex (w|x|y|z):
#x='foo="bar=o" huz=123' ; print "${x##~(E)([[:alnum:]_-:]*=[^[:blank:]\"]*|[[:alnum:]_-:]*=\"[^\"]*\"|[[:alnum:]_-:]*=\'[^\"]*\')}"
nextattr="${s##~(E)([[:alnum:]_-:]*=[^[:blank:]\"]*|[[:alnum:]_-:]*=\"[^\"]*\"|[[:alnum:]_-:]*=\'[^\"]*\'|[[:alnum:]_-:]*)}"
currattrlen=$(( ${#s} - ${#nextattr}))
# add entry
tagstr="${s:0:currattrlen}"
if [[ "${tagstr}" == *=* ]] ; then
# normal case: attribute with value
tagval="${tagstr#*=}"
# strip quotes ('' or "")
if [[ "${tagval}" == ~(Elr)(\'.*\'|\".*\") ]] ; then
tagval="${tagval:1:${#tagval}-2}"
fi
aa[${aa_count}]=( name="${tagstr%%=*}" value="${tagval}" )
else
# special case for HTML where you have something like <foo baz>
aa[${aa_count}]=( name="${tagstr}" )
fi
(( aa_count++ ))
(( aa_count > 1000 )) && fatal_error "$0: aa_count too large" # assert
done
}
function handle_document
{
#set -o xtrace
nameref callbacks=${1}
typeset tag_type="${2}"
typeset tag_value="${3}"
typeset tag_attributes="${4}"
nameref doc=${callbacks["arg_tree"]}
nameref nodepath="${stack.items[stack.pos]}"
nameref nodesnum="${stack.items[stack.pos]}num"
case "${tag_type}" in
tag_begin)
nodepath[${nodesnum}]+=(
typeset tagtype="element"
typeset tagname="${tag_value}"
compound -A tagattributes
compound -A nodes
integer nodesnum=0
)
# fill attributes
if [[ "${tag_attributes}" != "" ]] ; then
attrstrtoattrarray "${tag_attributes}" "nodepath[${nodesnum}].tagattributes"
fi
(( stack.pos++ ))
stack.items[stack.pos]="${stack.items[stack.pos-1]}[${nodesnum}].nodes"
(( nodesnum++ ))
;;
tag_end)
(( stack.pos-- ))
;;
tag_text)
nodepath[${nodesnum}]+=(
typeset tagtype="text"
typeset tagvalue="${tag_value}"
)
(( nodesnum++ ))
;;
tag_comment)
nodepath[${nodesnum}]+=(
typeset tagtype="comment"
typeset tagvalue="${tag_value}"
)
(( nodesnum++ ))
;;
document_start)
;;
document_end)
;;
esac
# print "xmltok: '${tag_type}' = '${tag_value}'"
}
function xml_tok
{
typeset buf=""
typeset namebuf=""
typeset attrbuf=""
typeset c=""
typeset isendtag # bool: true/false
typeset issingletag # bool: true/false (used for tags like "<br />")
nameref callbacks=${1}
[[ ! -z "${callbacks["document_start"]}" ]] && ${callbacks["document_start"]} "${1}" "document_start"
while IFS='' read -r -N 1 c ; do
isendtag=false
if [[ "$c" == "<" ]] ; then
# flush any text content
if [[ "$buf" != "" ]] ; then
[[ ! -z "${callbacks["tag_text"]}" ]] && ${callbacks["tag_text"]} "${1}" "tag_text" "$buf"
buf=""
fi
IFS='' read -r -N 1 c
if [[ "$c" == "/" ]] ; then
isendtag=true
else
buf="$c"
fi
IFS='' read -r -d '>' c
buf+="$c"
# handle comments
if [[ "$buf" == ~(El)!-- ]] ; then
# did we read the comment completely ?
if [[ "$buf" != ~(Elr)!--.*-- ]] ; then
buf+=">"
while [[ "$buf" != ~(Elr)!--.*-- ]] ; do
IFS='' read -r -N 1 c || break
buf+="$c"
done
fi
[[ ! -z "${callbacks["tag_comment"]}" ]] && ${callbacks["tag_comment"]} "${1}" "tag_comment" "${buf:3:${#buf}-5}"
buf=""
continue
fi
# check if the tag starts and ends at the same time (like "<br />")
if [[ "${buf}" == ~(Er).*/ ]] ; then
issingletag=true
buf="${buf%*/}"
else
issingletag=false
fi
# check if the tag has attributes (e.g. space after name)
if [[ "$buf" == ~(E)[[:space:][:blank:]] ]] ; then
namebuf="${buf%%~(E)[[:space:][:blank:]].*}"
attrbuf="${buf#~(E).*[[:space:][:blank:]]}"
else
namebuf="$buf"
attrbuf=""
fi
if ${isendtag} ; then
[[ ! -z "${callbacks["tag_end"]}" ]] && ${callbacks["tag_end"]} "${1}" "tag_end" "$namebuf"
else
[[ ! -z "${callbacks["tag_begin"]}" ]] && ${callbacks["tag_begin"]} "${1}" "tag_begin" "$namebuf" "$attrbuf"
# handle tags like <br/> (which are start- and end-tag in one piece)
if ${issingletag} ; then
[[ ! -z "${callbacks["tag_end"]}" ]] && ${callbacks["tag_end"]} "${1}" "tag_end" "$namebuf"
fi
fi
buf=""
else
buf+="$c"
fi
done
[[ ! -z "${callbacks["document_end"]}" ]] && ${callbacks["document_end"]} "${1}" "document_end" "exit_success"
print # final newline to make filters like "sed" happy
}
function print_sample1_xml
{
cat <<EOF
<br />
<score-partwise instrument="flute1">
<identification>
<kaiman>nocrocodile</kaiman>
</identification>
<!-- a comment -->
<partlist>
<foo>myfootext</foo>
<bar>mybartext</bar>
<snap />
<!-- another
comment -->
<ttt>myttttext</ttt>
</partlist>
</score-partwise>
EOF
}
function usage
{
OPTIND=0
getopts -a "${progname}" "${xmldocumenttree1_usage}" OPT '-?'
exit 2
}
# program start
builtin basename
builtin cat
builtin date
builtin uname
typeset progname="${ basename "${0}" ; }"
typeset -r xmldocumenttree1_usage=$'+
[-?\n@(#)\$Id: xmldocumenttree1 (Roland Mainz) 2009-05-09 \$\n]
[-author?Roland Mainz <roland.mainz@nrubsig.org>]
[+NAME?xmldocumenttree1 - XML tree demo]
[+DESCRIPTION?\bxmldocumenttree\b is a small ksh93 compound variable demo
which reads a XML input file, converts it into an internal
variable tree representation and outputs it in the format
specified by viewmode (either "list", "namelist", "tree" or "compacttree").]
file viewmode
[+SEE ALSO?\bksh93\b(1)]
'
while getopts -a "${progname}" "${xmldocumenttree1_usage}" OPT ; do
# printmsg "## OPT=|${OPT}|, OPTARG=|${OPTARG}|"
case ${OPT} in
*) usage ;;
esac
done
shift $((OPTIND-1))
typeset xmlfile="$1"
typeset viewmode="$2"
if [[ "${xmlfile}" == "" ]] ; then
fatal_error $"No file given."
fi
if [[ "${viewmode}" != ~(Elr)(list|namelist|tree|compacttree) ]] ; then
fatal_error $"Invalid view mode \"${viewmode}\"."
fi
compound xdoc
compound -A xdoc.nodes
integer xdoc.nodesnum=0
compound stack
typeset -a stack.items=( [0]="doc.nodes" )
integer stack.pos=0
# setup callbacks for xml_tok
typeset -A document_cb # callbacks for xml_tok
document_cb["document_start"]="handle_document"
document_cb["document_end"]="handle_document"
document_cb["tag_begin"]="handle_document"
document_cb["tag_end"]="handle_document"
document_cb["tag_text"]="handle_document"
document_cb["tag_comment"]="handle_document"
# argument for "handle_document"
document_cb["arg_tree"]="xdoc"
if [[ "${xmlfile}" == "#sample1" ]] ; then
print_sample1_xml | xml_tok document_cb
elif [[ "${xmlfile}" == "#sample2" ]] ; then
/usr/sfw/bin/wget \
--user-agent='ksh93_xmldocumenttree' \
--output-document=- \
'http://www.google.com/custom?q=gummi+bears' |
/usr/bin/iconv -f "ISO8859-1" |
xml_tok document_cb
else
cat "${xmlfile}" | xml_tok document_cb
fi
print -u2 "#parsing completed."
case "${viewmode}" in
list)
set | egrep "xdoc.*(tagname|tagtype|tagval|tagattributes)" | fgrep -v ']=$'
;;
namelist)
typeset + | egrep "xdoc.*(tagname|tagtype|tagval|tagattributes)"
;;
tree)
print -v xdoc
;;
compacttree)
print -C xdoc
;;
*)
fatal_error $"Invalid view mode \"${viewmode}\"."
;;
esac
print -u2 "#done."
exit 0
# EOF.
|