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

Source for file ManifestDocument.php

Documentation is available at ManifestDocument.php

  1. <?php
  2. /*
  3.  * Created on 29.12.2006 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: ManifestDocument.php 182 2007-06-11 13:32:34Z nmarkgraf $
  22.  */
  23. require_once 'OpenDocumentPHP/util/AbstractDocument.php';
  24. /**
  25.  * ManifestDocument class
  26.  * 
  27.  * This class extends DOMDocument to fit a manifest document as it is used
  28.  * by OpenDocumentArchives to store the meta information into it.
  29.  * 
  30.  * Example:
  31.  * <code>
  32.  * $manifest = new ManifestDocument( 'application/vnd.oasis.opendocument.text' );
  33.  * $manifest->addFileEntry( 'Configurations2/',
  34.  *                             'application/vnd.sun.xml.ui.configuration' );
  35.  * $manifest->addFileEntry( 'content.xml', 'text/xml' );
  36.  * </code>
  37.  * This code will create a manifest DOM document as follows:
  38.  * <code>
  39.  * <?xml version="1.0" encoding="UTF-8"?>
  40.  * <!DOCTYPE manifest:manifest PUBLIC "-//OpenOffice.org//DTD Manifest 1.0//EN"
  41.  *                                    "Manifest.dtd">
  42.  *  <manifest:manifest
  43.  *            xmlns:manifest="urn:oasis:names:tc:opendocument:xmlns:manifest:1.0">
  44.  *    <manifest:file-entry
  45.  *            manifest:media-type="application/vnd.oasis.opendocument.text"
  46.  *            manifest:full-path="/"/>
  47.  *    <manifest:file-entry
  48.  *           manifest:media-type="application/vnd.sun.xml.ui.configuration"
  49.  *           manifest:full-path="Configurations2/"/>
  50.  *    <manifest:file-entry
  51.  *           manifest:media-type="text/xml"
  52.  *           manifest:full-path="content.xml"/>
  53.  *  </manifest:manifest>
  54.  * </code>
  55.  * 
  56.  * $Id: ManifestDocument.php 182 2007-06-11 13:32:34Z nmarkgraf $
  57.  * 
  58.  * @author         Norman Markgraf (nmarkgraf(at)user.sourceforge.net)
  59.  * @copyright     Copyright in 2006, 2007 by The OpenDocumentPHP Team
  60.  * @license     http://www.gnu.org/licenses/gpl.html GNU General Public License 2.0.
  61.  * @version        $Revision: 182 $
  62.  * @package        OpenDocumentPHP
  63.  * @since         0.5.0 - 08.02.2007
  64.  * @subpackage    manifest
  65.  * @todo        We need to support encryption data.
  66.  * @todo        Also we need to support the optional size part.
  67.  */
  68.     /** 
  69.      * Doctype information for Manifest DOM documents
  70.      */
  71.     const odmDOCTYPE = '<!DOCTYPE manifest:manifest PUBLIC "-//OpenOffice.org//DTD Manifest 1.0//EN" "Manifest.dtd">';
  72.     /**
  73.      * Root Tag for Mainifest DOM documents
  74.      */
  75.     const odmROOT = 'manifest:manifest';
  76.     /**
  77.      * Link to the DOMElement which contains the mime type.
  78.      * 
  79.      * @var DOMElement 
  80.      * @access private
  81.      */
  82.     private $mimetype;
  83.     /**
  84.      * Constructor
  85.      * 
  86.      * @param         string mimetype Mime type of the archive
  87.      * @param         string encoding Encoding of the manifest file. The default
  88.      *                         is 'UTF-8'.
  89.      * @access        public
  90.      * @since         0.5.0 - 08.02.2007
  91.      */
  92.     function __construct($mimetype ''$encoding 'UTF-8'{
  93.         // We can not use the normal way to produce a DOM document, because 
  94.         // Manifest documents needs other DocType informtions. So we have to go this
  95.         // way and produce a DOM document 'by hand'.
  96.         parent :: __construct();
  97.         $this->loadXML('<?xml version="1.0" encoding="' $encoding '"?>' self :: odmDOCTYPE "\n" '<manifest/>');
  98.         $this->removeChild($this->getElementsByTagName('manifest')->item(0));
  99.         // This is a hack! Please do not use createManifestElement here!
  100.         $this->root = $this->createElementNS(self :: MANIFESTself :: odmROOT);
  101.         $this->appendChild($this->root);
  102.         // Add root informations. This line is part of every Manifest document.
  103.         $this->mimetype $this->makeFileEntryElement('/'$mimetype);
  104.         $this->root->appendChild($this->mimetype);
  105.     }
  106.     protected function _setRoot($documentRoot 0{
  107.     }
  108.     /**
  109.      * Make a path string unix-aware.
  110.      * 
  111.      * @return         string A unix-aware full path.
  112.      * @param        string fullpath Normal full path.
  113.      * @access        private
  114.      * @since         0.5.0 - 08.02.2007
  115.      */
  116.     private function fp($fullpath{
  117.         return str_replace("\\"'/'$fullpath);
  118.     }
  119.     /**
  120.      * Creates a DOM element with the tag 'manifest:file-enty' and
  121.      * adds 'manifest:full-path', and 'manifest:media-type' attributes to it.
  122.      *  
  123.      * @return         DOMElement A DOM element which containt a single file entry.
  124.      * @param         string fullpath The full path of the file that has been added.
  125.      * @param         string mimetype The mime type of this file
  126.      * @param         int size The size of this file. (default: not used)
  127.      * @access        private
  128.      * @since         0.5.0 - 08.02.2007
  129.      */
  130.     private function makeFileEntryElement($fullpath$mimetype$size = -1{
  131.         $node $this->createManifestElement('file-entry');
  132.         // remove this line!$node->setAttributeNS(self :: MANIFEST, 'manifest:full-path', $this->fp($fullpath));
  133.         $node->setManifestAttribute('full-path'$this->fp($fullpath));
  134.         // remove this line!$node->setAttributeNS(self :: MANIFEST, 'manifest:media-type', $mimetype);
  135.         $node->setManifestAttribute('media-type'$mimetype);
  136.         // If a sze attribute is needed, add some
  137.         if ($size >= 0{
  138.             //remove this line!$node->setAttributeNS(self :: MANIFEST, 'manifest:size', $size);
  139.             $node->setManifestAttribute('size'$size);
  140.         }
  141.         // *** FIX ME *** - We need to handle encrypted Entrys too
  142.         return $node;
  143.     }
  144.     /**
  145.      * Add a full path to the file entry list in this manifest document.
  146.      * 
  147.      * Example:
  148.      * <code>
  149.      * $manifest = new ManifestDocument();
  150.      * ...
  151.      * $manifest->addFileEntry('test/feature.xml', 'text/xml');
  152.      * ...
  153.      * </code>
  154.      * 
  155.      * will add
  156.      * 
  157.      * <code>
  158.      *   <manifest-file-entry manifest:full-path="test/feature.xml"
  159.      *                           manifest:media-type="text/xml" />
  160.      * </code>
  161.      * 
  162.      * to the manifest DOM document.
  163.      * 
  164.      * @param         string fullpath The full path of the file that has been added.
  165.      * @param         string mimetype The mime type of this file
  166.      * @param         int size The size of this file.
  167.      * @access        public
  168.      * @since         0.5.0 - 08.02.2007
  169.      */
  170.     function addFileEntry($fullpath$mimetype$size = -1{
  171.         $node $this->makeFileEntryElement($fullpath$mimetype$size);
  172.         // Append new file-entry node to root node        
  173.         $this->root->appendChild($node);
  174.     }
  175.     /**
  176.      * Remove a full path from this manifest document.
  177.      * 
  178.      * @param         string fullpath File to remove (given as a full path).
  179.      * @access        public
  180.      * @since         0.5.0 - 08.02.2007
  181.      */
  182.     function removeFileEntry($fullpath{
  183.         $ret false;
  184.         $element $this->getFileEntry($fullpath);
  185.         if ($element !== false{
  186.             $this->root->removeChild($element);
  187.             $ret true;
  188.         }
  189.         return $ret;
  190.     }
  191.     /**
  192.      * Get File Entry
  193.      * 
  194.      * @access        public
  195.      * @since         0.5.0 - 08.02.2007
  196.      */
  197.     function getFileEntry($fullpath{
  198.         $ret false;
  199.         $search $this->fp($fullpath);
  200.         foreach ($this->getElementsByTagNameNS(self :: MANIFEST'file-entry'as $element{
  201.             if ($element->getAttributeNS(self :: MANIFEST'full-path'=== $search{
  202.                 $ret $element;
  203.                 break;
  204.             }
  205.         }
  206.         return $ret;
  207.     }
  208.     /**
  209.      * 
  210.      * 
  211.      * @access        public
  212.      * @since         0.5.0 - 08.02.2007
  213.      */
  214.     function getFilelist({
  215.         $ret array ();
  216.         foreach ($this->getElementsByTagNameNS(self :: MANIFEST'file-entry'as $element{
  217.             $tmp array (
  218.                 'name' => $element->getAttributeNS(self :: MANIFEST,
  219.                 'full-path'
  220.             )'media-type' => $element->getAttributeNS(self :: MANIFEST'media-type'));
  221.             if ($element->hasAttributeNS(self :: MANIFEST'size'=== true{
  222.                 $tmp['manifest_size'$element->getAttributeNS(self :: MANIFEST'size');
  223.             }
  224.             $ret[$tmp;
  225.         }
  226.         return $ret;
  227.     }
  228.     /**
  229.      * Rename a file name entry in this manifest document.
  230.      * 
  231.      * @param         string fullpath Old file name (as a full path).
  232.      * @param         string newfullpath New file name (as a full path).
  233.      * @access        public
  234.      * @since         0.5.0 - 08.02.2007
  235.      */
  236.     function renameFileEntry($fullpath$newfullpath{
  237.         $ret false;
  238.         if ($fullpath != $newfullpath{
  239.             $element $this->getFileEntry($fullpath);
  240.             if ($element !== false{
  241.                 $element->removeAttributeNS(self :: MANIFEST'full-path');
  242.                 $element->setAttributeNS(self :: MANIFEST'manifest:full-path'$this->fp($newfullpath));
  243.                 $ret true;
  244.             }
  245.         }
  246.         return $ret;
  247.     }
  248.     /**
  249.      * Set mime type of the archive.
  250.      * 
  251.      * @param         string mimetype New mime type of this archive.
  252.      * @access        public
  253.      * @since         0.5.0 - 08.02.2007
  254.      */
  255.     function setMimeType($mimetype{
  256.         $this->mimetype->removeAttributeNS(self :: MANIFEST'media-type');
  257.         $this->mimetype->setAttributeNS(self :: MANIFEST'media-type'$mimetype);
  258.     }
  259.     /**
  260.      * Returns mime type of the archive.
  261.      * 
  262.      * @return         string Mime type of this archive
  263.      * @access        public
  264.      * @since         0.5.0 - 08.02.2007
  265.      */
  266.     function getMimeType({
  267.         return $this->mimetype->getAttributeNS(self :: MANIFEST'media-type');
  268.     }
  269.     /**
  270.      * Reads MimeType out of the current DOM document.
  271.      * 
  272.      * @access        private
  273.      * @since         0.5.0 - 08.02.2007
  274.      */
  275.     private function readMimeType({
  276.         $this->mimetype $this->getFileEntry('/');
  277.     }
  278.     /**
  279.      * Setup root of the DOM document.
  280.      * 
  281.      * @access        private
  282.      * @since         0.5.0 - 08.02.2007
  283.      */
  284.     private function setupRoot({
  285.         $this->root = $this->getElementsByTagNameNS(self :: MANIFEST'manifest')->item(0);
  286.     }
  287.     /**
  288.      * Loads an XML document from a string.
  289.      * This method may also be called statically to load and create a
  290.      * ManifestDocument object. The static invocation may be used when no
  291.      * ManifestDocument properties need to be set prior to loading.
  292.      * 
  293.      * @param         string source The string containing the XML.
  294.      * @return         boolean 
  295.      * @access        public
  296.      * @since         0.5.0 - 08.02.2007
  297.      */
  298.     function loadXML($source$options 0{
  299.         $ret parent :: loadXML($source$options);
  300.         $this->readMimeType();
  301.         $this->setupRoot();
  302.         return $ret;
  303.     }
  304.     /**
  305.      * Loads an XML document from a file.
  306.      * 
  307.      * This method may also be called statically to load and create a
  308.      * DOMDocument object. The static invocation may be used when no
  309.      * ManifestDocument properties need to be set prior to loading.
  310.      *  
  311.      * @param         string filename The path to the XML document.
  312.      * @param         int options
  313.      * @access        public
  314.      * @since         0.5.0 - 08.02.2007
  315.      */
  316.     function load($filename$options 0{
  317.         $ret parent :: load($filename$options);
  318.         $this->readMimeType();
  319.         $this->setupRoot();
  320.         return $ret;
  321.     }
  322. }
  323. ?>

Documentation generated on Tue, 12 Jun 2007 10:00:04 +0200 by phpDocumentor 1.3.2