示例#1
0
 /**
  * Load a plist file.
  * Load and import a plist file.
  * @param string $file Path of PropertyList, defaults to {@link $file}
  * @param integer $format The format of the property list, see {@link FORMAT_XML}, {@link FORMAT_BINARY} and {@link FORMAT_AUTO}, defaults to {@link $format}
  * @return void
  * @throws PListException if file format version is not 00
  * @throws IOException if file could not be read
  * @throws DOMException if plist file could not be parsed properly
  * @uses $file if argument $file was not specified
  * @uses $value reset to empty array
  * @uses import() for importing the values
  */
 public function load($file = null, $format = null)
 {
     $file = $file ? $file : $this->file;
     $format = $format !== null ? $format : $this->format;
     $this->value = array();
     if (!is_readable($file)) {
         throw IOException::notReadable($file);
     }
     switch ($format) {
         case CFPropertyList::FORMAT_BINARY:
             $this->readBinary($file);
             break;
         case CFPropertyList::FORMAT_AUTO:
             // what we now do is ugly, but neccessary to recognize the file format
             $fd = fopen($file, "rb");
             if (($magic_number = fread($fd, 8)) === false) {
                 throw IOException::notReadable($file);
             }
             fclose($fd);
             $filetype = substr($magic_number, 0, 6);
             $version = substr($magic_number, -2);
             if ($filetype == "bplist") {
                 if ($version != "00") {
                     throw new PListException("Wrong file format version! Expected 00, got {$version}!");
                 }
                 $this->readBinary($file);
                 break;
             }
             // else: xml format, break not neccessary
         // else: xml format, break not neccessary
         case CFPropertyList::FORMAT_XML:
             $doc = new DOMDocument();
             if (!$doc->load($file)) {
                 throw new DOMException();
             }
             $this->import($doc->documentElement, $this);
             break;
     }
 }
示例#2
0
 /**
  * Parse a plist string.
  * Parse and import a plist string.
  * @param string $str String containing the PropertyList, defaults to {@link $content}
  * @param integer $format The format of the property list, see {@link FORMAT_XML}, {@link FORMAT_BINARY} and {@link FORMAT_AUTO}, defaults to {@link $format}
  * @return void
  * @throws PListException if file format version is not 00
  * @throws IOException if file could not be read
  * @throws DOMException if plist file could not be parsed properly
  * @uses $content if argument $str was not specified
  * @uses $value reset to empty array
  * @uses import() for importing the values
  */
 public function parse($str = NULL, $format = NULL)
 {
     $format = $format !== null ? $format : $this->format;
     $str = $str !== null ? $str : $this->content;
     $this->value = array();
     switch ($format) {
         case CFPropertyList::FORMAT_BINARY:
             $this->parseBinary($str);
             break;
         case CFPropertyList::FORMAT_AUTO:
             // what we now do is ugly, but neccessary to recognize the file format
             if (($magic_number = substr($str, 0, 8)) === false) {
                 throw IOException::notReadable("<string>");
             }
             $filetype = substr($magic_number, 0, 6);
             $version = substr($magic_number, -2);
             if ($filetype == "bplist") {
                 if ($version != "00") {
                     throw new PListException("Wrong file format version! Expected 00, got {$version}!");
                 }
                 $this->parseBinary($str);
                 break;
             }
             // else: xml format, break not neccessary
         // else: xml format, break not neccessary
         case CFPropertyList::FORMAT_XML:
             $doc = new DOMDocument();
             if (!$doc->loadXML($str)) {
                 throw new DOMException();
             }
             $this->import($doc->documentElement, $this);
             break;
     }
 }