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

Source for file AbstractDocument.php

Documentation is available at AbstractDocument.php

  1. <?php
  2. /*
  3.  * Created on 05.01.2007 by Norman Markgraf (nmarkgraf(at)user.sourceforge.net)
  4.  *
  5.   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  6.  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  7.  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  8.  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  9.  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  10.  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  11.  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  12.  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  13.  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  14.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  15.  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16.  *
  17.  * This software consists of voluntary contributions made by many individuals
  18.  * and is licensed under the GPL. For more information please see
  19.  * <http://opendocumentphp.org>.
  20.  * 
  21.  * $Id: AbstractDocument.php 161 2007-03-19 09:35:16Z nmarkgraf $
  22. */
  23. require_once 'OpenDocumentPHP/util/ODPElement.php';
  24. require_once 'OpenDocumentPHP/styles/properties/ParagraphProperties.php';
  25. require_once 'OpenDocumentPHP/styles/properties/TextProperties.php';
  26. require_once 'OpenDocumentPHP/styles/DefaultStyle.php';
  27. require_once 'OpenDocumentPHP/styles/Style.php';
  28. /**
  29.  * AbstractDocument class.
  30.  *  
  31.  * @author         Norman Markgraf (nmarkgraf(at)user.sourceforge.net)
  32.  * @copyright     Copyright in 2006, 2007 by The OpenDocumentPHP Team
  33.  * @license     http://www.gnu.org/licenses/gpl.html GNU General Public License 2.0.
  34.  * @version        $Revision: 161 $
  35.  * @package        OpenDocumentPHP
  36.  * @subpackage  util
  37.  * @since         0.5.0 - 08.02.2007
  38.  */
  39. class AbstractDocument extends DOMDocument {
  40.     /**
  41.      * namespace OpenDocument meta
  42.      */
  43.     const META = 'urn:oasis:names:tc:opendocument:xmlns:meta:1.0';
  44.     /**
  45.      * namespace OpenDocument office
  46.      */
  47.     const OFFICE = 'urn:oasis:names:tc:opendocument:xmlns:office:1.0';
  48.     /**
  49.      * namespace OpenDocument manifest
  50.      */
  51.     const MANIFEST = 'urn:oasis:names:tc:opendocument:xmlns:manifest:1.0';
  52.     /**
  53.      * namespace OpenDocument style
  54.      */
  55.     const STYLE = 'urn:oasis:names:tc:opendocument:xmlns:style:1.0';
  56.     /**
  57.      * namespace OpenDocument text
  58.      */
  59.     const TEXT = 'urn:oasis:names:tc:opendocument:xmlns:text:1.0';
  60.     /**
  61.      * namespace OpenDocument draw
  62.      */
  63.     const DRAW = 'urn:oasis:names:tc:opendocument:xmlns:drawing:1.0';
  64.     /**
  65.      * namespace OpenDocument table
  66.      */
  67.     const TABLE = 'urn:oasis:names:tc:opendocument:xmlns:table:1.0';
  68.     /**
  69.      * namespace OpenDocument number
  70.      */
  71.     const NUMBER = 'urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0';
  72.     /**
  73.      * namespace OpenDocument chart
  74.      */
  75.     const CHART = 'urn:oasis:names:tc:opendocument:xmlns:chart:1.0';
  76.     /**
  77.      * namespace OpenDocument form
  78.      */
  79.     const FORM = 'urn:oasis:names:tc:opendocument:xmlns:form:1.0';
  80.     /**
  81.      * namespace OpenDocument config
  82.      */
  83.     const CONFIG = 'urn:oasis:names:tc:opendocument:xmlns:config:1.0';
  84.     /**
  85.      * namespace OpenDocument presentation
  86.      */
  87.     const PRESENTATION = 'urn:oasis:names:tc:opendocument:xmlns:presentation:1.0';
  88.     /**
  89.      * namespace OpenDocument dr3d
  90.      */
  91.     const DR3D = 'urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0';
  92.     /**
  93.      * namespace OpenDocument animation
  94.      */
  95.     const ANIM = 'urn:oasis:names:tc:opendocument:xmlns:animation:1.0';
  96.     /**
  97.      * namespace OpenDocument script
  98.      */
  99.     const SCRIPT = 'urn:oasis:names:tc:opendocument:xmlns:script:1.0';
  100.     /**
  101.      * namespace OpenDocument svg
  102.      */
  103.     const SVG = 'urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0';
  104.     /**
  105.      * namespace OpenDocument fo (formation objects)
  106.      */
  107.     const FO = 'urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0';
  108.     /**
  109.      * namespace OpenDocument smil
  110.      */
  111.     const SMIL = 'urn:oasis:names:tc:opendocument:xmlns:smil-compatible:1.0';
  112.     /**
  113.      * namespace Dublin Core
  114.      */
  115.     const DC = 'http://purl.org/dc/elements/1.1/';
  116.     /**
  117.      * namespace XLink
  118.      */
  119.     const XLINK = 'http://www.w3.org/1999/xlink';
  120.     /**
  121.      * namespace XForms
  122.      */
  123.     const XFORMS = 'http://www.w3.org/2002/xforms';
  124.     /**
  125.      * namespace MathML
  126.      */
  127.     const MATHML = 'http://www.w3.org/1998/Math/MathML';
  128.     /**
  129.      * Link to the DOMElement which is the root of this DOM document.
  130.      * 
  131.      * @var         DOMElement 
  132.      * @access        protected
  133.      * @since         0.5.0 - 08.02.2007
  134.      */
  135.     protected $root;
  136.     /**
  137.     * @access     protected
  138.     * @var         DOMXpath 
  139.     * @since     0.5.2 - 26.02.2007
  140.     */
  141.     protected $xpath = null;
  142.     /**
  143.      * Constructor method.
  144.      * 
  145.      * @since         0.5.0 - 08.02.2007
  146.      */
  147.     function __construct($documentRoot NULL{
  148.         parent :: __construct('1.0''UTF-8');
  149.         $this->formatOutput true;
  150.         $this->recover true;
  151.         //
  152.         $config $this->config;
  153.         if ($config != null{
  154.             // echo "config found!\n";
  155.             if ($config->canSetParameter('canonical-form''true')) {
  156.                 $config->setParameter('canonical-form''true');
  157.                 //    echo "canonical-form setted!\n";
  158.             }
  159.             if ($config->canSetParameter('datatype-normalizlation''true')) {
  160.                 $config->setParameter('datatype-normalizlation''true');
  161.                 //    echo "datatype-normalizlation setted!\n";
  162.             }
  163.         }
  164.         //
  165.         $this->_setRoot($documentRoot);
  166.         // I am not sure that this will work, but I try it:
  167.         /**/
  168.         $this->registerNodeClass('DOMDocument''AbstractDocument');
  169.         /**/
  170.         $this->registerNodeClass('DOMElement''ODPElement');
  171.     }
  172.     /**
  173.      * Set the root Element of this document.
  174.      * 
  175.      * @since         0.5.1 - 08.02.2007
  176.      * @access         protected
  177.      */
  178.     protected function _setRoot($documentRoot NULL{
  179.         if (isset ($documentRoot&& $documentRoot !== 0{
  180.             $this->root = $this->createElementNS(self :: OFFICE$documentRoot);
  181.             $this->root->setAttributeNS(self :: OFFICE'office:version''1.0');
  182.             $this->appendChild($this->root);
  183.         }
  184.     }
  185.     /**
  186.      * Retrieve the value of an element by its namespace and tag.
  187.      * 
  188.      * @access         public
  189.      * @since         0.5.0 - 08.02.2007
  190.      * @param        string $namespace Namespace of the element.
  191.      * @param        string $elemet Tag of the element.
  192.      * @return        mixed The value of the element as string or the boolean 'false',
  193.      *                        if no element was found.
  194.      */
  195.     function getElementNS($namespace$element{
  196.         $retValue false;
  197.         $nodelist $this->getElementsByTagNameNS($namespace$element);
  198.         if ($nodelist->length === 1{
  199.             $retValue $nodelist->item(0)->nodeValue;
  200.         }
  201.         return $retValue;
  202.     }
  203.     /**
  204.      * Dumps the internal XML tree back into a string.
  205.      * 
  206.      * Creates an XML document from the DOM representation. This function
  207.      * is usually called after building a new DOM document from scratch.
  208.      * 
  209.      * @access         public
  210.      * @since         0.5.0 - 08.02.2007
  211.      * @param        DOMNode $node Use this parameter to output only a specific node
  212.      *                                without XML declaration rather than the entire document.
  213.      * @param        int $options  Additional Options. Currently only LIBXML_NOEMPTYTAG
  214.      *                                is supported.
  215.      */
  216.     function saveXML($node NULL$options NULL{
  217.         $this->normalize();
  218.         $this->normalizeDocument();
  219.         if (isset ($options)) {
  220.             return parent :: saveXML($node$options);
  221.         }
  222.         elseif (isset ($node)) {
  223.             return parent :: saveXML($node);
  224.         else {
  225.             return parent :: saveXML();
  226.         }
  227.     }
  228.     /**
  229.      * This function is not needed, so we always return false!
  230.      * 
  231.      * @access         public
  232.      * @since         0.5.0 - 08.02.2007
  233.      */
  234.     function loadHTML($source{
  235.         return false;
  236.     }
  237.     /**
  238.      * This function is not needed, so we always return false!
  239.      * 
  240.      * @access         public
  241.      * @since         0.5.0 - 08.02.2007
  242.      */
  243.     function loadHTMLFile($filename{
  244.         return false;
  245.     }
  246.     /**
  247.      * This function is not needed, so we always return false!
  248.      * 
  249.      * @access         public
  250.      * @since         0.5.0 - 08.02.2007
  251.      */
  252.     function saveHTML({
  253.         return false;
  254.     }
  255.     /**
  256.      * This function is not needed, so we always return false!
  257.      * 
  258.      * @access         public
  259.      * @since         0.5.0 - 08.02.2007
  260.      */
  261.     function saveHTMLFile($filename{
  262.         return false;
  263.     }
  264.     /**
  265.      * Initialize xpath. You must do this before using xpath.
  266.      * 
  267.      * @access         protected
  268.      * @since         0.5.2 - 26.02.2007
  269.      */
  270.     protected function initXpath({
  271.         if (!isset ($this->xpath)) {
  272.             $this->xpath = new DOMXpath($this);
  273.             $this->xpath->registerNamespace('office'self :: OFFICE);
  274.             $this->xpath->registerNamespace('meta'self :: META);
  275.             $this->xpath->registerNamespace('text'self :: TEXT);
  276.             $this->xpath->registerNamespace('dc'self :: DC);
  277.             $this->xpath->registerNamespace('xlink'self :: XLINK);
  278.             $this->xpath->registerNamespace('style'self :: STYLE);
  279.             $this->xpath->registerNamespace('draw'self :: DRAW);
  280.             $this->xpath->registerNamespace('table'self :: TABLE);
  281.             $this->xpath->registerNamespace('mathml'self :: MATHML);
  282.         }
  283.     }
  284.     /**
  285.      * Create an DOMElement in the Office namespace with tag $tag.
  286.      * 
  287.      * Example:
  288.      * 
  289.      * <code>
  290.      *     $this->createOfficeElement('document');
  291.      * </code>
  292.      * will create an DOMElement like:
  293.      * <code>
  294.      *     <office:document xmlns:office="" />
  295.      * </code>
  296.      * .
  297.      * 
  298.      * @return        DOMElement 
  299.      * @param        string $tag Tag of the new DOMElement
  300.      * @param       string $value If seted, this is the node value.
  301.      * @access         protected
  302.      * @since         0.5.2 - 05.03.2007
  303.      */
  304.     protected function createOfficeElement($tag$value{
  305.         return $this->createElementNS(self :: OFFICE'office:' $tag$value);
  306.     }
  307.     /**
  308.      * Create an DOMElement in the Meta namespace with tag $tag.
  309.      * 
  310.      * @return        DOMElement 
  311.      * @param        string $tag Tag of the new DOMElement
  312.      * @param       string $value If seted, this is the node value.
  313.      * @access         protected
  314.      * @since         0.5.2 - 05.03.2007
  315.      */
  316.     protected function createMetaElement($tag$value null{
  317.         return $this->createElementNS(self :: META'meta:' $tag$value);
  318.     }
  319.     /**
  320.      * Create an DOMElement in the Meta namespace with tag $tag.
  321.      * 
  322.      * @return        DOMElement 
  323.      * @param        string $tag Tag of the new DOMElement
  324.      * @param       string $value If seted, this is the node value.
  325.      * @access         protected
  326.      * @since         0.5.2 - 05.03.2007
  327.      */
  328.     protected function createStyleElement($tag$value null{
  329.         /*
  330.         $ret = null;
  331.         if (is_null($value)) {
  332.             $ret = $this->createElementNS(self :: STYLE, 'style:' . $tag);
  333.         } else {
  334.             $ret = $this->createElementNS(self :: STYLE, 'style:' . $tag, $value);
  335.         }
  336.         return $ret;
  337.         */
  338.         return $this->createElementNS(self :: STYLE'style:' $tag$value);
  339.     }
  340.     /**
  341.      * Create an DOMElement in the DC namespace with tag $tag.
  342.      * 
  343.      * @return        DOMElement 
  344.      * @param        string $tag Tag of the new DOMElement
  345.      * @param       string $value If seted, this is the node value.
  346.      * @access         protected
  347.      * @since         0.5.2 - 05.03.2007
  348.      */
  349.     protected function createDCElement($tag$value null{
  350.         return $this->createElementNS(self :: DC'dc:' $tag$value);
  351.     }
  352.     /**
  353.      * Create an DOMElement in the Manifest namespace with tag $tag.
  354.      * 
  355.      * @return        DOMElement 
  356.      * @param        string $tag Tag of the new DOMElement
  357.      * @param       string $value If seted, this is the node value.
  358.      * @access         protected
  359.      * @since         0.5.2 - 05.03.2007
  360.      */
  361.     protected function createManifestElement($tag$value null{
  362.         return $this->createElementNS(self :: MANIFEST'manifest:' $tag$value);
  363.     }
  364.     /**
  365.      * Create an ODPElement as '<style:text-properties>'.
  366.      * 
  367.      * @return        ODPElement 
  368.      * @access         public
  369.      * @since         0.5.2 - 16.03.2007
  370.      */
  371.     public function createTextPropertiesElement({
  372.         /* 
  373.         //It should be:
  374.         $ret = new TextProperties('style:text-properties', NULL, self::STYLE );
  375.         $ret = $this->adoptNode($ret);
  376.         return $ret; 
  377.         //But we must use: _PHP IS A MESS_ */
  378.         return new TextProperties($this->createStyleElement('text-properties'));
  379.     }
  380.     /**
  381.      * Create an ODPElement as '<style:paragraph-properties>'.
  382.      * 
  383.      * @return        ODPElement 
  384.      * @access         public
  385.      * @since         0.5.2 - 16.03.2007
  386.      */
  387.     public function createParagraphPropertiesElement({
  388.         /* 
  389.         //It should be:
  390.         $ret = new ParagraphProperties('style:paragraph-properties', NULL, self::STYLE );
  391.         $ret = $this->adoptNode($ret);
  392.         return $ret; 
  393.         //But we must use: _PHP IS A MESS_ */
  394.         return new ParagraphProperties($this->createStyleElement('paragraph-properties'));
  395.     }
  396.     /**
  397.      * Create an ODPElement as '<style:graphic-properties>'.
  398.      * 
  399.      * @return        ODPElement 
  400.      * @access         public
  401.      * @since         0.5.2 - 16.03.2007
  402.      */
  403.     public function createGraphicPropertiesElement({
  404.         /* 
  405.         //It should be:
  406.         $ret = new GraphicProperties('style:graphic-properties', NULL, self::STYLE );
  407.         $ret = $this->adoptNode($ret);
  408.         return $ret;
  409.         // Alternativly this should work too:
  410.         */
  411.         $this->registerNodeClass('DOMElement''GraphicProperties');
  412.         $ret $this->createStyleElement('graphic-properties');
  413.         $this->registerNodeClass('DOMElement'NULL);
  414.         /*
  415.         //But we must use: _PHP IS A MESS_         
  416.         $ret= new GraphicProperties($this->createStyleElement('graphic-properties'));
  417.         */
  418.         return $ret;
  419.     }
  420.     /**
  421.      * Create an ODPElement as '<style:default-style>'.
  422.      * 
  423.      * @return        ODPElement 
  424.      * @access         public
  425.      * @since         0.5.2 - 19.03.2007
  426.      */
  427.     public function createDefaultStyleElement({
  428.         /* 
  429.         //It should be:
  430.         $ret = new ParagraphProperties('style:paragraph-properties', NULL, self::STYLE );
  431.         $ret = $this->adoptNode($ret);
  432.         return $ret; 
  433.         //But we must use: _PHP IS A MESS_ */
  434.         return new DefaultStyle($this->createStyleElement('default-style'));
  435.     }
  436.     /**
  437.      * Create an ODPElement as '<style:style>'.
  438.      * 
  439.      * @return        ODPElement 
  440.      * @access         public
  441.      * @since         0.5.2 - 19.03.2007
  442.      */
  443.     public function createStyleStyleElement({
  444.         /* 
  445.         //It should be:
  446.         $ret = new ParagraphProperties('style:paragraph-properties', NULL, self::STYLE );
  447.         $ret = $this->adoptNode($ret);
  448.         return $ret; 
  449.         //But we must use: _PHP IS A MESS_ */
  450.         return new Style($this->createStyleElement('style'));
  451.     }
  452. }
  453. ?>

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