* @license PHP License * @package wb * @subpackage Markup */ WBClass::load( 'WBClock', 'WBMarkup_Handler' ); /** * Markup Scanner Handler: Filter * * Simple handler to test scanner * * @version 0.1.0 * @package wb * @subpackage Markup */ class WBMarkup_Handler_Test implements WBMarkup_Handler { /** * current depth * @var int */ protected $depth = 0; /** * content * @var string */ protected $content = ''; /** * profile * @var string */ protected $profile = array(); /** * receive scanner profile * * @return array */ public function getProfile() { return $this->profile; } /** * test handler on start beginning of scan * * Reset counter etc. * * @param string $content * @return bool usually true, false to stop the scanner */ public function onScanStart( &$content ) { $this->profile['content_length'] = strlen( $content ); $this->profile['timer_start'] = WBClock::now(); $this->profile['timer_elapsed'] = 0; $this->profile['depth_max'] = 0; $this->profile['depth_end'] = 0; $this->profile['tag_start'] = 0; $this->profile['tag_end'] = 0; $this->profile['entities'] = 0; $this->depth = 0; $this->content = ''; return true; } /** * test handler on start element * * @param string $ns The used namespace, if set. otherwise null * @param string $tag the tag itself * @param array $attributes the attributes of the found element * @param bool $isEmpty true if it is a start- and closing-Element (e.g.
) * @return bool usually true, false to stop the scanner */ public function onStartElement( $ns, $tag, $attributes, $isEmpty ) { $this->content .= str_repeat( ' ', $this->depth * 4 ); if( $ns ) { $ns .= ':'; } $attString = $this->attributes2HTML( $attributes ); $depthString = $this->getDepthString();; if( $isEmpty ) { $this->content .="<$ns$tag$attString $depthString />
\n"; return true; } $this->content .= "<$ns$tag$attString $depthString >
\n"; ++$this->depth; ++$this->profile['tag_start']; $this->profile['depth_max'] = max( $this->profile['depth_max'], $this->depth ); return true; } /** * test handler fo cData * * @param string $cData character data * @return bool usually true, false to stop the scanner */ public function onCharacterData( $cData ) { $this->content .= str_repeat( ' ', ( $this->depth + 1 ) * 4 ); $this->content .= 'length: ' . mb_strlen( $cData ) . "
\n"; return true; } /** * test handler for entities * * Here we count every entity or unicode element as one character. * We have to do this because otherwise it could be possible that * we're truncating inside a entity or unicode character, * e.g.   could become then &nb. * * @param string $entity the entity element, e.g. nbsp for   * @param boolean $isUnicode true = the element is a unicode string */ public function onEntityElement( $entity, $isUnicode ) { $this->content .= str_repeat( ' ', $this->depth * 5 ) . ''; if( $isUnicode ){ $this->content .= 'Unicode: '; } $this->content .= $entity .'
'; ++$this->profile['entities']; return true; } /** * test handler on end element * * @param string $ns The used namespace, if set. otherwise null * @param string $tag the tag itself * @param bool $empty defines if the tag is empty * @return bool usually true, false to stop the scanner */ public function onEndElement( $ns, $tag, $empty) { if( $empty ) { return true; } if( $ns ) { $ns .= ':'; } ++$this->profile['tag_end']; --$this->depth; if( $this->depth < 0 ) { $this->depth = 0; } $this->content .= str_repeat( ' ', $this->depth * 4 ); $depthString = $this->getDepthString();; $this->content .= "</$ns$tag $depthString >
\n"; return true; } /** * called right after scan is complete * * @return bool usually true, false to stop the scanner */ public function onScanComplete() { $this->profile['timer_elapsed'] = WBClock::stop( $this->profile['timer_start'], 1000, 6 ) . 'ms'; $this->profile['depth_end'] = $this->depth; return true; } /** * receive translated content string * * @return string */ public function getParsedContent() { return $this->content; } /** * serialize attribute list to HTML attribute string * * @param array $att attribute list * @return string serialized attributes */ protected function attributes2HTML( $att ) { if( empty( $att ) ) { return ' '; } $str = ''; foreach( $att as $k => $v ) { $str .= ' ' . $k . '="' . $v . '"'; } return $str; } /** * serialize current depth to HTML string * * @param array $att attribute list * @return string serialized attributes */ protected function getDepthString() { return 'depth="' . $this->depth . '"' . "\n"; } } ?>