HtmlAssembler.class.php

Go to the documentation of this file.
00001 <?php
00002 /***************************************************************************
00003  *   Copyright (C) 2007 by Ivan Y. Khvostishkov                            *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU Lesser General Public License as        *
00007  *   published by the Free Software Foundation; either version 3 of the    *
00008  *   License, or (at your option) any later version.                       *
00009  *                                                                         *
00010  ***************************************************************************/
00011 /* $Id: HtmlAssembler.class.php 4687 2007-12-09 18:57:18Z voxus $ */
00012 
00016     final class HtmlAssembler
00017     {
00018         private $tags   = null;
00019         
00020         public function __construct($tags)
00021         {
00022             Assert::isTrue(current($tags) instanceof SgmlToken);
00023             
00024             $this->tags = $tags;
00025         }
00026         
00027         public static function makeTag(SgmlToken $tag)
00028         {
00029             if ($tag instanceof Cdata)
00030                 $result = $tag->getData();
00031             elseif ($tag instanceof SgmlIgnoredTag) {
00032                 Assert::isNotNull($tag->getId());
00033                 
00034                 $result = '<'.$tag->getId()
00035                     .$tag->getCdata()->getData()
00036                     .$tag->getEndMark().'>';
00037                 
00038             } elseif ($tag instanceof SgmlOpenTag) {
00039                 Assert::isNotNull($tag->getId());
00040                 
00041                 $attributes = self::getAttributes($tag);
00042                 
00043                 $result = '<'.$tag->getId()
00044                     .($attributes ? ' '.$attributes : null)
00045                     .($tag->isEmpty() ? '/' : null).'>';
00046                 
00047             } elseif ($tag instanceof SgmlEndTag) {
00048                 $result = '</'.$tag->getId().'>';
00049                 
00050             } else
00051                 throw new WrongArgumentException(
00052                     "don't know how to assemble tag class '"
00053                     .get_class($tag)."'"
00054                 );
00055             
00056             return $result;
00057         }
00058         
00059         public static function makeDomNode(DOMNode $node)
00060         {
00061                 $result = null;
00062                 
00063                 if ($node instanceof DOMElement) {
00064                         
00065                         $result = '<'.$node->nodeName;
00066                         
00067                         $attributes = self::getDomAttributes($node);
00068                         
00069                         if ($attributes)
00070                                 $result .= ' '.$attributes;
00071                         
00072                         if (!$node->firstChild) {
00073                                 $result .= ' />';
00074                         } else {
00075                                 $result .= '>';
00076                         }
00077                         
00078                         $childNode = $node->firstChild;
00079                         
00080                         while ($childNode) {
00081                                 $result .= self::makeDomNode($childNode);
00082                                 $childNode = $childNode->nextSibling;
00083                         }
00084                         
00085                         if ($node->firstChild)
00086                                 $result .= '</'.$node->nodeName.'>';
00087                         
00088                 } elseif ($node instanceof DOMCharacterData) {
00089                         
00090                         $result = $node->data;
00091                         
00092                 } else {
00093                         throw new UnimplementedFeatureException(
00094                                 'assembling of '.get_class($node).' is not implemented yet'
00095                         );
00096                 }
00097                 
00098                 return $result;
00099         }
00100         
00101         public function getHtml()
00102         {
00103             $result = null;
00104             
00105             foreach ($this->tags as $tag) {
00106                 $result .= self::makeTag($tag);
00107             }
00108             
00109             return $result;
00110         }
00111         
00112         private static function getAttributes(SgmlOpenTag $tag)
00113         {
00114             $attributes = array();
00115             
00116             foreach ($tag->getAttributesList() as $name => $value) {
00117                 if ($value === null)
00118                     $quotedValue = null;
00119                 else
00120                     // FIXME: is multibyte safe?
00121                     $quotedValue = '="'.str_replace('"', '&quot;', $value).'"';
00122                 
00123                 $attributes[] = $name.$quotedValue;
00124             }
00125             
00126             return implode(' ', $attributes);
00127         }
00128         
00129         private static function getDomAttributes(DOMNode $node)
00130         {
00131             $result = null;
00132             
00133             $attributes = array();
00134             
00135             if ($node->attributes) {
00136                 $i = 0;
00137                 
00138                 while ($item = $node->attributes->item($i)) {
00139                     $attributes[] = $item->name.'="'.$item->value.'"';
00140                     
00141                     ++$i;
00142                 }
00143             }
00144             
00145             if ($attributes)
00146                 $result = implode(' ', $attributes);
00147             
00148             return $result;
00149         }
00150     }
00151 ?>

Generated on Sun Dec 9 21:56:24 2007 for onPHP by  doxygen 1.5.4