phpDocumentor OpenDocumentPHP
util
[ class tree: OpenDocumentPHP ] [ index: OpenDocumentPHP ] [ all elements ]

Source for file Fragment.php

Documentation is available at Fragment.php

  1. <?php
  2.  
  3. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  4.  
  5. /*
  6.  * Created on 05. Jan. 2007 by Norman Markgraf (nmarkgraf(at)user.sourceforge.net)
  7.  */
  8.  
  9. /**
  10.  * Abstract Fragment class file
  11.  * 
  12.  * PHP Version 5
  13.  *  
  14.  * LICENSE:
  15.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  16.  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  17.  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  18.  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  19.  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20.  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  21.  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  22.  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  23.  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  25.  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26.  *
  27.  * This software consists of voluntary contributions made by many individuals
  28.  * and is licensed under the GPL. For more information please see
  29.  * <http://opendocumentphp.org>.
  30.  * 
  31.  * $Id: Fragment.php 207 2007-07-20 09:17:51Z nmarkgraf $
  32.  * 
  33.  * 
  34.  * @category    File Formats
  35.  * @package        OpenDocumentPHP
  36.  * @subpackage  util
  37.  * @author         Norman Markgraf (nmarkgraf(at)user.sourceforge.net)
  38.  * @copyright     Copyright in 2006, 2007 by The OpenDocumentPHP Team
  39.  * @license     http://www.gnu.org/licenses/gpl.html GNU General Public License 2.0.
  40.  * @version        SVN: $Id: Fragment.php 207 2007-07-20 09:17:51Z nmarkgraf $
  41.  * @link           http://opendocumentphp.org
  42.  * @link        http://www.oasis-open.org/committees/download.php/20493/UCR.pdf OpenDocument Metadata Use Cases and Requirements
  43.  * @since         0.5.0 - 08. Feb. 2007
  44.  * @deprecated  0.5.2 - 05. Mar. 2007 Use ODPElement instead of Fragment or ElementFragment!
  45.  */
  46.  
  47. /**
  48.  * Include namespace interface
  49.  */
  50. require_once 'OpenDocumentPHP/util/Namespaces.php';
  51.  
  52. /**
  53.  * Abstract Fragment class.
  54.  *  
  55.  * 
  56.  * @category    File Formats
  57.  * @package        OpenDocumentPHP
  58.  * @subpackage  util
  59.  * @author         Norman Markgraf (nmarkgraf(at)user.sourceforge.net)
  60.  * @copyright     Copyright in 2006, 2007 by OpenDocumentPHP Team
  61.  * @license     http://www.gnu.org/licenses/gpl.html GNU General Public License 2.0.
  62.  * @version     Release: @package_version@
  63.  * @link           http://opendocumentphp.org
  64.  * @abstract
  65.  * @since         0.5.0 - 08. Feb. 2007
  66.  * @deprecated  0.5.2 - 05. Mar. 2007 Use ODPElement instead of Fragment or ElementFragment!
  67.  */
  68. abstract class Fragment implements Namespaces {
  69.     /**
  70.       * @access protected
  71.      */
  72.     protected $domFragment;
  73.     /**
  74.      * @access protected
  75.      */
  76.     protected $root;
  77.     /**
  78.      * @access protected
  79.      * @var DOMXpath 
  80.      */
  81.     protected $xpath = null;
  82.     /**
  83.      * Constructor method.
  84.      * 
  85.      * @param         DOMDocument $domFragment Basic DOM document.
  86.      * @since         0.5.0 - 08. Feb. 2007
  87.      */
  88.     function __construct($domFragment$root NULL{
  89.         $this->domFragment = $domFragment;
  90.         if (isset($root&& ($root !== 0)) {
  91.             $this->root = $root;
  92.         else {            
  93.             $this->root = $this->domFragment;
  94.         }
  95.     }
  96.     /**
  97.      * Returns the root DOMElement of this DOM fragment.
  98.      * 
  99.      * @return        DOMElement Root node of this DOM fragment.
  100.      * @access         public
  101.      * @since         0.5.0 - 08. Feb. 2007
  102.      */
  103.     function getDocumentFragment({
  104.         return $this->root;
  105.     }
  106.     /**
  107.      * 
  108.      * @access         protected
  109.      * @since         0.5.0 - 08. Feb. 2007
  110.      */
  111.     protected function getTag($query$namespace$tag$defaultValue{
  112.         $ret null;
  113.         // init XPath
  114.         $this->initXpath();
  115.         // Do query
  116.         // ***fix me***:    
  117.         //echo 'Query:' . $query;
  118.         $result $this->xpath->query($query);
  119.         // Check result
  120.         if ($result->length <= 0{
  121.             // We did not find a node in the document, so we need to add a new tag:
  122.             if ($defaultValue !== -1{    
  123.                 //echo "-> create new node!";        
  124.                 $ret $this->domFragment->createElementNS($namespace$tag$defaultValue);
  125.                 $this->root->appendChild($ret);
  126.             else {
  127.                 //echo "-> no node found!";
  128.                 $ret '';
  129.             }                            
  130.         else {
  131.             // There where nodes in the list. So we grab the first one in the nodelist.
  132.             //echo '-> found node!'; 
  133.             $ret $result->item(0);
  134.         }
  135.         //echo "\n";
  136.         return $ret;
  137.     }
  138.     /**
  139.      * Set (which means append or replace an existing node) a new node.
  140.      * 
  141.      * @param        DOMElement $node Old node which should be replaced
  142.      * @param        DOMElement $newNode New node which should be appended or replace the <b>$node</b> node
  143.      * @access         protected
  144.      * @since         0.5.0 - 08. Feb. 2007
  145.      */
  146.     protected function setTag($node$newNode{
  147.         $tmp $node->parentNode;
  148.         if ($tmp != null{
  149.             //$tmp->replaceNode($newNode, $node);
  150.             //$tmp->replaceChild($node, $newNode);
  151.             //$tmp->replaceChild($newNode, $node);
  152.             $tmp->removeChild($node);
  153.             $tmp->appendChild($newNode);
  154.         else {
  155.             $this->root->appendChild($newNode);
  156.         }
  157.     }
  158.     /**
  159.      * Check if the current element has a child $tag with $namespace.
  160.      * 
  161.      * @return        bool True, if there is such an element else false.
  162.      * @access         public
  163.      * @since         0.5.2 - 04. Mar. 2007
  164.      */
  165.     function hasChild($namespace$tag{
  166.         // Get the list and if it has more than one Element return TRUE:
  167.         return ($this->root->getElementByTagNameNS($namespace$tag)->length 0);
  168.     }
  169.     /**
  170.      * Retrieve a child by its namespace and tag from the current node.
  171.      * 
  172.      * @return        bool True, if there is such an element else false.
  173.      * @access         public
  174.      * @since         0.5.2 - 05. Mar. 2007
  175.      */
  176.     function getChild($namespace$tag{
  177.         $ret true;
  178.         // read all possible nodes with this tag and namespace in to a node list
  179.         $nodeList $this->root->getElementByTagNameNS($namespace$tag);
  180.         if ($nodelist->length != )  {
  181.             // There is no or to many nodes in the node list.
  182.             $ret false;
  183.         else {
  184.             // Get first (and only) item of the node list.
  185.             $ret $nodeList->item(0);
  186.         
  187.         return $ret;
  188.     }
  189.     /**
  190.      * Initialize xpath. You must do this before using xpath.
  191.      * 
  192.      * @access         protected
  193.      * @since         0.5.0 - 08. Feb. 2007
  194.      */
  195.     protected function initXpath({
  196.         if (!isset ($this->xpath)) {
  197.             $this->xpath = new DOMXpath($this->domFragment);
  198.             $this->xpath->registerNamespace('office'self :: OFFICE);
  199.             $this->xpath->registerNamespace('meta'self :: META);
  200.             $this->xpath->registerNamespace('text'self :: TEXT);
  201.             $this->xpath->registerNamespace('dc'self :: DC);
  202.             $this->xpath->registerNamespace('xlink'self :: XLINK);
  203.             $this->xpath->registerNamespace('style'self :: STYLE);
  204.             $this->xpath->registerNamespace('draw'self :: DRAW);
  205.             $this->xpath->registerNamespace('table'self :: TABLE);
  206.             $this->xpath->registerNamespace('mathml'self :: MATHML);
  207.         }
  208.     }
  209.     /**
  210.      * Create an DOMElement in the Office namespace with tag $tag.
  211.      * 
  212.      * Example:
  213.      * 
  214.      * <code>
  215.      *     $this->createOfficeElement('document');
  216.      * </code>
  217.      * will create an DOMElement like:
  218.      * <code>
  219.      *     <office:document xmlns:office="" />
  220.      * </code>
  221.      * .
  222.      * 
  223.      * @return        DOMElement 
  224.      * @param        string $tag Tag of the new DOMElement
  225.      * @access         protected
  226.      * @since         0.5.0 - 08. Feb. 2007
  227.      * @deprecated  0.5.2 - 05. Mar. 2007 Use the AbstractDocument->create...Element() method instead!
  228.      */
  229.     protected function createOfficeElement($tag{
  230.         return $this->domFragment->createElementNS(self :: OFFICE'office:' $tag);
  231.     }
  232.     /**
  233.      * Create an DOMElement in the Meta namespace with tag $tag.
  234.      * 
  235.      * @return        DOMElement 
  236.      * @param        $string $tag Tag of the new DOMElement
  237.      * @access         protected
  238.      * @since         0.5.0 - 08. Feb. 2007
  239.      * @deprecated  0.5.2 - 05. Mar. 2007 Use the AbstractDocument->create...Element() method instead!
  240.      */
  241.     protected function createMetaElement($tag{
  242.         return $this->domFragment->createElementNS(self :: META'meta:' $tag);
  243.     }
  244.     /**
  245.      * Create an DOMElement in the Meta namespace with tag $tag.
  246.      * 
  247.      * @return        DOMElement 
  248.      * @param        $string $tag Tag of the new DOMElement
  249.      * @access         protected
  250.      * @since         0.5.0 - 08. Feb. 2007
  251.      * @deprecated  0.5.2 - 05. Mar. 2007 Use the AbstractDocument->create...Element() method instead!
  252.      */
  253.     protected function createStyleElement($tag{
  254.         return $this->domFragment->createElementNS(self :: SYTLE'style:' $tag);
  255.     }
  256.     /**
  257.      * Create an DOMElement in the DC namespace with tag $tag.
  258.      * 
  259.      * @return        DOMElement 
  260.      * @param        $string $tag Tag of the new DOMElement
  261.      * @access         protected
  262.      * @since         0.5.0 - 08. Feb. 2007
  263.      * @deprecated  0.5.2 - 05. Mar. 2007 Use the AbstractDocument->create...Element() method instead!
  264.      */
  265.     protected function createDCElement($tag{
  266.         return $this->domFragment->createElementNS(self :: DC'dc:' $tag);
  267.     }
  268.     /**
  269.      * Set attribute with namespace.
  270.      * 
  271.      * @access         public
  272.      * @since         0.5.2 - 16. Mar. 2007
  273.      */
  274.     function appendChild($child{
  275.         $ret NULL;
  276.         if ($child instanceof ODPElement{
  277.             $uc_child $child->getElement();
  278.             $ret $this->root->appendChild($uc_child);            
  279.         else {
  280.             $this->root->appendChild($child);
  281.         }
  282.         return $ret;
  283.     }
  284. }
  285. ?>

Documentation generated on Wed, 18 Jun 2008 06:28:48 +0200 by phpDocumentor 1.3.2