Author: dr
Date: Fri Aug 10 15:16:01 2007
New Revision: 5875

Log:
- Implemented invalid XML detection.

Added:
    trunk/Tree/src/exceptions/invalid_xml.php   (with props)
    trunk/Tree/src/exceptions/invalid_xml_format.php   (with props)
    trunk/Tree/tests/files/invalid-structure.xml
    trunk/Tree/tests/files/invalid-xml.xml
Modified:
    trunk/Tree/design/class_diagram.png
    trunk/Tree/src/backends/xml.php
    trunk/Tree/src/tree_autoload.php
    trunk/Tree/tests/xml_tree.php

Modified: trunk/Tree/design/class_diagram.png
==============================================================================
Binary files - no diff available.

Modified: trunk/Tree/src/backends/xml.php
==============================================================================
--- trunk/Tree/src/backends/xml.php [iso-8859-1] (original)
+++ trunk/Tree/src/backends/xml.php [iso-8859-1] Fri Aug 10 15:16:01 2007
@@ -96,9 +96,31 @@
      */
     public function __construct( $xmlFile, ezcTreeXmlDataStore $store )
     {
+        if ( !file_exists( $xmlFile ) )
+        {
+            throw new ezcBaseFileNotFoundException( $xmlFile, "XML" );
+        }
+
+        $previous = libxml_use_internal_errors( true );
+
         $dom = new DomDocument();
         $dom->load( $xmlFile );
-        $dom->relaxNGValidateSource( self::relaxNG );
+        $errors = libxml_get_errors();
+        libxml_clear_errors();
+        if ( count( $errors ) )
+        {
+            throw new ezcTreeInvalidXmlException( $xmlFile, $errors );
+        }
+
+        $valid = $dom->relaxNGValidateSource( self::relaxNG );
+        if ( !$valid )
+        {
+            $errors = libxml_get_errors();
+            libxml_clear_errors();
+            throw new ezcTreeInvalidXmlFormatException( $xmlFile, $errors );
+        }
+
+        libxml_use_internal_errors( $previous );
 
         $store->setDomTree( $dom );
 

Added: trunk/Tree/src/exceptions/invalid_xml.php
==============================================================================
--- trunk/Tree/src/exceptions/invalid_xml.php (added)
+++ trunk/Tree/src/exceptions/invalid_xml.php [iso-8859-1] Fri Aug 10 15:16:01 
2007
@@ -1,0 +1,36 @@
+<?php
+/**
+ * File containing the ezcTreeInvalidXmlException class
+ *
+ * @package Tree
+ * @version //autogen//
+ * @copyright Copyright (C) 2005-2007 eZ systems as. All rights reserved.
+ * @license http://ez.no/licenses/new_bsd New BSD License
+ */
+
+/**
+ * Exception that is thrown when an XML tree document is not well-formed.
+ *
+ * @package Tree
+ * @version //autogen//
+ */
+class ezcTreeInvalidXmlException extends ezcTreeException
+{
+    /**
+     * Constructs a new ezcTreeInvalidXmlException
+     *
+     * @param string $xmlFile
+     * @param array $errors
+     * @return void
+     */
+    function __construct( $xmlFile, $errors )
+    {
+        $message = '';
+        foreach( $errors as $error )
+        {
+            $message .= sprintf( "%s:%d:%d: %s\n", $error->file, $error->line, 
$error->column, trim( $error->message ) );
+        }
+        parent::__construct( "The XML file '$xmlFile' is not well-formed:\n". 
$message );
+    }
+}
+?>

Propchange: trunk/Tree/src/exceptions/invalid_xml.php
------------------------------------------------------------------------------
    svn:eol-style = native

Added: trunk/Tree/src/exceptions/invalid_xml_format.php
==============================================================================
--- trunk/Tree/src/exceptions/invalid_xml_format.php (added)
+++ trunk/Tree/src/exceptions/invalid_xml_format.php [iso-8859-1] Fri Aug 10 
15:16:01 2007
@@ -1,0 +1,36 @@
+<?php
+/**
+ * File containing the ezcTreeInvalidXmlFormatException class
+ *
+ * @package Tree
+ * @version //autogen//
+ * @copyright Copyright (C) 2005-2007 eZ systems as. All rights reserved.
+ * @license http://ez.no/licenses/new_bsd New BSD License
+ */
+
+/**
+ * Exception that is thrown when an XML tree document does not validate.
+ *
+ * @package Tree
+ * @version //autogen//
+ */
+class ezcTreeInvalidXmlFormatException extends ezcTreeException
+{
+    /**
+     * Constructs a new ezcTreeInvalidClassException
+     *
+     * @param string $xmlFile
+     * @param array $errors
+     * @return void
+     */
+    function __construct( $xmlFile, $errors )
+    {
+        $message = '';
+        foreach( $errors as $error )
+        {
+            $message .= sprintf( "%s:%d:%d: %s\n", $error->file, $error->line, 
$error->column, trim( $error->message ) );
+        }
+        parent::__construct( "The XML file '$xmlFile' does not validate 
according to the expected schema:\n". $message );
+    }
+}
+?>

Propchange: trunk/Tree/src/exceptions/invalid_xml_format.php
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: trunk/Tree/src/tree_autoload.php
==============================================================================
--- trunk/Tree/src/tree_autoload.php [iso-8859-1] (original)
+++ trunk/Tree/src/tree_autoload.php [iso-8859-1] Fri Aug 10 15:16:01 2007
@@ -15,6 +15,8 @@
     'ezcTreeIdsDoNotMatchException'             => 
'Tree/exceptions/ids_do_not_match.php',
     'ezcTreeInvalidClassException'              => 
'Tree/exceptions/invalid_class.php',
     'ezcTreeInvalidIdException'                 => 
'Tree/exceptions/invalid_id.php',
+    'ezcTreeInvalidXmlException'                => 
'Tree/exceptions/invalid_xml.php',
+    'ezcTreeInvalidXmlFormatException'          => 
'Tree/exceptions/invalid_xml_format.php',
     'ezcTreeTransactionAlreadyStartedException' => 
'Tree/exceptions/transaction_already_started.php',
     'ezcTreeTransactionNotStartedException'     => 
'Tree/exceptions/transaction_not_started.php',
     'ezcTreeDataStore'                          => 
'Tree/interfaces/data_store.php',

Added: trunk/Tree/tests/files/invalid-structure.xml
==============================================================================
--- trunk/Tree/tests/files/invalid-structure.xml (added)
+++ trunk/Tree/tests/files/invalid-structure.xml [iso-8859-1] Fri Aug 10 
15:16:01 2007
@@ -1,0 +1,29 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!DOCTYPE tree PUBLIC "" "">
+<tree xmlns="http://components.ez.no/Tree"; 
xmlns:etd="http://components.ez.no/Tree/data";>
+    <node id="id1">
+        <etd:data>Node 1</etd:data>
+        <node id="id2">
+            <etd:data>Node 2</etd:data>
+        </node>
+        <node id="id3">
+            <etd:data>Node 3</etd:data>
+        </node>
+        <foo id="id4">
+            <etd:data>Node 4</etd:data>
+            <node id="id6">
+                <etd:data>Node 6</etd:data>
+                <node id="id7">
+                    <etd:data>Node 7</etd:data>
+                </node>
+                <node id="id8">
+                    <etd:data>Node 8</etd:data>
+                </node>
+            </node>
+        </foo>
+        <node id="id5">
+            <etd:data>Node 5</etd:data>
+            <node id="id9"/>
+        </node>
+    </node>
+</tree>

Added: trunk/Tree/tests/files/invalid-xml.xml
==============================================================================
--- trunk/Tree/tests/files/invalid-xml.xml (added)
+++ trunk/Tree/tests/files/invalid-xml.xml [iso-8859-1] Fri Aug 10 15:16:01 2007
@@ -1,0 +1,28 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!DOCTYPE tree PUBLIC "" "">
+<tree xmlns="http://components.ez.no/Tree"; 
xmlns:etd="http://components.ez.no/Tree/data";>
+    <node id="id1">
+        <etd:data>Node 1</etd:data>
+        <node id="id2">
+            <etd:data>Node 2</etd:data>
+        </node>
+        <node id="id3">
+            <etd:data>Node 3</etd:data>
+        </node>
+        <node id="id4">
+            <etd:data>Node 4</etd:data>
+            <node id="id6">
+                <etd:data>Node 6</etd:data>
+                <node id="id7">
+                    <etd:data>Node 7</etd:data>
+                </node>
+                <node id="id8">
+                    <etd:data>Node 8</etd:data>
+                </node>
+            </node>
+        <node id="id5">
+            <etd:data>Node 5</etd:data>
+            <node id="id9"/>
+        </node>
+    </node>
+</tree>

Modified: trunk/Tree/tests/xml_tree.php
==============================================================================
--- trunk/Tree/tests/xml_tree.php [iso-8859-1] (original)
+++ trunk/Tree/tests/xml_tree.php [iso-8859-1] Fri Aug 10 15:16:01 2007
@@ -45,6 +45,54 @@
             new ezcTreeXmlInternalDataStore()
         );
         return $tree;
+    }
+
+    public function testOpenInvalidFormatXmlTree()
+    {
+        $dirname = dirname( __FILE__ );
+        try
+        {
+            $tree = new ezcTreeXml( 
+                "$dirname/files/invalid-structure.xml",
+                new ezcTreeXmlInternalDataStore()
+            );
+        }
+        catch ( ezcTreeInvalidXmlFormatException $e )
+        {
+            self::assertSame( "The XML file 
'$dirname/files/invalid-structure.xml' does not validate according to the 
expected schema:\n$dirname/files/invalid-structure.xml:12:0: Did not expect 
element foo there\n", $e->getMessage() );
+        }
+    }
+
+    public function testOpenInvalidXmlTree()
+    {
+        $dirname = dirname( __FILE__ );
+        try
+        {
+            $tree = new ezcTreeXml( 
+                "$dirname/files/invalid-xml.xml",
+                new ezcTreeXmlInternalDataStore()
+            );
+        }
+        catch ( ezcTreeInvalidXmlException $e )
+        {
+            self::assertSame( "The XML file '$dirname/files/invalid-xml.xml' 
is not well-formed:\n$dirname/files/invalid-xml.xml:28:8: Opening and ending 
tag mismatch: node line 4 and tree\n$dirname/files/invalid-xml.xml:29:1: 
Premature end of data in tag tree line 3\n", $e->getMessage() );
+        }
+    }
+
+    public function testOpenNonExistingXmlTree()
+    {
+        $dirname = dirname( __FILE__ );
+        try
+        {
+            $tree = new ezcTreeXml( 
+                "$dirname/files/does-not-exist.xml",
+                new ezcTreeXmlInternalDataStore()
+            );
+        }
+        catch ( ezcBaseFileNotFoundException $e )
+        {
+            self::assertSame( "The XML file 
'$dirname/files/does-not-exist.xml' could not be found.", $e->getMessage() );
+        }
     }
 
     public function testCreateXmlTree()


-- 
svn-components mailing list
[email protected]
http://lists.ez.no/mailman/listinfo/svn-components

Reply via email to