XML.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. <?php
  2. /**
  3. * Part of XML_XRD
  4. *
  5. * PHP version 5
  6. *
  7. * @category XML
  8. * @package XML_XRD
  9. * @author Christian Weiske <cweiske@php.net>
  10. * @license http://www.gnu.org/copyleft/lesser.html LGPL
  11. * @link http://pear.php.net/package/XML_XRD
  12. */
  13. /**
  14. * Loads XRD data from an XML file
  15. *
  16. * @category XML
  17. * @package XML_XRD
  18. * @author Christian Weiske <cweiske@php.net>
  19. * @license http://www.gnu.org/copyleft/lesser.html LGPL
  20. * @version Release: @package_version@
  21. * @link http://pear.php.net/package/XML_XRD
  22. */
  23. class XML_XRD_Loader_XML
  24. {
  25. /**
  26. * Data storage the XML data get loaded into
  27. *
  28. * @var XML_XRD
  29. */
  30. protected $xrd;
  31. /**
  32. * XRD 1.0 namespace
  33. */
  34. const NS_XRD = 'http://docs.oasis-open.org/ns/xri/xrd-1.0';
  35. /**
  36. * Init object with xrd object
  37. *
  38. * @param XML_XRD $xrd Data storage the XML data get loaded into
  39. */
  40. public function __construct(XML_XRD $xrd)
  41. {
  42. $this->xrd = $xrd;
  43. }
  44. /**
  45. * Loads the contents of the given file
  46. *
  47. * @param string $file Path to an XRD file
  48. *
  49. * @return void
  50. *
  51. * @throws XML_XRD_Loader_Exception When the XML is invalid or cannot be
  52. * loaded
  53. */
  54. public function loadFile($file)
  55. {
  56. $old = libxml_use_internal_errors(true);
  57. $x = simplexml_load_file($file);
  58. libxml_use_internal_errors($old);
  59. if ($x === false) {
  60. throw new XML_XRD_Loader_Exception(
  61. 'Error loading XML file: ' . libxml_get_last_error()->message,
  62. XML_XRD_Loader_Exception::LOAD
  63. );
  64. }
  65. return $this->load($x);
  66. }
  67. /**
  68. * Loads the contents of the given string
  69. *
  70. * @param string $xml XML string
  71. *
  72. * @return void
  73. *
  74. * @throws XML_XRD_Loader_Exception When the XML is invalid or cannot be
  75. * loaded
  76. */
  77. public function loadString($xml)
  78. {
  79. if ($xml == '') {
  80. throw new XML_XRD_Loader_Exception(
  81. 'Error loading XML string: string empty',
  82. XML_XRD_Loader_Exception::LOAD
  83. );
  84. }
  85. $old = libxml_use_internal_errors(true);
  86. $x = simplexml_load_string($xml);
  87. libxml_use_internal_errors($old);
  88. if ($x === false) {
  89. throw new XML_XRD_Loader_Exception(
  90. 'Error loading XML string: ' . libxml_get_last_error()->message,
  91. XML_XRD_Loader_Exception::LOAD
  92. );
  93. }
  94. return $this->load($x);
  95. }
  96. /**
  97. * Loads the XML element into the classes' data structures
  98. *
  99. * @param object $x XML element containing the whole XRD document
  100. *
  101. * @return void
  102. *
  103. * @throws XML_XRD_Loader_Exception When the XML is invalid
  104. */
  105. public function load(SimpleXMLElement $x)
  106. {
  107. $ns = $x->getDocNamespaces();
  108. if ($ns[''] !== self::NS_XRD) {
  109. throw new XML_XRD_Loader_Exception(
  110. 'Wrong document namespace', XML_XRD_Loader_Exception::DOC_NS
  111. );
  112. }
  113. if ($x->getName() != 'XRD') {
  114. throw new XML_XRD_Loader_Exception(
  115. 'XML root element is not "XRD"', XML_XRD_Loader_Exception::DOC_ROOT
  116. );
  117. }
  118. if (isset($x->Subject)) {
  119. $this->xrd->subject = (string)$x->Subject;
  120. }
  121. foreach ($x->Alias as $xAlias) {
  122. $this->xrd->aliases[] = (string)$xAlias;
  123. }
  124. foreach ($x->Link as $xLink) {
  125. $this->xrd->links[] = $this->loadLink($xLink);
  126. }
  127. $this->loadProperties($this->xrd, $x);
  128. if (isset($x->Expires)) {
  129. $this->xrd->expires = strtotime($x->Expires);
  130. }
  131. $xmlAttrs = $x->attributes('http://www.w3.org/XML/1998/namespace');
  132. if (isset($xmlAttrs['id'])) {
  133. $this->xrd->id = (string)$xmlAttrs['id'];
  134. }
  135. }
  136. /**
  137. * Loads the Property elements from XML
  138. *
  139. * @param object $store Data store where the properties get stored
  140. * @param object $x XML element
  141. *
  142. * @return boolean True when all went well
  143. */
  144. protected function loadProperties(
  145. XML_XRD_PropertyAccess $store, SimpleXMLElement $x
  146. ) {
  147. foreach ($x->Property as $xProp) {
  148. $store->properties[] = $this->loadProperty($xProp);
  149. }
  150. }
  151. /**
  152. * Create a link element object from XML element
  153. *
  154. * @param object $x XML link element
  155. *
  156. * @return XML_XRD_Element_Link Created link object
  157. */
  158. protected function loadLink(SimpleXMLElement $x)
  159. {
  160. $link = new XML_XRD_Element_Link();
  161. foreach (array('rel', 'type', 'href', 'template') as $var) {
  162. if (isset($x[$var])) {
  163. $link->$var = (string)$x[$var];
  164. }
  165. }
  166. foreach ($x->Title as $xTitle) {
  167. $xmlAttrs = $xTitle->attributes('http://www.w3.org/XML/1998/namespace');
  168. $lang = '';
  169. if (isset($xmlAttrs['lang'])) {
  170. $lang = (string)$xmlAttrs['lang'];
  171. }
  172. if (!isset($link->titles[$lang])) {
  173. $link->titles[$lang] = (string)$xTitle;
  174. }
  175. }
  176. $this->loadProperties($link, $x);
  177. return $link;
  178. }
  179. /**
  180. * Create a property element object from XML element
  181. *
  182. * @param object $x XML property element
  183. *
  184. * @return XML_XRD_Element_Property Created link object
  185. */
  186. protected function loadProperty(SimpleXMLElement $x)
  187. {
  188. $prop = new XML_XRD_Element_Property();
  189. if (isset($x['type'])) {
  190. $prop->type = (string)$x['type'];
  191. }
  192. $s = (string)$x;
  193. if ($s != '') {
  194. $prop->value = $s;
  195. }
  196. return $prop;
  197. }
  198. }
  199. ?>