7 delete @ENV{qw(PATH IFS CDPATH ENV BASH_ENV)}; # clean house for taint mode
9 my $PROGRAM_NAME = 'gbtodot';
10 my $USAGE = "$PROGRAM_NAME [options] book-code\n\t--meta=[metadata file]\n\t--xml=[book XML]\n\t--xsl=[XSL transformation]\n\t--baseURI=[URI of linked XHTML book]\n\t--language=[language area of input data (output determined by meta file)]\n\t--verbose\n";
12 my $FILENAME_SEPARATOR = '/';
14 my $RXP = '/usr/bin/rxp';
15 my $JAVA = '/usr/bin/java';
16 my $XALAN_JAR = '/usr/share/java/xalan2.jar';
23 my $dotXSL = 'common/xsl/dot.xsl';
29 while( $#ARGV > -1 ) {
30 my $cmdLineItem = shift @ARGV;
31 if( $cmdLineItem =~ /^--meta=(.+)$/ ) {
34 elsif( $cmdLineItem =~ /^--xml=(.+)$/ ) {
37 elsif( $cmdLineItem =~ /^--xsl=(.+)$/ ) {
40 elsif( $cmdLineItem =~ /^--base-uri=(.+)$/ ) {
43 elsif( $cmdLineItem =~ /^--language=(.+)$/ ) {
46 elsif( $cmdLineItem =~ /^--verbose/ ) {
50 $bookCode = $cmdLineItem;
54 if( $bookCode eq '' ) { die "Unspecified book code\n$USAGE"; }
55 if( $bookXML eq '' ) { $bookXML = "$language/xml/$bookCode.xml"; }
56 if( $metaFile eq '' ) { $metaFile = "$language/.publisher/rules/standard"; }
58 if( -e $metaFile && -f $metaFile && -r $metaFile ) {
59 open( META, '<', $metaFile ) or die qq{Unable to open metadata file ($metaFile): $!\n};
61 else { die qq{Improper metadata file ($metaFile)\n}; }
64 while( my $line = <META> ) {
65 $meta .= $line if $line !~ /^[[:space:]]*#/;
70 if( $meta =~ /^[[:space:]]*$bookCode[[:space:]]*{([^}]*)}/sm ) {
74 die "Book code ($bookCode) not found in metadata file or invalid file syntax\n";
77 my @rules = split( /[[:space:]\n]*;[[:space:]\n]*/, $rulesString );
79 foreach my $rule (@rules) {
80 if( $rule =~ /[[:space:]]*([^:]+)[[:space:]]*:[[:space:]]*(.+)$/s ) {
81 $rulesHash{ $1 } = $2;
84 die "Unrecognized rule syntax:\n$rule\n";
88 if( $bookXML =~ m{^([-\w\@./]+)$} ) {
90 if( -e $bookXML && -f $bookXML && -r $bookXML ) {
91 system( $RXP, '-Vs', $bookXML ) == 0 or die( "XML validation failed\n" );
94 unless( defined $rulesHash{'book-series'} ) { die "Metadata file leaves book series unspecified\n"; }
95 unless( defined $rulesHash{'language'} ) { die "Metadata file leaves language unspecified\n"; }
97 my $outPath = $rulesHash{'language'} . $FILENAME_SEPARATOR . 'dot' . $FILENAME_SEPARATOR . $rulesHash{'book-series'};
98 unless( -e $outPath && -d $outPath ) {
99 my @dirs = split ( /$FILENAME_SEPARATOR/, $outPath );
101 for( my $i = 0; $i <= $#dirs; ++$i ) {
102 $dirPath .= $dirs[$i] . $FILENAME_SEPARATOR;
103 if( -e $dirPath && ! -d $dirPath ) { die "Output directory name exists and is not a directory\n"; }
104 unless( -e $dirPath ) {
105 mkdir $dirPath or die( "Unable to create output directory ($outPath): $!\n" );
109 unless( -e $outPath && -d $outPath ) {
110 die "Unknown error creating output directory\n";
113 $baseURI = "http://www.projectaon.org/$rulesHash{'language'}/xhtml/$rulesHash{'book-series'}/$bookCode/" if( $baseURI eq '' );
114 print qx{$JAVA -classpath "$XALAN_JAR" org.apache.xalan.xslt.Process -IN "$bookXML" -XSL "$dotXSL" -OUT "$outPath$FILENAME_SEPARATOR$bookCode.dot" -PARAM base-URI "$baseURI" -PARAM language "$rulesHash{'language'}"};
117 print "Success\n" if $verbose;