echo Script file for generating XML Schema documentation
echo using xs3p schema documentation generator.
echo ""
echo Usage: schemaDocumentation pathToXSDFile imagesFolder mainDocBase mainDocFile digramsExtesion useHashcodes cssURL
echo ----------------------------------------------------------------------------------------------
echo Parameters:
echo - pathToXSDFile  - The path to XML Schema file
echo - imagesFolder   - The path to the folder where generated images will be saved into. Relative 
echo                     to the output file
echo - mainDocBase - The path to the html output directory. This contains the HTML files and the images directory. Must be ended in /
echo - mainDocFile - The name of the HTML file.
echo - diagramsExtension - Either ".png" or ".jpg" to specify the diagrams type
echo - useHashcodes - If enabled then the achors and links will be generated using the hashcode of the component
echo      identifier instead of using the identifier itself. This is useful when the schema componet
echo      names contain characters that are not directly supported by the browsers or by the file system. 
echo - cssURL - Optional. The URL of the stylesheet to be used by the generated HTML files. Must be specified in URL syntax.
echo Example: schemaDocumentation samples/personal.xsd schemaDiagrams samples personal.xsd.html .png true personal.css
echo ----------------------------------------------------------------------------------------------
# For large schemas you can use 'Generate chunks' option. 
# In this case you must specify the following parameters:
# generateChunks = "true"

case "$0" in
/*)
  p=$0
;;
*) p=`/bin/pwd`/$0
;;
esac
p=`dirname "$p"`

XSDPATH=$1
IMAGESFOLDER=$2
MAINDOCBASE=$3
MAINDOCFILE=$4
DIAGRAMS_EXTENSION=$5
USE_HASH_CODES=$6
CSS_URL=$7

# Set default values if all parameters are missing
if [ "$XSDPATH" == "" -a "$IMAGESFOLDER" == "" -a "$MAINDOCBASE" == "" -a "$MAINDOCFILE" == "" -a "$DIAGRAMS_EXTENSION" == "" -a "$USE_HASH_CODES" == "" ]; then 
XSDPATH="$p/samples/personal.xsd"
IMAGESFOLDER="schemaDiagrams"
MAINDOCBASE="samples/"
MAINDOCFILE="personal.xsd.html"
DIAGRAMS_EXTENSION=".png"
USE_HASH_CODES="true"
fi

if [ "$XSDPATH" != "" -a "$IMAGESFOLDER" != "" -a "$MAINDOCBASE" != "" -a "$MAINDOCFILE" != "" -a "$DIAGRAMS_EXTENSION" != "" -a "$USE_HASH_CODES" != "" ]; then
  echo Generate documentation for $XSDPATH
  echo Output directory $MAINDOCBASE
  echo Output file $MAINDOCFILE
  CP=$p:$p/classes:$p/lib/oxygen.jar:$p/lib/xml-apis.jar:$p/lib/xercesImpl.jar:$p/lib/log4j.jar:$p/lib/resolver.jar:$p/lib/jimi.zip


# generates diagrams
java -cp "$CP" ro.sync.exml.SwingSDImageGenerator "$XSDPATH" "$MAINDOCBASE/$IMAGESFOLDER" "$DIAGRAMS_EXTENSION" "$USE_HASH_CODES"

#generate the documentation file
  
  PARAMS="showDiagrams=true"
  PARAMS="$PARAMS imagesBaseURL=$IMAGESFOLDER"
  PARAMS="$PARAMS sortByComponent=true"
  PARAMS="$PARAMS useJavaScript=true"
  PARAMS="$PARAMS printAllSuperTypes=true"
  PARAMS="$PARAMS printAllSubTypes=true"
  PARAMS="$PARAMS printLegend=true"
  PARAMS="$PARAMS printGlossary=true"
  PARAMS="$PARAMS printNSPrefixes=true"
  PARAMS="$PARAMS mainSchemaOutputFile=$MAINDOCFILE"
  PARAMS="$PARAMS showLogicalDiagrams=true"
  PARAMS="$PARAMS mainDocBase=$MAINDOCBASE"
  PARAMS="$PARAMS mainDocFile=$MAINDOCFILE"
  PARAMS="$PARAMS diagramImagesExtension=$DIAGRAMS_EXTENSION"
  PARAMS="$PARAMS useHashcodes=$USE_HASH_CODES"
  
  if [ "$CSS_URL" != "" ]; then
  	PARAMS="$PARAMS externalCSSURL=$CSS_URL"  
  fi

java -cp "$p/lib/saxon8.jar" net.sf.saxon.Transform -o "$MAINDOCBASE/$MAINDOCFILE" "$XSDPATH"  "$p/frameworks/xs3p/xs3p.xsl" $PARAMS

else 
  ERROR_MESSAGE="The following parameters are not specified:"
  if [ "$XSDPATH" == "" ]; then
    ERROR_MESSAGE=$ERROR_MESSAGE" pathToXSDFile,"
  fi
  if [ "$IMAGESFOLDER" == "" ]; then
    ERROR_MESSAGE=$ERROR_MESSAGE" imagesFolder,"
  fi
  if [ "$MAINDOCBASE" == "" ]; then
    ERROR_MESSAGE=$ERROR_MESSAGE" mainDocBase,"
  fi
  if [ "$MAINDOCFILE" == "" ]; then
    ERROR_MESSAGE=$ERROR_MESSAGE" mainDocFile,"
  fi
  if [ "$DIAGRAMS_EXTENSION" == "" ]; then
    ERROR_MESSAGE=$ERROR_MESSAGE" digramsExtesion(.png | .jpg),"
  fi
  if [ "$USE_HASH_CODES" == "" ]; then
    ERROR_MESSAGE=$ERROR_MESSAGE" useHashCodes(true | false)"
  fi
  ERROR_MESSAGE=$ERROR_MESSAGE". Correct command line arguments and try again."
  echo $ERROR_MESSAGE
fi
