Changeset 860

Show
Ignore:
Timestamp:
08/21/07 17:45:19 (1 year ago)
Author:
mikey
Message:

added possibility to retrieve the full qualified class name of a xml stream writer

Files:

Legend:

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

    r678 r860  
    3535    public static function create($type) 
    3636    { 
    37         $className = 'stub' . $type . 'XMLStreamWriter'; 
    38         if (class_exists($className, false) == false) { 
    39             stubClassLoader::load('net.stubbles.xml.' . $className); 
     37        $fqClassName = self::getFqClassName($type); 
     38        $nqClassName = stubClassLoader::getNonQualifiedClassName($fqClassName); 
     39        if (class_exists($nqClassName, false) == false) { 
     40            stubClassLoader::load($fqClassName); 
    4041        } 
    4142 
    42         $xmlStreamWriter = new $className(); 
     43        $xmlStreamWriter = new $nqClassName(); 
    4344        return $xmlStreamWriter; 
    4445    } 
     
    4748     * creates a xml stream writer depending on available xml extensions 
    4849     * 
    49      * If an order is submitted it will use this order, else it uses the default order. 
     50     * If an order is submitted it will use this order, else it uses the default 
     51     * order. 
     52     * Warning: if a list of features is provided an instance of every possible 
     53     * xml stream writer will be created to check the feature list until a 
     54     * sufficient xml stream writer class was found. 
    5055     * 
    5156     * @param   array                $order     optional  extensions in order to use 
    5257     * @param   array                $features  optional  features the implementation must provide 
    5358     * @return  stubXMLStreamWriter 
     59     * @throws  stubXMLException 
    5460     */ 
    55     public static function createAsAvailable(array $order = null, $features = array()) 
     61    public static function createAsAvailable(array $order = null, array $features = array()) 
    5662    { 
    5763        if (null == $order) { 
     
    6066 
    6167        foreach ($order as $xmlExtension) { 
    62             if (extension_loaded($xmlExtension) == true) { 
    63                 if (isset(self::$types[$xmlExtension]) == true) { 
    64                     $name = self::$types[$xmlExtension]; 
    65                 } else { 
    66                     $name = $xmlExtension; 
    67                 } 
    68                 $writer = self::create($name); 
    69                 foreach ($features as $feature) { 
    70                     if (!$writer->hasFeature($feature)) { 
    71                         continue 2; 
    72                     } 
    73                 } 
    74                 return $writer; 
     68            $name = self::checkExtension($xmlExtension); 
     69            if (null === $name) { 
     70                continue; 
     71            } 
     72             
     73            $xmlStreamWriter = self::checkFeatures($name, $features); 
     74            if (null !== $xmlStreamWriter) { 
     75                return $xmlStreamWriter; 
    7576            } 
    7677        } 
     
    7879        throw new stubXMLException('Not any single xml extension available that provides the requested features, can not create a xml stream writer!'); 
    7980    } 
     81 
     82    /** 
     83     * returns full qualified class name for the xml stream writer of the given type 
     84     * 
     85     * @param   string  $type  concrete type of stream writer 
     86     * @return  string 
     87     */ 
     88    public static function getFqClassName($type) 
     89    { 
     90        return 'net.stubbles.xml.stub' . $type . 'XMLStreamWriter'; 
     91    } 
     92 
     93    /** 
     94     * returns full qualified class name for the xml stream writer depending on available xml extensions 
     95     *  
     96     * If an order is submitted it will use this order, else it uses the default 
     97     * order. 
     98     * Warning: if a list of features is provided an instance of every possible 
     99     * xml stream writer will be created to check the feature list until a 
     100     * sufficient xml stream writer class was found. 
     101     * 
     102     * @param   array   $order     optional  extensions in order to use 
     103     * @param   array   $features  optional  features the implementation must provide 
     104     * @return  string 
     105     * @throws  stubXMLException 
     106     */ 
     107    public static function getFqClassNameAsAvailable(array $order = null, array $features = array()) 
     108    { 
     109        if (null == $order) { 
     110            $order = array_keys(self::$types); 
     111        } 
     112         
     113        foreach ($order as $xmlExtension) { 
     114            $name = self::checkExtension($xmlExtension); 
     115            if (null === $name) { 
     116                continue; 
     117            } 
     118             
     119            if (count($features) == 0) { 
     120                return self::getFqClassName($name); 
     121            } 
     122             
     123            $xmlStreamWriter = self::checkFeatures($name, $features); 
     124            if (null !== $xmlStreamWriter) { 
     125                return $xmlStreamWriter->getClassName(); 
     126            } 
     127        } 
     128         
     129        throw new stubXMLException('Not any single xml extension available that provides the requested features, can not return a class name for a xml stream writer!'); 
     130    } 
     131 
     132    /** 
     133     * checks given extension and returns their type name 
     134     *  
     135     * If extension is not loaded the return value will be null. 
     136     * 
     137     * @param   string  $xmlExtension  name of the extension to check 
     138     * @return  string 
     139     */ 
     140    protected static function checkExtension($xmlExtension) 
     141    { 
     142        if (extension_loaded($xmlExtension) === false) { 
     143            return null; 
     144        } 
     145         
     146        return ((isset(self::$types[$xmlExtension]) === true) ? (self::$types[$xmlExtension]) : ($xmlExtension)); 
     147    } 
     148 
     149    /** 
     150     * method to check if a given type of xml stream writer has requested features 
     151     *  
     152     * Returns an instance of the xml stream writer if it has the requested 
     153     * features, else null. 
     154     * 
     155     * @param   string               $type      type of stream writer to check 
     156     * @param   array                $features  features the implementation must provide 
     157     * @return  stubXMLStreamWriter 
     158     */ 
     159    protected static function checkFeatures($type, array $features) 
     160    { 
     161        $writer = self::create($type); 
     162        foreach ($features as $feature) { 
     163            if ($writer->hasFeature($feature) === false) { 
     164                return null; 
     165            } 
     166        } 
     167         
     168        return $writer; 
     169    } 
    80170} 
    81171?> 
  • trunk/src/test/php/net/stubbles/xml/stubXMLStreamWriterFactoryTestCase.php

    r529 r860  
    11<?php 
    22/** 
    3  * Test for net.stubbles.xml.stubXMLStreamWriterFactory 
     3 * Test for net.stubbles.xml.stubXMLStreamWriterFactory. 
    44 * 
    55 * @author      Frank Kleine <mikey@stubbles.net> 
     
    1212Mock::generate('stubXMLStreamWriter', 'stubMockXMLStreamWriter'); 
    1313/** 
    14  * Test for net.stubbles.xml.stubXMLStreamWriterFactory 
     14 * Test for net.stubbles.xml.stubXMLStreamWriterFactory. 
    1515 * 
    1616 * @package     stubbles 
     
    2020{ 
    2121    /** 
    22      * Test the creation of a xml stream writer 
     22     * test the creation of a xml stream writer 
    2323     */ 
    2424    public function testCreate() 
     
    2929 
    3030    /** 
    31      * Test the creation of a xml stream writer with given order of extensions 
     31     * test the creation of a xml stream writer with given order of extensions 
    3232     */ 
    3333    public function testCreateAsAvailable() 
     
    3939       stubXMLStreamWriterFactory::createAsAvailable(array('ExtensionDoesNotExist')); 
    4040    } 
     41 
     42    /** 
     43     * test the finding class name of a xml stream writer with given order of extensions 
     44     */ 
     45    public function testGetFqClassNameAsAvailable() 
     46    { 
     47       $this->assertEqual(stubXMLStreamWriterFactory::getFqClassNameAsAvailable(), 'net.stubbles.xml.stubDomXMLStreamWriter'); 
     48       $this->assertEqual(stubXMLStreamWriterFactory::getFqClassNameAsAvailable(array('xmlwriter', 'Dom')), 'net.stubbles.xml.stubLibXmlXMLStreamWriter'); 
     49       $this->assertEqual(stubXMLStreamWriterFactory::getFqClassNameAsAvailable(array('xmlwriter', 'Dom'), array(stubXMLStreamWriter::FEATURE_IMPORT_WRITER)), 'net.stubbles.xml.stubDomXMLStreamWriter'); 
     50       $this->expectException('stubXMLException'); 
     51       stubXMLStreamWriterFactory::getFqClassNameAsAvailable(array('ExtensionDoesNotExist')); 
     52    } 
    4153} 
    4254?>