Permalink
Browse files

Fix examples after the implementation of request #16307.

Running the examples offered a lots of other bugs in reading elements,
clearly a sign that unittests are missing


git-svn-id: http://svn.php.net/repository/pear/packages/OpenDocument/trunk@283200 c90b9560-bf6c-de11-be94-00142212c4b1
  • Loading branch information...
1 parent 2cc7f80 commit 72a8eb4bb1c4fd219bf0358d1bedfa3ec2b87f57 @cweiske cweiske committed Jun 30, 2009
@@ -132,21 +132,22 @@ public function getChildren()
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;
- }
+ if (!$this->node instanceof DOMNode) {
+ return;
+ }
+ $childrenNodes = $this->node->childNodes;
+ foreach ($childrenNodes as $child) {
+ if ($child instanceof DOMText) {
+ $element = new OpenDocument_Element_Text($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;
}
}
}
@@ -88,8 +88,8 @@ public function __construct(DOMNode $node, OpenDocument_Document $document)
$this->level = $node->getAttributeNS(OpenDocument::NS_TEXT, 'outline-level');
$this->allowedElements = array(
- 'OpenDocument_Span',
- 'OpenDocument_Hyperlink',
+ 'OpenDocument_Element_Span',
+ 'OpenDocument_Element_Hyperlink',
);
}
@@ -81,8 +81,8 @@ public function __construct(DOMNode $node, OpenDocument_Document $document)
parent::__construct($node, $document);
$this->allowedElements = array(
- 'OpenDocument_Span',
- 'OpenDocument_Hyperlink',
+ 'OpenDocument_Element_Span',
+ 'OpenDocument_Element_Hyperlink',
);
}
View
@@ -2,7 +2,7 @@
require_once 'OpenDocument.php'; // open document class
//create a new OpenDocument Text file
-$odt = new OpenDocument;
+$odt = OpenDocument::text();
//add heading
$h = $odt->createHeading('Heading', 1);
//create paragraph
@@ -14,6 +14,7 @@
$p1->style->underlineStyle = 'dotted';
$p1->style->underlineColor = '#009999';
$p1->style->underlineWidth = '2pt';
+
//create second paragraph
$p2 = $odt->createParagraph('Paragraph 2');
//copy styles from first one
@@ -32,8 +33,9 @@
$a2 = $h->createHyperlink('Ya.ru', 'http://ya.ru');
//apply underline color to link
$a2->style->underlineColor = '#990000';
+
//save as test.odt
$odt->save('test.odt');
-echo 'saved as test.odt';
+echo "saved as test.odt\n";
?>
View
@@ -2,34 +2,34 @@
require_once 'OpenDocument.php'; // open document class
//open test.odt
-$odt = new OpenDocument('test.odt');
+$odt = OpenDocument::open('test.odt');
//output content as html
-echo toHTML($odt);
+echo toHTML($odt) . "\n";
function toHTML($obj)
{
$html = '';
foreach ($obj->getChildren() as $child) {
switch (get_class($child)) {
- case 'OpenDocument_TextElement':
+ case 'OpenDocument_Element_Text':
$html .= $child->text;
break;
- case 'OpenDocument_Paragraph':
+ case 'OpenDocument_Element_Paragraph':
$html .= '<p>';
$html .= toHTML($child);
$html .= '</p>';
break;
- case 'OpenDocument_Span':
+ case 'OpenDocument_Element_Span':
$html .= '<span>';
$html .= toHTML($child);
$html .= '</span>';
break;
- case 'OpenDocument_Heading':
+ case 'OpenDocument_Element_Heading':
$html .= '<h' . $child->level . '>';
$html .= toHTML($child);
$html .= '</h' . $child->level . '>';
break;
- case 'OpenDocument_Hyperlink':
+ case 'OpenDocument_Element_Hyperlink':
$html .= '<a href="' . $child->location . '" target="' . $child->target . '">';
$html .= toHTML($child);
$html .= '</a>';
View
@@ -2,17 +2,17 @@
require_once 'OpenDocument.php'; // open document class
//open test.odt
-$odt = new OpenDocument('test.odt');
+$odt = OpenDocument::open('test.odt');
//loop throught document children
foreach ($odt->getChildren() as $child) {
//strip headings
- if ($child instanceof OpenDocument_Heading) {
+ if ($child instanceof OpenDocument_Element_Heading) {
$child->delete();
}
}
//save as no_headings.pdt
$odt->save('no_headings.odt');
-echo 'saved as no-headings.odt';
+echo "saved as no-headings.odt\n";
?>

0 comments on commit 72a8eb4

Please sign in to comment.