xml.sh 2.21 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 committed
46 47
	
	logdebug "before XML_pad"
48
	__LECABASH__XML_pad__ $(stacksize __LECABASH__markup__)
Eric Coissac committed
49
	logdebug "after XML_pad"
50
	echo -n "<${tag}"
Eric Coissac committed
51
	logdebug "after opening tag"
52

53
	local n=${#attribs[@]}
54 55
	((n/=2))
	
Eric Coissac committed
56 57
	logdebug "$n attributes specified"
	
58
	if (( n > 0 )) ; then
Eric Coissac committed
59
	    logdebug "Some attributes must be written"
60 61 62 63 64 65 66
		for (( i=0; i<n; i+=1 )) ; do
			k=$((i*2))
			l=$((k+1))
			echo -n " ${attribs[$k]}=\"${attribs[$l]}\""
			shift
			shift
		done
Eric Coissac committed
67 68
	else
		logdebug "No attributes specified"
69
	fi
70 71
	echo ">"
	
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
	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}>"

}

94 95
function XML_tag_attr_data() {

96 97 98
	local i=0
	local k
	local l
99 100 101 102 103 104 105 106
	local tag=$1
	shift
	local attribs=( $(echo "$*") )

	__LECABASH__XML_pad__ $(stacksize __LECABASH__markup__)

	echo   -n "<${tag}"

107
	local n=${#attribs[@]}
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
	((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}>"

}


129 130 131 132 133 134 135 136 137 138
function XML_empty_tag() {

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

}


139 140 141 142 143 144 145
function XML_data() {

	__LECABASH__XML_pad__ $(stacksize __LECABASH__markup__)
	echo   "$*"

}