| 1 | # XML::Toolset::MSXML | ||
| 2 | |||
| 3 | package XML::Toolset::MSXML; | ||
| 4 | |||
| 5 | use strict; | ||
| 6 | |||
| 7 | use vars qw( $VERSION @ISA $MSXML_VERSION); | ||
| 8 | use XML::Toolset::DOM; | ||
| 9 | use Win32::OLE; | ||
| 10 | |||
| 11 | $VERSION = sprintf"%d.%03d", q$Revision: 1.9 $ =~ /: (\d+)\.(\d+)/; | ||
| 12 | @ISA = qw(XML::Toolset::DOM); | ||
| 13 | |||
| 14 | use constant XSI_NS => 'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"'; | ||
| 15 | |||
| 16 | my $VALID_OPTIONS = { validation => 1 }; | ||
| 17 | |||
| 18 | sub new { | ||
| 19 | &App::sub_entry if ($App::trace); | ||
| 20 | my $class = shift; | ||
| 21 | my %options = @_; | ||
| 22 | my $self = {}; | ||
| 23 | bless ($self, $class); | ||
| 24 | |||
| 25 | $self->set_options(\%options,$VALID_OPTIONS); | ||
| 26 | |||
| 27 | #print("Instantiating XML::Toolset::MSXML", $self, "\n"); | ||
| 28 | |||
| 29 | &App::sub_exit($self) if ($App::trace); | ||
| 30 | return $self; | ||
| 31 | } | ||
| 32 | |||
| 33 | sub version { | ||
| 34 | &App::sub_entry if ($App::trace); | ||
| 35 | eval {create_doc_and_cache()}; | ||
| 36 | my $version = $MSXML_VERSION; | ||
| 37 | &App::sub_exit($version) if ($App::trace); | ||
| 38 | return($version); | ||
| 39 | } | ||
| 40 | |||
| 41 | sub validate_document { | ||
| 42 | &App::sub_entry if ($App::trace); | ||
| 43 | my ($self, $doc) = @_; | ||
| 44 | my ($xml); | ||
| 45 | if (ref($doc)) { | ||
| 46 | $xml = $doc->{xml}; | ||
| 47 | } | ||
| 48 | else { | ||
| 49 | $xml = $doc; | ||
| 50 | } | ||
| 51 | |||
| 52 | $self->{dom} = undef; | ||
| 53 | |||
| 54 | die "validate_document called with no data to validate\n" unless defined $xml and length $xml > 0; | ||
| 55 | |||
| 56 | #print("the xml to validate : $xml ", $self, "\n"); | ||
| 57 | |||
| 58 | my ($msxml,$msxmlcache) = create_doc_and_cache(); | ||
| 59 | |||
| 60 | $msxml->{async} = 0; | ||
| 61 | $msxml->{validateOnParse} = 0; | ||
| 62 | $msxml->{resolveExternals} = 1; | ||
| 63 | |||
| 64 | #print("Starting to parse\n"); | ||
| 65 | $msxml->LoadXML($xml); | ||
| 66 | #print("Parsed the document\n"); | ||
| 67 | |||
| 68 | my $xmlroot = $msxml->{documentElement}; | ||
| 69 | |||
| 70 | if ($msxml->parseError()->{errorCode} != 0) { | ||
| 71 | #print("XML Parse Error (not syntactically valid)\n"); | ||
| 72 | my $error = $msxml->parseError(); | ||
| 73 | #print("Error", $error, "\n"); | ||
| 74 | $self->add_error({ | ||
| 75 | message => $error->{reason}, | ||
| 76 | line => $error->{line}, | ||
| 77 | column => $error->{linepos} | ||
| 78 | }); | ||
| 79 | return; | ||
| 80 | } | ||
| 81 | |||
| 82 | if ($self->{validation}) { | ||
| 83 | #print("XML Syntactically valid\n"); | ||
| 84 | load_schemas($msxml, $msxmlcache); | ||
| 85 | #print("Toolset against schema/DTD \n"); | ||
| 86 | if ($msxml->{doctype} || $msxml->{schemas}) { | ||
| 87 | #print($msxml->{doctype}, $msxml->{schemas}, "\n"); | ||
| 88 | $msxml->{validateOnParse} = 1; | ||
| 89 | $msxml->LoadXML($xml); | ||
| 90 | my $error = $msxml->parseError(); | ||
| 91 | if ($error->{errorCode} != 0) { | ||
| 92 | $self->add_error({ | ||
| 93 | message => $error->{reason}, | ||
| 94 | line => $error->{line}, | ||
| 95 | column => $error->{linepos} | ||
| 96 | }); | ||
| 97 | return; | ||
| 98 | } | ||
| 99 | $error = $msxml->validate(); | ||
| 100 | if ($error->{errorCode} != 0) { | ||
| 101 | $self->add_error({ | ||
| 102 | message => $error->{reason}, | ||
| 103 | line => $error->{line}, | ||
| 104 | column => $error->{linepos} | ||
| 105 | }); | ||
| 106 | return; | ||
| 107 | } | ||
| 108 | } else { | ||
| 109 | # If there is nothing to validate against, treat it as valid. | ||
| 110 | #print("No doctype or schema\n"); | ||
| 111 | } | ||
| 112 | } | ||
| 113 | |||
| 114 | #Valid | ||
| 115 | $self->{dom} = $msxml; | ||
| 116 | &App::sub_exit(1) if ($App::trace); | ||
| 117 | return 1; | ||
| 118 | } | ||
| 119 | |||
| 120 | sub load_schemas { | ||
| 121 | &App::sub_entry if ($App::trace); | ||
| 122 | my ($xml, $schema_cache) = @_; | ||
| 123 | |||
| 124 | my %schemas; | ||
| 125 | |||
| 126 | $xml->setProperty('SelectionNamespaces', XSI_NS); | ||
| 127 | my $no_ns_schema_xpath = q[//*/@xsi:noNamespaceSchemaLocation]; | ||
| 128 | my $no_ns_schema_nodes = $xml->{documentElement}->selectNodes($no_ns_schema_xpath); | ||
| 129 | |||
| 130 | for (my $i=0; $i < $no_ns_schema_nodes->{length}; $i++){ | ||
| 131 | my $schema_txt = $no_ns_schema_nodes->item($i)->{text}; | ||
| 132 | my %add_schemas = ('', $schema_txt); | ||
| 133 | %schemas = (%schemas, %add_schemas); | ||
| 134 | } | ||
| 135 | |||
| 136 | my $schema_xpath = q[//*/@xsi:schemaLocation]; | ||
| 137 | my $schema_location_nodes = $xml->{documentElement}->selectNodes($schema_xpath); | ||
| 138 | |||
| 139 | for (my $i=0; $i < $schema_location_nodes->{length}; $i++){ | ||
| 140 | my $schema_txt = $schema_location_nodes->item($i)->{text}; | ||
| 141 | my %add_schemas = split ' ', $schema_txt; | ||
| 142 | %schemas = (%schemas, %add_schemas); | ||
| 143 | } | ||
| 144 | |||
| 145 | while (my ($ns, $schema) = each %schemas) { | ||
| 146 | #print("Loading schema [$ns] -> $schema\n"); | ||
| 147 | $schema_cache->add($ns, $schema); | ||
| 148 | if (my $schema_error = Win32::OLE::LastError()) { | ||
| 149 | $schema_error =~ s/OLE exception .*\n\n//m; | ||
| 150 | $schema_error =~ s/Win32::OLE.*//s; | ||
| 151 | die $schema_error; | ||
| 152 | } | ||
| 153 | } | ||
| 154 | $xml->{schemas} = $schema_cache if %schemas; | ||
| 155 | &App::sub_exit() if ($App::trace); | ||
| 156 | return; | ||
| 157 | } | ||
| 158 | |||
| 159 | sub dependencies_available { | ||
| 160 | &App::sub_entry if ($App::trace); | ||
| 161 | create_doc_and_cache(); | ||
| 162 | &App::sub_exit(1) if ($App::trace); | ||
| 163 | return 1; | ||
| 164 | } | ||
| 165 | |||
| 166 | sub create_doc_and_cache { | ||
| 167 | &App::sub_entry if ($App::trace); | ||
| 168 | # Stop Win32::OLE from being noisy | ||
| 169 | my $warn_level = Win32::OLE->Option('Warn'); | ||
| 170 | Win32::OLE->Option(Warn => 0); | ||
| 171 | |||
| 172 | foreach my $version ('5.0', '4.0') { | ||
| 173 | my $doc = Win32::OLE->new('MSXML2.DOMDocument.' . $version) or next; | ||
| 174 | my $cache = Win32::OLE->new('MSXML2.XMLSchemaCache.' . $version) or next; | ||
| 175 | $MSXML_VERSION = $version; | ||
| 176 | Win32::OLE->Option(Warn => $warn_level); # restore warn level | ||
| 177 | return ($doc,$cache); | ||
| 178 | } | ||
| 179 | die "Unable to instantiate MSXML DOMDocument and SchemaCache. (Do you have a compatible version of MSXML installed?)"; | ||
| 180 | &App::sub_exit() if ($App::trace); | ||
| 181 | } | ||
| 182 | |||
| 183 | dependencies_available(); | ||
| 184 | |||
| 185 | __END__ |