/**
  * Test function getElement()
  */
 public function test_getElement_5()
 {
     $xmlCode = '</peng><a peng="dsfg"/><anotherOne></anotherOne>';
     $found = XMLUtil::getElement('a', $xmlCode, $end);
     $this->assertNotNull($found);
     $this->assertEquals('<a peng="dsfg"/>', $found);
     $this->assertEquals(strlen($xmlCode) - strlen('<anotherOne></anotherOne>'), $end);
     $this->assertEquals(23, $end);
 }
예제 #2
0
 /**
  * Build the document from scratch.
  * (code taken from old function 'document_end_scratch')
  *
  * @param string      $doc
  * @param string      $autostyles
  * @param array       $commonstyles
  * @param string      $meta
  * @param string      $userfields
  * @param ODTStyleSet $styleset
  * @return mixed
  */
 public function build($doc = null, $meta = null, $userfields = null, $pagestyles = null)
 {
     // add defaults
     $this->ZIP->add_File('application/vnd.oasis.opendocument.text', 'mimetype', 0);
     $this->ZIP->add_File($meta, 'meta.xml');
     $this->ZIP->add_File($this->settings->getContent(), 'settings.xml');
     $autostyles = $this->styleset->export('office:automatic-styles');
     $commonstyles = $this->styleset->export('office:styles');
     $value = '<' . '?xml version="1.0" encoding="UTF-8"?' . ">\n";
     $value .= '<office:document-content ';
     $value .= 'xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0" ';
     $value .= 'xmlns:style="urn:oasis:names:tc:opendocument:xmlns:style:1.0" ';
     $value .= 'xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0" ';
     $value .= 'xmlns:table="urn:oasis:names:tc:opendocument:xmlns:table:1.0" ';
     $value .= 'xmlns:draw="urn:oasis:names:tc:opendocument:xmlns:drawing:1.0" ';
     $value .= 'xmlns:fo="urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0" ';
     $value .= 'xmlns:xlink="http://www.w3.org/1999/xlink" ';
     $value .= 'xmlns:dc="http://purl.org/dc/elements/1.1/" ';
     $value .= 'xmlns:meta="urn:oasis:names:tc:opendocument:xmlns:meta:1.0" ';
     $value .= 'xmlns:number="urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0" ';
     $value .= 'xmlns:svg="urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0" ';
     $value .= 'xmlns:chart="urn:oasis:names:tc:opendocument:xmlns:chart:1.0" ';
     $value .= 'xmlns:dr3d="urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0" ';
     $value .= 'xmlns:math="http://www.w3.org/1998/Math/MathML" ';
     $value .= 'xmlns:form="urn:oasis:names:tc:opendocument:xmlns:form:1.0" ';
     $value .= 'xmlns:script="urn:oasis:names:tc:opendocument:xmlns:script:1.0" ';
     $value .= 'xmlns:dom="http://www.w3.org/2001/xml-events" ';
     $value .= 'xmlns:xforms="http://www.w3.org/2002/xforms" ';
     $value .= 'xmlns:xsd="http://www.w3.org/2001/XMLSchema" ';
     $value .= 'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ';
     $value .= 'office:version="1.0">';
     $value .= '<office:scripts/>';
     $value .= '<office:font-face-decls>';
     $value .= '<style:font-face style:name="Tahoma1" svg:font-family="Tahoma"/>';
     $value .= '<style:font-face style:name="Lucida Sans Unicode" svg:font-family="&apos;Lucida Sans Unicode&apos;" style:font-pitch="variable"/>';
     $value .= '<style:font-face style:name="Tahoma" svg:font-family="Tahoma" style:font-pitch="variable"/>';
     $value .= '<style:font-face style:name="Times New Roman" svg:font-family="&apos;Times New Roman&apos;" style:font-family-generic="roman" style:font-pitch="variable"/>';
     $value .= '</office:font-face-decls>';
     $value .= $autostyles;
     $value .= '<office:body>';
     $value .= '<office:text>';
     $value .= '<office:forms form:automatic-focus="false" form:apply-design-mode="false"/>';
     $value .= '<text:sequence-decls>';
     $value .= '<text:sequence-decl text:display-outline-level="0" text:name="Illustration"/>';
     $value .= '<text:sequence-decl text:display-outline-level="0" text:name="Table"/>';
     $value .= '<text:sequence-decl text:display-outline-level="0" text:name="Text"/>';
     $value .= '<text:sequence-decl text:display-outline-level="0" text:name="Drawing"/>';
     $value .= '</text:sequence-decls>';
     $value .= $userfields;
     $value .= $doc;
     $value .= '</office:text>';
     $value .= '</office:body>';
     $value .= '</office:document-content>';
     $this->ZIP->add_File($value, 'content.xml');
     // Edit 'styles.xml'
     $value = io_readFile(DOKU_PLUGIN . 'odt/styles.xml');
     // Add page styles
     $page = '';
     foreach ($pagestyles as $name => $layout_name) {
         $page .= '<style:master-page style:name="' . $name . '" style:page-layout-name="' . $layout_name . '"/>';
     }
     if (!empty($page)) {
         $value = str_replace('</office:master-styles>', $page . '</office:master-styles>', $value);
     }
     // Add common styles.
     $original = XMLUtil::getElement('office:styles', $value);
     $value = str_replace($original, $commonstyles, $value);
     // Add automatic styles.
     $value = str_replace('<office:automatic-styles/>', $autostyles, $value);
     $this->ZIP->add_File($value, 'styles.xml');
     // build final manifest
     $this->ZIP->add_File($this->manifest->getContent(), 'META-INF/manifest.xml');
 }
 /**
  * Build the document from the template.
  * (code taken from old function 'document_end_scratch')
  *
  * @param string      $doc
  * @param string      $autostyles
  * @param array       $commonstyles
  * @param string      $meta
  * @param string      $userfields
  * @param ODTDefaultStyles $styleset
  * @return mixed
  */
 public function build($doc = null, $meta = null, $userfields = null, $pagestyles = null)
 {
     // for the temp dir
     global $ID;
     // Temp dir
     if (is_dir($this->config->getParam('tmpdir'))) {
         // version > 20070626
         $temp_dir = $this->config->getParam('tmpdir');
     } else {
         // version <= 20070626
         $temp_dir = $this->config->getParam('savedir') . '/cache/tmp';
     }
     $temp_dir = $temp_dir . "/odt/" . str_replace(':', '-', $ID);
     if (is_dir($temp_dir)) {
         io_rmdir($temp_dir, true);
     }
     io_mkdir_p($temp_dir);
     // Extract template
     $template_path = $this->config->getParam('mediadir') . '/' . $this->directory . "/" . $this->template;
     $ok = $this->ZIP->Extract($template_path, $temp_dir);
     if ($ok == -1) {
         throw new Exception(' Error extracting the zip archive:' . $template_path . ' to ' . $temp_dir);
     }
     // Import styles from ODT template
     //$this->styleset->importFromODTFile($temp_dir.'/content.xml', 'office:automatic-styles');
     //$this->styleset->importFromODTFile($temp_dir.'/styles.xml', 'office:styles');
     $autostyles = $this->styleset->export('office:automatic-styles');
     $commonstyles = $this->styleset->export('office:styles');
     // Prepare content
     $missingfonts = $this->styleset->getMissingFonts($temp_dir . '/styles.xml');
     // Insert content
     $old_content = io_readFile($temp_dir . '/content.xml');
     if (strpos($old_content, 'DOKUWIKI-ODT-INSERT') !== FALSE) {
         // Replace the mark
         $this->_odtReplaceInFile('/<text:p[^>]*>DOKUWIKI-ODT-INSERT<\\/text:p>/', $doc, $temp_dir . '/content.xml', true);
     } else {
         // Append to the template
         $this->_odtReplaceInFile('</office:text>', $doc . '</office:text>', $temp_dir . '/content.xml');
     }
     // Cut off unwanted content
     if (strpos($old_content, 'DOKUWIKI-ODT-CUT-START') !== FALSE && strpos($old_content, 'DOKUWIKI-ODT-CUT-STOP') !== FALSE) {
         $this->_odtReplaceInFile('/DOKUWIKI-ODT-CUT-START.*DOKUWIKI-ODT-CUT-STOP/', '', $temp_dir . '/content.xml', true);
     }
     // Insert userfields
     if (strpos($old_content, "text:user-field-decls") === FALSE) {
         // no existing userfields
         $this->_odtReplaceInFile('/<office:text([^>]*)>/U', '<office:text\\1>' . $userfields, $temp_dir . '/content.xml', TRUE);
     } else {
         $this->_odtReplaceInFile('</text:user-field-decls>', substr($userfields, 23), $temp_dir . '/content.xml');
     }
     // Insert styles & fonts
     $value = io_readFile($temp_dir . '/content.xml');
     $original = XMLUtil::getElement('office:automatic-styles', $value);
     $this->_odtReplaceInFile($original, $autostyles, $temp_dir . '/content.xml');
     $value = io_readFile($temp_dir . '/styles.xml');
     $original = XMLUtil::getElement('office:automatic-styles', $value);
     $this->_odtReplaceInFile($original, $autostyles, $temp_dir . '/styles.xml');
     $value = io_readFile($temp_dir . '/styles.xml');
     $original = XMLUtil::getElement('office:styles', $value);
     $this->_odtReplaceInFile($original, $commonstyles, $temp_dir . '/styles.xml');
     $this->_odtReplaceInFile('</office:font-face-decls>', $missingfonts . '</office:font-face-decls>', $temp_dir . '/styles.xml');
     // Insert page styles
     $page = '';
     foreach ($pagestyles as $name => $layout_name) {
         $page .= '<style:master-page style:name="' . $name . '" style:page-layout-name="' . $layout_name . '"/>';
     }
     if (!empty($page)) {
         $this->_odtReplaceInFile('</office:master-styles>', $page . '</office:master-styles>', $temp_dir . '/styles.xml');
     }
     // Add manifest data
     $this->_odtReplaceInFile('</manifest:manifest>', $this->manifest->getExtraContent() . '</manifest:manifest>', $temp_dir . '/META-INF/manifest.xml');
     // Build the Zip
     $this->ZIP->Compress(null, $temp_dir, null);
     io_rmdir($temp_dir, 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;
 }
 /**
  * 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 ODTTextOutlineStyle();
     $attrs = 0;
     $open = XMLUtil::getElementOpenTag('text:outline-style', $xmlCode);
     if (!empty($open)) {
         // This properties are stored in the properties of ODTStyle
         $attrs += $style->importODTStyleInternal(self::$outline_fields, $open);
     }
     $pos = 0;
     $end = 0;
     $max = strlen($xmlCode);
     $level = XMLUtil::getElement('text:outline-level-style', substr($xmlCode, $pos), $end);
     $pos += $end;
     $text_fields = ODTTextStyle::getTextProperties();
     $check = 0;
     while ($level != NULL) {
         // We can have multiple level definitons with all the same properties.
         // So we store this in our own array. The "text:level" is the array key.
         if (!empty($level)) {
             $properties = array();
             $attrs += $style->importODTStyleInternal($text_fields, $level, $properties);
             $attrs += $style->importODTStyleInternal(self::$outline_fields, $level, $properties);
             // Assign properties array to our level array
             $level_number = $style->getPropertyInternal('level', $properties);
             $style->outline_level_styles[$level_number] = $properties;
         }
         // Get XML code for next level.
         $level = XMLUtil::getElement('text:outline-level-style', substr($xmlCode, $pos), $end);
         $pos += $end;
         if ($pos >= $max) {
             break;
         }
     }
     // If style has no meaningfull content then throw it away
     if ($attrs == 0) {
         return NULL;
     }
     return $style;
 }
 /**
  * Test setProperty() and toString(), including tab-stops.
  * This is a test case for issue #123.
  */
 public function test_set_toc_paragraph()
 {
     $indent = 2;
     $properties = array();
     $properties['style-name'] = 'TOC-Test';
     $properties['style-parent'] = 'Index';
     $properties['style-class'] = 'index';
     $properties['style-position'] = 17 - $indent . 'cm';
     $properties['style-type'] = 'right';
     $properties['style-leader-style'] = 'dotted';
     $properties['style-leader-text'] = '.';
     $properties['margin-left'] = $indent . 'cm';
     $properties['margin-right'] = '0cm';
     $properties['text-indent'] = '0cm';
     // This variable is just used to show the expected result but is not used for test comparsion.
     // We explicitly parse the exported XML code to be independent from attributes position.
     // The attrbitues positions might change in the future if ODTParagraphStyle.php is changed.
     $expected = '<style:style style:name="TOC-Test" style:parent-style-name="Index" style:class="index" style:family="paragraph" >';
     $expected .= '<style:paragraph-properties fo:margin-left="2cm" fo:margin-right="0cm" fo:text-indent="0cm" >';
     $expected .= '<style:tab-stops>';
     $expected .= '<style:tab-stop style:position="15cm" style:type="right" style:leader-style="dotted" style:leader-text="." />';
     $expected .= '</style:tab-stops>';
     $expected .= '</style:paragraph-properties>';
     $expected .= '</style:style>';
     // Create style, set all properties and get XML code of the style
     $style = new ODTParagraphStyle();
     $this->assertNotNull($style);
     foreach ($properties as $key => $value) {
         $style->setProperty($key, $value);
     }
     $style_string = $style->toString();
     // We should have the following elements:
     // style:style, style:paragraph-properties, style:text-properties, style:tab-stops
     $style_style = XMLUtil::getElementOpenTag('style:style', $style_string);
     $this->assertNotNull($style_style);
     $paragraph_props = XMLUtil::getElementOpenTag('style:paragraph-properties', $style_string);
     $paragraph = XMLUtil::getElement('style:paragraph-properties', $style_string);
     $this->assertNotNull($paragraph_props);
     $tab_stops_props = XMLUtil::getElement('style:tab-stops', $paragraph);
     $this->assertNotNull($tab_stops_props);
     $tab_stop_props = XMLUtil::getElementOpenTag('style:tab-stop', $tab_stops_props);
     $this->assertNotNull($tab_stop_props);
     // Check attribute values of element "style:style", see $expected
     // Remark: attribute 'style:family' must always be present even if it was not set
     $attributes = array();
     $found = XMLUtil::getAttributes($attributes, $style_style);
     $this->assertEquals(4, $found);
     $this->assertEquals('TOC-Test', $attributes['style:name']);
     $this->assertEquals('Index', $attributes['style:parent-style-name']);
     $this->assertEquals('index', $attributes['style:class']);
     $this->assertEquals('paragraph', $attributes['style:family']);
     // Check attribute values of element "style:paragraph-properties", see $expected
     $attributes = array();
     $found = XMLUtil::getAttributes($attributes, $paragraph_props);
     $this->assertEquals(3, $found);
     $this->assertEquals('2cm', $attributes['fo:margin-left']);
     $this->assertEquals('0cm', $attributes['fo:margin-right']);
     $this->assertEquals('0cm', $attributes['fo:text-indent']);
     // Check attribute values of element "style:tab-stop", see $expected
     $attributes = array();
     $found = XMLUtil::getAttributes($attributes, $tab_stop_props);
     $this->assertEquals(4, $found);
     $this->assertEquals('15cm', $attributes['style:position']);
     $this->assertEquals('right', $attributes['style:type']);
     $this->assertEquals('dotted', $attributes['style:leader-style']);
     $this->assertEquals('.', $attributes['style:leader-text']);
 }
 /**
  * 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;
 }