Skip to content
Permalink
567702babd
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
executable file 152 lines (136 sloc) 2.96 KB
#!/bin/bash
BASE_DIR=$(dirname $0)/..
DEP_DIR=$BASE_DIR/dependencies
#######################################
# variables
#######################################
INPUT_DIR_DEFAULT="$BASE_DIR/schema/odd"
INPUT_DIRS=()
OUTPUT_DIR_DEFAULT="$BASE_DIR/schema/generated"
STYLESHEET=
STYLESHEET_DIR="$BASE_DIR/stylesheets"
NAME_PATTERN='*'
SUFFIX='.xml'
CACHE=
SAXON_ARGS=()
OUTPUT_DIR=
#######################################
# functions
#######################################
function print_help {
echo "for every file in INPUT_DIR:"
echo "apply STYLESHEET,"
echo "send output to OUTPUT_DIR"
echo
echo "usage: $0 [OPTIONS] STYLESHEET [NAME_PATTERN]"
echo
echo "OPTIONS:"
echo " -h | --help: print this help"
echo " -i|--input-dir INPUT_DIR (multiple options add multiple directories). default: '$INPUT_DIR_DEFAULT'"
echo " -o|--output-dir OUTPUT_DIR. default: '$OUTPUT_DIR_DEFAULT/\$STYLESHEET'"
echo " -s|--stylesheet-dir STYLESHEET_DIR. default: '$STYLESHEET_DIR'"
echo " -x | --suffix SUFFIX: output files suffix. default: '$SUFFIX'"
echo " -c | --cache: only write output if not yet existing"
echo " -o | --options OPTS append these options for saxon"
echo
echo "ARGUMENTS:"
echo " STYLESHEET: a stylesheet in \$STYLESHEET_DIR to apply"
echo " NAME_PATTERN: apply only on files which match this pattern. default: '$NAME_PATTERN'"
echo
echo "possible values for CMD:"
for f in $(ls $STYLESHEET_DIR); do
echo " $f"
done
}
#######################################
# parse command line arguments
#######################################
while [[ $# > 0 ]]; do
key="$1"
case $key in
-h | --help)
print_help
exit 0
;;
-i|--input-dir)
shift
INPUT_DIRS+=( "$1" )
shift
;;
-o|--output-dir)
shift
OUTPUT_DIR="$1"
shift
;;
-s|--stylesheet-dir)
shift
STYLESHEET_DIR="$1"
shift
;;
-x | --suffix)
shift
SUFFIX="$1"
shift
;;
-o | --options)
shift
SAXON_ARGS+=( "$1" )
shift
;;
-c | --cache)
shift
CACHE=1
;;
-* )
echo "wrong syntax!"
print_help
exit 1
;;
*)
break
;;
esac
done
if [[ $# = 0 ]]; then
print_help
exit 1
fi
if [[ "$1" != "" ]]; then
STYLESHEET="$1"
shift
fi
if [[ "$1" != "" ]]; then
NAME_PATTERN="$1"
fi
if [[ "$OUTPUT_DIR" = "" ]]; then
OUTPUT_DIR="$OUTPUT_DIR_DEFAULT/${STYLESHEET##*/}"
fi
if [[ ${#INPUT_DIRS[@]} = 0 ]]; then
INPUT_DIRS=( "$INPUT_DIR_DEFAULT" )
fi
echo "creating '$OUTPUT_DIR'"
mkdir -p "$OUTPUT_DIR"
echo "pattern: '$NAME_PATTERN'"
while read file
do
output=${file##*/}
output=${output%.*}$SUFFIX
# echo "output: $output"
read -d '' cmd << EOF
java -jar "$DEP_DIR/Stylesheets/lib/saxon9he.jar"
"$STYLESHEET_DIR/$STYLESHEET"
-s:"$file"
-o:"$OUTPUT_DIR/$output"
${SAXON_ARGS[*]}
EOF
if [[ $CACHE ]] && [[ -e $OUTPUT_DIR/$output ]]; then
echo "'$OUTPUT_DIR/$output' already exists, skipping..."
else
echo "execute $cmd"
eval $cmd
fi
done < <(
find "${INPUT_DIRS[@]}" \
\( -type f -o -type l \) \
-name "$NAME_PATTERN"
)