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

Source for file AbstractDocument.php

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

Documentation generated on Sun, 20 May 2012 06:27:01 +0200 by phpDocumentor 1.3.2