Changeset 842

Show
Ignore:
Timestamp:
08/15/07 18:10:27 (1 year ago)
Author:
mikey
Message:

fixed ticket #58, xml files containing invalid xml will now throw a stubXMLException

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/src/main/php/net/stubbles/websites/xml/stubXMLPostInterceptor.php

    r839 r842  
    7272        $xslProcessor->setXMLDocument($this->createXMLSkinDocument($skin)); 
    7373        $resultXSL = $xslProcessor->transformToDoc(); 
    74         $resultXSL->xinclude(); 
     74        @$resultXSL->xinclude(); 
    7575 
    7676        // now we use the created xsl to transform the xml document created by the application 
  • trunk/src/main/php/net/stubbles/xml/stubXMLXIncludeStreamWrapper.php

    r837 r842  
    77 * @subpackage  xml 
    88 */ 
    9 stubClassLoader::load('net.stubbles.xml.xsl.stubXSLProcessor'); 
     9stubClassLoader::load('net.stubbles.xml.stubXMLException', 
     10                      'net.stubbles.xml.xsl.stubXSLProcessor' 
     11); 
    1012/** 
    1113 * Class to wrap xincludes transparently as stream wrapper. 
     
    2123     * @var  bool 
    2224     */ 
    23     private static $registered = false; 
     25    private static $registered     = false; 
    2426    /** 
    2527     * the xsl processor to use for the transformation 
     
    6769    /** 
    6870     * registers the class as stream wrapper for the sml protocol 
     71     * 
     72     * @throws  stubXMLException 
    6973     */ 
    7074    public static function register() 
     
    9498     * sets the cache path to use 
    9599     * 
    96      * @param  string  $cachePath 
     100     * @param   string  $cachePath 
     101     * @throws  stubException 
    97102     */ 
    98103    public static function setCachePath($cachePath) 
     
    124129     * @param   string  $opened_path  full path that was actually opened 
    125130     * @return  bool 
     131     * @throws  stubXMLException 
    126132     */ 
    127133    public function stream_open($path, $mode, $options, $opened_path) 
     
    165171    /** 
    166172     * processes the file and creates a cached version of it 
     173     * 
     174     * @throws  stubXMLException 
    167175     */ 
    168176    protected function processFile() 
     
    170178        $previousErrorHandling = libxml_use_internal_errors(true); 
    171179        $xslProcessor          = clone self::$xslProcessor; 
    172         $xslProcessor->setXMLDocument(DOMDocument::load($this->fileName)); 
     180        $domDocument           = DOMDocument::load($this->fileName); 
     181        if (false === $domDocument) { 
     182            $this->handleErrors(libxml_get_errors(), $previousErrorHandling); 
     183            libxml_clear_errors(); 
     184        } 
     185         
     186        $xslProcessor->setXMLDocument($domDocument); 
    173187        $resultDoc = $xslProcessor->transformToDoc(); 
    174188        // we save first to prevent a infinite loop in  case of recursions 
     
    179193            unlink($this->cachedFileName); 
    180194            libxml_clear_errors(); 
    181             foreach ($errors as $error) { 
    182                 $message = trim($error->message) . (($error->file) ? (' in file ' . $error->file) : ('')) . ' on line ' . $error->line . ' in column ' . $error->column; 
    183                 switch ($error->level) { 
    184                     case LIBXML_ERR_WARNING: 
    185                         $this->appendError($resultDoc, 'warning', $message); 
    186                         break; 
    187                     case LIBXML_ERR_ERROR: 
    188                         $this->appendError($resultDoc, 'error', $message); 
    189                         break; 
    190                     case LIBXML_ERR_FATAL: 
    191                         throw new stubXMLException('Fatal error: ' . $message); 
    192                 } 
    193             } 
     195            $this->handleErrors($errors, $previousErrorHandling); 
    194196        } 
    195197 
     
    199201        } 
    200202        $resultDoc->save($this->cachedFileName); 
     203    } 
     204 
     205    /** 
     206     * handles libxml errors 
     207     * 
     208     * @param   array  $errors 
     209     * @throws  stubXMLException 
     210     */ 
     211    protected function handleErrors(array $errors, $previousErrorHandling) 
     212    { 
     213        foreach ($errors as $error) { 
     214            $message = trim($error->message) . (($error->file) ? (' in file ' . $error->file) : ('')) . ' on line ' . $error->line . ' in column ' . $error->column; 
     215            switch ($error->level) { 
     216                case LIBXML_ERR_WARNING: 
     217                    $this->appendError($resultDoc, 'warning', $message); 
     218                    break; 
     219                case LIBXML_ERR_ERROR: 
     220                    $this->appendError($resultDoc, 'error', $message); 
     221                    break; 
     222                case LIBXML_ERR_FATAL: 
     223                    libxml_use_internal_errors($previousErrorHandling); 
     224                    throw new stubXMLException('Fatal error: ' . $message); 
     225            } 
     226        } 
    201227    } 
    202228