Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/lib/zend/Zend/Gdata/App/Base.php
Go to the documentation of this file.
00001 <?php
00002 
00027 require_once 'Zend/Gdata/App/Util.php';
00028 
00038 abstract class Zend_Gdata_App_Base
00039 {
00040 
00044     protected $_rootElement = null;
00045 
00049     protected $_rootNamespace = 'atom';
00050 
00055     protected $_rootNamespaceURI = null;
00056 
00060     protected $_extensionElements = array();
00061 
00065     protected $_extensionAttributes = array();
00066 
00070     protected $_text = null;
00071 
00078     protected static $_namespaceLookupCache = array();
00079 
00095    protected $_namespaces = array(
00096         'atom'      => array(
00097             1 => array(
00098                 0 => 'http://www.w3.org/2005/Atom'
00099                 )
00100             ),
00101         'app'       => array(
00102             1 => array(
00103                 0 => 'http://purl.org/atom/app#'
00104                 ),
00105             2 => array(
00106                 0 => 'http://www.w3.org/2007/app'
00107                 )
00108             )
00109         );
00110 
00111     public function __construct()
00112     {
00113     }
00114 
00121     public function getText($trim = true)
00122     {
00123         if ($trim) {
00124             return trim($this->_text);
00125         } else {
00126             return $this->_text;
00127         }
00128     }
00129 
00137     public function setText($value)
00138     {
00139         $this->_text = $value;
00140         return $this;
00141     }
00142 
00149     public function getExtensionElements()
00150     {
00151         return $this->_extensionElements;
00152     }
00153 
00162     public function setExtensionElements($value)
00163     {
00164         $this->_extensionElements = $value;
00165         return $this;
00166     }
00167 
00176     public function getExtensionAttributes()
00177     {
00178         return $this->_extensionAttributes;
00179     }
00180 
00191     public function setExtensionAttributes($value)
00192     {
00193         $this->_extensionAttributes = $value;
00194         return $this;
00195     }
00196 
00207     public function getDOM($doc = null, $majorVersion = 1, $minorVersion = null)
00208     {
00209         if ($doc === null) {
00210             $doc = new DOMDocument('1.0', 'utf-8');
00211         }
00212         if ($this->_rootNamespaceURI != null) {
00213             $element = $doc->createElementNS($this->_rootNamespaceURI, $this->_rootElement);
00214         } elseif ($this->_rootNamespace !== null) {
00215             if (strpos($this->_rootElement, ':') === false) {
00216                 $elementName = $this->_rootNamespace . ':' . $this->_rootElement;
00217             } else {
00218                 $elementName = $this->_rootElement;
00219             }
00220             $element = $doc->createElementNS($this->lookupNamespace($this->_rootNamespace), $elementName);
00221         } else {
00222             $element = $doc->createElement($this->_rootElement);
00223         }
00224         if ($this->_text != null) {
00225             $element->appendChild($element->ownerDocument->createTextNode($this->_text));
00226         }
00227         foreach ($this->_extensionElements as $extensionElement) {
00228             $element->appendChild($extensionElement->getDOM($element->ownerDocument));
00229         }
00230         foreach ($this->_extensionAttributes as $attribute) {
00231             $element->setAttribute($attribute['name'], $attribute['value']);
00232         }
00233         return $element;
00234     }
00235 
00243     protected function takeChildFromDOM($child)
00244     {
00245         if ($child->nodeType == XML_TEXT_NODE) {
00246             $this->_text = $child->nodeValue;
00247         } else {
00248             $extensionElement = new Zend_Gdata_App_Extension_Element();
00249             $extensionElement->transferFromDOM($child);
00250             $this->_extensionElements[] = $extensionElement;
00251         }
00252     }
00253 
00261     protected function takeAttributeFromDOM($attribute)
00262     {
00263         $arrayIndex = ($attribute->namespaceURI != '')?(
00264                 $attribute->namespaceURI . ':' . $attribute->name):
00265                 $attribute->name;
00266         $this->_extensionAttributes[$arrayIndex] =
00267                 array('namespaceUri' => $attribute->namespaceURI,
00268                       'name' => $attribute->localName,
00269                       'value' => $attribute->nodeValue);
00270     }
00271 
00279     public function transferFromDOM($node)
00280     {
00281         foreach ($node->childNodes as $child) {
00282             $this->takeChildFromDOM($child);
00283         }
00284         foreach ($node->attributes as $attribute) {
00285             $this->takeAttributeFromDOM($attribute);
00286         }
00287     }
00288 
00298     public function transferFromXML($xml)
00299     {
00300         if ($xml) {
00301             // Load the feed as an XML DOMDocument object
00302             @ini_set('track_errors', 1);
00303             $doc = new DOMDocument();
00304             $success = @$doc->loadXML($xml);
00305             @ini_restore('track_errors');
00306             if (!$success) {
00307                 require_once 'Zend/Gdata/App/Exception.php';
00308                 throw new Zend_Gdata_App_Exception("DOMDocument cannot parse XML: $php_errormsg");
00309             }
00310             $element = $doc->getElementsByTagName($this->_rootElement)->item(0);
00311             if (!$element) {
00312                 require_once 'Zend/Gdata/App/Exception.php';
00313                 throw new Zend_Gdata_App_Exception('No root <' . $this->_rootElement . '> element');
00314             }
00315             $this->transferFromDOM($element);
00316         } else {
00317             require_once 'Zend/Gdata/App/Exception.php';
00318             throw new Zend_Gdata_App_Exception('XML passed to transferFromXML cannot be null');
00319         }
00320     }
00321 
00327     public function saveXML()
00328     {
00329         $element = $this->getDOM();
00330         return $element->ownerDocument->saveXML($element);
00331     }
00332 
00339     public function getXML()
00340     {
00341         return $this->saveXML();
00342     }
00343 
00352     public function encode()
00353     {
00354         return $this->saveXML();
00355     }
00356 
00372     public function lookupNamespace($prefix,
00373                                     $majorVersion = 1,
00374                                     $minorVersion = null)
00375     {
00376         // Check for a memoized result
00377         $key = $prefix . ' ' .
00378                (is_null($majorVersion) ? 'NULL' : $majorVersion) .
00379                ' '. (is_null($minorVersion) ? 'NULL' : $minorVersion);
00380         if (array_key_exists($key, self::$_namespaceLookupCache))
00381           return self::$_namespaceLookupCache[$key];
00382         // If no match, return the prefix by default
00383         $result = $prefix;
00384 
00385         // Find tuple of keys that correspond to the namespace we should use
00386         if (isset($this->_namespaces[$prefix])) {
00387             // Major version search
00388             $nsData = $this->_namespaces[$prefix];
00389             $foundMajorV = Zend_Gdata_App_Util::findGreatestBoundedValue(
00390                     $majorVersion, $nsData);
00391             // Minor version search
00392             $nsData = $nsData[$foundMajorV];
00393             $foundMinorV = Zend_Gdata_App_Util::findGreatestBoundedValue(
00394                     $minorVersion, $nsData);
00395             // Extract NS
00396             $result = $nsData[$foundMinorV];
00397         }
00398 
00399         // Memoize result
00400         self::$_namespaceLookupCache[$key] = $result;
00401 
00402         return $result;
00403     }
00404 
00426     public function registerNamespace($prefix,
00427                                       $namespaceUri,
00428                                       $majorVersion = 1,
00429                                       $minorVersion = 0)
00430     {
00431         $this->_namespaces[$prefix][$majorVersion][$minorVersion] =
00432         $namespaceUri;
00433     }
00434 
00442     public static function flushNamespaceLookupCache()
00443     {
00444         self::$_namespaceLookupCache = array();
00445     }
00446 
00457     public function registerAllNamespaces($namespaceArray)
00458     {
00459         foreach($namespaceArray as $namespace) {
00460                 $this->registerNamespace(
00461                     $namespace[0], $namespace[1], $namespace[2], $namespace[3]);
00462         }
00463     }
00464 
00465 
00475     public function __get($name)
00476     {
00477         $method = 'get'.ucfirst($name);
00478         if (method_exists($this, $method)) {
00479             return call_user_func(array(&$this, $method));
00480         } else if (property_exists($this, "_${name}")) {
00481             return $this->{'_' . $name};
00482         } else {
00483             require_once 'Zend/Gdata/App/InvalidArgumentException.php';
00484             throw new Zend_Gdata_App_InvalidArgumentException(
00485                     'Property ' . $name . ' does not exist');
00486         }
00487     }
00488 
00501     public function __set($name, $val)
00502     {
00503         $method = 'set'.ucfirst($name);
00504         if (method_exists($this, $method)) {
00505             return call_user_func(array(&$this, $method), $val);
00506         } else if (isset($this->{'_' . $name}) || ($this->{'_' . $name} === null)) {
00507             $this->{'_' . $name} = $val;
00508         } else {
00509             require_once 'Zend/Gdata/App/InvalidArgumentException.php';
00510             throw new Zend_Gdata_App_InvalidArgumentException(
00511                     'Property ' . $name . '  does not exist');
00512         }
00513     }
00514 
00520     public function __isset($name)
00521     {
00522         $rc = new ReflectionClass(get_class($this));
00523         $privName = '_' . $name;
00524         if (!($rc->hasProperty($privName))) {
00525             require_once 'Zend/Gdata/App/InvalidArgumentException.php';
00526             throw new Zend_Gdata_App_InvalidArgumentException(
00527                     'Property ' . $name . ' does not exist');
00528         } else {
00529             if (isset($this->{$privName})) {
00530                 if (is_array($this->{$privName})) {
00531                     if (count($this->{$privName}) > 0) {
00532                         return true;
00533                     } else {
00534                         return false;
00535                     }
00536                 } else {
00537                     return true;
00538                 }
00539             } else {
00540                 return false;
00541             }
00542         }
00543     }
00544 
00550     public function __unset($name)
00551     {
00552         if (isset($this->{'_' . $name})) {
00553             if (is_array($this->{'_' . $name})) {
00554                 $this->{'_' . $name} = array();
00555             } else {
00556                 $this->{'_' . $name} = null;
00557             }
00558         }
00559     }
00560 
00567     public function __toString()
00568     {
00569         return $this->getText();
00570     }
00571 
00572 }
 All Data Structures Namespaces Files Functions Variables Enumerations