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
14 require_once 'XML/XRD/Loader/Exception.php';
17 * File/string loading dispatcher.
18 * Loads the correct loader for the type of XRD file (XML or JSON).
19 * Also provides type auto-detection.
23 * @author Christian Weiske <cweiske@php.net>
24 * @license http://www.gnu.org/copyleft/lesser.html LGPL
25 * @version Release: @package_version@
26 * @link http://pear.php.net/package/XML_XRD
30 public function __construct(XML_XRD $xrd)
36 * Loads the contents of the given file.
38 * Note: Only use file type auto-detection for local files.
39 * Do not use it on remote files as the file gets requested several times.
41 * @param string $file Path to an XRD file
42 * @param string $type File type: xml or json, NULL for auto-detection
46 * @throws XML_XRD_Loader_Exception When the file is invalid or cannot be
49 public function loadFile($file, $type = null)
52 $type = $this->detectTypeFromFile($file);
54 $loader = $this->getLoader($type);
55 $loader->loadFile($file);
59 * Loads the contents of the given string
61 * @param string $str XRD string
62 * @param string $type File type: xml or json, NULL for auto-detection
66 * @throws XML_XRD_Loader_Exception When the string is invalid or cannot be
69 public function loadString($str, $type = null)
72 $type = $this->detectTypeFromString($str);
74 $loader = $this->getLoader($type);
75 $loader->loadString($str);
79 * Creates a XRD loader object for the given type
81 * @param string $type File type: xml or json
83 * @return XML_XRD_Loader
85 protected function getLoader($type)
87 $class = 'XML_XRD_Loader_' . strtoupper($type);
88 $file = str_replace('_', '/', $class) . '.php';
90 if (class_exists($class)) {
91 return new $class($this->xrd);
94 throw new XML_XRD_Loader_Exception(
95 'No loader for XRD type "' . $type . '"',
96 XML_XRD_Loader_Exception::NO_LOADER
101 * Tries to detect the file type (xml or json) from the file content
103 * @param string $file File name to check
105 * @return string File type ('xml' or 'json')
107 * @throws XML_XRD_Loader_Exception When opening the file fails.
109 public function detectTypeFromFile($file)
111 if (!file_exists($file)) {
112 throw new XML_XRD_Loader_Exception(
113 'Error loading XRD file: File does not exist',
114 XML_XRD_Loader_Exception::OPEN_FILE
117 $handle = fopen($file, 'r');
119 throw new XML_XRD_Loader_Exception(
120 'Cannot open file to determine type',
121 XML_XRD_Loader_Exception::OPEN_FILE
125 $str = (string)fgets($handle, 10);
127 return $this->detectTypeFromString($str);
131 * Tries to detect the file type from the content of the file
133 * @param string $str Content of XRD file
135 * @return string File type ('xml' or 'json')
137 * @throws XML_XRD_Loader_Exception When the type cannot be detected
139 public function detectTypeFromString($str)
141 if (substr($str, 0, 1) == '{') {
143 } else if (substr($str, 0, 5) == '<?xml') {
147 throw new XML_XRD_Loader_Exception(
148 'Detecting file type failed',
149 XML_XRD_Loader_Exception::DETECT_TYPE