| 1 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 2 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | |||
| 4 | package Win32::MinXSLT; | ||
| 5 | $Win32::MinXSLT::VERSION = '0.04'; | ||
| 6 | 1 | ▶ | use Carp; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 0 | ▶ | use Win32::OLE; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 8 | |||
| 9 | our $Dom; | ||
| 10 | our $MsVer; | ||
| 11 | our $status; | ||
| 12 | |||
| 13 | BEGIN { | ||
| 14 | $MsVer = undef; | ||
| 15 | $status = 'ok'; | ||
| 16 | } | ||
| 17 | |||
| 18 | sub import { | ||
| 19 | my $self = shift; | ||
| 20 | |||
| 21 | $status = 'unknown'; | ||
| 22 | |||
| 23 | for my $v (@_) { | ||
| 24 | unless ($v =~ m{\A : v ([\d.]+) \z}xmsi) { | ||
| 25 | local $" = "', '"; | ||
| 26 | croak "Invalid parameter - use Win32::MinXSLT('@_') - not format /^:v9.9\$/ ==> '$v'"; | ||
| 27 | } | ||
| 28 | |||
| 29 | if (defined $MsVer) { | ||
| 30 | croak "Invalid duplicate of use Win32::MinXSLT (old version = '$MsVer', new version = '$1')"; | ||
| 31 | } | ||
| 32 | |||
| 33 | $MsVer = $1; | ||
| 34 | } | ||
| 35 | |||
| 36 | $status = 'ok'; | ||
| 37 | } | ||
| 38 | |||
| 39 | CHECK { | ||
| 40 | unless ($status eq 'ok') { return; } | ||
| 41 | |||
| 42 | unless (defined $MsVer) { $MsVer = '6.0'; } | ||
| 43 | |||
| 44 | unless ($MsVer =~ m{\.}xms) { | ||
| 45 | $MsVer .= '.0'; | ||
| 46 | } | ||
| 47 | |||
| 48 | if ($MsVer eq '1.0') { | ||
| 49 | $Dom = 'Microsoft.XMLDOM'; | ||
| 50 | } | ||
| 51 | else { | ||
| 52 | $Dom = 'Msxml2.DOMDocument'; | ||
| 53 | unless ($MsVer eq '2.0') { | ||
| 54 | $Dom .= '.'.$MsVer; | ||
| 55 | } | ||
| 56 | } | ||
| 57 | |||
| 58 | my $test_doc = Win32::OLE->new($Dom) or croak "Can not create Win32::OLE->new('$Dom') during initialisation"; | ||
| 59 | |||
| 60 | $test_doc->{async} = 'False'; | ||
| 61 | my $test = '<?xml version="1.0"?><root></root>'; | ||
| 62 | unless ($test_doc->LoadXML($test)) { | ||
| 63 | croak "Can not do '$Dom' Win32::OLE->LoadXML('$test')"; | ||
| 64 | } | ||
| 65 | } | ||
| 66 | |||
| 67 | sub new { bless [], $_[0]; } | ||
| 68 | |||
| 69 | sub parse_stylesheet { $_[1]; } | ||
| 70 | |||
| 71 | # ************************************************************************** | ||
| 72 | # Here is a new package "Win32::MinXML" that does the bulk of the workload: | ||
| 73 | # ************************************************************************** | ||
| 74 | |||
| 75 | package Win32::MinXML; | ||
| 76 | $Win32::MinXML::VERSION = '0.04'; | ||
| 77 | use Carp; | ||
| 78 | use Win32::OLE; | ||
| 79 | |||
| 80 | sub new { bless [], $_[0]; } | ||
| 81 | |||
| 82 | sub parse_file { | ||
| 83 | my $xml_doc = Win32::OLE->new($Dom) | ||
| 84 | or croak "Can not create Win32::OLE->new('$Dom') during Win32::MinXML->parse_file"; | ||
| 85 | |||
| 86 | $xml_doc->{async} = 'False'; | ||
| 87 | $xml_doc->{validateOnParse} = 'True'; | ||
| 88 | |||
| 89 | unless ($xml_doc->Load($_[1])) { | ||
| 90 | my $Rs = $xml_doc->{parseError}->{reason}; $Rs =~ s{\r}''xmsg; chomp $Rs; | ||
| 91 | my $Ln = $xml_doc->{parseError}->{line}; | ||
| 92 | my $Ps = $xml_doc->{parseError}->{linePos}; | ||
| 93 | my $Tx = $xml_doc->{parseError}->{srcText}; | ||
| 94 | croak "XML-file '$_[1]' did not load for $Dom at line $Ln, pos $Ps, reason: $Rs, text: '$Tx'"; | ||
| 95 | } | ||
| 96 | |||
| 97 | bless \$xml_doc, ref($_[0]); | ||
| 98 | } | ||
| 99 | |||
| 100 | sub parse_string { | ||
| 101 | my $xml_doc = Win32::OLE->new($Dom) | ||
| 102 | or croak "Can not create Win32::OLE->new('$Dom') during Win32::MinXML->parse_string"; | ||
| 103 | |||
| 104 | $xml_doc->{async} = 'False'; | ||
| 105 | $xml_doc->{validateOnParse} = 'True'; | ||
| 106 | |||
| 107 | unless ($xml_doc->LoadXML($_[1])) { | ||
| 108 | my $Rs = $xml_doc->{parseError}->{reason}; $Rs =~ s{\r}''xmsg; chomp $Rs; | ||
| 109 | my $Ln = $xml_doc->{parseError}->{line}; | ||
| 110 | my $Ps = $xml_doc->{parseError}->{linePos}; | ||
| 111 | my $Tx = $xml_doc->{parseError}->{srcText}; | ||
| 112 | croak "XML-text did not load for $Dom at line $Ln, pos $Ps, reason: $Rs, text: '$Tx'"; | ||
| 113 | } | ||
| 114 | |||
| 115 | bless \$xml_doc, ref($_[0]); | ||
| 116 | } | ||
| 117 | |||
| 118 | sub transform { | ||
| 119 | my ($xslt_doc, $xml_doc) = (${$_[0]}, ${$_[1]}); | ||
| 120 | |||
| 121 | my $html_doc = Win32::OLE->new($Dom) or croak "Can not create Win32::OLE->new('$Dom') during Win32::MinXML->transform"; | ||
| 122 | |||
| 123 | # Do the work, i.e. take the xml-input and transform it (using the xslt stylesheet) | ||
| 124 | $xml_doc->transformNodeToObject($xslt_doc, $html_doc); | ||
| 125 | if (Win32::OLE::LastError()) { | ||
| 126 | my $Rs = Win32::OLE::LastError(); $Rs =~ s{\s+}' 'xmsg; | ||
| 127 | croak "XSLT-file has syntax-errors for $Dom: $Rs"; | ||
| 128 | } | ||
| 129 | |||
| 130 | bless \$html_doc; | ||
| 131 | } | ||
| 132 | |||
| 133 | sub output_file { | ||
| 134 | my $self = shift; | ||
| 135 | |||
| 136 | my ($html_doc, $filename) = (${$_[0]}, $_[1]); | ||
| 137 | |||
| 138 | # Save the html to the output-file | ||
| 139 | my $rc = $html_doc->save($filename); | ||
| 140 | if (Win32::OLE::LastError()) { | ||
| 141 | my $Rs = Win32::OLE::LastError(); $Rs =~ s{\s+}' 'xmsg; | ||
| 142 | croak "Can't save to output-file '$filename' for $Dom, reason: $Rs"; | ||
| 143 | } | ||
| 144 | elsif ($rc) { | ||
| 145 | croak "Can't save to output-file '$filename' for $Dom, returncode = $rc"; | ||
| 146 | } | ||
| 147 | } | ||
| 148 | |||
| 149 | sub output_string { ${$_[1]}->xml; } | ||
| 150 | |||
| 151 | 1; | ||
| 152 | __END__ | ||