cvs: pear /OpenDocument Bookmark.php Element.php ElementStyle.php Exception.php Heading.php Hyperlink.php OpenDocument.php Paragraph.php Span.php
Style.php StyledElement.php TextElement.php ZipWrapper.php

From: Date: Wed, 30 Aug 2006 17:59:16 +0000
Subject: cvs: pear /OpenDocument Bookmark.php Element.php ElementStyle.php Exception.php Heading.php Hyperlink.php OpenDocument.php Paragraph.php Span.php
Style.php StyledElement.php TextElement.php ZipWrapper.php
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-41925@lists.php.net to get a copy of this message
irokez Wed Aug 30 17:59:16 2006 UTC Added files: /pear/OpenDocument Bookmark.php Element.php ElementStyle.php Exception.php Heading.php Hyperlink.php OpenDocument.php Paragraph.php Span.php Style.php StyledElement.php TextElement.php ZipWrapper.php Log: First commit

http://cvs.php.net/viewvc.cgi/pear/OpenDocument/Bookmark.php?view=markup&rev=1.1 Index: pear/OpenDocument/Bookmark.php +++ pear/OpenDocument/Bookmark.php <?php require_once 'Element.php'; class OpenDocument_Bookmark extends OpenDocument_Element { private $name; public function __constructor($node, $document, $name) { parent::__constructor($node, $document); $this->name = $name; } } ?> http://cvs.php.net/viewvc.cgi/pear/OpenDocument/Element.php?view=markup&rev=1.1 Index: pear/OpenDocument/Element.php +++ pear/OpenDocument/Element.php <?php /** * OpenDocument_Element abstract class * * OpenDocument_Element absract class - all other elements inherit from this one * * PHP version 5 * * LICENSE: This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * * @category File Formats * @package OpenDocument * @author Alexander Pak <irokez@gmail.com> * @license http://www.gnu.org/copyleft/lesser.html Lesser General Public License 2.1 * @version 0.1.0 * @link http://pear.php.net/package/OpenDocument * @since File available since Release 0.1.0 */ /** * OpenDocument_Element abstract class * * OpenDocument_Element absract class - all other elements inherit from this one * * @category File Formats * @package OpenDocument * @author Alexander Pak <irokez@gmail.com> * @license http://www.gnu.org/copyleft/lesser.html Lesser General Public License 2.1 * @version 0.1.0 * @link http://pear.php.net/package/OpenDocument * @since File available since Release 0.1.0 */ abstract class OpenDocument_Element { /** * Element DOMNode * * @var DOMNode */ protected $node; /** * Element OpenDocument * * @var OpenDocument */ protected $document; /** * Array of allowed documents * * @var array */ protected $allowedElements; /** * ArrayObject of children elements * * @var ArrayObject */ protected $children; /** * Constructor * * @param DOMNode $node * @param OpenDocument $document */ public function __construct(DOMNode $node, OpenDocument $document) { $this->node = $node; $this->document = $document; $this->allowedElements = array(); } /** * Get element DOMNode * * @return DOMNode */ public function getNode() { return $this->node; } /** * Get element document * * @return OpenDocument */ public function getDocument() { return $this->document; } /** * Delete element * */ public function delete() { $this->document->deleteElement($this); } /** * Get element children * * @return ArrayIterator */ public function getChildren() { $this->listChildren(); return $this->children->getIterator(); } /** * Prepare element children * */ protected function listChildren() { $this->children = new ArrayObject; if ($this->node instanceof DOMNode) { $childrenNodes = $this->node->childNodes; foreach ($childrenNodes as $child) { if ($child instanceof DOMText) { $element = new OpenDocument_TextElement($child, $this->document); $this->children->append($element); } else { foreach ($this->allowedElements as $class) { $ReflectionClass = new ReflectionClass($class); $tag = $ReflectionClass->getConstant('nodePrefix') . ':' . $ReflectionClass->getConstant('nodeName'); if ($child->nodeName == $tag) { $element = new $class($child, $this->document); $this->children->append($element); break; } } } } } } } ?> http://cvs.php.net/viewvc.cgi/pear/OpenDocument/ElementStyle.php?view=markup&rev=1.1 Index: pear/OpenDocument/ElementStyle.php +++ pear/OpenDocument/ElementStyle.php <?php /** * OpenDocument_ElementStyle class * * OpenDocument_ElementStyle concretes element style object * * PHP version 5 * * LICENSE: This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * * @category File Formats * @package OpenDocument * @author Alexander Pak <irokez@gmail.com> * @license http://www.gnu.org/copyleft/lesser.html Lesser General Public License 2.1 * @version 0.1.0 * @link http://pear.php.net/package/OpenDocument * @since File available since Release 0.1.0 */ require_once 'Style.php'; /** * OpenDocument_ElementStyle class * * OpenDocument_ElementStyle concretes element style object * * @category File Formats * @package OpenDocument * @author Alexander Pak <irokez@gmail.com> * @license http://www.gnu.org/copyleft/lesser.html Lesser General Public License 2.1 * @version 0.1.0 * @link http://pear.php.net/package/OpenDocument * @since File available since Release 0.1.0 */ class OpenDocument_ElementStyle extends OpenDocument_Style { /** * Possible properties values * */ const FONT_WEIGHT_BOLD = 'bold', FONT_WEIGHT_NORMAL = 'normal', FONT_STYLE_ITALIC = 'italic', FONT_STYLE_NORMAL = 'normal', UNDERLINE_STYLE_SOLID = 'solid'; /** * List of properties * */ protected $fontWeight, $fontStyle, $fontName, $fontSize, $backgroundColor, $color, $lineHeight, $underlineStyle, $underlineWidth, $underlineColor; /** * Constructor * * @param OpenDocument_StyledElement $element */ public function __construct($element) { $this->map = array( 'fontWeight' => 'fo:font-weight', 'fontStyle' => 'fo:font-style', 'fontName' => 'style:font-name', 'fontSize' => 'fo:font-size', 'backgroundColor' => 'fo:background-color', 'color' => 'fo:color', 'lineHeight' => 'style:line-height', 'underlineStyle' => 'style:text-underline-style', 'underlineWidth' => 'style:text-underline-width', 'underlineColor' => 'style:text-underline-color', ); parent::__construct($element); } /** * Set style value * * @param string $name * @param mixed $value */ protected function __set($name, $value) { if ($name == 'fontName') { $this->element->getDocument()->addFont($value); } parent::__set($name, $value); } } ?> http://cvs.php.net/viewvc.cgi/pear/OpenDocument/Exception.php?view=markup&rev=1.1 Index: pear/OpenDocument/Exception.php +++ pear/OpenDocument/Exception.php <?php /** * OpenDocument_Exception class * * OpenDocument_Exception class extends PEAR_Exception * * PHP version 5 * * LICENSE: This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * * @category File Formats * @package OpenDocument * @author Alexander Pak <irokez@gmail.com> * @license http://www.gnu.org/copyleft/lesser.html Lesser General Public License 2.1 * @version 0.1.0 * @link http://pear.php.net/package/OpenDocument * @since File available since Release 0.1.0 */ require_once 'PEAR/Exception.php'; /** * OpenDocument_Exception class * * OpenDocument_Exception class extends PEAR_Exception * * @category File Formats * @package OpenDocument * @author Alexander Pak <irokez@gmail.com> * @license http://www.gnu.org/copyleft/lesser.html Lesser General Public License 2.1 * @version 0.1.0 * @link http://pear.php.net/package/OpenDocument * @since File available since Release 0.1.0 */ class OpenDocument_Exception extends PEAR_Exception { /** * Error while accessing OpenDocument file */ const ACCESS_FILE_ERR = 1; /** * Error while loading mimetype file */ const LOAD_MIMETYPE_ERR = 2; /** * Error while loading content file */ const LOAD_CONTENT_ERR = 3; /** * Error while loading meta file */ const LOAD_META_ERR = 4; /** * Error while loading settings file */ const LOAD_SETTINGS_ERR = 5; /** * Error while loading styles file */ const LOAD_STYLES_ERR = 6; /** * Error while loading manifest file */ const LOAD_MANIFEST_ERR = 7; /** * Error while writing mimetype file */ const WRITE_MIMETYPE_ERR = 8; /** * Error while writing content file */ const WRITE_CONTENT_ERR = 9; /** * Error while writing meta file */ const WRITE_META_ERR = 10; /** * Error while writing settings file */ const WRITE_SETTINGS_ERR = 11; /** * Error while writing styles file */ const WRITE_STYLES_ERR = 12; /** * Error while writing manifest file */ const WRITE_MANIFEST_ERR = 13; /** * OpenDocument_Element or OpenDocument expected * */ const ELEM_OR_DOC_EXPECTED = 14; } ?> http://cvs.php.net/viewvc.cgi/pear/OpenDocument/Heading.php?view=markup&rev=1.1 Index: pear/OpenDocument/Heading.php +++ pear/OpenDocument/Heading.php <?php /** * OpenDocument_Heading class * * PHP version 5 * * LICENSE: This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * * @category File Formats * @package OpenDocument * @author Alexander Pak <irokez@gmail.com> * @license http://www.gnu.org/copyleft/lesser.html Lesser General Public License 2.1 * @version 0.1.0 * @link http://pear.php.net/package/OpenDocument * @since File available since Release 0.1.0 */ require_once 'StyledElement.php'; /** * OpenDocument_Heading element * * @category File Formats * @package OpenDocument * @author Alexander Pak <irokez@gmail.com> * @license http://www.gnu.org/copyleft/lesser.html Lesser General Public License 2.1 * @version 0.1.0 * @link http://pear.php.net/package/OpenDocument * @since File available since Release 0.1.0 */ class OpenDocument_Heading extends OpenDocument_StyledElement { /** * Heading level * * @var integer */ private $level; /** * Node namespace */ const nodeNS = OpenDocument::NS_TEXT; /** * Node namespace */ const nodePrefix = 'text'; /** * Node name */ const nodeName = 'h'; /** * Element style name prefix */ const styleNamePrefix = 'H'; /** * Constructor * * @param DOMNode $node * @param OpenDocument $document */ public function __construct(DOMNode $node, OpenDocument $document) { parent::__construct($node, $document); $this->level = $node->getAttributeNS(OpenDocument::NS_TEXT, 'outline-level'); $this->allowedElements = array( 'OpenDocument_Span', 'OpenDocument_Hyperlink', ); } /** * Create OpenDocument_Heading element * * @param mixed $object * @param mixed $content * @param integer $level optional * @return OpenDocument_Heading */ public static function instance($object, $content, $level = 1) { if ($object instanceof OpenDocument) { $document = $object; $node = $object->cursor; } else if ($object instanceof OpenDocument_Element) { $document = $object->getDocument(); $node = $object->getNode(); } else { throw new OpenDocument_Exception(OpenDocument_Exception::ELEM_OR_DOC_EXPECTED); } $element = new OpenDocument_Heading($node->ownerDocument->createElementNS(self::nodeNS, self::nodeName), $document); $node->appendChild($element->node); if (is_scalar($content)) { $element->createTextElement($content); } $element->__set('level', $level); return $element; } /** * Set element properties * * @param string $name * @param mixed $value */ public function __set($name, $value) { switch ($name) { case 'level': if (!is_int($value) && !ctype_digit($value)) { $value = 1; } $this->type = $value; $this->node->setAttributeNS(OpenDocument::NS_TEXT, 'outline-level', $value); break; default: } } /** * Get element properties * * @param string $name * @return mixed */ protected function __get($name) { if ($value = parent::__get($name)) { return $value; } if (isset($this->$name)) { return $this->$name; } } /** * Generate element new style name * * @return string */ public function generateStyleName() { self::$styleNameMaxNumber ++; return self::styleNamePrefix . self::$styleNameMaxNumber; } /************** Elements ***********************/ /** * Create OpenDocument_TextElement * * @param string $text * @return OpenDocument_TextElement */ public function createTextElement($text) { return OpenDocument_TextElement::instance($this, $text); } /** * Create OpenDocument_Hyperlink * * @param string $text * @param string $location * @param string $type optional * @param string $target optional * @param string $name optional * @return OpenDocument_Hyperlink */ public function createHyperlink($text, $location, $type = 'simple', $target = '', $name = '') { return OpenDocument_Hyperlink::instance($this, $text, $location, $type, $target, $name); } /** * Create OpenDocument_Span element * * @param string $text * @return OpenDocument_Span */ public function createSpan($text) { return OpenDocument_Span::instance($this, $text); } } ?> http://cvs.php.net/viewvc.cgi/pear/OpenDocument/Hyperlink.php?view=markup&rev=1.1 Index: pear/OpenDocument/Hyperlink.php +++ pear/OpenDocument/Hyperlink.php <?php /** * OpenDocument_Hyperlink class * * PHP version 5 * * LICENSE: This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * * @category File Formats * @package OpenDocument * @author Alexander Pak <irokez@gmail.com> * @license http://www.gnu.org/copyleft/lesser.html Lesser General Public License 2.1 * @version 0.1.0 * @link http://pear.php.net/package/OpenDocument * @since File available since Release 0.1.0 */ require_once 'StyledElement.php'; /** * OpenDocument_Hyperlink element * * @category File Formats * @package OpenDocument * @author Alexander Pak <irokez@gmail.com> * @license http://www.gnu.org/copyleft/lesser.html Lesser General Public License 2.1 * @version 0.1.0 * @link http://pear.php.net/package/OpenDocument * @since File available since Release 0.1.0 */ class OpenDocument_Hyperlink extends OpenDocument_StyledElement { /** * Link location * * @var string */ private $location; /** * Link type * * @var string */ private $type; /** * Link target * * @var string */ private $target; /** * Link name * * @var string */ private $name; /** * Node namespace */ const nodeNS = OpenDocument::NS_TEXT; /** * Node namespace */ const nodePrefix = 'text'; /** * Node name */ const nodeName = 'a'; /** * Element style name prefix */ const styleNamePrefix = 'A'; /** * Constructor * * @param DOMNode $node * @param OpenDocument $document */ public function __construct(DOMNode $node, OpenDocument $document) { parent::__construct($node, $document); $this->location = $node->getAttributeNS(OpenDocument::NS_XLINK, 'href'); $this->type = $node->getAttributeNS(OpenDocument::NS_XLINK, 'type'); $this->target = $node->getAttributeNS(OpenDocument::NS_OFFICE, 'target'); $this->name = $node->getAttributeNS(OpenDocument::NS_OFFICE, 'name'); $this->allowedElements = array( 'OpenDocument_Span', ); } /** * Create OpenDocument_Hyperlink instance * * @param mixed $object * @param mixed $content * @param string $location * @param string $type optional * @param string $target optional * @param string $name optional * @return OpenDocument_Hyperlink */ public static function instance($object, $content, $location, $type = 'simple', $target = '', $name = '') { if ($object instanceof OpenDocument) { $document = $object; $node = $object->cursor; } else if ($object instanceof OpenDocument_Element) { $document = $object->getDocument(); $node = $object->getNode(); } else { throw new Exception('Object must be OpenDocument or OpenDocument_Element'); } $element = new OpenDocument_Hyperlink($node->ownerDocument->createElementNS(self::nodeNS, self::nodeName), $document); $node->appendChild($element->node); $element->__set('location', $location); $element->__set('type', $type); $element->__set('target', $target); $element->__set('name', $name); if (is_scalar($content)) { $element->createTextElement($content); } return $element; } /** * Set element property * * @param string $name * @param mixed $value */ public function __set($name, $value) { switch ($name) { case 'location': $this->location = $value; $this->node->setAttributeNS(OpenDocument::NS_XLINK, 'href', $value); break; case 'type': if (!in_array($value, array('simple'))) { $value = 'simple'; } $this->type = $value; $this->node->setAttributeNS(OpenDocument::NS_XLINK, 'type', $value); break; case 'target': $this->target = $value; $this->node->setAttributeNS(OpenDocument::NS_OFFICE, 'target-frame-name', $value); break; case 'name': $this->name = $value; $this->node->setAttributeNS(OpenDocument::NS_OFFICE, 'name', $value); break; default: } } /** * Get element property * * @param string $name * @return mixed */ protected function __get($name) { /*if ($value = parent::__get($name)) { return $value; }*/ if (isset($this->$name)) { return $this->$name; } } /** * Generate element new style name * * @return string */ public function generateStyleName() { self::$styleNameMaxNumber ++; return self::styleNamePrefix . self::$styleNameMaxNumber; } /************** Elements ***********************/ /** * Create OpenDocument_TextElement * * @param string $text * @return OpenDocument_TextElement */ public function createTextElement($text) { return OpenDocument_TextElement::instance($this, $text); } /** * Create OpenDocument_Span element * * @param string $text * @return OpenDocument_Span */ public function createSpan($text) { return OpenDocument_Span::instance($this, $text); } } ?> http://cvs.php.net/viewvc.cgi/pear/OpenDocument/OpenDocument.php?view=markup&rev=1.1 Index: pear/OpenDocument/OpenDocument.php +++ pear/OpenDocument/OpenDocument.php <?php /** * OpenDocument base class * * OpenDocument class handles reading and modifying files in OpenDocument format * * PHP version 5 * * LICENSE: This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * * @category File Formats * @package OpenDocument * @author Alexander Pak <irokez@gmail.com> * @license http://www.gnu.org/copyleft/lesser.html Lesser General Public License 2.1 * @version 0.1.0 * @link http://pear.php.net/package/OpenDocument * @since File available since Release 0.1.0 */ require_once 'OpenDocument/ZipWrapper.php'; require_once 'OpenDocument/Exception.php'; require_once 'OpenDocument/Textelement.php'; require_once 'OpenDocument/Span.php'; require_once 'OpenDocument/Paragraph.php'; require_once 'OpenDocument/Heading.php'; require_once 'OpenDocument/Bookmark.php'; require_once 'OpenDocument/Hyperlink.php'; /** * OpenDocument base class * * OpenDocument class handles reading and modifying files in OpenDocument format * * @category File Formats * @package OpenDocument * @author Alexander Pak <irokez@gmail.com> * @license http://www.gnu.org/copyleft/lesser.html Lesser General Public License 2.1 * @version 0.1.0 * @link http://pear.php.net/package/OpenDocument * @since File available since Release 0.1.0 */ class OpenDocument { /** * Path to opened OpenDocument file * * @var string * @access private */ private $path; /** * DOMNode of current node * * @var DOMNode * @access provate */ private $cursor; /** * DOMNode contains style information * * @var DOMNode * @access private */ private $styles; /** * DOMNode contains fonts declarations * * @var DOMNode * @access private */ private $fonts; /** * Mime type information * * @var string * @access private */ private $mimetype; /** * Flag indicates whether it is a new file * * @var bool * @access private */ private $create = false; /** * DOMDocument for content file * * @var DOMDocument * @access private */ private $contentDOM; /** * DOMXPath object for content file * * @var DOMXPath * @access private */ private $contentXPath; /** * DOMDocument for meta file * * @var DOMDocument * @access private */ private $metaDOM; /** * DOMXPath for meta file * * @var DOMXPath * @access private */ private $metaXPath; /** * DOMDocument for settings file * * @var DOMDocument * @access private */ private $settingsDOM; /** * DOMXPath for setting file * * @var DOMXPath * @access private */ private $settingsXPath; /** * DOMDocument for styles file * * @var DOMDocument * @access private */ private $stylesDOM; /** * DOMXPath for styles file * * @var DOMXPath * @access private */ private $stylesXPath; /** * DOMDocument for styles file * * @var DOMDocument * @access private */ private $manifestDOM; /** * DOMXPath for manifest file * * @var DOMXPath * @access private */ private $manifestXPath; /** * Collection of children objects * * @var ArrayIterator * @access read-only */ private $children; /** * File with document contents */ const FILE_CONTENT = 'content.xml'; /** * File with meta information */ const FILE_META = 'meta.xml'; /** * File with editor settings */ const FILE_SETTINGS = 'settings.xml'; /** * File with document styles */ const FILE_STYLES = 'styles.xml'; /** * File with mime type */ const FILE_MIMETYPE = 'mimetype'; /** * File with manifest information */ const FILE_MANIFEST = 'META-INF/manifest.xml'; /** * text namespace URL */ const NS_TEXT = 'urn:oasis:names:tc:opendocument:xmlns:text:1.0'; /** * style namespace URL */ const NS_STYLE = 'urn:oasis:names:tc:opendocument:xmlns:style:1.0'; /** * fo namespace URL */ const NS_FO = 'urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0'; /** * office namespace URL */ const NS_OFFICE = 'urn:oasis:names:tc:opendocument:xmlns:office:1.0'; /** * svg namespace URL */ const NS_SVG = 'urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0'; /** * xlink namespace URL */ const NS_XLINK = 'http://www.w3.org/1999/xlink'; /** * Constructor * * @param string $filename optional * specify file name if you want to open existing file * to create new document pass nothing or empty string * @throws OpenDocument_Exception */ public function __construct($filename = '') { if (!strlen($filename)) { $filename = dirname(__FILE__) . '/OpenDocument/templates/default.odt'; $this->create = true; } if (!is_readable($filename)) { throw new OpenDocument_Exception(OpenDocument_Exception::ACCESS_FILE_ERR); } $this->path = $filename; //get mimetype if (!$this->mimetype = ZipWrapper::read($filename, self::FILE_MIMETYPE)) { throw new OpenDocument_Exception(OpenDocument_Exception::LOAD_MIMETYPE_ERR); } //get content $this->contentDOM = new DOMDocument(); if (!$this->contentDOM->loadXML(ZipWrapper::read($filename, self::FILE_CONTENT))) { throw new OpenDocument_Exception(OpenDocument_Exception::LOAD_CONTENT_ERR); } $this->contentXPath = new DOMXPath($this->contentDOM); //get meta data $this->metaDOM = new DOMDocument(); if (!$this->metaDOM->loadXML(ZipWrapper::read($filename, self::FILE_META))) { throw new OpenDocument_Exception(OpenDocument_Exception::LOAD_META_ERR); } $this->metaXPath = new DOMXPath($this->metaDOM); //get settings $this->settingsDOM = new DOMDocument(); if (!$this->settingsDOM->loadXML(ZipWrapper::read($filename, self::FILE_SETTINGS))) { throw new OpenDocument_Exception(OpenDocument_Exception::LOAD_SETTINGS_ERR); } $this->settingsXPath = new DOMXPath($this->settingsDOM); //get styles $this->stylesDOM = new DOMDocument(); if (!$this->stylesDOM->loadXML(ZipWrapper::read($filename, self::FILE_STYLES))) { throw new OpenDocument_Exception(OpenDocument_Exception::LOAD_STYLES_ERR); } $this->stylesXPath = new DOMXPath($this->stylesDOM); //get manifest information $this->manifestDOM = new DOMDocument(); if (!$this->manifestDOM->loadXML(ZipWrapper::read($filename, self::FILE_MANIFEST))) { throw new OpenDocument_Exception(OpenDocument_Exception::LOAD_MANIFEST_ERR); } $this->manifestXPath = new DOMXPath($this->manifestDOM); //set cursor $this->cursor = $this->contentXPath->query('/office:document-content/office:body/office:text')->item(0); $this->styles = $this->contentXPath->query('/office:document-content/office:automatic-styles')->item(0); $this->fonts = $this->contentXPath->query('/office:document-content/office:font-face-decls')->item(0); $this->contentXPath->registerNamespace('text', self::NS_TEXT); $this->listChildren(); $this->setMax(); } /** * Magic method * Provide read only access to cursor private variable * * @param string $name * @return mixed */ private function __get($name) { switch ($name) { case 'cursor': return $this->cursor; default: } } /** * Get children list * * @return ArrayIterator * @access public */ public function getChildren() { return $this->children->getIterator(); } /** * Create ArrayObject of document children objects * * @access private */ private function listChildren() { $this->children = new ArrayObject; if ($this->cursor instanceof DOMNode) { $childrenNodes = $this->cursor->childNodes; foreach ($childrenNodes as $child) { switch ($child->nodeName) { case 'text:p': $element = new OpenDocument_Paragraph($child, $this); break; case 'text:h': $element = new OpenDocument_Heading($child, $this); break; default: $element = false; } if ($element) { $this->children->append($element); } } } } /** * Delete document child element * * @param OpenDocument_Element $element * @access public */ public function deleteElement(OpenDocument_Element $element) { $this->cursor->removeChild($element->getNode()); unset($element); } /** * Set maximum values of style name suffixes * * @access private */ private function setMax() { $classes = array('OpenDocument_Paragraph', 'OpenDocument_Heading', 'OpenDocument_Hyperlink'); $max = array(); if ($this->cursor instanceof DOMNode) { $nodes = $this->cursor->getElementsByTagName('*'); foreach ($nodes as $node) { if ($node->hasAttributeNS(self::NS_TEXT, 'style-name')) { $style_name = $node->getAttributeNS(self::NS_TEXT, 'style-name'); foreach ($classes as $class) { $reflection = new ReflectionClass($class); $prefix = $reflection->getConstant('styleNamePrefix'); if (preg_match("/^$prefix(\d)+$/", $style_name, $m)) { $max[$class] = isset($max[$class]) ? ($max[$class] < $m[1] ? $m[1] : $max[$class]) : $m[1]; } } } } } foreach ($classes as $class) { $method = new ReflectionMethod($class, 'setStyleNameMaxNumber'); if (!isset($max[$class])) { $max[$class] = 0; } $method->invoke(null, $max[$class]); } } /************************* Elements **************************/ /** * Create OpenDocument_Paragraph * * @param string $text optional * @return OpenDocument_Paragraph * @access public */ public function createParagraph($text = '') { return OpenDocument_Paragraph::instance($this, $text); } /** * Create Open_document_Heading * * @param string $text * @param integer $level * @return OpenDocument_Heading * @access public */ public function createHeading($text = '', $level = 1) { return OpenDocument_Heading::instance($this, $text, $level); } /** * Create OpenDocument_Bookmark * * @param string $name * @param string $type * @return OpenDocument_Bookmark * @access public * @todo finish method */ public function createBookmark($name, $type = 'start') { if (!in_array($type, array('start', 'end'))) { $type = 'start'; } $bookmark = new OpenDocument_Bookmark($this->contentDOM->createElementNS(self::NS_TEXT, 'bookmark-' . $type), $this, $name, $type); $this->cursor->appendChild($bookmark->getNode()); $bookmark->getNode()->setAttributeNS(self::NS_TEXT, 'name', $name); return $bookmark; } /********************* Styles ****************************/ /** * Apply style information to object * If object has no style information yet, then create new style node * If object style information is similar to other object's style info, then apply the same style name * And if object old style information was not shared with other objects then delete old style info * Else leave old style info * Else just add new style description * * @param string $style_name * @param string $name * @param mixed $value * @param OpenDocument_StyledElement $object * @return string $style_name */ public function applyStyle($style_name, $name, $value, OpenDocument_StyledElement $object) { //check if other nodes have the same style name $nodes = $this->cursor->getElementsByTagName('*'); $count = 0; foreach ($nodes as $node) { if ($node->hasAttributeNS(self::NS_TEXT, 'style-name') && $node->getAttributeNS(self::NS_TEXT, 'style-name') == $style_name) { $count ++; if ($count > 1) { break; } } } $generate = false; //get style node if ($count > 1) { $style = $this->getStyleNode($style_name)->cloneNode(true); $this->styles->appendChild($style); $generate = true; $style_name = uniqid('tmp');//$object->generateStyleName(); $style->setAttributeNS(self::NS_STYLE, 'name', $style_name); } else { $style = $this->getStyleNode($style_name); } if (empty($style)) { if (empty($style_name)) { $generate = true; $style_name = uniqid('tmp'); } $style = $this->contentDOM->createElementNS(self::NS_STYLE, 'style'); $style->setAttributeNS(self::NS_STYLE, 'name', $style_name); $style->setAttributeNS(self::NS_STYLE, 'family', 'paragraph'); $style->setAttributeNS(self::NS_STYLE, 'parent-style-name', 'Standard'); $this->styles->appendChild($style); } $nodes = $style->getElementsByTagNameNS(self::NS_STYLE, 'text-properties'); if ($nodes->length) { $text_properties = $nodes->item(0); } else { $text_properties = $this->contentDOM->createElementNS(self::NS_STYLE, 'text-properties'); $style->appendChild($text_properties); } $text_properties->setAttribute($name, $value); //find alike style $nodes = $this->styles->getElementsByTagNameNS(self::NS_STYLE, 'style'); foreach ($nodes as $node) { if (!$style->isSameNode($node) && $this->compareChildNodes($style, $node)) { $style->parentNode->removeChild($style); return $node->getAttributeNS(self::NS_STYLE, 'name'); } } if ($generate) { $style_name = $object->generateStyleName(); $style->setAttributeNS(self::NS_STYLE, 'name', $style_name); } return $style->getAttributeNS(self::NS_STYLE, 'name'); } /** * Get array of style values * * @param string $style_name * @param array $properties * @return array */ public function getStyle($style_name, $properties) { $style = array(); if ($node = $this->getStyleNode($style_name)) { $nodes = $node->getElementsByTagNameNS(self::NS_STYLE, 'text-properties'); if ($nodes->length) { $text_properties = $nodes->item(0); foreach ($properties as $property) { list($prefix, $name) = explode(':', $property); $ns = $text_properties->lookupNamespaceURI($prefix); $style[$property] = $text_properties->getAttributeNS($ns, $name); } } } return $style; } /** * Get style node * * @param string $style_name * @return DOMNode */ private function getStyleNode($style_name) { $nodes = $this->styles->getElementsByTagNameNS(self::NS_STYLE, 'style'); foreach ($nodes as $node) { $node->getAttributeNS(self::NS_STYLE, 'name'); if ($node->getAttributeNS(self::NS_STYLE, 'name') == $style_name) { return $node; } } return false; } /** * Check if two style info are similar * * @param string $style_name1 * @param string $style_name2 * @return bool */ private function compareStyles($style_name1, $style_name2) { $style_node1 = $this->getStyleNode($style_name1); $style_node2 = $this->getStyleNode($style_name2); return $this->compareNodes($style_node1, $style_node2); } /********************* Fonts ****************************/ /** * Get array of declared font names * * @return array */ private function getFonts() { $nodes = $this->fonts->getElementsByTagNameNS(self::NS_STYLE, 'font-face'); $fonts = array(); foreach ($nodes as $node) { $fonts[] = $node->getAttributeNS(self::NS_STYLE, 'name'); } return $fonts; } /** * Add new font declaration * * @param string $font_name * @param string $font_family optional */ public function addFont($font_name, $font_family = '') { if (!in_array($font_name, $this->getFonts())) { $node = $this->contentDOM->createElementNS(self::NS_STYLE, 'font-face'); $this->fonts->appendChild($node); $node->setAttributeNS(self::NS_STYLE, 'name', $font_name); if (!strlen($font_family)) { $font_family = $font_name; } $node->setAttributeNS(self::NS_SVG, 'font-family', $font_family); } } /** * Compare two DOMNode nodes * * @param mixed $node1 * @param mixed $node2 * @return bool */ function compareNodes($node1, $node2) { if (!($node1 instanceof DOMNode) || !($node2 instanceof DOMNode)) { return false; } $attributes = $node1->attributes; if ($attributes->length == $node2->attributes->length) { for ($i = 0; $i < $attributes->length; $i ++) { $name = $attributes->item($i)->name; $value = $attributes->item($i)->value; if (!$node2->hasAttribute($name) || $node2->getAttribute($name) != $value) { return false; } } } else { return false; } $children = $node1->childNodes; if ($children->length == $node2->childNodes->length) { for ($i = 0; $i < $children->length; $i ++) { $node = $children->item($i); $matches = $this->getChildrenByName($node2, $node->nodeName); $test = false; foreach ($matches as $match) { if ($this->compareNodes($node, $match)) { $test = true; break; } } if (!$test) { return false; } } } else { return false; } return true; } /** * Compare DOMNode children * * @param DOMNode $node1 * @param DOMNode $node2 * @return bool */ private function compareChildNodes(DOMNode $node1, DOMNode $node2) { $children = $node1->childNodes; if ($children->length == $node2->childNodes->length) { for ($i = 0; $i < $children->length; $i ++) { $node = $children->item($i); $matches = $this->getChildrenByName($node2, $node->nodeName); $test = false; foreach ($matches as $match) { if ($this->compareNodes($node, $match)) { $test = true; break; } } if (!$test) { return false; } } } else { return false; } return true; } /** * Get DOMNode children by name * * @param DOMNode $node * @param string $name * @return array */ private function getChildrenByName(DOMNode $node, $name) { $nodes = array(); foreach ($node->childNodes as $node) { if ($node->nodeName == $name) { array_push($nodes, $node); } } return $nodes; } /** * Test function * @todo remove or finish function */ public function output() { $list = $this->contentXPath->query('/office:document-content/office:font-face-decls/style:font-face'); echo $list->length; foreach ($list as $node) { echo '<br />'; foreach ($node->attributes as $attribute) { echo $attribute->name . '=' . $attribute->value; } } echo $this->contentDOM->saveXML(); } /** * Save changes in document or save as a new document / under another name * * @param string $filename optional * @throws OpenDocument_Exception */ public function save($filename = '') { if (strlen($filename)) { $this->path = $filename; } //write mimetype if (!ZipWrapper::write($this->path, self::FILE_MIMETYPE, $this->mimetype)) { throw new OpenDocument_Exception(OpenDocument_Exception::WRITE_MIMETYPE_ERR); } //write content $xml = str_replace("'", '&apos;', $this->contentDOM->saveXML()); if (!ZipWrapper::write($this->path, self::FILE_CONTENT, $xml)) { throw new OpenDocument_Exception(OpenDocument_Exception::WRITE_CONTENT_ERR); } //write meta $xml = str_replace("'", '&apos;', $this->metaDOM->saveXML()); if (!ZipWrapper::write($this->path, self::FILE_META, $xml)) { throw new OpenDocument_Exception(OpenDocument_Exception::WRITE_META_ERR); } //write settings $xml = str_replace("'", '&apos;', $this->settingsDOM->saveXML()); if (!ZipWrapper::write($this->path, self::FILE_SETTINGS, $xml)) { throw new OpenDocument_Exception(OpenDocument_Exception::WRITE_SETTINGS_ERR); } //write styles $xml = str_replace("'", '&apos;', $this->stylesDOM->saveXML()); if (!ZipWrapper::write($this->path, self::FILE_STYLES, $xml)) { throw new OpenDocument_Exception(OpenDocument_Exception::WRITE_STYLES_ERR); } //write manifest $xml = str_replace("'", '&apos;', $this->manifestDOM->saveXML()); if (!ZipWrapper::write($this->path, self::FILE_MANIFEST, $xml)) { throw new OpenDocument_Exception(OpenDocument_Exception::WRITE_MANIFEST_ERR); } } } ?> http://cvs.php.net/viewvc.cgi/pear/OpenDocument/Paragraph.php?view=markup&rev=1.1 Index: pear/OpenDocument/Paragraph.php +++ pear/OpenDocument/Paragraph.php <?php /** * OpenDocument_Paragraph class * * PHP version 5 * * LICENSE: This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * * @category File Formats * @package OpenDocument * @author Alexander Pak <irokez@gmail.com> * @license http://www.gnu.org/copyleft/lesser.html Lesser General Public License 2.1 * @version 0.1.0 * @link http://pear.php.net/package/OpenDocument * @since File available since Release 0.1.0 */ require_once 'StyledElement.php'; /** * OpenDocument_Paragraph element * * @category File Formats * @package OpenDocument * @author Alexander Pak <irokez@gmail.com> * @license http://www.gnu.org/copyleft/lesser.html Lesser General Public License 2.1 * @version 0.1.0 * @link http://pear.php.net/package/OpenDocument * @since File available since Release 0.1.0 */ class OpenDocument_Paragraph extends OpenDocument_StyledElement { /** * Node namespace */ const nodeNS = OpenDocument::NS_TEXT; /** * Node namespace */ const nodePrefix = 'text'; /** * Node name */ const nodeName = 'p'; /** * Element style name prefix * */ const styleNamePrefix = 'P'; /** * Constructor * * @param DOMNode $node * @param OpenDocument $document */ public function __construct(DOMNode $node, OpenDocument $document) { parent::__construct($node, $document); $this->allowedElements = array( 'OpenDocument_Span', 'OpenDocument_Hyperlink', ); } /** * Create element instance * * @param mixed $object * @param mixed $content * @return OpenDocument_Paragraph * @throws OpenDocument_Exception */ public static function instance($object, $content) { if ($object instanceof OpenDocument) { $document = $object; $node = $object->cursor; } else if ($object instanceof OpenDocument_Element) { $document = $object->getDocument(); $node = $object->getNode(); } else { throw new OpenDocument_Exception(OpenDocument_Exception::ELEM_OR_DOC_EXPECTED); } $element = new OpenDocument_Paragraph($node->ownerDocument->createElementNS(self::nodeNS, self::nodeName), $document); $node->appendChild($element->node); if (is_scalar($content)) { $element->createTextElement($content); } return $element; } /** * Generate element new style name * * @return string */ public function generateStyleName() { self::$styleNameMaxNumber ++; return self::styleNamePrefix . self::$styleNameMaxNumber; } /************** Elements ****************/ /** * Create OpenDocument_TextElement * * @param string $text * @return OpenDocument_TextElement */ public function createTextElement($text) { return OpenDocument_TextElement::instance($this, $text); } /** * Create OpenDocument_Hyperlink * * @param string $text * @param string $location * @param string $type optional * @param string $target optional * @param string $name optional * @return OpenDocument_Hyperlink */ public function createHyperlink($text, $location, $type = 'simple', $target = '', $name = '') { return OpenDocument_Hyperlink::instance($this, $text, $location, $type, $target, $name); } /** * Create OpenDocument_Span element * * @param string $text * @return OpenDocument_Span */ public function createSpan($text) { return OpenDocument_Span::instance($this, $text); } } ?> http://cvs.php.net/viewvc.cgi/pear/OpenDocument/Span.php?view=markup&rev=1.1 Index: pear/OpenDocument/Span.php +++ pear/OpenDocument/Span.php <?php /** * OpenDocument_Span class * * PHP version 5 * * LICENSE: This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * * @category File Formats * @package OpenDocument * @author Alexander Pak <irokez@gmail.com> * @license http://www.gnu.org/copyleft/lesser.html Lesser General Public License 2.1 * @version 0.1.0 * @link http://pear.php.net/package/OpenDocument * @since File available since Release 0.1.0 */ require_once 'StyledElement.php'; /** * OpenDocument_Span element * * @category File Formats * @package OpenDocument * @author Alexander Pak <irokez@gmail.com> * @license http://www.gnu.org/copyleft/lesser.html Lesser General Public License 2.1 * @version 0.1.0 * @link http://pear.php.net/package/OpenDocument * @since File available since Release 0.1.0 */ class OpenDocument_Span extends OpenDocument_StyledElement { /** * Node namespace */ const nodeNS = OpenDocument::NS_TEXT; /** * Node amespace */ const nodePrefix = 'text'; /** * Node name */ const nodeName = 'span'; /** * Element style name prefix */ const styleNamePrefix = 'T'; /** * Create element instance * * @param mixed $object * @param mixed $content * @return OpenDocument_Span * @throws OpenDocument_Exception */ public static function instance($object, $content) { if ($object instanceof OpenDocument) { $document = $object; $node = $object->cursor; } else if ($object instanceof OpenDocument_Element) { $document = $object->getDocument(); $node = $object->getNode(); } else { throw new OpenDocument_Exception(OpenDocument_Exception::ELEM_OR_DOC_EXPECTED); } $element = new OpenDocument_Span($node->ownerDocument->createElementNS(self::nodeNS, self::nodeName), $document); $node->appendChild($element->node); if (is_scalar($content)) { $element->createTextElement($content); } return $element; } /** * Generate new style name * * @return string $stylename */ public function generateStyleName() { self::$styleNameMaxNumber ++; return self::styleNamePrefix . self::$styleNameMaxNumber; } /************** Elements ****************/ /** * Create OpenDocument_TextElement * * @param string $text * @return OpenDocument_TextElement */ public function createTextElement($text) { return OpenDocument_TextElement::instance($this, $text); } } ?> http://cvs.php.net/viewvc.cgi/pear/OpenDocument/Style.php?view=markup&rev=1.1 Index: pear/OpenDocument/Style.php +++ pear/OpenDocument/Style.php <?php /** * OpenDocument_Style abstract class * * OpenDocument_Style absract class - handles element style * all other elements inherit from this one * * PHP version 5 * * LICENSE: This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * * @category File Formats * @package OpenDocument * @author Alexander Pak <irokez@gmail.com> * @license http://www.gnu.org/copyleft/lesser.html Lesser General Public License 2.1 * @version 0.1.0 * @link http://pear.php.net/package/OpenDocument * @since File available since Release 0.1.0 */ /** * OpenDocument_Style abstract class * * OpenDocument_Style absract class - handles element style * all other elements inherit from this one * * @category File Formats * @package OpenDocument * @author Alexander Pak <irokez@gmail.com> * @license http://www.gnu.org/copyleft/lesser.html Lesser General Public License 2.1 * @version 0.1.0 * @link http://pear.php.net/package/OpenDocument * @since File available since Release 0.1.0 */ abstract class OpenDocument_Style { /** * OpenDocument Element * * @var OpenDocument_StyledElement */ protected $element; /** * Style map of properties * * @var array */ protected $map; /** * Constructor * * @param OpenDocument_StyledElement $element */ public function __construct(OpenDocument_StyledElement $element) { $this->element = $element; $this->loadStyle(); } /** * Load element style information * */ private function loadStyle() { $map = array_flip($this->map); $style = $this->element->getStyle(array_keys($map)); foreach ($style as $name => $value) { if (isset($map[$name])) { $this->$map[$name] = $value; } } } /** * Magic method * Set property value * * @param string $name * @param mixed $value */ protected function __set($name, $value) { if (isset($this->map[$name])) { $this->element->applyStyle($this->map[$name], $value); $this->$name = $value; } } /** * Magic method * Get property value * * @param string $name * @return mixed */ protected function __get($name) { if (isset($this->$name)) { return $this->$name; } else { return null; } } /** * Export style information * * @return array */ public function export() { $style = array(); foreach ($this->map as $name => $value) { $style[$name] = $this->__get($name); } return $style; } /** * Import style information * * @param array $style */ public function import($style) { foreach ($style as $name => $value) { if (isset($this->map[$name])) { $this->__set($name, $value); } } } /** * Copy style from another element * * @param OpenDocument_Style $styleObject */ public function copy(OpenDocument_Style $styleObject) { $style = $styleObject->export(); $this->import($style); } } ?> http://cvs.php.net/viewvc.cgi/pear/OpenDocument/StyledElement.php?view=markup&rev=1.1 Index: pear/OpenDocument/StyledElement.php +++ pear/OpenDocument/StyledElement.php <?php /** * OpenDocument_StyledElement abstract class * * OpenDocument_StyledElement absract class - all elements that have styles inherit from this one * * PHP version 5 * * LICENSE: This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * * @category File Formats * @package OpenDocument * @author Alexander Pak <irokez@gmail.com> * @license http://www.gnu.org/copyleft/lesser.html Lesser General Public License 2.1 * @version 0.1.0 * @link http://pear.php.net/package/OpenDocument * @since File available since Release 0.1.0 */ require_once 'Element.php'; require_once 'ElementStyle.php'; /** * OpenDocument_StyledElement abstract class * * OpenDocument_StyledElement absract class - all elements that have styles inherit from this one * * @category File Formats * @package OpenDocument * @author Alexander Pak <irokez@gmail.com> * @license http://www.gnu.org/copyleft/lesser.html Lesser General Public License 2.1 * @version 0.1.0 * @link http://pear.php.net/package/OpenDocument * @since File available since Release 0.1.0 */ abstract class OpenDocument_StyledElement extends OpenDocument_Element { /** * Style information * * @var array */ protected $style; /** * Style name suffix max value * * @var integer */ protected static $styleNameMaxNumber = 0; /** * Style name prefix * */ const styleNamePrefix = 'E'; /** * Generate new style name * */ abstract public function generateStyleName(); /** * Constructor * * @param DOMNode $node * @param OpenDocument $document */ public function __construct(DOMNode $node, OpenDocument $document) { parent::__construct($node, $document); $this->style = new OpenDocument_ElementStyle($this); } /** * Magic method * Get property value * * @param string $name * @return mixed */ protected function __get($name) { if ($name == 'style') { return $this->style; } } /** * Get style information * * @param array $properties * @return array */ public function getStyle($properties) { return $this->document->getStyle($this->getStyleName(), $properties); } /** * Get style name * * @return string */ public function getStyleName() { return $this->node->getAttributeNS(OpenDocument::NS_TEXT, 'style-name'); } /** * Get style name prefix * * @return string */ public function getStyleNamePrefix() { return $this->styleNamePrefix; } /** * Get style name suffix max value * * @return integer */ public static function getStyleNameMaxNumber() { return self::$styleNameMaxNumber; } /** * Set style name suxxif max value * * @param integer $number */ public static function setStyleNameMaxNumber($number) { self::$styleNameMaxNumber = $number; } /** * Apply style information * * @param string $name * @param mixed $value */ public function applyStyle($name, $value) { $style_name = $this->node->getAttributeNS(OpenDocument::NS_TEXT, 'style-name'); $style_name = $this->document->applyStyle($style_name, $name, $value, $this); $this->node->setAttributeNS(OpenDocument::NS_TEXT, 'style-name', $style_name); } } ?> http://cvs.php.net/viewvc.cgi/pear/OpenDocument/TextElement.php?view=markup&rev=1.1 Index: pear/OpenDocument/TextElement.php +++ pear/OpenDocument/TextElement.php <?php /** * OpenDocument_TextElement class * * OpenDocument_TextElement represents text in OpenDocument * * PHP version 5 * * LICENSE: This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * * @category File Formats * @package OpenDocument * @author Alexander Pak <irokez@gmail.com> * @license http://www.gnu.org/copyleft/lesser.html Lesser General Public License 2.1 * @version 0.1.0 * @link http://pear.php.net/package/OpenDocument * @since File available since Release 0.1.0 */ require_once 'Element.php'; /** * OpenDocument_TextElement class * * OpenDocument_TextElement represents text in OpenDocument * * @category File Formats * @package OpenDocument * @author Alexander Pak <irokez@gmail.com> * @license http://www.gnu.org/copyleft/lesser.html Lesser General Public License 2.1 * @version 0.1.0 * @link http://pear.php.net/package/OpenDocument * @since File available since Release 0.1.0 */ class OpenDocument_TextElement extends OpenDocument_Element { /** * Element text * * @var string */ private $text; /** * Constructor * * @param DOMNode $node * @param OpenDocument $document */ public function __construct(DOMNode $node, OpenDocument $document) { parent::__construct($node, $document); $this->text = $node->wholeText; } /** * Create object instance * * @param mixed $object * @param string $text * @return OpenDocument_TextElement */ public static function instance($object, $text) { if ($object instanceof OpenDocument) { $document = $object; $node = $object->cursor; } else if ($object instanceof OpenDocument_Element) { $document = $object->getDocument(); $node = $object->getNode(); } else { throw new Exception('Object must be OpenDocument or OpenDocument_Element'); } $element = new OpenDocument_TextElement($node->ownerDocument->createTextNode($text), $document); $node->appendChild($element->node); $element->text = $text; return $element; } /** * Magic method * Set property value * * @param string $name * @param mixed $value */ protected function __set($name, $value) { switch ($name) { case 'text': $this->text = $value; $this->setText($value); break; default: } } /** * Magic method * Get property value * * @param string $name * @return mixed */ protected function __get($name) { if (isset($this->$name)) { return $this->$name; } } /** * Set element text * * @param string $text */ private function setText($text) { $node = $this->node->ownerDocument->createTextNode($text); $this->node->parentNode->replaceChild($node, $this->node); } } ?> http://cvs.php.net/viewvc.cgi/pear/OpenDocument/ZipWrapper.php?view=markup&rev=1.1 Index: pear/OpenDocument/ZipWrapper.php +++ pear/OpenDocument/ZipWrapper.php <?php class ZipWrapper { public static function read($archive, $filename) { $zip = new ZipArchive; if (file_exists($archive)) { if ($zip->open(realpath($archive))) { if ($zip->locateName($filename) !== false) { return $zip->getFromName($filename); } } } return false; } public static function write($archive, $filename, $content) { $zip = new ZipArchive; if (file_exists($archive)) { $zip->open(realpath($archive)); } else { $zip->open(getcwd() . '/' . $archive, ZipArchive::CREATE); } if ($zip->locateName($filename) !== false) { $zip->deleteName($filename); } $error = $zip->addFromString($filename, $content); return $error; } } ?>
« previous php.pear.cvs (#41925) next »