Changeset 860
- Timestamp:
- 08/21/07 17:45:19 (1 year ago)
- Files:
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/src/main/php/net/stubbles/xml/stubXMLStreamWriterFactory.php
r678 r860 35 35 public static function create($type) 36 36 { 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); 40 41 } 41 42 42 $xmlStreamWriter = new $ className();43 $xmlStreamWriter = new $nqClassName(); 43 44 return $xmlStreamWriter; 44 45 } … … 47 48 * creates a xml stream writer depending on available xml extensions 48 49 * 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. 50 55 * 51 56 * @param array $order optional extensions in order to use 52 57 * @param array $features optional features the implementation must provide 53 58 * @return stubXMLStreamWriter 59 * @throws stubXMLException 54 60 */ 55 public static function createAsAvailable(array $order = null, $features = array())61 public static function createAsAvailable(array $order = null, array $features = array()) 56 62 { 57 63 if (null == $order) { … … 60 66 61 67 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; 75 76 } 76 77 } … … 78 79 throw new stubXMLException('Not any single xml extension available that provides the requested features, can not create a xml stream writer!'); 79 80 } 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 } 80 170 } 81 171 ?> trunk/src/test/php/net/stubbles/xml/stubXMLStreamWriterFactoryTestCase.php
r529 r860 1 1 <?php 2 2 /** 3 * Test for net.stubbles.xml.stubXMLStreamWriterFactory 3 * Test for net.stubbles.xml.stubXMLStreamWriterFactory. 4 4 * 5 5 * @author Frank Kleine <mikey@stubbles.net> … … 12 12 Mock::generate('stubXMLStreamWriter', 'stubMockXMLStreamWriter'); 13 13 /** 14 * Test for net.stubbles.xml.stubXMLStreamWriterFactory 14 * Test for net.stubbles.xml.stubXMLStreamWriterFactory. 15 15 * 16 16 * @package stubbles … … 20 20 { 21 21 /** 22 * Test the creation of a xml stream writer22 * test the creation of a xml stream writer 23 23 */ 24 24 public function testCreate() … … 29 29 30 30 /** 31 * Test the creation of a xml stream writer with given order of extensions31 * test the creation of a xml stream writer with given order of extensions 32 32 */ 33 33 public function testCreateAsAvailable() … … 39 39 stubXMLStreamWriterFactory::createAsAvailable(array('ExtensionDoesNotExist')); 40 40 } 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 } 41 53 } 42 54 ?>
