diff --git a/xml/doc/Tools manual.md b/xml/doc/Tools manual.md index 54f3276..8b8f179 100644 --- a/xml/doc/Tools manual.md +++ b/xml/doc/Tools manual.md @@ -12,8 +12,13 @@ This sounds cool (and it is), but it does mean you may need to use some software ## Downloading and installing +### Installing with the setup script +Use the 'setup' script in the templates directory to install jEdit, Saxon, Java and FOP on Linux. It can be found in the 'templates/xml/scripts' directory. The script has been tested to run on a Debian/Ubuntu and Redhat/CentOS system. When you're done, you still have to configure jEdit. + + ### Java + Make sure you have at least Java 7 installed (Java 8 is fine as well). If not, download it at www.java.com. ### jEdit @@ -31,14 +36,12 @@ Download Saxon Home Edition (HE) 9.6 **for Java** at: http://saxon.sourceforge.n Download Apache FOP 1.1 at https://xmlgraphics.apache.org/fop/download.html and unzip. ### Fonts - -Download the Liberation Sans font from http://www.fontsquirrel.com/fonts/liberation-sans and install. - -Download the Liberation Mono font from http://www.fontsquirrel.com/fonts/Liberation-Mono and install. +The required fonts are found in the 'templates/xml/rosbot' directory. They must be installed. The 'setup' script can do this for you, or they must be installed to a font directory manually. ## Configuring + ### jEdit In jEdit, you're going to have to install the XML plugin: @@ -104,6 +107,18 @@ Use the pentestreport.xml template (which already contains some default stuff) t Make sure the XML file you've created with jEdit is valid (no errors in the Error List in jEdit). +### Autobuild PDF files + +Instead of running the Saxon and FOP commands as described in the next sections, it is also possible to run: + +```./convertxmltopdf myreport.xml mystylesheet.xsl myreport.pdf``` + +The script, located in the templates directory, will then build and display the generated PDF file. It refreshes automatically. + +The script accepts an XML and XSL file as arguments and optionally the name for your PDF file. If a PDF filename is ommitted, the name of the XML file is used. +The paths where Saxon and FOP are installed, as well as the refresh rate (in seconds) can be modified in the top section of the script. Just open convertxmltopdf with your favorite editor and edit! +This executable script is found in the directory templates/xml and runs on Linux only. + ### Saxon To transform your XML file into XSL-FO, use the following command from the saxon directory: diff --git a/xml/rosbot/LiberationSansNarrow-Regular.ttf b/xml/rosbot/LiberationSansNarrow-Regular.ttf new file mode 100644 index 0000000..738b1c3 Binary files /dev/null and b/xml/rosbot/LiberationSansNarrow-Regular.ttf differ diff --git a/xml/rosbot/fop.patch b/xml/rosbot/fop.patch new file mode 100644 index 0000000..7c82bca --- /dev/null +++ b/xml/rosbot/fop.patch @@ -0,0 +1,35 @@ +20c20,21 +< +--- +> /usr/share/fonts/truetype/liberation +> +28c29 +< +--- +> +80a82,106 +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> +> diff --git a/xml/scripts/convertxmltopdf b/xml/scripts/convertxmltopdf new file mode 100755 index 0000000..a431b66 --- /dev/null +++ b/xml/scripts/convertxmltopdf @@ -0,0 +1,84 @@ +#!/usr/bin/env bash +# +# Convert xml file with xsl stylesheet to pdf document +# @author: Deborah Maaijen +# +# GPL 3+ licensed +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. + + +#modify this if needed +declare -r XCONF='/etc/fop/conf/rosfop.xconf' +declare -r SAXON_JAR='/opt/saxon/saxon9he.jar' +declare -r PDF_READER='xdg-open' #opens the default PDF program +declare -r REFRESH=60 #refresh rate in seconds for the PDF + +#don't modify unless you know what you're doing +############################################### +# Arguments: *.xml Optional *.pdf +# Returns: none +############################################## +declare -r USAGE='Usage: convertxmltopdf myfile.xml [/path/to/report.pdf]' +declare -r WHATIS='Creates a PDF file from the given offer or report XML files. + By default it uses the document type and date as the PDF name' + +converter(){ + local -i ret_value=0 + local xmlname="$1" + local xslname="" + + if [ $xmlname == 'report.xml' ]; then + xslname='../xslt/generate_report.xsl' + elif [ $xmlname == 'offerte.xml' ] || [ $xmlname == 'offerte-basic.xml' ]; then + xslname='../xslt/generate_offerte.xsl' + else + echo "please run this script inside the source directory." + echo "when not generating a report or offerte, modify this script" + exit 1 + fi + local report_fo=$(mktemp) #use a temp file + java -jar $SAXON_JAR -s:$xmlname -xsl:$xslname -o:$report_fo -xi #create a .fo file + if [[ "$?" -eq 0 ]]; then #if the .fo file was created + local pdfname='../target/'$(_getPdfname $xmlname $2) #set the given PDF name or the date + fop -c $XCONF $report_fo $pdfname >&2 #create a pdf file from the fo file + rm -f "$report_fo" #delete the temp file + $PDF_READER "$pdfname" #open the PDF + else + ret_value+=1 + fi + return $ret_value +} + +_getPdfname(){ + local pdfname=$1 + pdfname=$(echo "$pdfname" | cut -d'.' -f1) + if [ $# -eq 2 ];then + pdfname=$2 + else + local datum=$(date +"%Y-%m-%d_%H") + pdfname="$pdfname.fo.$datum" + fi + echo "$pdfname"".pdf" + return 0 +} + +main(){ + local -i state=0 + if [ $# -eq 1 ] && [ $1 == '-h' ]; then + echo "$WHATIS" + state+=1 + elif [[ $1 != *.xml || $# -gt 2 ]]; then + echo "$USAGE" + state+=1 + else + while [[ $state -eq 0 ]]; do # if no errors occur, recompile PDF + state=$(converter "$@") + sleep $REFRESH + done + fi +} + +main "$@" diff --git a/xml/scripts/setup b/xml/scripts/setup new file mode 100755 index 0000000..4efc784 --- /dev/null +++ b/xml/scripts/setup @@ -0,0 +1,193 @@ +#!/usr/bin/env bash +# +#Sets up an environment to edit XML files and transform them to PDF documents +#Installs Saxon, FOP, Java, jEdit and required fonts +#tested on a Debian/Ubuntu system +# + +#Edit if needed +declare -r SAXON_URL='http://sourceforge.net/projects/saxon/files/latest/download' +declare -r FONTS_ORIG="../rosbot/*.ttf" +declare -r FONTS_DIR='/usr/share/fonts/truetype' +declare -r XCONF_INSTALL_PATH='/etc/fop/conf' #the path normally varies +declare -r FONTS_SRV="xlsfonts" +declare -r JEDIT_URL='http://sourceforge.net/projects/jedit/files/latest/download' + +#Don't touch unless you know what you're doing. +############################################### + +_readInput(){ + while true; do + read -p "$1"' is not found. Do you wish to install? (Yes/No/Quit): ' answer + local answer=$(echo $answer | tr '[:upper:]' '[:lower:]') + case "$answer" in + y|yes|n|no|skip) + echo $answer + break; + ;; + q|quit) + exit 1 + ;; + esac + done +} + +_isInstalled(){ + echo "$1 is already installed" +} + +_deadLink(){ + echo "$1 can't be installed. Check link $2" +} + +_isZip(){ + file --mime-type "$1" | grep -q 'zip$' + echo "$?" +} + +installFonts(){ + local -i result="$1" + local liberation_dir=$FONTS_DIR'/liberation' + local narrow_installed=$( ls $liberation_dir/'LiberationSansNarrow*'>/dev/null 2>&1 | echo $?) + local mono_installed=$( ls $liberation_dir/'LiberationMono*' >/dev/null 2>&1 | echo $?) + if [ -d $liberation_dir ] && [ $narrow_installed == 0 ] && [ $mono_installed == 0 ]; then + _isInstalled 'font' + else + echo 'installing fonts' + mkdir -p "$liberation_dir" >/dev/null 2>&1 + cp $FONTS_ORIG "$liberation_dir" + echo 'updating font cache' + fc-cache -fv >/dev/null + fi + return $result +} + +installSaxon() { + local -i result=$1 + local saxon_zip="saxon.zip" + local saxon_usage='To use the program, type "java -jar /opt/saxon/saxon9he.jar" or just "saxon"' + local install_dir='/opt/saxon' + local sys_install=$(which saxon >/dev/null 2>&1; echo $?) + if [ ! -d "$install_dir" ] && [ $sys_install != 0 ]; then + local answer=$(_readInput 'saxon') + case "$answer" in + yes|y) + echo 'installing in /opt/saxon. '"$saxon_usage" + wget -O "$saxon_zip" "$SAXON_URL" + if [ $(_isZip "$saxon_zip") == 0 ]; then + mkdir -p "$install_dir" && + unzip -qq "$saxon_zip" -d "$install_dir" + rm -f "$saxon_zip" + else + _deadLink 'Saxon' "$SAXON_URL" + result+=1 + fi + ;; + n|no|s|skip) + return $result + ;; + esac + else + _isInstalled 'saxon' + echo "$saxon_usage" + fi + return $result +} + +installJedit(){ + wget -O jedit.jar $JEDIT_URL + if [ $(_isZip "jedit.jar") == 0 ]; then + java -jar jedit.jar + rm jedit.jar + else + _deadLink 'jEdit' $JEDIT_URL + fi +} +_patchFop(){ + echo 'searching for fop config file' + local xconf=$(find / -name fop.xconf) + mkdir -p $XCONF_INSTALL_PATH + local new_xconf="$XCONF_INSTALL_PATH""/rosfop.xconf" + cp "$xconf" "$new_xconf" + patch "$new_xconf" '../rosbot/fop.patch' + echo 'added patched fop configuration' + return 0 +} + +_getInstaller(){ + if [ $(which apt-get >/dev/null 2>&1; echo $?) = 0 ];then + echo "apt-get" + elif [ $(which yum >/dev/null 2>&1; echo $?) = 0 ];then + echo "yum" + else + echo "none" + fi + return 0 +} + +installPkgmProgram() { + local -i result=0 + local -r openjdk_ver="openjdk-7-jre" #yep, has to be updated + local installer="$1" + shift #$1 is a program name now + while (($#)); do + local program="$1" + local -i prog_path=$(which $program >/dev/null 2>&1; echo $?) + if [ "$prog_path" != 0 ]; then + local answer=$(_readInput $program) + case $answer in + yes|y) + case $program in + java) + "$installer" install -y "$openjdk_ver" + ;; + jedit) + "$installer" install -y "$program" + if [ $(echo "$?") != 0 ]; then + installJedit + fi + ;; + fop) + "$installer" install -y "$program" + _patchFop + ;; + esac + shift #skip to the next program name + ;; + no|n|skip|s) + shift #skip to the next program name + ;; + esac + else + _isInstalled "$program" + shift #skip to the next program name + fi + done + return $result +} + + +main(){ + local -r user=$(whoami) + local -i result=0 + if [ "$user" != 'root' ]; then + echo "please run as root or sudo $0" + exit "$result" + fi + local installer=$(_getInstaller) + if [ $installer != "yum" ] && [ $installer != "apt-get" ]; then + echo 'no supported package manager found.' + echo 'Supported are yum(Red Hat/CentOS) and apt-get(Debian/Ubuntu)' + result+=1 + else + installPkgmProgram "$installer" 'java' 'jedit' 'fop' + result+=$? + fi + installSaxon $result + result+=$? + installFonts $result + result+=$? + exit $result +} + +main "$@" diff --git a/xml/xslt/generate_report.xsl b/xml/xslt/generate_report.xsl index 0a6b39c..d68eed1 100644 --- a/xml/xslt/generate_report.xsl +++ b/xml/xslt/generate_report.xsl @@ -29,7 +29,7 @@ - +