Browse Source

Converted the OSInet ODF Calc reader classes to PSR0

- classes are now namespace.
- added a demo in apps/odf_calc_reader.
- added associated cleanup in Makefile.
Frederic G. MARAND 9 years ago
parent
commit
6a058cd843

+ 1 - 0
Makefile

@@ -1,2 +1,3 @@
 clean:
 	find . \( -name "*.gv" -o -name "*.svg" -o -name "*.png" -o -name php_errors.log \) -exec rm {} \;
+	rm -fr apps/odf_calc_reader/odf_sample

+ 29 - 42
ooo/Calc_Cell.php → OSInet/Open_Document/Calc/Cell.php

@@ -2,20 +2,20 @@
 /**
  * Wrap an OpenDocument spreadsheet cell
  *
- * Requires PHP 5.2
+ * Requires PHP 5.3
  *
- * @version $Id: Calc_Cell.php,v 1.3 2007-09-09 19:50:22 marand Exp $
  * @license CeCILL 2.0
- * @copyright 2005-2006 Ouest Systemes Informatiques
+ * @copyright 2005-2012 Ouest Systemes Informatiques
  */
 
+namespace OSInet\Open_Document\Calc;
+
+use OSInet\Open_Document\Calc\Reader;
+
 /**
  * OpenDocument Spreadsheet cell
  */
