5 * Will be set to true or false to indicate whether the file is
6 * well-formed XML. Note that this doesn't check schema validity.
8 public $wellFormed = false;
11 * Name of the document's root element, including any namespace
14 public $rootElement = '';
16 private $softNamespaces;
17 private $namespaces = array();
20 * @param $file string filename
21 * @param $softNamespaces bool
22 * If set to true, use of undeclared XML namespaces will be ignored.
23 * This matches the behavior of rsvg, but more compliant consumers
24 * such as Firefox will reject such files.
25 * Leave off for the default, stricter checks.
27 function __construct( $file, $softNamespaces=false ) {
28 $this->softNamespaces
= $softNamespaces;
32 private function run( $fname ) {
33 if( $this->softNamespaces
) {
34 $parser = xml_parser_create( 'UTF-8' );
36 $parser = xml_parser_create_ns( 'UTF-8' );
39 // case folding violates XML standard, turn it off
40 xml_parser_set_option( $parser, XML_OPTION_CASE_FOLDING
, false );
42 xml_set_element_handler( $parser, array( $this, 'elementOpen' ), false );
44 $file = fopen( $fname, "rb" );
46 $chunk = fread( $file, 32768 );
47 $ret = xml_parse( $parser, $chunk, feof( $file ) );
49 // XML isn't well-formed!
51 xml_parser_free( $parser );
54 } while( !feof( $file ) );
56 $this->wellFormed
= true;
59 xml_parser_free( $parser );
62 private function elementOpen( $parser, $name, $attribs ) {
63 if( $this->softNamespaces
) {
64 // Check namespaces manually, so expat doesn't throw
65 // errors on use of undeclared namespaces.
66 foreach( $attribs as $attrib => $val ) {
67 if( $attrib == 'xmlns' ) {
68 $this->namespaces
[''] = $val;
69 } elseif( substr( $attrib, 0, strlen( 'xmlns:' ) ) == 'xmlns:' ) {
70 $this->namespaces
[substr( $attrib, strlen( 'xmlns:' ) )] = $val;
74 if( strpos( $name, ':' ) === false ) {
78 list( $ns, $subname ) = explode( ':', $name, 2 );
81 if( isset( $this->namespaces
[$ns] ) ) {
82 $name = $this->namespaces
[$ns] . ':' . $subname;
84 // Technically this is invalid for XML with Namespaces.
85 // But..... we'll just let it slide in soft mode.
89 // We only need the first open element
90 $this->rootElement
= $name;
91 xml_set_element_handler( $parser, false, false );