50bcace94dd4cea5d834673b658f737081ad99a9
[project-aon.git] / common / scripts / gbtodot.pl
1 #!/usr/bin/perl -w
2 #
3 # gbtodot.pl
4
5 use strict;
6
7 delete @ENV{qw(PATH IFS CDPATH ENV BASH_ENV)}; # clean house for taint mode
8
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";
11
12 my $FILENAME_SEPARATOR = '/';
13
14 my $RXP        = '/usr/bin/rxp';
15 my $JAVA       = '/usr/bin/java';
16 my $XALAN_JAR  = '/usr/share/java/xalan2.jar';
17
18 ###
19
20 my $bookCode     = '';
21 my $metaFile     = '';
22 my $bookXML      = '';
23 my $dotXSL       = 'common/xsl/dot.xsl';
24 my $language     = 'en';
25 my $baseURI      = '';
26
27 my $verbose = 0;
28
29 while( $#ARGV > -1 ) {
30     my $cmdLineItem = shift @ARGV;
31     if( $cmdLineItem =~ /^--meta=(.+)$/ ) {
32         $metaFile = $1;
33     }
34     elsif( $cmdLineItem =~ /^--xml=(.+)$/ ) {
35         $bookXML = $1;
36     }
37     elsif( $cmdLineItem =~ /^--xsl=(.+)$/ ) {
38         $dotXSL = $1;
39     }
40     elsif( $cmdLineItem =~ /^--base-uri=(.+)$/ ) {
41         $baseURI = $1;
42     }
43     elsif( $cmdLineItem =~ /^--language=(.+)$/ ) {
44         $language = $1;
45     }
46     elsif( $cmdLineItem =~ /^--verbose/ ) {
47         $verbose = 1;
48     }
49     else { 
50         $bookCode = $cmdLineItem;
51     }
52 }
53
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"; }
57
58 if( -e $metaFile && -f $metaFile && -r $metaFile ) {
59     open( META, '<', $metaFile ) or die qq{Unable to open metadata file ($metaFile): $!\n};
60 }
61 else { die qq{Improper metadata file ($metaFile)\n}; }
62
63 my $meta = '';
64 while( my $line = <META> ) {
65     $meta .= $line if $line !~ /^[[:space:]]*#/;
66 }
67 close META;
68
69 my $rulesString = '';
70 if( $meta =~ /^[[:space:]]*$bookCode[[:space:]]*{([^}]*)}/sm ) {
71     $rulesString = $1;
72 }
73 else {
74     die "Book code ($bookCode) not found in metadata file or invalid file syntax\n";
75 }
76
77 my @rules = split( /[[:space:]\n]*;[[:space:]\n]*/, $rulesString );
78 my %rulesHash;
79 foreach my $rule (@rules) {
80     if( $rule =~ /[[:space:]]*([^:]+)[[:space:]]*:[[:space:]]*(.+)$/s ) {
81         $rulesHash{ $1 } = $2;
82     }
83     else {
84         die "Unrecognized rule syntax:\n$rule\n";
85     }
86 }
87
88 if( $bookXML =~ m{^([-\w\@./]+)$} ) {
89     $bookXML = $1;
90     if( -e $bookXML && -f $bookXML && -r $bookXML ) {
91         system( $RXP, '-Vs', $bookXML ) == 0 or die( "XML validation failed\n" );
92     }
93
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"; }
96
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 );
100         my $dirPath = '';
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" );
106             }
107         }
108     }
109     unless( -e $outPath && -d $outPath ) {
110         die "Unknown error creating output directory\n";
111     }
112
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'}"};
115 }
116
117 print "Success\n" if $verbose;