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

Source for file ManifestDocument.php

Documentation is available at ManifestDocument.php

  1. <?php
  2.  
  3. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  4.  
  5. /*
  6.  * Created on 29. Dec. 2006 by Norman Markgraf (nmarkgraf(at)user.sourceforge.net)
  7.  */
  8.  
  9. /**
  10.  * ManifestDocument class file.
  11.  * 
  12.  * This class extends DOMDocument to fit a manifest document as it is used
  13.  * by OpenDocumentArchives to store the meta information into it.
  14.  * 
  15.  * PHP versions 5
  16.  *   
  17.  * LICENSE:
  18.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19.  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20.  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21.  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22.  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23.  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24.  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25.  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26.  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28.  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29.  *
  30.  * This software consists of voluntary contributions made by many individuals
  31.  * and is licensed under the GPL. For more information please see
  32.  * <http://opendocumentphp.org>.
  33.  * 
  34.  * $Id: ManifestDocument.php 239 2007-07-24 16:25:10Z nmarkgraf $
  35.  * 
  36.  * @category    File Formats
  37.  * @package        OpenDocumentPHP
  38.  * @subpackage    manifest
  39.  * @author         Norman Markgraf (nmarkgraf(at)user.sourceforge.net)
  40.  * @copyright     Copyright in 2006, 2007 by The OpenDocumentPHP Team
  41.  * @license     http://www.gnu.org/licenses/gpl.html GNU General Public License 2.0.
  42.  * @version        SVN: $Id: ManifestDocument.php 239 2007-07-24 16:25:10Z nmarkgraf $
  43.  * @link           http://opendocumentphp.org
  44.  * @since         0.5.0 - 08. Feb. 2007
  45.  * @todo        We need to support encryption data.
  46.  * @todo        Also we need to support the optional size part.
  47.  */
  48.  
  49. /**
  50.  * 
  51.  */
  52. require_once 'OpenDocumentPHP/util/AbstractDocument.php';
  53.  
  54. /**
  55.  * ManifestDocument class
  56.  * 
  57.  * This class extends DOMDocument to fit a manifest document as it is used
  58.  * by OpenDocumentArchives to store the meta information into it.
  59.  * 
  60.  * Example:
  61.  * <code>
  62.  *      $manifest = new ManifestDocument( 'application/vnd.oasis.opendocument.text' );
  63.  *      $manifest->addFileEntry( 'Configurations2/',
  64.  *                                  'application/vnd.sun.xml.ui.configuration' );
  65.  *      $manifest->addFileEntry( 'content.xml', 'text/xml' );
  66.  * </code>
  67.  * This code will create a manifest DOM document as follows:
  68.  * <code>
  69.  * <?xml version="1.0" encoding="UTF-8"?>
  70.  * <!DOCTYPE manifest:manifest PUBLIC "-//OpenOffice.org//DTD Manifest 1.0//EN"
  71.  *                                    "Manifest.dtd">
  72.  *  <manifest:manifest
  73.  *            xmlns:manifest="urn:oasis:names:tc:opendocument:xmlns:manifest:1.0">
  74.  *    <manifest:file-entry
  75.  *            manifest:media-type="application/vnd.oasis.opendocument.text"
  76.  *            manifest:full-path="/"/>
  77.  *    <manifest:file-entry
  78.  *           manifest:media-type="application/vnd.sun.xml.ui.configuration"
  79.  *           manifest:full-path="Configurations2/"/>
  80.  *    <manifest:file-entry
  81.  *           manifest:media-type="text/xml"
  82.  *           manifest:full-path="content.xml"/>
  83.  *  </manifest:manifest>
  84.  * </code>
  85.  * 
  86.  * @category    File Formats
  87.  * @package        OpenDocumentPHP
  88.  * @subpackage    manifest
  89.  * @author         Norman Markgraf (nmarkgraf(at)user.sourceforge.net)
  90.  * @copyright     Copyright in 2006, 2007 by The OpenDocumentPHP Team
  91.  * @license     http://www.gnu.org/licenses/gpl.html GNU General Public License 2.0.
  92.  * @version     Release: @package_version@
  93.  * @link           http://opendocumentphp.org
  94.  * @since         0.5.0 - 08. Feb. 2007
  95.  * @todo        We need to support encryption data.
  96.  * @todo        Also we need to support the optional size part.
  97.  */
  98.     /** 
  99.      * Doctype information for Manifest DOM documents
  100.      */
  101.     const odmDOCTYPE = '<!DOCTYPE manifest:manifest PUBLIC "-//OpenOffice.org//DTD Manifest 1.0//EN" "Manifest.dtd">';
  102.     /**
  103.      * Root Tag for Mainifest DOM documents
  104.      */
  105.     const odmROOT = 'manifest:manifest';
  106.     /**
  107.      * Link to the DOMElement which contains the mime type.
  108.      * 
  109.      * @var DOMElement 
  110.      * @access private
  111.      */
  112.     private $mimetype;
  113.     /**
  114.      * Constructor
  115.      * 
  116.      * @param         string mimetype Mime type of the archive
  117.      * @param         string encoding Encoding of the manifest file. The default is 'UTF-8'.
  118.      * @access        public
  119.      * @since         0.5.0 - 08. Feb. 2007
  120.      */
  121.     function __construct($mimetype ''$encoding 'UTF-8'{
  122.         /*
  123.          * We can not use the normal way to produce a DOM document, because
  124.          * Manifest documents needs other DocType informations. So we have to go this
  125.          * way and produce a DOM document 'by hand'.
  126.          */ 
  127.         parent :: __construct();
  128.         /*
  129.          * We must use this way to ensure that we have the right encoding and doc type.
  130.          */
  131.         $this->loadXML('<?xml version="1.0" encoding="' $encoding '"?>' self :: odmDOCTYPE "\n" '<manifest/>');
  132.         /*
  133.          * But we must change the root element, so we first remove it ...
  134.          */
  135.         $this->removeChild($this->getElementsByTagName('manifest')->item(0));
  136.         /*
  137.          * ...and now we create a new one.
  138.          * This is a hack! Please do not use createManifestElement here!
  139.          */
  140.         $this->root = $this->createElementNS(self :: MANIFESTself :: odmROOT);
  141.         /*
  142.          * and append it to the document as new root.
  143.          */
  144.         $this->appendChild($this->root);
  145.         /*
  146.          * Add root informations. This line is part of every Manifest document.
  147.          */
  148.         $this->mimetype $this->makeFileEntryElement('/'$mimetype);
  149.         $this->root->appendChild($this->mimetype);
  150.     }
  151.     protected function _setRoot($documentRoot 0{
  152.     }
  153.     /**
  154.      * Make a path string unix-aware.
  155.      * 
  156.      * @return         string A unix-aware full path.
  157.      * @param        string fullpath Normal full path.
  158.      * @access        private
  159.      * @since         0.5.0 - 08. Feb. 2007
  160.      */
  161.     private function fp($fullpath{
  162.         return str_replace("\\"'/'$fullpath);
  163.     }
  164.     /**
  165.      * Creates a DOM element with the tag 'manifest:file-enty' and
  166.      * adds 'manifest:full-path', and 'manifest:media-type' attributes to it.
  167.      *  
  168.      * @return         DOMElement A DOM element which containt a single file entry.
  169.      * @param         string fullpath The full path of the file that has been added.
  170.      * @param         string mimetype The mime type of this file
  171.      * @param         int size The size of this file. (default: not used)
  172.      * @access        private
  173.      * @since         0.5.0 - 08. Feb. 2007
  174.      */
  175.     private function makeFileEntryElement($fullpath$mimetype$size = -1{
  176.         /*
  177.          * Create a new file-entry element ...
  178.          */
  179.         $node $this->createManifestElement('file-entry');
  180.         /*
  181.          * ... add the fullpath of the file to full-path attribute, ...
  182.          */
  183.         $node->setManifestAttribute('full-path'$this->fp($fullpath));
  184.         /*
  185.          * ... add mime type of the file into media-type attribute.
  186.          */
  187.         $node->setManifestAttribute('media-type'$mimetype);
  188.         /*
  189.          *  If a size attribute is needed, add some ...
  190.          */
  191.         if ($size >= 0{
  192.             /*
  193.              * ... to the size attribute of the file-entry element.
  194.              */
  195.             $node->setManifestAttribute('size'$size);
  196.         }
  197.         // *** FIX ME *** - We need to handle encrypted Entrys too
  198.         return $node;
  199.     }
  200.     /**
  201.      * Add a full path to the file entry list in this manifest document.
  202.      * 
  203.      * Example:
  204.      * <code>
  205.      *     $manifest = new ManifestDocument();
  206.      *     ...
  207.      *     $manifest->addFileEntry('test/feature.xml', 'text/xml');
  208.      *     ...
  209.      * </code>
  210.      * 
  211.      * will add
  212.      * 
  213.      * <code>
  214.      *   <manifest-file-entry manifest:full-path="test/feature.xml"
  215.      *                           manifest:media-type="text/xml" />
  216.      * </code>
  217.      * 
  218.      * to the manifest DOM document.
  219.      * 
  220.      * @param         string fullpath The full path of the file that has been added.
  221.      * @param         string mimetype The mime type of this file
  222.      * @param         int size The size of this file.
  223.      * @access        public
  224.      * @since         0.5.0 - 08. Feb. 2007
  225.      */
  226.     function addFileEntry($fullpath$mimetype$size = -1{
  227.         $node $this->makeFileEntryElement($fullpath$mimetype$size);
  228.         // Append new file-entry node to root node        
  229.         $this->root->appendChild($node);
  230.     }
  231.     /**
  232.      * Remove a full path from this manifest document.
  233.      * 
  234.      * @param         string fullpath File to remove (given as a full path).
  235.      * @access        public
  236.      * @since         0.5.0 - 08. Feb. 2007
  237.      */
  238.     function removeFileEntry($fullpath{
  239.         $ret false;
  240.         $element $this->getFileEntry($fullpath);
  241.         if ($element !== false{
  242.             $this->root->removeChild($element);
  243.             $ret true;
  244.         }
  245.         return $ret;
  246.     }
  247.     /**
  248.      * Get File Entry
  249.      * 
  250.      * @access        public
  251.      * @since         0.5.0 - 08. Feb. 2007
  252.      */
  253.     function getFileEntry($fullpath{
  254.         $ret false;
  255.         $search $this->fp($fullpath);
  256.         foreach ($this->getElementsByTagNameNS(self :: MANIFEST'file-entry'as $element{
  257.             if ($element->getAttributeNS(self :: MANIFEST'full-path'=== $search{
  258.                 $ret $element;
  259.                 break;
  260.             }
  261.         }
  262.         return $ret;
  263.     }
  264.     /**
  265.      * 
  266.      * 
  267.      * @access        public
  268.      * @since         0.5.0 - 08. Feb. 2007
  269.      */
  270.     function getFilelist({
  271.         $ret array ();
  272.         foreach ($this->getElementsByTagNameNS(self :: MANIFEST'file-entry'as $element{
  273.             $tmp array (
  274.                 'name' => $element->getAttributeNS(self :: MANIFEST,
  275.                 'full-path'
  276.             )'media-type' => $element->getAttributeNS(self :: MANIFEST'media-type'));
  277.             if ($element->hasAttributeNS(self :: MANIFEST'size'=== true{
  278.                 $tmp['manifest_size'$element->getAttributeNS(self :: MANIFEST'size');
  279.             }
  280.             $ret[$tmp;
  281.         }
  282.         return $ret;
  283.     }
  284.     /**
  285.      * Rename a file name entry in this manifest document.
  286.      * 
  287.      * @param         string fullpath Old file name (as a full path).
  288.      * @param         string newfullpath New file name (as a full path).
  289.      * @access        public
  290.      * @since         0.5.0 - 08. Feb. 2007
  291.      */
  292.     function renameFileEntry($fullpath$newfullpath{
  293.         $ret false;
  294.         if ($fullpath != $newfullpath{
  295.             $element $this->getFileEntry($fullpath);
  296.             if ($element !== false{
  297.                 $element->removeAttributeNS(self :: MANIFEST'full-path');
  298.                 $element->setAttributeNS(self :: MANIFEST'manifest:full-path'$this->fp($newfullpath));
  299.                 $ret true;
  300.             }
  301.         }
  302.         return $ret;
  303.     }
  304.     /**
  305.      * Set mime type of the archive.
  306.      * 
  307.      * @param         string mimetype New mime type of this archive.
  308.      * @access        public
  309.      * @since         0.5.0 - 08. Feb. 2007
  310.      */
  311.     function setMimeType($mimetype{
  312.         /*
  313.          * First we remove the old media-type attribute ...
  314.          */
  315.         $this->mimetype->removeAttributeNS(self :: MANIFEST'media-type');
  316.         /*
  317.          * ... and now we can add the new mimetype to the media-type attribute.
  318.          */
  319.         $this->mimetype->setAttributeNS(self :: MANIFEST'media-type'$mimetype);
  320.     }
  321.     /**
  322.      * Returns mime type of the archive.
  323.      * 
  324.      * @return         string Mime type of this archive
  325.      * @access        public
  326.      * @since         0.5.0 - 08. Feb. 2007
  327.      */
  328.     function getMimeType({
  329.         return $this->mimetype->getAttributeNS(self :: MANIFEST'media-type');
  330.     }
  331.     /**
  332.      * Reads MimeType out of the current DOM document.
  333.      * 
  334.      * @access        private
  335.      * @since         0.5.0 - 08. Feb. 2007
  336.      */
  337.     private function readMimeType({
  338.         $this->mimetype $this->getFileEntry('/');
  339.     }
  340.     /**
  341.      * Setup root of the DOM document.
  342.      * 
  343.      * @access        private
  344.      * @since         0.5.0 - 08. Feb. 2007
  345.      */
  346.     private function setupRoot({
  347.         $this->root = $this->getElementsByTagNameNS(self :: MANIFEST'manifest')->item(0);
  348.     }
  349.     /**
  350.      * Loads an XML document from a string.
  351.      * This method may also be called statically to load and create a
  352.      * <i>ManifestDocument</i> object. The static invocation may be used when no
  353.      * <i>ManifestDocument</i> properties need to be set prior to loading.
  354.      * 
  355.      * @param         string source The string containing the XML.
  356.      * @return         boolean 
  357.      * @access        public
  358.      * @since         0.5.0 - 08. Feb. 2007
  359.      */
  360.     function loadXML($source$options 0{
  361.         $ret parent :: loadXML($source$options);
  362.         $this->readMimeType();
  363.         $this->setupRoot();
  364.         return $ret;
  365.     }
  366.     /**
  367.      * Loads an XML document from a file.
  368.      * 
  369.      * This method may also be called statically to load and create a
  370.      * <i>DOMDocument</i> object. The static invocation may be used when no
  371.      * <i>ManifestDocument</i> properties need to be set prior to loading.
  372.      *  
  373.      * @param         string filename The path to the XML document.
  374.      * @param         int options
  375.      * @access        public
  376.      * @since         0.5.0 - 08. Feb. 2007
  377.      */
  378.     function load($filename$options 0{
  379.         $ret parent :: load($filename$options);
  380.         $this->readMimeType();
  381.         $this->setupRoot();
  382.         return $ret;
  383.     }
  384. }
  385. ?>

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