source: subversion/applications/rendering/osmarender6/xsltrans @ 8309

Last change on this file since 8309 was 6117, checked in by jochen, 12 years ago

Scripts to call osmarender more easily

  • Property svn:executable set to *
File size: 4.1 KB
Line 
1#!/bin/bash
2#
3#  xsltrans [options] <xmlfile> [<xslfile>]
4#
5#  This little utility script will call the XSL transformator of your choice.
6#
7#  Options:
8#     -p, --param name=value          Set XSL XPATH parameter
9#     -s, --stringparam name=value    Set XSL string parameter
10#
11#  The <xmlfile> is the input XML file. The <xslfile> is the XSL stylesheet.
12#  If you have a xml-stylesheet processing instruction in your XML file,
13#  you don't need to add the <xslfile>. Output is always on STDOUT.
14#
15#  If the environment variable XSLTRANS is set, it will call the program
16#  named in the variable. If the content of XSLTRANS ends in .jar, it will
17#  call java with this JAR file as a parameter.
18#
19#  xsltrans knows the calling syntax of xsltproc, xmlstarlet, xalan (c++)
20#  and xalan (Java) and will supply the parameters in the right form.
21#  Note that not all programs support all options.
22#
23
24#set -x
25
26prgname=`basename $0`
27
28function usage() {
29    echo >&2 "Usage: $prgname [options] <xmlfile> [<xslfile>]"
30    echo >&2 "Options: -p, --param name=value          Set XSL XPATH parameter"
31    echo >&2 "         -s, --stringparam name=value    Set XSL string parameter"
32}
33
34paralist=$(getopt --unquoted "--name=$prgname" --options=p:s: --longoptions=param:,stringparam: -- $@)
35
36if  [ $? -ne 0 ]; then
37    usage
38    exit 1
39fi
40
41set -- $paralist
42pp=""
43ps=""
44for option
45    do case "$option" in
46        -p|--param)       shift; pp="$pp $1"; shift;;
47        -s|--stringparam) shift; ps="$ps $1"; shift;;
48        --)               shift; break;;
49    esac
50done
51
52xml="$1"; shift
53if [ "$xml" = "" ]; then
54    echo >&2 "$prgname: Missing input XML file"
55    usage
56    exit 1
57fi
58
59xsl="$1"; shift
60if [ "$*" != '' ]; then
61    echo >&2 "$prgname: Too many parameters"
62    usage
63    exit 1
64fi
65
66##echo "pp=$pp"
67##echo "ps=$ps"
68##echo "xml=$xml"
69##echo "xsl=$xsl"
70
71# if XSLTRANS environment variable is not set, we try to autodetect it
72if [ "$XSLTRANS" = "" ]; then
73    XSLTRANS=`which xsltproc`
74    if [ "$XSLTRANS" = "" ]; then
75        XSLTRANS=`which xmlstarlet`
76        if [ "$XSLTRANS" = "" ]; then
77            XSLTRANS=`which xalan`
78            if [ "$XSLTRANS" = "" ]; then
79                if [ -e /usr/share/java/xalan2.jar ]; then
80                    XSLTRANS="/usr/share/java/xalan2.jar"
81                fi
82            fi
83        fi
84    fi
85fi
86
87if [ "$XSLTRANS" = "" ]; then
88    echo >&2 "Can't find any XSL transformer on your system."
89    echo >&2 "Please set the XSLTRANS environment variable to path of an XSL transformer."
90    exit 2
91fi
92
93##echo "XSLTRANS=$XSLTRANS"
94
95# We now have the XSL transformator we want to use in XSLTRANS
96
97# If the transformator is a java .jar file, change the command to call java
98command="$XSLTRANS"
99if [[ "$command" =~ '\.jar$' ]]; then
100    command="java -jar /usr/share/java/$XSLTRANS"
101fi
102
103##echo "command=$command"
104
105xsltrans=`basename $XSLTRANS`
106
107if [ "$xsltrans" = "xsltproc" ]; then
108    pl=''
109    for p in $pp; do
110        k=${p%%=*}
111        v=${p##*=}
112        pl="$pl --param $k $v"
113    done
114    for p in $ps; do
115        k=${p%%=*}
116        v=${p##*=}
117        pl="$pl --stringparam $k $v"
118    done
119    $command $pl $xsl $xml
120elif [ "$xsltrans" = "xmlstarlet" ]; then
121    if [ "$xsl" = '' ]; then
122        echo >&2 "xmlstarlet doesn't work without explicit stylesheet"
123        exit 4
124    fi
125
126    pl=''
127    for p in $pp; do
128        pl="$pl -p $p"
129    done
130    for p in $ps; do
131        pl="$pl -s $p"
132    done
133    $command tr $xsl $pl $xml
134elif [ "$xsltrans" = "xalan" ]; then
135    pl=''
136    for p in $pp; do
137        k=${p%%=*}
138        v=${p##*=}
139        pl="$pl -param $k $v"
140    done
141    for p in $ps; do
142        k=${p%%=*}
143        v=${p##*=}
144        pl="$pl -param $k '$v'"
145    done
146    if [ "$xsl" != '' ]; then
147        xsl="-xsl $xsl"
148    fi
149    $command $pl -in $xml $xsl
150elif [ "$xsltrans" = "xalan2.jar" ]; then
151    pl=''
152    for p in $pp; do
153        k=${p%%=*}
154        v=${p##*=}
155        pl="$pl -PARAM $k $v"
156    done
157    for p in $ps; do
158        k=${p%%=*}
159        v=${p##*=}
160        pl="$pl -PARAM $k $v"
161    done
162    if [ "$xsl" != '' ]; then
163        xsl="-XSL $xsl"
164    fi
165    $command $pl -IN $xml $xsl
166else
167    echo >&2 "Unknown XSL transformator '$xsltrans'"
168    exit 3
169fi
170
Note: See TracBrowser for help on using the repository browser.