]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/WWW/IDL2js.pm
Whitespace. gah.
[working/Evergreen.git] / Open-ILS / src / perlmods / OpenILS / WWW / IDL2js.pm
1 package OpenILS::WWW::IDL2js;
2 use strict; use warnings;
3 use XML::LibXML;
4 use XML::LibXSLT;
5 use Apache2::Const -compile => qw(OK DECLINED HTTP_INTERNAL_SERVER_ERROR);
6 use Error qw/:try/;
7 use OpenSRF::System;
8 use OpenSRF::Utils::SettingsClient;
9
10 my $bs_config;
11 my $stylesheet;
12 my $idl_doc;
13
14
15 # load and parse the stylesheet
16 sub import {
17     my $self = shift;
18     $bs_config = shift;
19 }
20
21 # parse the IDL, loaded from the network
22 my $__initted = 0;
23 sub child_init {
24     $__initted = 1;
25
26     OpenSRF::System->bootstrap_client(config_file => $bs_config);
27     my $sclient = OpenSRF::Utils::SettingsClient->new();
28
29     my $xsl_file = $sclient->config_value('IDL2js');
30
31     unless($xsl_file) {
32         warn "XSL2js XSL file required for IDL2js Apache module\n";
33         return;
34     }
35
36     $xsl_file = $sclient->config_value(dirs => 'xsl')."/$xsl_file";
37     my $idl_file = $sclient->config_value("IDL");
38
39     my $xslt = XML::LibXSLT->new();
40
41     try {
42
43         my $style_doc = XML::LibXML->load_xml(location => $xsl_file, no_cdata=>1);
44         $stylesheet = $xslt->parse_stylesheet($style_doc);
45
46     } catch Error with {
47         my $e = shift;
48         warn "Invalid XSL File: $xsl_file: $e\n";
49     };
50
51     $idl_doc = XML::LibXML->load_xml(location => $idl_file);
52 }
53
54
55 sub handler {
56     my $r = shift;
57     my $args = $r->args || '';
58     child_init() unless $__initted;
59
60     return Apache2::Const::HTTP_INTERNAL_SERVER_ERROR unless $stylesheet and $idl_doc;
61     return Apache2::Const::DECLINED if $args and $args !~ /^[a-zA-Z,]*$/;
62
63     my $output;
64     try {
65         my $results = $stylesheet->transform($idl_doc, class_list => "'$args'");
66         $output = $stylesheet->output_as_bytes($results);
67     } catch Error with {
68         my $e = shift;
69         $r->log->error("IDL XSL Error: $e");
70     };
71
72     return Apache2::Const::HTTP_INTERNAL_SERVER_ERROR unless $output;
73
74     $r->content_type('application/x-javascript; encoding=utf8');
75     $r->print($output);
76     return Apache2::Const::OK;
77 }
78
79 1;