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

Source for file Fragment.php

Documentation is available at Fragment.php

  1. <?php
  2. /*
  3.  * Created on 05.01.2007 by Norman Markgraf (nmarkgraf(at)user.sourceforge.net)
  4.  *
  5.  * PHP versions 5.2 or better.
  6.  *
  7.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  8.  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  9.  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  10.  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  11.  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  12.  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  13.  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  14.  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  15.  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  16.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  17.  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  18.  *
  19.  * This software consists of voluntary contributions made by many individuals
  20.  * and is licensed under the GPL. For more information please see
  21.  * <http://opendocumentphp.org>.
  22.  * 
  23.  * $Id: Fragment.php 159 2007-03-16 14:12:15Z nmarkgraf $
  24.  */
  25. /**
  26.  * Abstract Fragment class.
  27.  *  
  28.  * @author         Norman Markgraf (nmarkgraf(at)user.sourceforge.net)
  29.  * @copyright     Copyright in 2006, 2007 by The OpenDocumentPHP Team
  30.  * @license     http://www.gnu.org/licenses/gpl.html GNU General Public License 2.0.
  31.  * @version        $Revision: 159 $
  32.  * @package        OpenDocumentPHP
  33.  * @subpackage  util
  34.  * @abstract
  35.  * @since         0.5.0 - 08.02.2007
  36.  * @deprecated  0.5.2 - 05.03.2007 Use ODPElement instead of Fragment or ElementFragment!
  37.  */
  38. abstract class Fragment {
  39.     /**
  40.      * namespace OpenDocument meta
  41.      */
  42.     const META 'urn:oasis:names:tc:opendocument:xmlns:meta:1.0';
  43.     /**
  44.      * namespace OpenDocument office
  45.      */
  46.     const OFFICE 'urn:oasis:names:tc:opendocument:xmlns:office:1.0';
  47.     /**
  48.      * namespace OpenDocument manifest
  49.      */
  50.     const MANIFEST 'urn:oasis:names:tc:opendocument:xmlns:manifest:1.0';
  51.     /**
  52.      * namespace OpenDocument style
  53.      */
  54.     const STYLE 'urn:oasis:names:tc:opendocument:xmlns:style:1.0';
  55.     /**
  56.      * namespace OpenDocument text
  57.      */
  58.     const TEXT 'urn:oasis:names:tc:opendocument:xmlns:text:1.0';
  59.     /**
  60.      * namespace OpenDocument draw
  61.      */
  62.     const DRAW 'urn:oasis:names:tc:opendocument:xmlns:drawing:1.0';
  63.     /**
  64.      * namespace OpenDocument table
  65.      */
  66.     const TABLE 'urn:oasis:names:tc:opendocument:xmlns:table:1.0';
  67.     /**
  68.      * namespace OpenDocument number
  69.      */
  70.     const NUMBER 'urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0';
  71.     /**
  72.      * namespace OpenDocument chart
  73.      */
  74.     const CHART 'urn:oasis:names:tc:opendocument:xmlns:chart:1.0';
  75.     /**
  76.      * namespace OpenDocument form
  77.      */
  78.     const FORM 'urn:oasis:names:tc:opendocument:xmlns:form:1.0';
  79.     /**
  80.      * namespace OpenDocument config
  81.      */
  82.     const CONFIG 'urn:oasis:names:tc:opendocument:xmlns:config:1.0';
  83.     /**
  84.      * namespace OpenDocument presentation
  85.      */
  86.     const PRESENTATION 'urn:oasis:names:tc:opendocument:xmlns:presentation:1.0';
  87.     /**
  88.      * namespace OpenDocument dr3d
  89.      */
  90.     const DR3D 'urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0';
  91.     /**
  92.      * namespace OpenDocument animation
  93.      */
  94.     const ANIM 'urn:oasis:names:tc:opendocument:xmlns:animation:1.0';
  95.     /**
  96.      * namespace OpenDocument script
  97.      */
  98.     const SCRIPT 'urn:oasis:names:tc:opendocument:xmlns:script:1.0';
  99.     /**
  100.      * namespace OpenDocument svg
  101.      */
  102.     const SVG 'urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0';
  103.     /**
  104.      * namespace OpenDocument fo (formation objects)
  105.      */
  106.     const FO 'urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0';
  107.     /**
  108.      * namespace OpenDocument smil
  109.      */
  110.     const SMIL 'urn:oasis:names:tc:opendocument:xmlns:smil-compatible:1.0';
  111.     /**
  112.      * namespace Dublin Core
  113.      */
  114.     const DC 'http://purl.org/dc/elements/1.1/';
  115.     /**
  116.      * namespace XLink
  117.      */
  118.     const XLINK 'http://www.w3.org/1999/xlink';
  119.     /**
  120.      * namespace XForms
  121.      */
  122.     const XFORMS 'http://www.w3.org/2002/xforms';
  123.     /**
  124.      * namespace MathML
  125.      */
  126.     const MATHML 'http://www.w3.org/1998/Math/MathML';
  127.     /**
  128.     * @access protected
  129.     */
  130.     protected $domFragment;
  131.     /**
  132.     * @access protected
  133.     */
  134.     protected $root;
  135.     /**
  136.     * @access protected
  137.     * @var DOMXpath 
  138.     */
  139.     protected $xpath = null;
  140.     /**
  141.      * Constructor method.
  142.      * 
  143.      * @param         DOMDocument $domFragment Basic DOM document.
  144.      * @since         0.5.0 - 08.02.2007
  145.      */
  146.     function __construct($domFragment$root NULL{
  147.         $this->domFragment = $domFragment;
  148.         if (isset($root&& ($root !== 0)) {
  149.             $this->root = $root;
  150.         else {            
  151.             $this->root = $this->domFragment;
  152.         }
  153.     }
  154.     /**
  155.      * Returns the root DOMElement of this DOM fragment.
  156.      * 
  157.      * @return        DOMElement Root node of this DOM fragment.
  158.      * @access         public
  159.      * @since         0.5.0 - 08.02.2007
  160.      */
  161.     function getDocumentFragment({
  162.         return $this->root;
  163.     }
  164.     /**
  165.      * 
  166.      * @access         protected
  167.      * @since         0.5.0 - 08.02.2007
  168.      */
  169.     protected function getTag($query$namespace$tag$defaultValue{
  170.         $ret null;
  171.         // init XPath
  172.         $this->initXpath();
  173.         // Do query
  174.         // ***fix me***:    
  175.         //echo 'Query:' . $query;
  176.         $result $this->xpath->query($query);
  177.         // Check result
  178.         if ($result->length <= 0{
  179.             // We did not find a node in the document, so we need to add a new tag:
  180.             if ($defaultValue !== -1{    
  181.                 //echo "-> create new node!";        
  182.                 $ret $this->domFragment->createElementNS($namespace$tag$defaultValue);
  183.                 $this->root->appendChild($ret);
  184.             else {
  185.                 //echo "-> no node found!";
  186.                 $ret '';
  187.             }                            
  188.         else {
  189.             // There where nodes in the list. So we grab the first one in the nodelist.
  190.             //echo '-> found node!'; 
  191.             $ret $result->item(0);
  192.         }
  193.         //echo "\n";
  194.         return $ret;
  195.     }
  196.     /**
  197.      * Set (which means append or replace an existing node) a new node.
  198.      * 
  199.      * @param        DOMElement $node Old node which should be replaced
  200.      * @param        DOMElement $newNode New node which should be appended or replace the <b>$node</b> node
  201.      * @access         protected
  202.      * @since         0.5.0 - 08.02.2007
  203.      */
  204.     protected function setTag($node$newNode{
  205.         $tmp $node->parentNode;
  206.         if ($tmp != null{
  207.             //$tmp->replaceNode($newNode, $node);
  208.             //$tmp->replaceChild($node, $newNode);
  209.             //$tmp->replaceChild($newNode, $node);
  210.             $tmp->removeChild($node);
  211.             $tmp->appendChild($newNode);
  212.         else {
  213.             $this->root->appendChild($newNode);
  214.         }
  215.     }
  216.     /**
  217.      * Check if the current element has a child $tag with $namespace.
  218.      * 
  219.      * @return        bool True, if there is such an element else false.
  220.      * @access         public
  221.      * @since         0.5.2 - 04.03.2007
  222.      */
  223.     function hasChild($namespace$tag{
  224.         // Get the list and if it has more than one Element return TRUE:
  225.         return ($this->root->getElementByTagNameNS($namespace$tag)->length 0);
  226.     }
  227.     /**
  228.      * Retrieve a child by its namespace and tag from the current node.
  229.      * 
  230.      * @return        bool True, if there is such an element else false.
  231.      * @access         public
  232.      * @since         0.5.2 - 05.03.2007
  233.      */
  234.     function getChild($namespace$tag{
  235.         $ret true;
  236.         // read all possible nodes with this tag and namespace in to a node list
  237.         $nodeList $this->root->getElementByTagNameNS($namespace$tag);
  238.         if ($nodelist->length != )  {
  239.             // There is no or to many nodes in the node list.
  240.             $ret false;
  241.         else {
  242.             // Get first (and only) item of the node list.
  243.             $ret $nodeList->item(0);
  244.         
  245.         return $ret;
  246.     }
  247.     /**
  248.      * Initialize xpath. You must do this before using xpath.
  249.      * 
  250.      * @access         protected
  251.      * @since         0.5.0 - 08.02.2007
  252.      */
  253.     protected function initXpath({
  254.         if (!isset ($this->xpath)) {
  255.             $this->xpath = new DOMXpath($this->domFragment);
  256.             $this->xpath->registerNamespace('office'self :: OFFICE);
  257.             $this->xpath->registerNamespace('meta'self :: META);
  258.             $this->xpath->registerNamespace('text'self :: TEXT);
  259.             $this->xpath->registerNamespace('dc'self :: DC);
  260.             $this->xpath->registerNamespace('xlink'self :: XLINK);
  261.             $this->xpath->registerNamespace('style'self :: STYLE);
  262.             $this->xpath->registerNamespace('draw'self :: DRAW);
  263.             $this->xpath->registerNamespace('table'self :: TABLE);
  264.             $this->xpath->registerNamespace('mathml'self :: MATHML);
  265.         }
  266.     }
  267.     /**
  268.      * Create an DOMElement in the Office namespace with tag $tag.
  269.      * 
  270.      * Example:
  271.      * 
  272.      * <code>
  273.      *     $this->createOfficeElement('document');
  274.      * </code>
  275.      * will create an DOMElement like:
  276.      * <code>
  277.      *     <office:document xmlns:office="" />
  278.      * </code>
  279.      * .
  280.      * 
  281.      * @return        DOMElement 
  282.      * @param        string $tag Tag of the new DOMElement
  283.      * @access         protected
  284.      * @since         0.5.0 - 08.02.2007
  285.      * @deprecated  0.5.2 - 05.03.2007 Use the AbstractDocument->create...Element() method instead!
  286.      */
  287.     protected function createOfficeElement($tag{
  288.         return $this->domFragment->createElementNS(self :: OFFICE'office:' $tag);
  289.     }
  290.     /**
  291.      * Create an DOMElement in the Meta namespace with tag $tag.
  292.      * 
  293.      * @return        DOMElement 
  294.      * @param        $string $tag Tag of the new DOMElement
  295.      * @access         protected
  296.      * @since         0.5.0 - 08.02.2007
  297.      * @deprecated  0.5.2 - 05.03.2007 Use the AbstractDocument->create...Element() method instead!
  298.      */
  299.     protected function createMetaElement($tag{
  300.         return $this->domFragment->createElementNS(self :: META'meta:' $tag);
  301.     }
  302.     /**
  303.      * Create an DOMElement in the Meta namespace with tag $tag.
  304.      * 
  305.      * @return        DOMElement 
  306.      * @param        $string $tag Tag of the new DOMElement
  307.      * @access         protected
  308.      * @since         0.5.0 - 08.02.2007
  309.      * @deprecated  0.5.2 - 05.03.2007 Use the AbstractDocument->create...Element() method instead!
  310.      */
  311.     protected function createStyleElement($tag{
  312.         return $this->domFragment->createElementNS(self :: SYTLE'style:' $tag);
  313.     }
  314.     /**
  315.      * Create an DOMElement in the DC namespace with tag $tag.
  316.      * 
  317.      * @return        DOMElement 
  318.      * @param        $string $tag Tag of the new DOMElement
  319.      * @access         protected
  320.      * @since         0.5.0 - 08.02.2007
  321.      * @deprecated  0.5.2 - 05.03.2007 Use the AbstractDocument->create...Element() method instead!
  322.      */
  323.     protected function createDCElement($tag{
  324.         return $this->domFragment->createElementNS(self :: DC'dc:' $tag);
  325.     }
  326.     /**
  327.      * Set attribute with namespace.
  328.      * 
  329.      * @access         public
  330.      * @since         0.5.2 - 16.03.2007
  331.      */
  332.     function appendChild($child{
  333.         $ret NULL;
  334.         if ($child instanceof ODPElement{
  335.             $uc_child $child->getElement();
  336.             $ret $this->root->appendChild($uc_child);            
  337.         else {
  338.             $this->root->appendChild($child);
  339.         }
  340.         return $ret;
  341.     }
  342.     
  343. }
  344. ?>

Documentation generated on Tue, 12 Jun 2007 09:59:53 +0200 by phpDocumentor 1.3.2