LOCKED = false; $this->doc = new DOMDocument('1.0', 'UTF-8'); $this->doc->load($domDoc); $this->doc ->formatOutput = true; $this->doc ->preserveWhitespace = false; $this->file = $domDoc; $f = explode('/', $this->file); $len = count($f); // Removed lock handling // $this->lockFileName = $ACTIVE_LOCK_PATH . '/' . $f[$len-3] . "_" // . $f[$len-2] . '_' . $f[$len-1] . '.lock'; // $this->lockFile = fopen($this->lockFileName, 'w+'); $this->tidy_charset = array( 'UTF-8' => 'utf8', 'ISO-8859-1' => 'latin1' ); $this->tidy_options = array( 'indent' => true, 'input-xml' => true, 'output-xml' => true, 'wrap' => false, 'char-encoding' => $this->tidy_charset[$CHARSET], 'output-encoding' => $this->tidy_charset[$CHARSET], 'input-encoding' => $this->tidy_charset[$CHARSET], ); } // // ---------------------------------------- // // __destruct // // // // TODO: ** Description of functionality ** // // // // TODO: ** Description of arguments ** // // // // TODO: ** Description of return value ** // // ---------------------------------------- // public function __destruct() { // // Removed lock handling // // if ( is_file($this->lockFileName) ) { // // flock($this->lockFile, LOCK_UN); // // fclose($this->lockFile); // // unlink($this->lockFileName); // // $this->LOCKED = false; // // } // } // ---------------------------------------- // get_xml_handler_for_file // // TODO: ** Description of functionality ** // // TODO: ** Description of arguments ** // // TODO: ** Description of return value ** // ---------------------------------------- public static function get_xml_handler_for_file($docPath) { if ( ( ! is_string($docPath) ) || ( ! is_file(realpath($docPath)) ) ) { return null; } if ( ! isset(self::$instances[$docPath]) ) { self::$instances[$docPath] = new xml_handler($docPath); } return self::$instances[$docPath]; } // ---------------------------------------- // lock_document // // TODO: ** Description of functionality ** // // TODO: ** Description of arguments ** // // TODO: ** Description of return value ** // ---------------------------------------- public function lock_document() { // Removed lock handling // if ( ! $this->LOCKED ) { // flock($this->lockFile, LOCK_EX); // $this->LOCKED = TRUE; $this->doc ->load($this->file); $this->doc ->formatOutput = true; $this->doc ->preserveWhitespace = false; return $this->doc; // } // else { // die("File $this->file is locked!"); // } } // ---------------------------------------- // unlock_document // // TODO: ** Description of functionality ** // // TODO: ** Description of arguments ** // // TODO: ** Description of return value ** // ---------------------------------------- public function unlock_document($doc) { // Removed lock handling // if ( $this->LOCKED ) { $tidy = new tidy(); $tidy->parseString($doc->saveXML(), $this->tidy_options); $tidy->cleanRepair(); fwrite(fopen($this->file . '.TMP', 'w'), $tidy); rename($this->file . '.TMP', $this->file); // flock($this->lockFile, LOCK_UN); //$this->LOCKED = false; // } } // ---------------------------------------- // insert // // TODO: ** Description of functionality ** // // TODO: ** Description of arguments ** // // TODO: ** Description of return value ** // // Till insert skickas en array där varje element är // en array med tre poster ('parent', 'id' och 'element'); // en xpath till den blivande // föräldernoden, en xpath som identifierar datat unikt // och det element som ska stoppas in. // ---------------------------------------- public function insert($vals) { if ( ! is_array($vals) ) { die('Felaktiga värden'); } $inserted = 0; // flock($this->lockFile, LOCK_EX); $this->doc ->load($this->file); $this->doc ->formatOutput = true; $this->doc ->preserveWhitespace = false; $xpath = new DOMXPath($this->doc); foreach ( $vals as $arr ) { if ( is_array($arr) && ( count($arr) == 3 ) ) { $parent = $xpath -> query($arr['parent']); $id = $xpath -> query($arr['id']); $element = $arr['element']; if ( ( $parent->length != 1 ) || ( $id->length > 0 ) || ( ! ($element instanceof DOMElement) ) ) { log_to_file(array($arr, array('$parent->length'=>$parent->length, '$id->length' =>$id->length, 'element' =>$element))); continue; } $element = $this->doc->importNode($element, TRUE); $parent = $parent->item(0); $parent -> appendChild($element); ++$inserted; } } //$doc -> save($file); $tidy = new tidy(); $tidy->parseString($this->doc->saveXML(), $this->tidy_options); $tidy->cleanRepair(); fwrite(fopen($this->file . '.TMP', 'w'), $tidy); rename($this->file . '.TMP', $this->file); // flock($this->lockFile, LOCK_UN); return $inserted; } // ---------------------------------------- // replace // // TODO: ** Description of functionality ** // // TODO: ** Description of arguments ** // // TODO: ** Description of return value ** // ---------------------------------------- public function replace($vals) { if ( ! is_array($vals) ) { die('Felaktiga värden'); } // flock($this->lockFile, LOCK_EX); $this->doc ->load($this->file); $this->doc ->formatOutput = true; $this->doc ->preserveWhitespace = false; $xpath = new DOMXPath($this->doc); foreach ( $vals as $arr ) { if ( is_array($arr) && ( count($arr) == 3 ) ) { $parent = $xpath->query($arr['parent']); $id = $xpath->query($arr['id']); $element = $arr['element']; if ( ( $parent->length != 1 ) || ( $id->length != 1 ) || ( ! ($element instanceof DOMElement) ) ) { log_to_file("parent:{$parent->length} id:{$id->length}"); continue; } $parent = $parent->item(0); $oldNode = $id->item(0); $element = $this->doc->importNode($element, TRUE); $parent -> replaceChild($element, $oldNode); } else { log_to_file("fel argument: ".print_r($arr, true)); } } // $this->doc -> save($file); $tidy = new tidy(); $tidy->parseString($this->doc->saveXML(), $this->tidy_options); $tidy->cleanRepair(); fwrite(fopen($this->file.'.TMP','w'), $tidy); rename($this->file.'.TMP', $this->file); // flock($this->lockFile, LOCK_UN); } // ---------------------------------------- // replace_or_insert // // TODO: ** Description of functionality ** // // TODO: ** Description of arguments ** // // TODO: ** Description of return value ** // ---------------------------------------- public function replace_or_insert($vals) { if ( ! is_array($vals) ) { die('Felaktiga värden'); } // flock($this->lockFile, LOCK_EX); $this->doc ->load($this->file); $this->doc ->formatOutput = true; $this->doc ->preserveWhitespace = false; $xpath = new DOMXPath($this->doc); foreach ( $vals as $arr ) { if ( is_array($arr) && ( count($arr) == 3 ) ) { $parent = $xpath->query($arr['parent']); $id = $xpath->query($arr['id']); $element = $arr['element']; if ( ( $parent->length != 1 ) || ( ! ($element instanceof DOMElement) ) ) { //echo "parent:{$parent->length} id:{$id->length}"; continue; } $parent = $parent->item(0); $element = $this->doc->importNode($element, TRUE); if ( $id->length === 1 ) { $oldNode = $id->item(0); $parent->replaceChild($element, $oldNode); } elseif ( $id->length === 0 ) { $parent->appendChild($element); } } // else echo "replace: " . print_r($arr, true); } // $this->doc->save($file); $tidy = new tidy(); $tidy->parseString($this->doc->saveXML(), $this->tidy_options); $tidy->cleanRepair(); fwrite(fopen($this->file . '.TMP', 'w'), $tidy); rename($this->file . '.TMP', $this->file); // flock($this->lockFile, LOCK_UN); } // ---------------------------------------- // delete // // TODO: ** Description of functionality ** // // TODO: ** Description of arguments ** // // TODO: ** Description of return value ** // ---------------------------------------- public function delete($vals) { if ( ! is_array($vals) ) { die('Felaktiga värden'); } // flock($this->lockFile, LOCK_EX); $this->doc ->load($this->file); $this->doc ->formatOutput = true; $this->doc ->preserveWhitespace = false; $xpath = new DOMXPath($this->doc); foreach ( $vals as $arr ) { if ( is_array($arr) && ( count($arr) == 2 ) ) { $parent = $xpath->query($arr['parent']); $id = $xpath->query($arr['id']); if ( ( $parent->length != 1 ) || ( $id->length != 1 ) ) { continue; } $parent = $parent->item(0); $oldNode = $id->item(0); $parent -> removeChild($oldNode); } } $tidy = new tidy(); $tidy->parseString($this->doc->saveXML(), $this->tidy_options); $tidy->cleanRepair(); fwrite(fopen($this->file . '.TMP', 'w'), $tidy); rename($this->file . '.TMP', $this->file); // flock($this->lockFile, LOCK_UN); } // ---------------------------------------- // get_node_array // // TODO: ** Description of functionality ** // // TODO: ** Description of arguments ** // // TODO: ** Description of return value ** // ---------------------------------------- public function get_node_array($xpathStr) { // flock($this->lockFile, LOCK_EX); $this->doc->load($this->file); $xPath = new DOMXPath($this->doc); $res = $xPath->query($xpathStr); if ( $res->length === 0 ) { // echo "xml_handler::get_node_array: '" . $xpathStr . "' not found in ".$this->file; // flock($this->lockFile, LOCK_UN); return NULL; } $resArr = array(); for ( $i = 0 ; $i < $res->length ; ++$i ) { $resArr[] = $res->item($i); } // flock($this->lockFile, LOCK_UN); return $resArr; } // ---------------------------------------- // get_node_list // // TODO: ** Description of functionality ** // // TODO: ** Description of arguments ** // // TODO: ** Description of return value ** // ---------------------------------------- public function get_node_list($xpathStr) { // flock($this->lockFile, LOCK_EX); $this->doc->load($this->file); $xPath = new DOMXPath($this->doc); $res = $xPath->query($xpathStr); // flock($this->lockFile, LOCK_UN); return $res; } // ---------------------------------------- // __toString // // TODO: ** Description of functionality ** // // TODO: ** Description of arguments ** // // TODO: ** Description of return value ** // ---------------------------------------- public function __toString() { return $this->file; } // ---------------------------------------- // get_doc // // TODO: ** Description of functionality ** // // TODO: ** Description of arguments ** // // TODO: ** Description of return value ** // ---------------------------------------- public function get_doc() { return $this->doc; } // ---------------------------------------- // End of class // ---------------------------------------- } // ---------------------------------------------------------------------------- ?>