Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/backup/cc/cc_lib/xmlbase.php
Go to the documentation of this file.
00001 <?php
00002 
00015 require_once 'gral_lib/cssparser.php';
00016 
00021 class XMLGenericDocument {
00022 
00023 
00024     private   $charset;
00030     public    $doc = null;
00036     protected $dxpath = null;
00037     protected   $filename;
00038     private   $filepath;
00039     private   $isloaded         = false;
00040     private   $arrayPrefixNS    = array();
00041     private   $is_html          = false;
00042 
00043 
00048     public static function safexml($value) {
00049         $result = htmlspecialchars(html_entity_decode($value, ENT_QUOTES, 'UTF-8'),
00050                                    ENT_NOQUOTES,
00051                                    'UTF-8',
00052                                    false);
00053         return $result;
00054     }
00055 
00056     function __construct($ch = 'UTF-8',$validatenow=true){
00057         $this->charset = $ch;
00058         $this->documentInit();
00059         $this->doc->validateOnParse = $validatenow;
00060     }
00061 
00062     function __destruct(){
00063         $this->dxpath = null;
00064         $this->doc = null;
00065     }
00066 
00067     private function documentInit($withonCreate=true) {
00068         $hg = false;
00069         if ($this->isloaded){
00070             $guardstate = $this->doc->validateOnParse;
00071             $hg = true;
00072             unset($this->dxpath);
00073             unset($this->doc);
00074             $this->isloaded = false;
00075           }
00076         $this->doc = new DOMDocument("1.0",$this->charset);
00077         $this->doc->strictErrorChecking = true;
00078         if ($hg) {
00079             $this->doc->validateOnParse = $guardstate;
00080         }
00081         $this->doc->formatOutput = true;
00082         $this->doc->preserveWhiteSpace = true;
00083         if($withonCreate) {
00084             $this->on_create();
00085         }
00086     }
00087 
00088     public function viewXML (){
00089         return $this->doc->saveXML();
00090     }
00091 
00092     public function registerNS($prefix, $nsuri) {
00093         $this->arrayPrefixNS[$prefix]=$nsuri;
00094     }
00095 
00096     public function load($fname) {
00097     // Sine xml will remain loaded should the repeated load fail we should recreate document to be empty.
00098         $this->documentInit(false);
00099         $this->isloaded = $this->doc->load($fname);
00100         if ($this->isloaded){
00101             $this->filename = $fname;
00102             $this->processPath();
00103             $this->is_html = false;
00104         }
00105         return $this->on_load();
00106     }
00107 
00108     public function loadUrl($url){
00109         $this->documentInit();
00110         $this->isloaded = true;
00111         $this->doc->loadXML( file_get_contents($url) );
00112         $this->is_html = false;
00113         return $this->on_load();
00114     }
00115 
00116     public function loadHTML ($content){
00117         $this->documentInit();
00118         $this->doc->validateOnParse = false;
00119         $this->isloaded = true;
00120         $this->doc->loadHTML($content);
00121         $this->is_html = true;
00122         return $this->on_load();
00123     }
00124 
00125     public function loadXML ($content){
00126         $this->documentInit();
00127         $this->doc->validateOnParse = false;
00128         $this->isloaded = true;
00129         $this->doc->load($content);
00130         $this->is_html = true;
00131         return $this->on_load();
00132     }
00133 
00134     public function loadHTMLFile($fname){
00135       //Sine xml will remain loaded should the repeated load fail
00136       //we shoudl recreate document to be empty
00137         $this->documentInit();
00138         $this->doc->validateOnParse = false;
00139         $this->isloaded = $this->doc->loadHTMLFile($fname);
00140         if ($this->isloaded) {
00141             $this->filename = $fname;
00142             $this->processPath();
00143             $this->is_html=true;
00144         }
00145         return $this->on_load();
00146     }
00147 
00148     public function loadXMLFile($fname){
00149     //Sine xml will remain loaded should the repeated load fail
00150     //we shoudl recreate document to be empty
00151         $this->documentInit();
00152         $this->doc->validateOnParse = false;
00153         $this->isloaded = $this->doc->load($fname);
00154         if ($this->isloaded) {
00155             $this->filename = $fname;
00156             $this->processPath();
00157             $this->is_html=true;
00158         }
00159         return $this->on_load();
00160     }
00161 
00162 
00163     public function loadString($content){
00164 
00165         $this->doc = new DOMDocument("1.0",$this->charset);
00166         $content = '<virtualtag>'.$content.'</virtualtag>';
00167         $this->doc->loadXML($content);
00168 
00169         return true;
00170 
00171     }
00172 
00173     public function save() {
00174         $this->saveTo($this->filename);
00175     }
00176 
00177     public function saveTo($fname) {
00178         $status = false;
00179         if ($this->on_save()) {
00180             if ($this->is_html) {
00181                 $this->doc->saveHTMLFile($fname);
00182             } else {
00183                 $this->doc->save($fname);
00184             }
00185             $this->filename = $fname;
00186             $this->processPath();
00187             $status = true;
00188         }
00189         return $status;
00190     }
00191 
00192     public function validate() {
00193         return $this->doc->validate();
00194     }
00195 
00196     public function attributeValue($path,$attrname,$node=null) {
00197         $this->chkxpath();
00198         $result = null;
00199         $resultlist = null;
00200         if (is_null($node)) {
00201             $resultlist = $this->dxpath->query($path);
00202         } else {
00203             $resultlist = $this->dxpath->query($path,$node);
00204         }
00205         if ( is_object($resultlist) &&
00206         ($resultlist->length > 0) &&
00207         $resultlist->item(0)->hasAttribute($attrname)){
00208             $result = $resultlist->item(0)->getAttribute($attrname);
00209         }
00210         return $result;
00211     }
00212 
00221     public function nodeValue($path, $node = null, $count = 1){
00222         $nd = $this->node($path, $node, $count);
00223         return $this->nodeTextValue($nd);
00224     }
00225 
00232     public function nodeTextValue($node){
00233         $result = '';
00234         if (is_object($node)) {
00235             if ($node->hasChildNodes()) {
00236                 $chnodesList = $node->childNodes;
00237                 $types = array(XML_TEXT_NODE, XML_CDATA_SECTION_NODE);
00238                 foreach ($chnodesList as $chnode) {
00239                     if (in_array($chnode->nodeType, $types)){
00240                            $result .= $chnode->wholeText;
00241                       }
00242                   }
00243             }
00244         }
00245         return $result;
00246     }
00247 
00256     public function node($path, $nd = null, $count = 1) {
00257         $result = null;
00258         $resultlist = $this->nodeList($path,$nd);
00259         if ( is_object($resultlist) &&
00260         ($resultlist->length > 0) ){
00261             $result = $resultlist->item($count -1);
00262         }
00263         return $result;
00264     }
00265 
00273     public function nodeList($path,$node=null){
00274 
00275         $this->chkxpath();
00276 
00277         $resultlist = null;
00278         if (is_null($node)) {
00279             $resultlist = $this->dxpath->query($path);
00280         } else {
00281             $resultlist = $this->dxpath->query($path,$node);
00282         }
00283         return $resultlist;
00284     }
00285 
00294     public function create_attribute_ns($namespace, $name, $value = null) {
00295         $result = $this->doc->createAttributeNS($namespace, $name);
00296         if (!is_null($value)) {
00297             $result->nodeValue = $value;
00298         }
00299         return $result;
00300     }
00301 
00309     public function create_attribute($name, $value = null) {
00310         $result = $this->doc->createAttribute($name);
00311         if (!is_null($value)) {
00312             $result->nodeValue = $value;
00313         }
00314         return $result;
00315     }
00316 
00326     public function append_new_element_ns(DOMNode &$parentnode, $namespace, $name, $value = null) {
00327         $newnode = null;
00328         if (is_null($value)) {
00329             $newnode = $this->doc->createElementNS($namespace, $name);
00330         } else {
00331             $newnode = $this->doc->createElementNS($namespace, $name, $value);
00332         }
00333         return $parentnode->appendChild($newnode);
00334     }
00335 
00344     public function append_new_element_ns_cdata(DOMNode &$parentnode, $namespace, $name, $value = null) {
00345         $newnode = $this->doc->createElementNS($namespace, $name);
00346         if (!is_null($value)) {
00347             $cdata = $this->doc->createCDATASection($value);
00348             $newnode->appendChild($cdata);
00349         }
00350         return $parentnode->appendChild($newnode);
00351     }
00352 
00361     public function append_new_element(DOMNode &$parentnode, $name, $value = null) {
00362         $newnode = null;
00363         if (is_null($value)) {
00364             $newnode = $this->doc->createElement($name);
00365         } else {
00366             $newnode = $this->doc->createElement($name, $value);
00367         }
00368         return $parentnode->appendChild($newnode);
00369     }
00370 
00379     public function append_new_attribute(DOMNode &$node, $name, $value = null) {
00380         return $node->appendChild($this->create_attribute($name, $value));
00381     }
00382 
00392     public function append_new_attribute_ns(DOMNode &$node, $namespace, $name, $value = null) {
00393         return $node->appendChild($this->create_attribute_ns($namespace, $name, $value));
00394     }
00395 
00396     public function fileName() {return $this->filename;}
00397     public function filePath() {return $this->filepath;}
00398 
00399     protected function on_load() {return $this->isloaded;}
00400     protected function on_save() {return true;}
00401     protected function on_create() {return true;}
00402 
00403     public function resetXpath() {
00404         $this->dxpath = null;
00405         $this->chkxpath();
00406     }
00407 
00408     private function chkxpath() {
00409         if (!isset($this->dxpath) || is_null($this->dxpath)){
00410             $this->dxpath = new DOMXPath($this->doc);
00411             foreach ($this->arrayPrefixNS as $nskey => $nsuri) {
00412                 $this->dxpath->registerNamespace($nskey,$nsuri);
00413             }
00414         }
00415     }
00416 
00417     protected function processPath(){
00418         $path_parts = pathinfo($this->filename);
00419         $this->filepath = array_key_exists('dirname',$path_parts) ? $path_parts['dirname']."/" : '';
00420     }
00421 }
 All Data Structures Namespaces Files Functions Variables Enumerations