## Apache Zeta Components Manual :: File Source for processor.php

## Source for file processor.php

Documentation is available at processor.php

1. <?php
2. /**
3. * File containing the ezcFeedProcessor class.
4. *
5. * Licensed to the Apache Software Foundation (ASF) under one
6. * or more contributor license agreements. See the NOTICE file
7. * distributed with this work for additional information
8. * regarding copyright ownership. The ASF licenses this file
9. * to you under the Apache License, Version 2.0 (the
10. * "License"); you may not use this file except in compliance
11. * with the License. You may obtain a copy of the License at
12. *
13. * http://www.apache.org/licenses/LICENSE-2.0
14. *
15. * Unless required by applicable law or agreed to in writing,
16. * software distributed under the License is distributed on an
17. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
18. * KIND, either express or implied. See the License for the
19. * specific language governing permissions and limitations
20. * under the License.
21. *
22. * @package Feed
23. * @version //autogentag//
24. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
25. * @filesource
26. */
28. /**
29. * Base class for all feed processors.
30. *
31. * Currently implemented for these feed types:
32. * - RSS1 ({@link ezcFeedRss1})
33. * - RSS2 ({@link ezcFeedRss2})
34. * - ATOM ({@link ezcFeedAtom})
35. *
36. * Child classes must implement these methods:
37. * - generate() - Returns an XML string from the feed information contained.
38. *
39. * @package Feed
40. * @version //autogentag//
41. */
42. abstract class ezcFeedProcessor
43. {
44. /**
45. * Holds the feed data container.
46. *
47. * @var ezcFeed
48. * @ignore
49. */
50. protected $feedContainer;
52. /**
53. * Holds the XML document which is being generated.
54. *
55. * @var DOMDocument
56. * @ignore
57. */
58. protected $xml;
60. /**
61. * Holds the root node of the XML document being generated.
62. *
63. * @var DOMNode
64. * @ignore
65. */
66. protected $root;
68. /**
69. * Holds the channel element of the XML document being generated.
70. *
71. * @var DOMElement
72. * @ignore
73. */
74. protected $channel;
76. /**
77. * Holds the prefixes used in the feed generation process.
78. *
79. * @var array(string)
80. * @ignore
81. */
82. protected $usedPrefixes = array();
84. /**
85. * Sets the value of element $name to $value based on the feed schema.
86. *
87. * @param string $name The element name
88. * @param mixed $value The new value for the element $name
89. * @ignore
90. */
91. public function __set( $name, $value )
92. {
93. $this->feedContainer->$name = $value;
94. }
96. /**
97. * Returns the value of element $name based on the feed schema.
98. *
99. * @param string $name The element name
100. * @return mixed
101. * @ignore
102. */
103. public function __get( $name )
104. {
105. return $this->feedContainer->$name;
106. }
108. /**
109. * Returns if the property $name is set.
110. *
111. * @param string $name The property name
112. * @return bool
113. * @ignore
114. */
115. public function __isset( $name )
116. {
117. return isset( $this->feedContainer->$name );
118. }
120. /**
121. * Returns an array with all the modules loaded at feed-level.
122. *
123. * @return array(ezcFeedModule)
124. */
125. public function getModules ()
126. {
127. return $this->feedContainer->getModules();
128. }
130. /**
131. * Creates a node in the XML document being generated with name $element
132. * and value(s) $value.
133. *
134. * @param DOMNode $root The root in which to create the node $element
135. * @param string $element The name of the XML element
136. * @param mixed|array(mixed)$value The value(s) for $element
137. * @ignore
138. */
139. protected function generateMetaData( DOMNode $root, $element, $value )
140. {
141. if ( ! is_array ( $value ) )
142. {
143. $value = array( $value );
144. }
145. foreach ( $value as $valueElement )
146. {
147. $meta = $this->xml->createElement( $element, ( $valueElement instanceof ezcFeedElement ) ? $valueElement->__toString() : (string)$valueElement );
148. $root->appendChild( $meta );
149. }
150. }
152. /**
153. * Creates elements in the XML document being generated with name $element
154. * and value(s) $value.
155. *
156. * @param DOMNode $root The root in which to create the node $element
157. * @param string $element The name of the XML element
158. * @param mixed|array(mixed)$value The value(s) for $element
159. * @param array(string=>mixed) $attributes The attributes to add to the node
160. * @ignore
161. */
162. protected function generateMetaDataWithAttributes( DOMNode $root, $element, $value = false, array $attributes )
163. {
164. if ( !is_array( $value ) )
165. {
166. $value = array( $value );
167. }
168. foreach ( $value as $valueElement )
169. {
170. if ( $valueElement === false )
171. {
172. $meta = $this->xml->createElement( $element );
173. }
174. else
175. {
176. $meta = $this->xml->createElement( $element, ( $valueElement instanceof ezcFeedElement ) ? $valueElement->__toString() : (string)$valueElement );
177. }
178. foreach ( $attributes as $attrName => $attrValue )
179. {
180. $attr = $this->xml->createAttribute( $attrName );
181. $text = $this->xml->createTextNode( $attrValue );
182. $attr->appendChild( $text );
183. $meta->appendChild( $attr );
184. }
185. $root->appendChild( $meta );
186. }
187. }
189. /**
190. * Creates elements for all modules loaded at item-level, and adds the
191. * namespaces required by the modules in the XML document being generated.
192. *
193. * @param ezcFeedEntryElement $item The feed item containing the modules
194. * @param DOMElement $node The XML element in which to add the module elements
195. * @ignore
196. */
197. protected function generateItemModules( ezcFeedEntryElement $item, DOMElement $node )
198. {
199. foreach ( $item-> getModules () as $module )
200. {
201. $this->addAttribute( $this->root, 'xmlns:' . $module->getNamespacePrefix(), $module->getNamespace() );
202. $module-> generate ( $this->xml, $node );
203. }
204. }
206. /**
207. * Creates elements for all modules loaded at feed-level, and adds the
208. * namespaces required by the modules in the XML document being generated.
209. *
210. * @param DOMElement $node The XML element in which to add the module elements
211. * @ignore
212. */
213. protected function generateFeedModules( DOMElement $node )
214. {
215. foreach ( $this-> getModules () as $module )
216. {
217. $this->addAttribute( $this->root, 'xmlns:' . $module->getNamespacePrefix(), $module->getNamespace() );
218. $module-> generate ( $this->xml, $node );
219. }
220. }
222. /**
223. * Parses the XML element $node and creates modules in the feed or
224. * feed item $item.
225. *
226. * @param ezcFeedEntryElement|ezcFeed$item The feed or feed item which will contain the modules
227. * @param DOMElement $node The XML element from which to get the module elements
228. * @param string $tagName The XML tag name (if it contains ':' it will be considered part of a module)
229. * @ignore
230. */
231. protected function parseModules( $item, DOMElement $node, $tagName )
232. {
233. $supportedModules = ezcFeed :: getSupportedModules ();
234. if ( strpos ( $tagName, ':' ) !== false )
235. {
236. list( $prefix, $key ) = explode ( ':', $tagName );
237. $moduleName = isset( $this->usedPrefixes[$prefix] ) ? $this->usedPrefixes[$prefix] : null;
238. if ( isset( $supportedModules[$moduleName] ) )
239. {
240. $module = $item->hasModule( $moduleName ) ? $item->$moduleName : $item->addModule( $moduleName );
241. $module->parse( $key, $node );
242. }
243. // If the feed contains the ATOM namespace (xmlns:atom="http://www.w3.org/2005/Atom")
244. else if ( $moduleName === 'Atom' )
245. {
246. $element = $item->add( 'id' );
247. if ( $node->hasAttribute( 'href' ) )
248. {
249. $item->id = $node->getAttribute( 'href' );
250. }
251. }
252. }
253. }
255. /**
256. * Fetches the supported prefixes and namespaces from the XML document $xml.
257. *
258. * @param DOMDocument $xml The XML document object to parse
259. * @return array(string=>string)
260. * @ignore
261. */
262. protected function fetchUsedPrefixes( DOMDocument $xml )
263. {
264. $usedPrefixes = array();
266. $xp = new DOMXpath( $xml );
267. $set = $xp->query( './namespace::*', $xml->documentElement );
268. $usedNamespaces = array();
270. foreach ( $set as $node )
271. {
272. foreach ( ezcFeed :: getSupportedModules () as $moduleName => $moduleClass )
273. {
274. $moduleNamespace = call_user_func ( array( $moduleClass, 'getNamespace' ) );
276. // compare the namespace URIs from the XML source with the supported ones
277. if ( $moduleNamespace === $node->nodeValue )
278. {
279. // the nodeName looks like: xmlns:some_module
280. list( $xmlns, $prefix ) = explode ( ':', $node->nodeName );
282. // use the prefix from the XML source as a key in the array $usedPrefixes
283. // eg. array( 'some_prefix' => 'DublinCore' );
284. // then, when calling later parseModules(), if encountering an element
285. // like <some_prefix:creator>, it is checked if 'DublinCore' is supported
286. $usedPrefixes[$prefix] = $moduleName;
287. }
288. }
290. // If the feed contains the ATOM namespace (xmlns:atom="http://www.w3.org/2005/Atom")
291. // and the feed itself is not of type ATOM, then add the 'atom' prefix (or any alias)
292. // to the $usedPrefixes array which is returned by the function
293. if ( ezcFeedAtom ::NAMESPACE_URI === $node->nodeValue
294. && $xml->documentElement->tagName !== 'feed' )
295. {
296. // the nodeName looks like: xmlns:some_module
297. list( $xmlns, $prefix ) = explode ( ':', $node->nodeName );
299. $usedPrefixes[$prefix] = 'Atom';
300. }
301. }
303. return $usedPrefixes;
304. }
306. /**
307. * Adds an attribute to the XML node $node.
308. *
309. * @param DOMNode $node The node to add the attribute to
310. * @param string $attribute The name of the attribute to add
311. * @param mixed $value The value of the attribute
312. * @ignore
313. */
314. protected function addAttribute( DOMNode $node, $attribute, $value )
315. {
316. $attr = $this->xml->createAttribute( $attribute );
317. $val = $this->xml->createTextNode( $value );
318. $attr->appendChild( $val );
319. $node->appendChild( $attr );
320. }
322. /**
323. * Returns a DOMNode child of $parent with name $nodeName and which has an
324. * attribute $attribute with the value $value. Returns null if no such node
325. * is found.
326. *
327. * @param DOMNode $parent The XML parent node
328. * @param string $nodeName The node name to find
329. * @param string $attribute The attribute of the node
330. * @param mixed $value The value of the attribute
331. * @return DOMNode
332. * @ignore
333. */
334. protected function getNodeByAttribute( DOMNode $parent, $nodeName, $attribute, $value )
335. {
336. $result = null;
337. $nodes = $parent->getElementsByTagName( $nodeName );
339. foreach ( $nodes as $node )
340. {
341. $nodeAttribute = $node->getAttribute( $attribute );
342. if ( $nodeAttribute !== null
343. && $nodeAttribute === $value )
344. {
345. $result = $node;
346. break;
347. }
348. }
350. return $result;
351. }
353. /**
354. * Returns a DOMNode child of $parent with name $nodeName and which has an
355. * attribute $attribute in the namespace $namespace with the value $value.
356. * Returns null if no such node is found.
357. *
358. * @param DOMNode $parent The XML parent node
359. * @param string $nodeName The node name to find
360. * @param string $namespace The namespace of the attribute
361. * @param string $attribute The attribute of the node
362. * @param mixed $value The value of the attribute
363. * @return DOMNode
364. * @ignore
365. */
366. protected function getNodeByAttributeNS( DOMNode $parent, $nodeName, $namespace, $attribute, $value )
367. {
368. $result = null;
369. $nodes = $parent->getElementsByTagName( $nodeName );
371. foreach ( $nodes as $node )
372. {
373. $nodeAttribute = $node->getAttributeNS( $namespace, $attribute );
374. if ( $nodeAttribute !== null
375. && $nodeAttribute === $value )
376. {
377. $result = $node;
378. break;
379. }
380. }
382. return $result;
383. }
385. /**
386. * Returns an XML string from the feed information contained in this
387. * processor.
388. *
389. * @return string
390. */
391. abstract public function generate ();
393. ?>

Documentation generated by phpDocumentor 1.4.3