| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
stubClassLoader::load('net::stubbles::xml::stubXMLException', |
|---|
| 11 |
'net::stubbles::xml::stubXMLStreamWriter' |
|---|
| 12 |
); |
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
class stubXMLStreamWriterFactory |
|---|
| 21 |
{ |
|---|
| 22 |
|
|---|
| 23 |
* list of available streamwriter types |
|---|
| 24 |
* |
|---|
| 25 |
* @var array |
|---|
| 26 |
*/ |
|---|
| 27 |
protected static $types = array('dom' => 'Dom', |
|---|
| 28 |
'xmlwriter' => 'LibXml' |
|---|
| 29 |
); |
|---|
| 30 |
|
|---|
| 31 |
* default version of xml stream writers to create |
|---|
| 32 |
* |
|---|
| 33 |
* @var string |
|---|
| 34 |
*/ |
|---|
| 35 |
protected static $version = '1.0'; |
|---|
| 36 |
|
|---|
| 37 |
* default encoding of xml stream writers to create |
|---|
| 38 |
* |
|---|
| 39 |
* @var string |
|---|
| 40 |
*/ |
|---|
| 41 |
protected static $encoding = 'UTF-8'; |
|---|
| 42 |
|
|---|
| 43 |
|
|---|
| 44 |
* sets the default version of xml stream writers to create |
|---|
| 45 |
* |
|---|
| 46 |
* @param string $version |
|---|
| 47 |
*/ |
|---|
| 48 |
public static function setVersion($version) |
|---|
| 49 |
{ |
|---|
| 50 |
self::$version = $version; |
|---|
| 51 |
} |
|---|
| 52 |
|
|---|
| 53 |
|
|---|
| 54 |
* sets the default encoding of xml stream writers to create |
|---|
| 55 |
* |
|---|
| 56 |
* @param string $encoding |
|---|
| 57 |
*/ |
|---|
| 58 |
public static function setEncoding($encoding) |
|---|
| 59 |
{ |
|---|
| 60 |
self::$encoding = $encoding; |
|---|
| 61 |
} |
|---|
| 62 |
|
|---|
| 63 |
|
|---|
| 64 |
* creates a xml stream writer of the given type |
|---|
| 65 |
* |
|---|
| 66 |
* @param string $type concrete type to create |
|---|
| 67 |
* @return stubXMLStreamWriter |
|---|
| 68 |
*/ |
|---|
| 69 |
public static function create($type) |
|---|
| 70 |
{ |
|---|
| 71 |
$fqClassName = self::getFqClassName($type); |
|---|
| 72 |
$nqClassName = stubClassLoader::getNonQualifiedClassName($fqClassName); |
|---|
| 73 |
if (class_exists($nqClassName, false) === false) { |
|---|
| 74 |
stubClassLoader::load($fqClassName); |
|---|
| 75 |
} |
|---|
| 76 |
|
|---|
| 77 |
$xmlStreamWriter = new $nqClassName(self::$version, self::$encoding); |
|---|
| 78 |
return $xmlStreamWriter; |
|---|
| 79 |
} |
|---|
| 80 |
|
|---|
| 81 |
|
|---|
| 82 |
* creates a xml stream writer depending on available xml extensions |
|---|
| 83 |
* |
|---|
| 84 |
* If an order is submitted it will use this order, else it uses the default |
|---|
| 85 |
* order. |
|---|
| 86 |
* Warning: if a list of features is provided an instance of every possible |
|---|
| 87 |
* xml stream writer will be created to check the feature list until a |
|---|
| 88 |
* sufficient xml stream writer class was found. |
|---|
| 89 |
* |
|---|
| 90 |
* @param array $order optional extensions in order to use |
|---|
| 91 |
* @param array $features optional features the implementation must provide |
|---|
| 92 |
* @return stubXMLStreamWriter |
|---|
| 93 |
* @throws stubXMLException |
|---|
| 94 |
*/ |
|---|
| 95 |
public static function createAsAvailable(array $order = null, array $features = array()) |
|---|
| 96 |
{ |
|---|
| 97 |
if (null === $order) { |
|---|
| 98 |
$order = array_keys(self::$types); |
|---|
| 99 |
} |
|---|
| 100 |
|
|---|
| 101 |
foreach ($order as $xmlExtension) { |
|---|
| 102 |
$name = self::checkExtension($xmlExtension); |
|---|
| 103 |
if (null === $name) { |
|---|
| 104 |
continue; |
|---|
| 105 |
} |
|---|
| 106 |
|
|---|
| 107 |
$xmlStreamWriter = self::checkFeatures($name, $features); |
|---|
| 108 |
if (null !== $xmlStreamWriter) { |
|---|
| 109 |
return $xmlStreamWriter; |
|---|
| 110 |
} |
|---|
| 111 |
} |
|---|
| 112 |
|
|---|
| 113 |
throw new stubXMLException('Not any single xml extension available that provides the requested features, can not create a xml stream writer!'); |
|---|
| 114 |
} |
|---|
| 115 |
|
|---|
| 116 |
|
|---|
| 117 |
* returns full qualified class name for the xml stream writer of the given type |
|---|
| 118 |
* |
|---|
| 119 |
* @param string $type concrete type of stream writer |
|---|
| 120 |
* @return string |
|---|
| 121 |
*/ |
|---|
| 122 |
public static function getFqClassName($type) |
|---|
| 123 |
{ |
|---|
| 124 |
return 'net::stubbles::xml::stub' . $type . 'XMLStreamWriter'; |
|---|
| 125 |
} |
|---|
| 126 |
|
|---|
| 127 |
|
|---|
| 128 |
* returns full qualified class name for the xml stream writer depending on available xml extensions |
|---|
| 129 |
* |
|---|
| 130 |
* If an order is submitted it will use this order, else it uses the default |
|---|
| 131 |
* order. |
|---|
| 132 |
* Warning: if a list of features is provided an instance of every possible |
|---|
| 133 |
* xml stream writer will be created to check the feature list until a |
|---|
| 134 |
* sufficient xml stream writer class was found. |
|---|
| 135 |
* |
|---|
| 136 |
* @param array $order optional extensions in order to use |
|---|
| 137 |
* @param array $features optional features the implementation must provide |
|---|
| 138 |
* @return string |
|---|
| 139 |
* @throws stubXMLException |
|---|
| 140 |
*/ |
|---|
| 141 |
public static function getFqClassNameAsAvailable(array $order = null, array $features = array()) |
|---|
| 142 |
{ |
|---|
| 143 |
if (null === $order) { |
|---|
| 144 |
$order = array_keys(self::$types); |
|---|
| 145 |
} |
|---|
| 146 |
|
|---|
| 147 |
foreach ($order as $xmlExtension) { |
|---|
| 148 |
$name = self::checkExtension($xmlExtension); |
|---|
| 149 |
if (null === $name) { |
|---|
| 150 |
continue; |
|---|
| 151 |
} |
|---|
| 152 |
|
|---|
| 153 |
if (count($features) === 0) { |
|---|
| 154 |
return self::getFqClassName($name); |
|---|
| 155 |
} |
|---|
| 156 |
|
|---|
| 157 |
$xmlStreamWriter = self::checkFeatures($name, $features); |
|---|
| 158 |
if (null !== $xmlStreamWriter) { |
|---|
| 159 |
return $xmlStreamWriter->getClassName(); |
|---|
| 160 |
} |
|---|
| 161 |
} |
|---|
| 162 |
|
|---|
| 163 |
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!'); |
|---|
| 164 |
} |
|---|
| 165 |
|
|---|
| 166 |
|
|---|
| 167 |
* checks given extension and returns their type name |
|---|
| 168 |
* |
|---|
| 169 |
* If extension is not loaded the return value will be null. |
|---|
| 170 |
* |
|---|
| 171 |
* @param string $xmlExtension name of the extension to check |
|---|
| 172 |
* @return string |
|---|
| 173 |
*/ |
|---|
| 174 |
protected static function checkExtension($xmlExtension) |
|---|
| 175 |
{ |
|---|
| 176 |
if (extension_loaded($xmlExtension) === false) { |
|---|
| 177 |
return null; |
|---|
| 178 |
} |
|---|
| 179 |
|
|---|
| 180 |
return ((isset(self::$types[$xmlExtension]) === true) ? (self::$types[$xmlExtension]) : ($xmlExtension)); |
|---|
| 181 |
} |
|---|
| 182 |
|
|---|
| 183 |
|
|---|
| 184 |
* method to check if a given type of xml stream writer has requested features |
|---|
| 185 |
* |
|---|
| 186 |
* Returns an instance of the xml stream writer if it has the requested |
|---|
| 187 |
* features, else null. |
|---|
| 188 |
* |
|---|
| 189 |
* @param string $type type of stream writer to check |
|---|
| 190 |
* @param array $features features the implementation must provide |
|---|
| 191 |
* @return stubXMLStreamWriter |
|---|
| 192 |
*/ |
|---|
| 193 |
protected static function checkFeatures($type, array $features) |
|---|
| 194 |
{ |
|---|
| 195 |
$writer = self::create($type); |
|---|
| 196 |
foreach ($features as $feature) { |
|---|
| 197 |
if ($writer->hasFeature($feature) === false) { |
|---|
| 198 |
return null; |
|---|
| 199 |
} |
|---|
| 200 |
} |
|---|
| 201 |
|
|---|
| 202 |
return $writer; |
|---|
| 203 |
} |
|---|
| 204 |
} |
|---|
| 205 |
?> |
|---|