-$erCalc_Cell = error_reporting(E_ALL | E_STRICT);
-
-class Calc_Cell
-  {
+class Cell {
   /**
    * Values of the 'office:value-type' attribute
    */
@@ -28,22 +28,18 @@ class Calc_Cell
   public $displayValue;
 
   /**
-   * @param DOMNode $cellNode
+   * @param \DOMNode $cellNode
    */
-  function __construct(DOMNode $cellNode)
-    {
-    if ($cellNode instanceof DOMElement)
-      {
-      $this->dataType  = $cellNode->getAttributeNS(Calc_Reader::NS_OFFICE, 'value-type');
+  function __construct(\DOMNode $cellNode) {
+    if ($cellNode instanceof \DOMElement) {
+      $this->dataType  = $cellNode->getAttributeNS(Reader::NS_OFFICE, 'value-type');
 
-      if ($cellNode->childNodes->length == 0)
-        {
-        $this->dataType  = Calc_Cell::TYPE_EMPTY;
+      if ($cellNode->childNodes->length == 0) {
+        $this->dataType  = self::TYPE_EMPTY;
         $this->dataValue = NULL;
         $this->displayValue = '';
-        }
-      else
-        {
+      }
+      else {
         $p = $cellNode->childNodes->item(0);
         $this->displayValue = $p->textContent;
         unset($p);
@@ -51,52 +47,47 @@ class Calc_Cell
         /**
          * Note: cells with office:value-type==string don't have a office:value
          */
-        if ($this->dataType == Calc_Cell::TYPE_STRING)
-          {
+        if ($this->dataType == self::TYPE_STRING) {
           //$this->dataValue = $this->displayValue;
-          }
-        else
-          {
-          $this->dataValue = $cellNode->getAttributeNS(Calc_Reader::NS_OFFICE, 'value');
-          }
+        }
+        else {
+          $this->dataValue = $cellNode->getAttributeNS(Reader::NS_OFFICE, 'value');
         }
       }
+    }
     /* echo "children: " . $cellNode->childNodes->length
      . ", type = "    . $this->dataType
      . ", value = "   . substr($this->dataValue, 0, 20)
      . ", display = <" . substr($this->displayValue, 0, 20)
      . ">" . PHP_EOL; */
-
-    }
+  }
 
   /**
    * Convert a PHP value to a wrapped OpenDocument cell value
    *
    * @param mixed $x
    */
-  function set($x = NULL)
-    {
+  function set($x = NULL) {
     $type = gettype($x);
-    switch ($type)
-      {
+    switch ($type) {
       case 'NULL':
-        $this->dataType = Calc_Cell::TYPE_EMPTY;
+        $this->dataType = self::TYPE_EMPTY;
         $this->dataValue = NULL;
         $this->displayValue = NULL;
         break;
       case 'string':
-        $this->dataType = Calc_Cell::TYPE_STRING;
+        $this->dataType = self::TYPE_STRING;
         $this->dataValue = NULL;
         $this->displayValue = $x;
         break;
       case 'double':
-        $this->dataType = Calc_Cell::TYPE_FLOAT;
+        $this->dataType = self::TYPE_FLOAT;
         $this->dataValue = $x;
         $this->displayValue = number_format($x);
         break;
       default:
         $this->set();
-        throw new Exception('Calc_Cell::set unsupported value type ', $type);
+        throw new \Exception('Cell::set unsupported value type ', $type);
         break;
       }
     }
@@ -106,11 +97,7 @@ class Calc_Cell
    *
    * @return string
    */
-  function __toString()
-    {
+  function __toString() {
     return (string) $this->displayValue;
-    }
   }
-
-error_reporting($erCalc_Cell);
-unset($erCalc_Cell);
+}

+ 26 - 33
ooo/Calc_Reader.php → OSInet/Open_Document/Calc/Reader.php

@@ -2,20 +2,19 @@
 /**
  * Utility class to access OpenDocument spreadsheets read-only
  *
- * Requires PHP 5.2
+ * Requires PHP 5.3
  *
- * @version $Id: Calc_Reader.php,v 1.4 2007-09-02 20:50:12 marand Exp $
  * @license CeCILL 2.0
- * @copyright 2005-2006 Ouest Systemes Informatiques
+ * @copyright 2005-2012 Ouest Systemes Informatiques
  */
 
-$erCalc_Reader = error_reporting(E_ALL | E_STRICT);
+namespace OSInet\Open_Document\Calc;
+
 
 /**
  * Utility class to access OpenDocument spreadsheets read-only
  */
-class Calc_Reader
-  {
+class Reader {
   const NS_TABLE  = "urn:oasis:names:tc:opendocument:xmlns:table:1.0";
   const NS_OFFICE = "urn:oasis:names:tc:opendocument:xmlns:office:1.0";
 
@@ -29,59 +28,56 @@ class Calc_Reader
    */
   public $xpath;
 
-  function __construct($path)
-    {
-    $this->xml = new DOMDocument();
+  function __construct($path) {
+    $this->xml = new \DOMDocument();
     $this->xml->preserveWhiteSpace = FALSE;
     $this->xml->load($path);
 
-    $this->xpath = new DOMXPath($this->xml);
-    $this->xpath->registerNameSpace('office', Calc_Reader::NS_OFFICE);
-    $this->xpath->registerNameSpace('table', Calc_Reader::NS_TABLE);
-    }
+    $this->xpath = new \DOMXPath($this->xml);
+    $this->xpath->registerNameSpace('office', self::NS_OFFICE);
+    $this->xpath->registerNameSpace('table', self::NS_TABLE);
+  }
 
   /**
    * Return a DOMNodeList of the worksheets available in the document
    *
-   * @return DOMNodeList
+   * @return \DOMNodeList
    */
-  function getCalcSheets()
-    {
+  function getCalcSheets() {
     $sq = '//table:table';
     $list = $this->xpath->query($sq);
     unset($sq);
 
     return $list;
-    }
+  }
 
   /**
    * Return the names of the worksheets available in the document
    *
    * @return array
    */
-  function getCalcSheetNames()
-    {
+  function getCalcSheetNames() {
     $dnlSheets = $this->getCalcSheets();
 
     $ret = array();
-    foreach ($dnlSheets as $node)
-      {
-      $ret[] = $node->getAttributeNS(Calc_Reader::NS_TABLE, 'name');
-      }
+    foreach ($dnlSheets as $node) {
+      $ret[] = $node->getAttributeNS(self::NS_TABLE, 'name');
+    }
 
     unset($dnlSheets);
     return $ret;
-    }
+  }
 
   /**
-   * Return the rows in the spreadsheet as a DOMNodeList
+   * Return the rows in the spreadsheet as a DOMNodeList.
+   *
    * WARNING: this means ALL the rows, in all the sheets, not just the first sheet
    *
-   * @param string $namedRange Optional name of range, notably Sheet name
-   * @return DOMNodeList
+   * @param string $namedRange
+   *   Optional name of range, notably Sheet name
+   * @return \DOMNodeList
    */
-  function getRows ($namedRange = NULL)
-    {
+  function getRows ($namedRange = NULL) {
     $query = empty($namedRange)
       ? '//table:table-row'
       : '//table:table[@table:name="' . $namedRange . '"]/table:table-row';
@@ -90,8 +86,5 @@ class Calc_Reader
     unset($query);
 
     return $ret;
-    }
   }
-
-error_reporting($erCalc_Reader);
-unset ($erCalc_Reader);
+}

+ 60 - 0
OSInet/Open_Document/Calc/Row.php

@@ -0,0 +1,60 @@
+<?php
+/**
+ * Wrap an OpenDocument spreadsheet row
+ *
+ * Requires PHP 5.3
+ *
+ * @license CeCILL 2.0
+ * @copyright 2005-2012 Ouest Systemes Informatiques
+ */
+
+namespace OSInet\Open_Document\Calc;
+
+class Row {
+  /**
+   * @var DOMElement $row
+   */
+  protected $row;
+
+  /**
+   * Analyze a spreadsheet row and return the cells as an array of Calc_Cell
+   *
+   * @return array
+   */
+  function getCells () {
+    $cellNodeList = $this->row->childNodes;
+    $cells = array();
+
+    $iCell = 0;
+    for ($icol = 0 ; $icol < $cellNodeList->length ; $icol++) {
+      $cellElement = $cellNodeList->item($icol);
+      $count = NULL;
+      if (is_object($cellElement)) {
+        $count = $cellElement->getAttributeNS(Reader::NS_TABLE, 'number-columns-repeated');
+      }
+      if (empty($count)) {
+        $count = 1;
+      }
+      for ($j = 0 ; $j < $count  ; $j++) {
+        $cells[$iCell++] = new Cell($cellElement);
+      }
+    }
+
+    return $cells;
+  }
+
+  /**
+   * Construct a Row from the DOMElement representing it.
+   *
+   * This DOMElement must bear a table:table-row tag
+   *
+   * @param \DOMNodeList $row
+   * @return void
+   */
+  function __construct(\DOMElement $row) {
+    if ((!($row instanceof \DOMElement)) || ($row->tagName != 'table:table-row')) {
+      die('Row::__construct() needs a DOMElement parameter for a table-row element ');
+    }
+    $this->row = $row;
+  }
+}

+ 41 - 0
apps/odf_calc_reader/odf_calc_reader.php

@@ -0,0 +1,41 @@
+<?php
+/**
+ * This code uses several external classes:
+ *
+ * - PSR0-compliant
+ *   - OSInet Open_Document package.
+ * - PHP 5.3
+ */
+
+use \OSInet\Open_Document\Calc\Reader as ODFReader;
+use \OSInet\Open_Document\Calc\Row as ODFRow;
+use \OSInet\Open_Document\Calc\Cell;
+
+error_reporting(-1);
+
+// Include the OSInet PSR0 sample autoloader.
+require __DIR__ .'/../../misc/psr0.php';
+
+// Initialize PSR0 autoloader for remaining classes.
+spl_autoload_register('psr0_autoload', TRUE);
+
+// Use the path of "content.xml" in an unpacked ODF spreadsheet.
+// Suggested: use the supplied odf_sample.odf, unpacked
+$path = $argv[1];
+
+$reader = new ODFReader($path);
+echo "This file contains the following sheets:\n";
+$sheetNames = $reader->getCalcSheetNames();
+print_r($sheetNames);
+echo PHP_EOL;
+
+$rows = $reader->getRows();
+foreach ($rows as $rowNum => $nodeList) {
+  $row = new ODFRow($nodeList);
+  printf("%3d", $rowNum);
+  $cells = $row->getCells();
+  for ($i = 0 ; $i < count($cells) ; $i++) {
+    printf('|%11s', $cells[$i]->displayValue);
+  }
+  echo PHP_EOL;
+}

BIN
apps/odf_calc_reader/odf_sample.ods


+ 0 - 72
ooo/Calc_Row.php

@@ -1,72 +0,0 @@
-<?php
-/**
- * Wrap an OpenDocument spreadsheet row
- *
- * Requires PHP 5.2
- *
- * @version $Id: Calc_Row.php,v 1.4 2007-09-09 19:49:24 marand Exp $
- * @license CeCILL 2.0
- * @copyright 2005-2006 Ouest Systemes Informatiques
- */
-
-$erCalc_Row = error_reporting(E_ALL | E_STRICT);
-
-class Calc_Row
-  {
-  /**
-   * @var DOMElement $row
-   */
-  protected $row;
-
-  /**
-   * Analyze a spreadsheet row and return the cells as an array of Calc_Cell
-   *
-   * @return array
-   */
-  function getCells ()
-    {
-    $cellNodeList = $this->row->childNodes;
-    $cells = array();
-
-    $iCell = 0;
-    for ($icol = 0 ; $icol < $cellNodeList->length ; $icol++)
-      {
-      $cellElement = $cellNodeList->item($icol);
-      $count = NULL;
-      if (is_object($cellElement))
-        {
-        $count = $cellElement->getAttributeNS(Calc_Reader::NS_TABLE, 'number-columns-repeated');
-        }
-      if (empty($count))
-        {
-        $count = 1;
-        }
-      for ($j = 0 ; $j < $count  ; $j++)
-        {
-        $cells[$iCell++] = new Calc_Cell($cellElement);
-        }
-      }
-
-    return $cells;
-    }
-
-  /**
-   * Construct a Calc_Row from the DOMElement representing it.
-   *
-   * This DOMElement must bear a table:table-row tag
-   *
-   * @param DOMNodeList $row
-   * @return void
-   */
-  function __construct(DOMElement $row)
-    {
-    if ((!($row instanceof DOMElement)) || ($row->tagName != 'table:table-row'))
-      {
-      die('Calc_Row::__construct() needs a DOMElement parameter for a table-row element ');
-      }
-    $this->row = $row;
-    }
-  }
-
-error_reporting($erCalc_Row);
-unset($erCalc_Row);