123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219 |
- <?php
- class XML_XRD_Loader_XML
- {
-
- protected $xrd;
-
- const NS_XRD = 'http://docs.oasis-open.org/ns/xri/xrd-1.0';
-
- public function __construct(XML_XRD $xrd)
- {
- $this->xrd = $xrd;
- }
-
- public function loadFile($file)
- {
- $old = libxml_use_internal_errors(true);
- $x = simplexml_load_file($file);
- libxml_use_internal_errors($old);
- if ($x === false) {
- throw new XML_XRD_Loader_Exception(
- 'Error loading XML file: ' . libxml_get_last_error()->message,
- XML_XRD_Loader_Exception::LOAD
- );
- }
- return $this->load($x);
- }
-
- public function loadString($xml)
- {
- if ($xml == '') {
- throw new XML_XRD_Loader_Exception(
- 'Error loading XML string: string empty',
- XML_XRD_Loader_Exception::LOAD
- );
- }
- $old = libxml_use_internal_errors(true);
- $x = simplexml_load_string($xml);
- libxml_use_internal_errors($old);
- if ($x === false) {
- throw new XML_XRD_Loader_Exception(
- 'Error loading XML string: ' . libxml_get_last_error()->message,
- XML_XRD_Loader_Exception::LOAD
- );
- }
- return $this->load($x);
- }
-
- public function load(SimpleXMLElement $x)
- {
- $ns = $x->getDocNamespaces();
- if ($ns[''] !== self::NS_XRD) {
- throw new XML_XRD_Loader_Exception(
- 'Wrong document namespace', XML_XRD_Loader_Exception::DOC_NS
- );
- }
- if ($x->getName() != 'XRD') {
- throw new XML_XRD_Loader_Exception(
- 'XML root element is not "XRD"', XML_XRD_Loader_Exception::DOC_ROOT
- );
- }
- if (isset($x->Subject)) {
- $this->xrd->subject = (string)$x->Subject;
- }
- foreach ($x->Alias as $xAlias) {
- $this->xrd->aliases[] = (string)$xAlias;
- }
- foreach ($x->Link as $xLink) {
- $this->xrd->links[] = $this->loadLink($xLink);
- }
- $this->loadProperties($this->xrd, $x);
- if (isset($x->Expires)) {
- $this->xrd->expires = strtotime($x->Expires);
- }
- $xmlAttrs = $x->attributes('http://www.w3.org/XML/1998/namespace');
- if (isset($xmlAttrs['id'])) {
- $this->xrd->id = (string)$xmlAttrs['id'];
- }
- }
-
- protected function loadProperties(
- XML_XRD_PropertyAccess $store, SimpleXMLElement $x
- ) {
- foreach ($x->Property as $xProp) {
- $store->properties[] = $this->loadProperty($xProp);
- }
- }
-
- protected function loadLink(SimpleXMLElement $x)
- {
- $link = new XML_XRD_Element_Link();
- foreach (array('rel', 'type', 'href', 'template') as $var) {
- if (isset($x[$var])) {
- $link->$var = (string)$x[$var];
- }
- }
- foreach ($x->Title as $xTitle) {
- $xmlAttrs = $xTitle->attributes('http://www.w3.org/XML/1998/namespace');
- $lang = '';
- if (isset($xmlAttrs['lang'])) {
- $lang = (string)$xmlAttrs['lang'];
- }
- if (!isset($link->titles[$lang])) {
- $link->titles[$lang] = (string)$xTitle;
- }
- }
- $this->loadProperties($link, $x);
- return $link;
- }
-
- protected function loadProperty(SimpleXMLElement $x)
- {
- $prop = new XML_XRD_Element_Property();
- if (isset($x['type'])) {
- $prop->type = (string)$x['type'];
- }
- $s = (string)$x;
- if ($s != '') {
- $prop->value = $s;
- }
- return $prop;
- }
- }
- ?>
|