xml.sh 2.06 KB
Newer Older
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
#
# LECA Bash library
#
# The LECA bash library provides a set of function used for helping
# development of bash script mainly to write job script on the luke
# cluster
#
# The files from the LECA Bash library must be sourced from your main script
#

include logging
logdebug "Load XML package"

include stacks


######
#
# XML formating function
#

newstack __LECABASH__markup__

function __LECABASH__XML_pad__() {
	local pad=$(printf '%0.1s' " "{1..60})
	local padlength=$(( $1 * 2 ))
	
	printf '%*.*s' 0 "$padlength" "$pad"
	
}

function XML_header() {

	echo '<?xml version="1.0" encoding="UTF-8"?>'

}

function XML_push() {

40 41 42
	local i=0
	local k
	local l
43 44
	local tag=$1
	shift
45
	local attribs=( $(echo "$*") )
Eric Coissac's avatar
Eric Coissac committed
46
	
47 48
	__LECABASH__XML_pad__ $(stacksize __LECABASH__markup__)
	echo -n "<${tag}"
49

50
	local n=${#attribs[@]}
51
	n=$((n/2))
52
	
53
	logdebug "XML_push ${tag} : $n attributes specified"
Eric Coissac's avatar
Eric Coissac committed
54
	
55 56 57 58 59 60 61 62
	if (( n > 0 )) ; then
		for (( i=0; i<n; i+=1 )) ; do
			k=$((i*2))
			l=$((k+1))
			echo -n " ${attribs[$k]}=\"${attribs[$l]}\""
			shift
			shift
		done
63
	fi
64 65
	echo ">"
	
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
	pushvalue __LECABASH__markup__ "${tag}"
	
}

function XML_pop() {
	local tag=""
	popvalue __LECABASH__markup__ tag

	__LECABASH__XML_pad__ $(stacksize __LECABASH__markup__)
	echo   "</${tag}>"

}

function XML_tag_data() {

	local tag=$1
	shift
	__LECABASH__XML_pad__ $(stacksize __LECABASH__markup__)
	echo   "<${tag}>$*</${tag}>"

}

88 89
function XML_tag_attr_data() {

90 91 92
	local i=0
	local k
	local l
93 94 95 96 97 98 99 100
	local tag=$1
	shift
	local attribs=( $(echo "$*") )

	__LECABASH__XML_pad__ $(stacksize __LECABASH__markup__)

	echo   -n "<${tag}"

101
	local n=${#attribs[@]}
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
	((n/=2))

	if (( n > 0 )) ; then
		for (( i=0; i<n; i+=1 )) ; do
			k=$((i*2))
			l=$((k+1))
			if [[ "${attribs[$k]}" != "--" ]] ; then
				echo -n " ${attribs[$k]}=\"${attribs[$l]}\""
				shift
			else
				i="$n"
			fi
			shift
		done
	fi
	
	echo ">$*</${tag}>"

}


123 124 125 126 127 128 129 130 131 132
function XML_empty_tag() {

	local tag=$1
	shift
	__LECABASH__XML_pad__ $(stacksize __LECABASH__markup__)
	echo   "<${tag}/>"

}


133 134 135 136 137 138 139
function XML_data() {

	__LECABASH__XML_pad__ $(stacksize __LECABASH__markup__)
	echo   "$*"

}