8 delete @ENV{qw(PATH IFS CDPATH ENV BASH_ENV)}; # clean house for taint mode
10 my $PROGRAM_NAME = 'gbtodot';
11 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";
13 my $FILENAME_SEPARATOR = '/';
15 my $RXP = '/usr/bin/rxp';
16 my $JAVA = '/usr/bin/java';
17 my $XALAN_JAR = '/usr/share/java/xalan2.jar';
18 my $DOT = '/usr/local/bin/dot';
21 # Check that all the binaries are were want them
24 push @BINARIES, ($RXP, $JAVA, $XALAN_JAR, $DOT);
28 die "$PROGRAM_NAME: Cannot find binary '".$_."'. Please install it.\n";
38 my $dotXSL = 'common/xsl/dot.xsl';
44 while( $#ARGV > -1 ) {
45 my $cmdLineItem = shift @ARGV;
46 if( $cmdLineItem =~ /^--meta=(.+)$/ ) {
49 elsif( $cmdLineItem =~ /^--xml=(.+)$/ ) {
52 elsif( $cmdLineItem =~ /^--xsl=(.+)$/ ) {
55 elsif( $cmdLineItem =~ /^--base-uri=(.+)$/ ) {
58 elsif( $cmdLineItem =~ /^--language=(.+)$/ ) {
61 elsif( $cmdLineItem =~ /^--verbose/ ) {
65 $bookCode = $cmdLineItem;
69 if( $bookCode eq '' ) { die "Unspecified book code\n$USAGE"; }
70 if( $bookXML eq '' ) { $bookXML = "$language/xml/$bookCode.xml"; }
71 if( $metaFile eq '' ) { $metaFile = "$language/.publisher/rules/standard"; }
73 if( -e $metaFile && -f $metaFile && -r $metaFile ) {
74 open( META, '<', $metaFile )
75 or die qq{Unable to open metadata file ($metaFile): $!\n};
78 die qq{Improper metadata file ($metaFile)\n};
82 while( my $line = <META> ) {
83 $meta .= $line if $line !~ /^[[:space:]]*#/;
88 if( $meta =~ /^[[:space:]]*$bookCode[[:space:]]*{([^}]*)}/sm ) {
92 die "Book code ($bookCode) not found in metadata file or invalid file syntax\n";
95 my @rules = split( /[[:space:]\n]*;[[:space:]\n]*/, $rulesString );
97 foreach my $rule (@rules) {
98 if( $rule =~ /[[:space:]]*([^:]+)[[:space:]]*:[[:space:]]*(.+)$/s ) {
99 $rulesHash{ $1 } = $2;
102 die "Unrecognized rule syntax:\n$rule\n";
106 if( $bookXML =~ m{^([-\w\@./]+)$} ) {
108 if( -e $bookXML && -f $bookXML && -r $bookXML ) {
109 system( $RXP, '-Vs', $bookXML ) == 0
110 or die( "XML validation failed\n" );
113 unless( defined $rulesHash{'book-series'} ) {
114 die "Metadata file leaves book series unspecified\n";
116 unless( defined $rulesHash{'language'} ) {
117 die "Metadata file leaves language unspecified\n";
120 my $outPath = $rulesHash{'language'} . $FILENAME_SEPARATOR . 'dot' . $FILENAME_SEPARATOR . $rulesHash{'book-series'};
121 &make_path( $outPath );
122 unless( -e $outPath && -d $outPath ) {
123 die "Unknown error creating output directory\n";
126 $baseURI = "http://www.projectaon.org/$rulesHash{'language'}/xhtml/$rulesHash{'book-series'}/$bookCode/" if( $baseURI eq '' );
127 my $dotPath = "$outPath$FILENAME_SEPARATOR$bookCode.dot";
128 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'}"};
130 my $svgPath = $rulesHash{'language'} . $FILENAME_SEPARATOR . 'svg' . $FILENAME_SEPARATOR . $rulesHash{'book-series'};
131 &make_path( $svgPath );
132 unless( -e $svgPath && -d $svgPath ) {
133 die "Unknown error creating output directory\n";
135 $svgPath .= "$FILENAME_SEPARATOR$bookCode.svgz";
136 print qx{$DOT -Tsvgz -o $svgPath $dotPath};
139 print "Success\n" if $verbose;
144 my ( $path ) = ( @_ );
146 my @dirs = split ( /$FILENAME_SEPARATOR/, $path );
148 for( my $i = 0; $i <= $#dirs; ++$i ) {
149 $dirPath .= $dirs[$i] . $FILENAME_SEPARATOR;
150 if( -e $dirPath && ! -d $dirPath ) {
151 die "Output directory name exists and is not a directory\n";
153 unless( -e $dirPath ) {
155 or die( "Unable to create output directory ($path): $!\n" );