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
15 * Loads XRD data from an XML file
19 * @author Christian Weiske <cweiske@php.net>
20 * @license http://www.gnu.org/copyleft/lesser.html LGPL
21 * @version Release: @package_version@
22 * @link http://pear.php.net/package/XML_XRD
24 class XML_XRD_Loader_XML
27 * Data storage the XML data get loaded into
36 const NS_XRD = 'http://docs.oasis-open.org/ns/xri/xrd-1.0';
41 * Init object with xrd object
43 * @param XML_XRD $xrd Data storage the XML data get loaded into
45 public function __construct(XML_XRD $xrd)
51 * Loads the contents of the given file
53 * @param string $file Path to an XRD file
57 * @throws XML_XRD_Loader_Exception When the XML is invalid or cannot be
60 public function loadFile($file)
62 $old = libxml_use_internal_errors(true);
63 $x = simplexml_load_file($file);
64 libxml_use_internal_errors($old);
66 throw new XML_XRD_Loader_Exception(
67 'Error loading XML file: ' . libxml_get_last_error()->message,
68 XML_XRD_Loader_Exception::LOAD
71 return $this->load($x);
75 * Loads the contents of the given string
77 * @param string $xml XML string
81 * @throws XML_XRD_Loader_Exception When the XML is invalid or cannot be
84 public function loadString($xml)
87 throw new XML_XRD_Loader_Exception(
88 'Error loading XML string: string empty',
89 XML_XRD_Loader_Exception::LOAD
92 $old = libxml_use_internal_errors(true);
93 $x = simplexml_load_string($xml);
94 libxml_use_internal_errors($old);
96 throw new XML_XRD_Loader_Exception(
97 'Error loading XML string: ' . libxml_get_last_error()->message,
98 XML_XRD_Loader_Exception::LOAD
101 return $this->load($x);
105 * Loads the XML element into the classes' data structures
107 * @param object $x XML element containing the whole XRD document
111 * @throws XML_XRD_Loader_Exception When the XML is invalid
113 public function load(SimpleXMLElement $x)
115 $ns = $x->getDocNamespaces();
116 if ($ns[''] !== self::NS_XRD) {
117 throw new XML_XRD_Loader_Exception(
118 'Wrong document namespace', XML_XRD_Loader_Exception::DOC_NS
121 if ($x->getName() != 'XRD') {
122 throw new XML_XRD_Loader_Exception(
123 'XML root element is not "XRD"', XML_XRD_Loader_Exception::DOC_ROOT
127 if (isset($x->Subject)) {
128 $this->xrd->subject = (string)$x->Subject;
130 foreach ($x->Alias as $xAlias) {
131 $this->xrd->aliases[] = (string)$xAlias;
134 foreach ($x->Link as $xLink) {
135 $this->xrd->links[] = $this->loadLink($xLink);
138 $this->loadProperties($this->xrd, $x);
140 if (isset($x->Expires)) {
141 $this->xrd->expires = strtotime($x->Expires);
144 $xmlAttrs = $x->attributes('http://www.w3.org/XML/1998/namespace');
145 if (isset($xmlAttrs['id'])) {
146 $this->xrd->id = (string)$xmlAttrs['id'];
151 * Loads the Property elements from XML
153 * @param object $store Data store where the properties get stored
154 * @param object $x XML element
156 * @return boolean True when all went well
158 protected function loadProperties(
159 XML_XRD_PropertyAccess $store, SimpleXMLElement $x
161 foreach ($x->Property as $xProp) {
162 $store->properties[] = $this->loadProperty($xProp);
167 * Create a link element object from XML element
169 * @param object $x XML link element
171 * @return XML_XRD_Element_Link Created link object
173 protected function loadLink(SimpleXMLElement $x)
175 $link = new XML_XRD_Element_Link();
176 foreach (array('rel', 'type', 'href', 'template') as $var) {
177 if (isset($x[$var])) {
178 $link->$var = (string)$x[$var];
182 foreach ($x->Title as $xTitle) {
183 $xmlAttrs = $xTitle->attributes('http://www.w3.org/XML/1998/namespace');
185 if (isset($xmlAttrs['lang'])) {
186 $lang = (string)$xmlAttrs['lang'];
188 if (!isset($link->titles[$lang])) {
189 $link->titles[$lang] = (string)$xTitle;
192 $this->loadProperties($link, $x);
198 * Create a property element object from XML element
200 * @param object $x XML property element
202 * @return XML_XRD_Element_Property Created link object
204 protected function loadProperty(SimpleXMLElement $x)
206 $prop = new XML_XRD_Element_Property();
207 if (isset($x['type'])) {
208 $prop->type = (string)$x['type'];