<commit_before><?php
/**
 * @package Swat
 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
 * @copyright silverorange 2004
 */
require_once('Swat/SwatObject.php');

/**
 * Generates a Swat widget tree from an XML layout file.
 */
class SwatLayout extends SwatObject {

	private $widgets;
	private $toplevel = null;

	/**
	 * @param string $filename Filename of the layout XML file to load.
	 */
	function __construct($filename) {
		$xmlfile = null;

		if (file_exists($filename)) {
			$xmlfile = $filename;
		} else {
			$paths = explode(':', ini_get('include_path'));

			foreach ($paths as $path) {
				if (file_exists($path.'/'.$filename)) {
					$xmlfile = $path.'/'.$filename;
					break;
				}
			}
		}

		if ($xmlfile == null)
			throw new SwatException('SwatLayout: XML file not found: '.$filename);

		$xml = simplexml_load_file($xmlfile);

		$this->widgets = array();
		$widget_tree = $this->build($xml, $this->toplevel);
	}

	/**
	 * Retrieve a widget.
	 * Lookup a widget in the widget tree by name.
	 * @return SwatWidget A reference to the widget.
	 * @param string $name Name of the widget to retrieve.
	 */
	public function getWidget($name) {
		if (array_key_exists($name, $this->widgets))
			return $this->widgets[$name];
		else
			throw new SwatException(__CLASS__.": no widget named '$name'");
	}

	/**
	 * Retrieve the top widget.
	 * Lookup the widget at the root of the widget tree.
	 * @return SwatWidget A reference to the widget.
	 */
	public function getRoot() {
		return $this->toplevel;
	}

	private function build($node, $parent_widget) {
		foreach ($node->children() as $childname => $childnode) {

			$widget = $this->buildWidget($childname, $childnode, $parent_widget);

			if ($widget instanceof SwatWidget)
				$this->widgets[$widget->name] = $widget;

			if ($parent_widget == null) {
				$this->toplevel = $widget;
				$parent_widget = $widget;

			} elseif ($parent_widget instanceof SwatTableView) {
				if ($widget instanceof SwatTableViewColumn)
					$parent_widget->appendColumn($widget);
				else
					throw new SwatException('SwatLayout: Only '.
						'SwatTableViewColumns can be nested within '.
						'SwatTableViews ('.$xmlfile.')');

			} elseif ($parent_widget instanceof SwatTableViewColumn) {
				if ($widget instanceof SwatCellRenderer)
					$parent_widget->renderer = $widget;
				else
					throw new SwatException('SwatLayout: Only '.
						'SwatCellRenders can be nested within '.
						'SwatTableViewsColumns ('.$xmlfile.')');

			} else {
				$parent_widget->add($widget);
			}

			$this->build($childnode, $widget);
		}
	}<commit_msg>Make SwatLayout work if some classes have not yet been required.<commit_after><?php
/**
 * @package Swat
 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
 * @copyright silverorange 2004
 */
require_once('Swat/SwatObject.php');

/**
 * Generates a Swat widget tree from an XML layout file.
 */
class SwatLayout extends SwatObject {

	private $widgets;
	private $toplevel = null;

	/**
	 * @param string $filename Filename of the layout XML file to load.
	 */
	function __construct($filename) {
		$xmlfile = null;

		if (file_exists($filename)) {
			$xmlfile = $filename;
		} else {
			$paths = explode(':', ini_get('include_path'));

			foreach ($paths as $path) {
				if (file_exists($path.'/'.$filename)) {
					$xmlfile = $path.'/'.$filename;
					break;
				}
			}
		}

		if ($xmlfile == null)
			throw new SwatException('SwatLayout: XML file not found: '.$filename);

		$xml = simplexml_load_file($xmlfile);

		$this->widgets = array();
		$widget_tree = $this->build($xml, $this->toplevel);
	}

	/**
	 * Retrieve a widget.
	 * Lookup a widget in the widget tree by name.
	 * @return SwatWidget A reference to the widget.
	 * @param string $name Name of the widget to retrieve.
	 */
	public function getWidget($name) {
		if (array_key_exists($name, $this->widgets))
			return $this->widgets[$name];
		else
			throw new SwatException(__CLASS__.": no widget named '$name'");
	}

	/**
	 * Retrieve the top widget.
	 * Lookup the widget at the root of the widget tree.
	 * @return SwatWidget A reference to the widget.
	 */
	public function getRoot() {
		return $this->toplevel;
	}

	private function build($node, $parent_widget) {
		foreach ($node->children() as $childname => $childnode) {

			$widget = $this->buildWidget($childname, $childnode, $parent_widget);

			if (class_exists('SwatWidget') && $widget instanceof SwatWidget)
				$this->widgets[$widget->name] = $widget;

			if ($parent_widget == null) {
				$this->toplevel = $widget;
				$parent_widget = $widget;

			} elseif (class_exists('SwatTableView') && 
				$parent_widget instanceof SwatTableView) {

				if ($widget instanceof SwatTableViewColumn)
					$parent_widget->appendColumn($widget);
				else
					throw new SwatException('SwatLayout: Only '.
						'SwatTableViewColumns can be nested within '.
						'SwatTableViews ('.$xmlfile.')');

			} elseif (class_exists('SwatTableViewColumn') &&
				$parent_widget instanceof SwatTableViewColumn) {

				if ($widget instanceof SwatCellRenderer)
					$parent_widget->renderer = $widget;
				else
					throw new SwatException('SwatLayout: Only '.
						'SwatCellRenders can be nested within '.
						'SwatTableViewsColumns ('.$xmlfile.')');

			} else {
				$parent_widget->add($widget);
			}

			$this->build($childnode, $widget);
		}
	}
{'fraction_non_alphanumeric': 0.14150943396226415, 'fraction_numerical': 0.0014798372179060304, 'mean_word_length': 3.5860899067005936, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 20}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5243589', 'n_tokens_mistral': 1993, 'n_tokens_neox': 1687, 'n_words': 461}