Login   Register  
PHP Classes
elePHPant
Icontem

File: bin/example_DOM.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Stefan Lwe  >  Reingold Tilford  >  bin/example_DOM.php  >  Download  
File: bin/example_DOM.php
Role: Example script
Content type: text/plain
Description: example script that plots the DOM node structure of a given HTML document.
Class: Reingold Tilford
Render tree structures in several image formats
Author: By
Last change: made inclusion of bootstrap script more robust
Date: 2011-11-10 09:09
Size: 2,096 bytes
 

Contents

Class file image Download
<?php

/**
 * This example exports the DOM tree of any given web site's HTML code to an SVG image.
 *
 * Just direct your browser to this file.
 */

use Utils\Autoload\Autoloader;
use 
ReingoldTilford\Algorithms\ReingoldTilfordAlgorithm;
use 
ReingoldTilford\Models\DomTreeModel;
use 
ReingoldTilford\Plotters\Plotter;
use 
ReingoldTilford\Styles\TreeStyle;
use 
Utils\Graphics2D\DrawingPanes\SvgDrawingPane;
use 
Utils\Geom\Dimension;
use 
Utils\Color\RgbColor;

require_once 
__DIR__.'/../app/bootstrap.inc';

header("Content-type: image/svg+xml");

$htmlBody getHtmlBodyElement('http://www.phpclasses.org/');

// get the default style for a tree ...
$style = new TreeStyle();
// ... but do a bit of customisation
$style->shapeStyle->setWidth(48);
$style->shapeStyle->setHeight(30);
$style->shapeStyle->setColor(new RgbColor(232232232));
$style->shapeStyle->border->setWidth(3);
$style->shapeStyle->border->setColor(new RgbColor(50140198));
$style->edgeStyle->setWidth(3);

// get a new instance of the algorithm
$algorithm  = new ReingoldTilfordAlgorithm();

// get the model for the DOM tree, so that the algorithm can traverse it
$model      = new DomTreeModel($htmlBody);

// calculate the layout of the tree
$layout     $algorithm->getLayout($model$style->orientation)->normalize();

// pass it to the plotter
$plotter    = new Plotter($model$layoutnullnullnull);

// create a new SVG document
$document   = new SvgDrawingPane($layout->getDimension($style));

// plot the tree onto the document
$plotter->plot($style$document);

// print the document
echo $document->save();

/**
 * This helper function gets the body element from a given website
 *
 * Obviously, this call can fail, but for mere demonstration purposes, any error handling has been left out for brevity.
 *
 * @param type $url
 * @return type
 */
function getHtmlBodyElement($url)
{
    
error_reporting(0);
    
$domDocument = new DOMDocument();

    
$domDocument->loadHTML(file_get_contents($url));
    
error_reporting(E_ALL E_STRICT);

    return 
$domDocument->getElementsByTagName('body')->item(0);
}