File Coverage

blib/lib/Win32/MinXSLT.pm
Criterion Covered Total %
statement 10 12 83.3
branch n/a
condition n/a
subroutine 4 4 100.0
pod n/a
total 14 16 87.5


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