Exemple #1
0
 /**
  *
  * Imports XML, defined in $resource, below position $xpathparent and overwrites
  * the element $xpathreplaces.
  *
  * @param string $xpathparent XPath of the parent
  * @param string $xpathreplaces XPath of the node that will be replaced
  * @param false  $xpathreplaces just adds the resource below xpathparent
  * @param XMLNode $resource resource to import
  * @param null    $resource delete resource at position $xpathreplaces
  */
 protected function importResource($xpathparent, $resource, $xpathreplaces = false)
 {
     if (is_a($resource, "XMLNode")) {
         $newcontent = $resource;
     } else {
         if ($resource == null && is_string($xpathreplaces)) {
             $newcontent = null;
         } else {
             $newcontent = new XMLNode($resource, false);
         }
     }
     $parentnodes = $this->xpath($xpathparent, $this->resource);
     if ($parentnodes->length != 1) {
         throw new Exception("Ambiguous position for new resource! Found " . $parentnodes->length . " possible parent nodes.");
     }
     $parent = $parentnodes->item(0);
     $oldnode = false;
     if ($xpathreplaces != false) {
         $childnodes = $this->xpath($xpathreplaces, $this->resource);
         if ($childnodes->length > 1) {
             throw new Exception("Ambiguous replace position for new resource! Found " . $childnodes->length . " possible nodes.");
         } else {
             if ($childnodes->length == 1) {
                 $oldnode = $childnodes->item(0);
             }
         }
     }
     //newcontent is null -> remove the subtree
     if ($newcontent == null) {
         if ($oldnode) {
             $parent->removeChild($oldnode);
         }
         return;
     }
     //Do nothing, if a node should be replaced by itself. Due to referencing this can happen.
     if ($oldnode !== false && $oldnode->isSameNode($newcontent->getRootNode())) {
         return;
     }
     $newnode = $this->dom->importNode($newcontent->getRootNode(), true);
     if ($oldnode) {
         $parent->replaceChild($newnode, $oldnode);
     } else {
         $parent->appendChild($newnode);
     }
 }