예제 #1
0
 protected function importFromODT($styles_xml_content, $root_element)
 {
     if (empty($styles_xml_content) || empty($root_element)) {
         return false;
     }
     // Only import known style elements
     switch ($root_element) {
         case 'office:styles':
         case 'office:automatic-styles':
             $style_elements = XMLUtil::getElementContent($root_element, $styles_xml_content, $end);
             break;
         default:
             return false;
     }
     $pos = 0;
     $max = strlen($style_elements);
     while ($pos < $max) {
         $xml_code = XMLUtil::getNextElement($element, substr($style_elements, $pos), $end);
         if ($xml_code == NULL) {
             break;
         }
         $pos += $end + 1;
         // Create new ODTStyle
         $object = ODTStyle::importODTStyle($xml_code);
         if ($object != NULL) {
             // Success, add it
             switch ($root_element) {
                 case 'office:styles':
                     $this->addStyle($object);
                     break;
                 case 'office:automatic-styles':
                     $this->addAutomaticStyle($object);
                     break;
             }
         }
     }
     return true;
 }
 /**
  * Create new style by importing ODT style definition.
  *
  * @param  $xmlCode Style definition in ODT XML format
  * @return ODTStyle New specific style
  */
 public static function importODTStyle($xmlCode)
 {
     $style = new ODTParagraphStyle();
     $attrs = 0;
     $open = XMLUtil::getElementOpenTag('style:style', $xmlCode);
     if (!empty($open)) {
         $attrs += $style->importODTStyleInternal(ODTStyleStyle::getStyleProperties(), $open, $style->style_properties);
     } else {
         $open = XMLUtil::getElementOpenTag('style:default-style', $xmlCode);
         if (!empty($open)) {
             $style->setDefault(true);
             $attrs += $style->importODTStyleInternal(ODTStyleStyle::getStyleProperties(), $open, $style->style_properties);
         }
     }
     $open = XMLUtil::getElementOpenTag('style:paragraph-properties', $xmlCode);
     if (!empty($open)) {
         $attrs += $style->importODTStyleInternal(self::$paragraph_fields, $xmlCode);
     }
     $open = XMLUtil::getElementOpenTag('style:text-properties', $xmlCode);
     if (!empty($open)) {
         $attrs += $style->importODTStyleInternal(ODTTextStyle::getTextProperties(), $open, $style->text_properties);
     }
     // Get all tab-stops.
     $tabs = XMLUtil::getElementContent('style:tab-stops', $xmlCode);
     if ($tabs != NULL) {
         $max = strlen($tabs);
         $pos = 0;
         $index = 0;
         $tab = XMLUtil::getElement('style:tab-stop', $tabs, $end);
         $pos = $end;
         while ($tab != NULL) {
             $style->tab_stops[$index] = array();
             $attrs += $style->importODTStyleInternal(self::$tab_stop_fields, $tab, $style->tab_stops[$index]);
             $index++;
             $tab = XMLUtil::getElement('style:tab-stop', substr($tabs, $pos), $end);
             $pos += $end;
         }
     }
     // If style has no meaningfull content then throw it away
     if ($attrs == 0) {
         return NULL;
     }
     return $style;
 }
 /**
  * Test function getElement()
  */
 public function test_getElementContent_3()
 {
     $xmlCode = '</peng><abc peng="dsfg"></abc><anotherOne></anotherOne>';
     $found = XMLUtil::getElementContent('abc', $xmlCode, $end);
     $this->assertNull($found);
     $this->assertEquals(strlen($xmlCode) - strlen('<anotherOne></anotherOne>'), $end);
 }
 /**
  * Create new style by importing ODT style definition.
  *
  * @param  $xmlCode Style definition in ODT XML format
  * @return ODTStyle New specific style
  */
 public static function importODTStyle($xmlCode)
 {
     $style = new ODTMasterPageStyle();
     // Get attributes for element 'style:master-page'
     $open = XMLUtil::getElementOpenTag('style:master-page', $xmlCode);
     if (!empty($open)) {
         $style->importODTStyleInternal(self::$master_fields, $open, $style->master_style);
     }
     // Get attributes for element 'style:header'
     $open = XMLUtil::getElementOpenTag('style:header', $xmlCode);
     if (!empty($open)) {
         $style->importODTStyleInternal(self::$header_footer_fields, $open, $style->style_header);
         $content_header = XMLUtil::getElementContent('style:header', $xmlCode);
     }
     // Get attributes for element 'style:footer'
     $open = XMLUtil::getElementOpenTag('style:footer', $xmlCode);
     if (!empty($open)) {
         $style->importODTStyleInternal(self::$header_footer_fields, $open, $style->style_footer);
         $content_footer = XMLUtil::getElementContent('style:footer', $xmlCode);
     }
     // Get attributes for element 'style:header-left'
     $open = XMLUtil::getElementOpenTag('style:header-left', $xmlCode);
     if (!empty($open)) {
         $style->importODTStyleInternal(self::$header_footer_fields, $open, $style->style_header_left);
         $content_header_left = XMLUtil::getElementContent('style:header-left', $xmlCode);
     }
     // Get attributes for element 'style:footer-left'
     $open = XMLUtil::getElementOpenTag('style:footer-left', $xmlCode);
     if (!empty($open)) {
         $style->importODTStyleInternal(self::$header_footer_fields, $open, $style->style_footer_left);
         $content_footer_left = XMLUtil::getElementContent('style:footer-left', $xmlCode);
     }
     return $style;
 }
 /**
  * This function checks that the document does not include extra
  * paragraphs if the wiki page starts with a table.
  * 
  * Extra paragraphs without text content would cause the document to
  * start with an empty line(s).
  */
 public function test_start_table()
 {
     // Render document with one table only.
     $files = array();
     $page = "^ Heading 1      ^ Heading 2       ^ Heading 3          ^\n";
     $page .= "| Row 1 Col 1    | Row 1 Col 2     | Row 1 Col 3        |\n";
     $ok = ODTTestUtils::getRenderedODTDocument($files, $page);
     $this->assertFalse($ok == false, 'Error rendering, creating, unpacking, reading ODT doc!');
     // Examine ODT XML content.
     // Get office:text
     $office_text = XMLUtil::getElementContent('office:text', $files['content-xml']);
     $this->assertFalse($office_text == NULL, 'Element "office:text" not found!');
     // Get first paragraph
     $paragraph = XMLUtil::getElementContent('text:p', $office_text);
     // Get first table and first table paragraph
     $table = XMLUtil::getElementContent('table:table', $office_text);
     $table_paragraph = XMLUtil::getElementContent('text:p', $table);
     $found = preg_match('/Heading 1/', $table_paragraph);
     $this->assertFalse($found != 1, "First table paragraph does not include first heading!");
     $this->assertFalse($table_paragraph != $paragraph, "First table paragraph is not the first paragraph!");
 }
 /**
  * Create new style by importing ODT style definition.
  *
  * @param  $xmlCode Style definition in ODT XML format
  * @return ODTStyle New specific style
  */
 public static function importODTStyle($xmlCode)
 {
     $style = new ODTPageLayoutStyle();
     // Get attributes for element 'style:master-page'
     $open = XMLUtil::getElementOpenTag('style:page-layout', $xmlCode);
     if (!empty($open)) {
         $style->importODTStyleInternal(self::$page_layout_fields, $open, $style->page_layout_style);
         $childs = XMLUtil::getElementContent('style:page-layout', $xmlCode);
         if (!empty($childs)) {
             // Get attributes for element 'style:page-layout-properties'
             $open = XMLUtil::getElementOpenTag('style:page-layout-properties', $childs);
             $style->content_header = XMLUtil::getElement('style:header-style', $childs);
             $style->content_footer = XMLUtil::getElement('style:footer-style', $childs);
             if (!empty($open)) {
                 $style->importODTStyleInternal(self::$layout_props_fields, $open, $style->layout_props);
                 $childs = XMLUtil::getElementContent('style:page-layout-properties', $xmlCode);
                 if (!empty($childs)) {
                     // Get 'style:background-image'
                     $open = XMLUtil::getElementOpenTag('style:background-image', $childs);
                     if (!empty($open)) {
                         $style->importODTStyleInternal(self::$bgi_fields, $open, $style->bgi_props);
                         $style->content_bgi = XMLUtil::getElementContent('style:background-image', $childs);
                     }
                     // Get 'style:columns'
                     $open = XMLUtil::getElementOpenTag('style:columns', $childs);
                     if (!empty($open)) {
                         $style->importODTStyleInternal(self::$columns_fields, $open, $style->columns_props);
                         $style->content_columns = XMLUtil::getElementContent('style:columns', $childs);
                     }
                     // Get 'style:footnote-sep'
                     $open = XMLUtil::getElementOpenTag('style:footnote-sep', $childs);
                     if (!empty($open)) {
                         $style->importODTStyleInternal(self::$footnote_fields, $open, $style->footnote_props);
                     }
                 }
             }
         }
     }
     return $style;
 }
 /**
  * Create new style by importing ODT style definition.
  *
  * @param  $xmlCode Style definition in ODT XML format
  * @return ODTStyle New specific style
  */
 public static function importODTStyle($xmlCode)
 {
     $style = new ODTTextListStyle();
     $attrs = 0;
     $open = XMLUtil::getElementOpenTag('text:list-style', $xmlCode);
     if (!empty($open)) {
         // This properties are stored in the properties of ODTStyle
         $attrs += $style->importODTStyleInternal(self::$list_fields, $open);
         $content = XMLUtil::getElementContent('text:list-style', $xmlCode);
     }
     $pos = 0;
     $end = 0;
     $max = strlen($content);
     $text_fields = ODTTextStyle::getTextProperties();
     while ($pos < $max) {
         // Get XML code for next level.
         $level = XMLUtil::getNextElement($element, substr($content, $pos), $end);
         $level_content = XMLUtil::getNextElementContent($element, $level, $ignore);
         if (!empty($level)) {
             $list_style_properties = array();
             $list_level_properties = array();
             $label_properties = array();
             $text_properties = array();
             $properties = array();
             switch ($element) {
                 case 'text:list-level-style-number':
                     $attrs += $style->importODTStyleInternal(self::$style_number_fields, $level, $list_style_properties);
                     $list_level_style = 'number';
                     break;
                 case 'text:list-level-style-bullet':
                     $attrs += $style->importODTStyleInternal(self::$style_bullet_fields, $level, $list_style_properties);
                     $list_level_style = 'bullet';
                     break;
                 case 'text:list-level-style-image':
                     $attrs += $style->importODTStyleInternal(self::$style_image_fields, $level, $list_style_properties);
                     $list_level_style = 'image';
                     break;
             }
             $temp_content = XMLUtil::getElement('style:text-properties', $level_content);
             $attrs += $style->importODTStyleInternal($text_fields, $temp_content, $text_properties);
             $temp_content = XMLUtil::getElementOpenTag('style:list-level-properties', $level_content);
             $attrs += $style->importODTStyleInternal(self::$list_level_props_fields, $temp_content, $list_level_properties);
             $temp_content = XMLUtil::getElement('style:list-level-label-alignment', $level_content);
             $attrs += $style->importODTStyleInternal(self::$label_align_fields, $temp_content, $label_properties);
             // Assign properties array to our level array
             $level_number = $style->getPropertyInternal('level', $list_style_properties);
             $properties['list-style'] = $list_style_properties;
             $properties['list-level'] = $list_level_properties;
             $properties['label'] = $label_properties;
             $properties['text'] = $text_properties;
             $style->list_level_styles[$level_number] = $properties;
             // Set special property 'list-level-style' to remember element to encode
             // on call to toString()!
             $style->setPropertyForLevel($level_number, 'list-level-style', $list_level_style);
         }
         $pos += $end;
     }
     // If style has no meaningfull content then throw it away
     if ($attrs == 0) {
         return NULL;
     }
     return $style;
 }
 /**
  * This function checks the rendering of deleted text.
  */
 public function test_deleted_text()
 {
     $files = array();
     $ok = ODTTestUtils::getRenderedODTDocument($files, '<del>This is strike-through text.</del>');
     $this->assertFalse($ok == false, 'Error rendering, creating, unpacking, reading ODT doc!');
     // Examine ODT XML content.
     // Get office:text
     $office_text = XMLUtil::getElementContent('office:text', $files['content-xml']);
     $this->assertFalse($office_text == NULL, 'Element "office:text" not found!');
     // Get first paragraph
     $paragraph = XMLUtil::getElementContent('text:p', $office_text);
     $this->assertFalse($paragraph == NULL, 'Element "text:p" not found!');
     // The paragraph shouild have a text span.
     $span_attrs = XMLUtil::getElementOpenTag('text:span', $office_text);
     $span_content = XMLUtil::getElementContent('text:span', $office_text);
     $this->assertFalse($span_content == NULL, 'Element "text:p" not found!');
     // The span should have our text content and the style for bold text
     $this->assertEquals($span_content, 'This is strike-through text.');
     $attributes = array();
     $found = XMLUtil::getAttributes($attributes, $span_attrs);
     $this->assertEquals(1, $found);
     $this->assertEquals('del', $attributes['text:style-name']);
 }