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';
17 my $DOT = '/usr/local/bin/dot';
20 # Check that all the binaries are were want them
23 push @BINARIES, ($RXP, $JAVA, $XALAN_JAR, $DOT);
27 die "$PROGRAM_NAME: Cannot find binary '".$_."'. Please install it.\n";
37 my $dotXSL = 'common/xsl/dot.xsl';
43 while( $#ARGV > -1 ) {
44 my $cmdLineItem = shift @ARGV;
45 if( $cmdLineItem =~ /^--meta=(.+)$/ ) {
48 elsif( $cmdLineItem =~ /^--xml=(.+)$/ ) {
51 elsif( $cmdLineItem =~ /^--xsl=(.+)$/ ) {
54 elsif( $cmdLineItem =~ /^--base-uri=(.+)$/ ) {
57 elsif( $cmdLineItem =~ /^--language=(.+)$/ ) {
60 elsif( $cmdLineItem =~ /^--verbose/ ) {
64 $bookCode = $cmdLineItem;
68 if( $bookCode eq '' ) { die "Unspecified book code\n$USAGE"; }
69 if( $bookXML eq '' ) { $bookXML = "$language/xml/$bookCode.xml"; }
70 if( $metaFile eq '' ) { $metaFile = "$language/.publisher/rules/standard"; }
72 if( -e $metaFile && -f $metaFile && -r $metaFile ) {
73 open( META, '<', $metaFile )
74 or die qq{Unable to open metadata file ($metaFile): $!\n};
77 die qq{Improper metadata file ($metaFile)\n};
81 while( my $line = <META> ) {
82 $meta .= $line if $line !~ /^[[:space:]]*#/;
87 if( $meta =~ /^[[:space:]]*$bookCode[[:space:]]*{([^}]*)}/sm ) {
91 die "Book code ($bookCode) not found in metadata file or invalid file syntax\n";
94 my @rules = split( /[[:space:]\n]*;[[:space:]\n]*/, $rulesString );
96 foreach my $rule (@rules) {
97 if( $rule =~ /[[:space:]]*([^:]+)[[:space:]]*:[[:space:]]*(.+)$/s ) {
98 $rulesHash{ $1 } = $2;
101 die "Unrecognized rule syntax:\n$rule\n";
105 if( $bookXML =~ m{^([-\w\@./]+)$} ) {
107 if( -e $bookXML && -f $bookXML && -r $bookXML ) {
108 system( $RXP, '-Vs', $bookXML ) == 0
109 or die( "XML validation failed\n" );
112 unless( defined $rulesHash{'book-series'} ) {
113 die "Metadata file leaves book series unspecified\n";
115 unless( defined $rulesHash{'language'} ) {
116 die "Metadata file leaves language unspecified\n";
119 my $outPath = $rulesHash{'language'} . $FILENAME_SEPARATOR . 'dot' . $FILENAME_SEPARATOR . $rulesHash{'book-series'};
120 &make_path( $outPath );
121 unless( -e $outPath && -d $outPath ) {
122 die "Unknown error creating output directory\n";
125 $baseURI = "http://www.projectaon.org/$rulesHash{'language'}/xhtml/$rulesHash{'book-series'}/$bookCode/" if( $baseURI eq '' );
126 my $dotPath = "$outPath$FILENAME_SEPARATOR$bookCode.dot";
127 print qx{$JAVA -classpath "$XALAN_JAR" org.apache.xalan.xslt.Process -IN "$bookXML" -XSL "$dotXSL" -OUT "$dotPath" -PARAM base-URI "$baseURI" -PARAM language "$rulesHash{'language'}"};
129 my $svgPath = $rulesHash{'language'} . $FILENAME_SEPARATOR . 'svg' . $FILENAME_SEPARATOR . $rulesHash{'book-series'};
130 &make_path( $svgPath );
131 unless( -e $svgPath && -d $svgPath ) {
132 die "Unknown error creating output directory\n";
134 $svgPath .= "$FILENAME_SEPARATOR$bookCode.svgz";
135 print qx{$DOT -Tsvgz -o $svgPath $dotPath};
138 print "Success\n" if $verbose;
143 my ( $path ) = ( @_ );
145 my @dirs = split ( /$FILENAME_SEPARATOR/, $path );
147 for( my $i = 0; $i <= $#dirs; ++$i ) {
148 $dirPath .= $dirs[$i] . $FILENAME_SEPARATOR;
149 if( -e $dirPath && ! -d $dirPath ) {
150 die "Output directory name exists and is not a directory\n";
152 unless( -e $dirPath ) {
154 or die( "Unable to create output directory ($path): $!\n" );