public function generate() { $result = XSLProc::transform($this->xml, $this->xsl, XSLProc::XML, $this->parameters, $this->registered_php_functions); if (XSLProc::hasErrors()) { return false; } return $result; }
public function save(MessageStack $errors) { $xsl_errors = new MessageStack(); if (strlen(trim($this->parameters()->xml)) == 0) { $errors->append('xml', __('This is a required field')); } elseif (!General::validateXML($this->parameters()->xml, $xsl_errors)) { if (XSLProc::hasErrors()) { $errors->append('xml', sprintf('XSLT specified is invalid. The following error was returned: "%s near line %s"', $xsl_errors->current()->message, $xsl_errors->current()->line)); } else { $errors->append('xml', 'XSLT specified is invalid.'); } } return parent::save($errors); }
public static function validateXML($string, &$errors, $encoding = 'UTF-8') { $xsl = '<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"></xsl:template> </xsl:stylesheet>'; $xml = trim($string); if (strpos($string, '<?xml') === false) { $xml = sprintf('<?xml version="1.0" encoding="%s"?><rootelement>%s</rootelement>', $encoding, $string); } XSLProc::transform($xml, $xsl); if (XSLProc::hasErrors()) { $errors = XSLProc::getErrors(); return false; } return true; }
public function render(Register $Parameters, XMLDocument &$Document = NULL, DocumentHeaders &$Headers = NULL) { $ParameterOutput = new Register(); if (!is_null($Headers)) { $Headers->append('Content-Type', $this->{'content-type'}); } else { header('Content-Type: ' . $this->{'content-type'}); } if (is_null($Document)) { $Document = new XMLDocument(); $Document->appendChild($Document->createElement('data')); } $root = $Document->documentElement; $datasources = $events = array(); $events_wrapper = $Document->createElement('events'); $root->appendChild($events_wrapper); if (is_array($this->about()->{'events'}) && !empty($this->about()->{'events'})) { $events = $this->about()->{'events'}; } if (is_array($this->about()->{'data-sources'}) && !empty($this->about()->{'data-sources'})) { $datasources = $this->about()->{'data-sources'}; } #### # Delegate: FrontendEventsAppend # Description: Append additional Events. # Global: Yes Extension::notify('FrontendEventsAppend', '/frontend/', array('events' => &$events)); if (!empty($events)) { $postdata = General::getPostData(); $events_ordered = array(); foreach ($events as $handle) { $events_ordered[] = Event::loadFromHandle($handle); } uasort($events_ordered, array($this, '__cbSortEventsByPriority')); foreach ($events_ordered as $e) { if (!$e->canTrigger($postdata)) { continue; } $fragment = $e->trigger($ParameterOutput, $postdata); if ($fragment instanceof DOMDocument && !is_null($fragment->documentElement)) { $node = $Document->importNode($fragment->documentElement, true); $events_wrapper->appendChild($node); } } } #### # Delegate: FrontendDataSourceAppend # Description: Append additional DataSources. # Global: Yes Extension::notify('FrontendDataSourcesAppend', '/frontend/', array('datasources' => &$datasources)); // Find dependancies and order accordingly $datasource_pool = array(); $dependency_list = array(); $datasources_ordered = array(); $all_dependencies = array(); foreach ($datasources as $handle) { $datasource_pool[$handle] = Datasource::loadFromHandle($handle); $dependency_list[$handle] = $datasource_pool[$handle]->parameters()->dependencies; } $datasources_ordered = $this->__sortByDependencies($dependency_list); if (!empty($datasources_ordered)) { foreach ($datasources_ordered as $handle) { $ds = $datasource_pool[$handle]; try { $fragment = $ds->render($ParameterOutput); } catch (FrontendPageNotFoundException $e) { FrontendPageNotFoundExceptionHandler::render($e); } if ($fragment instanceof DOMDocument && !is_null($fragment->documentElement)) { $node = $Document->importNode($fragment->documentElement, true); $root->appendChild($node); } } } if ($ParameterOutput->length() > 0) { foreach ($ParameterOutput as $p) { $Parameters->{$p->key} = $p->value; } } #### # Delegate: FrontendParamsPostResolve # Description: Access to the resolved param pool, including additional parameters provided by Data Source outputs # Global: Yes Extension::notify('FrontendParamsPostResolve', '/frontend/', array('params' => $Parameters)); $element = $Document->createElement('parameters'); $root->appendChild($element); foreach ($Parameters as $key => $parameter) { if (is_array($parameter->value) && count($parameter->value) > 1) { $p = $Document->createElement($key); $p->setAttribute('value', (string) $parameter); foreach ($parameter->value as $v) { $p->appendChild($Document->createElement('item', (string) $v)); } $element->appendChild($p); } else { $element->appendChild($Document->createElement($key, (string) $parameter)); } } $template = $this->template; #### # Delegate: FrontendTemplatePreRender # Description: Access to the template source, before it is rendered. # Global: Yes Extension::notify('FrontendTemplatePreRender', '/frontend/', array('document' => $Document, 'template' => &$template)); $this->template = $template; // When the XSLT executes, it uses the CWD as set here $cwd = getcwd(); chdir(WORKSPACE); $output = XSLProc::transform($Document, $this->template, XSLProc::XML, $Parameters->toArray(), array()); chdir($cwd); if (XSLProc::hasErrors()) { throw new XSLProcException('Transformation Failed'); } /* header('Content-Type: text/plain; charset=utf-8'); $Document->formatOutput = true; print $Document->saveXML(); die(); */ return $output; }
public static function transform($xml, $xsl, $output = self::XML, array $parameters = array(), array $register_functions = array()) { self::flush(); self::$lastXML = $xml; self::$lastXSL = $xsl; $result = null; libxml_use_internal_errors(true); if ($xml instanceof DOMDocument) { $XMLDoc = $xml; } else { $XMLDoc = new DOMDocument(); $XMLDoc->loadXML($xml); } self::processLibXMLerrors(self::ERROR_XML); if ($xsl instanceof DOMDocument) { $XSLDoc = $xsl; } else { $XSLDoc = new DOMDocument(); $XSLDoc->loadXML($xsl); } if (!self::hasErrors() && $XSLDoc instanceof DOMDocument && $XMLDoc instanceof DOMDocument) { $XSLProc = new XSLTProcessor(); if (!empty($register_functions)) { $XSLProc->registerPHPFunctions($register_functions); } $XSLProc->importStyleSheet($XSLDoc); if (is_array($parameters) && !empty($parameters)) { $XSLProc->setParameter('', $parameters); } self::processLibXMLerrors(self::ERROR_XSL); if (!self::hasErrors()) { $result = $XSLProc->{'transformTo' . ($output == self::XML ? 'XML' : 'Doc')}($XMLDoc); self::processLibXMLerrors(self::ERROR_XML); } } return $result; }
protected static function __transform(DOMDocument $xml, $template = 'exception.generic.xsl') { $path = TEMPLATES . '/' . $template; if (file_exists(MANIFEST . '/templates/' . $template)) { $path = MANIFEST . '/templates/' . $template; } return XSLProc::transform($xml, file_get_contents($path), XSLProc::XML, array('root' => URL)); }
/** * Performs an XSLT transformation on a SymphonyView's $document using its * $stylesheet. * * @param string $directory * Base directory to perform the transformation in * * @return string containing result of transformation */ public function transform($directory = null) { // Set directory for performing the transformation // This is for XSLT import/include I believe. // Defaults to root views dir (/workspace/views/ or // /symphony/content/). Can be overridden if called // with $directory param. if (is_null($directory)) { $dir = $this->location; } else { $dir = $directory; } // Get current directory $cwd = getcwd(); // Move to tranformation directory chdir($dir); // Perform transformation $output = XSLProc::transform($this->document->saveXML(), $this->stylesheet->saveXML(), XSLProc::XML, array(), array()); // Move back to original directory chdir($cwd); if (XSLProc::hasErrors() && !isset($_REQUEST['debug'])) { throw new XSLProcException('Transformation Failed'); } // HACK: Simple debug output: if (isset($_REQUEST['debug'])) { $this->document->formatOutput = true; echo '<pre>', htmlentities($this->document->saveXML()); exit; } // Return result of transformation return $output; }