Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 42 |
|
0.00% |
0 / 6 |
CRAP | |
0.00% |
0 / 1 |
| taoItems_helpers_Xslt | |
0.00% |
0 / 42 |
|
0.00% |
0 / 6 |
210 | |
0.00% |
0 / 1 |
| quickTransform | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
| getProcessor | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| prepareXsl | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| transform | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
20 | |||
| getDOM | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| addParams | |
0.00% |
0 / 20 |
|
0.00% |
0 / 1 |
30 | |||
| 1 | <?php |
| 2 | |
| 3 | /* |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; under version 2 |
| 7 | * of the License (non-upgradable). |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | * |
| 18 | * Copyright (c) 2002-2008 (original work) Public Research Centre Henri Tudor & University of Luxembourg |
| 19 | * (under the project TAO & TAO2); |
| 20 | * 2008-2010 (update and modification) Deutsche Institut für Internationale Pädagogische Forschung |
| 21 | * (under the project TAO-TRANSFER); |
| 22 | * 2009-2012 (update and modification) Public Research Centre Henri Tudor |
| 23 | * (under the project TAO-SUSTAIN & TAO-DEV); |
| 24 | */ |
| 25 | |
| 26 | /** |
| 27 | * Helper for automatisation of Xsl transformation |
| 28 | * @access public |
| 29 | * @author Matteo Melis |
| 30 | * @package taoItems |
| 31 | |
| 32 | */ |
| 33 | class taoItems_helpers_Xslt |
| 34 | { |
| 35 | // namespace for the set parameters function |
| 36 | public const PARAMS_NS = ''; |
| 37 | // xlst namespace short and full |
| 38 | public const XSLT_NS = 'http://www.w3.org/1999/XSL/Transform'; |
| 39 | public const XSLT_SHORT_NS = 'xsl'; |
| 40 | |
| 41 | /** |
| 42 | * Transform the given xml with the given stylesheet adding params if necessary |
| 43 | * |
| 44 | * @param string $xml the xml raw or filepath |
| 45 | * @param string $xsl the xsl raw or filepath |
| 46 | * @param array $params key = name of param and value = value of param |
| 47 | * @return string the result of transformation |
| 48 | */ |
| 49 | public static function quickTransform($xml, $xsl, $params = []) |
| 50 | { |
| 51 | // load xml |
| 52 | $xml_dom = self::getDOM($xml); |
| 53 | // get the xsl preparated |
| 54 | $xsl_dom = self::prepareXsl($xsl, $params); |
| 55 | // return the transformation |
| 56 | return self::transform($xml_dom, $xsl_dom, $params); |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * return the xslt processor with the xsl stylesheet loaded |
| 61 | * @param DOMDocument $xsl |
| 62 | * @return XSLTProcessor |
| 63 | */ |
| 64 | public static function getProcessor($xsl_dom) |
| 65 | { |
| 66 | // get the processor and import stylesheet xsl |
| 67 | $xsl_processor = new XSLTProcessor(); |
| 68 | $xsl_processor->importStylesheet($xsl_dom); |
| 69 | return $xsl_processor; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * prepare the xsl stylesheet and add params dynamically |
| 74 | * @param string $xsl |
| 75 | * @param array $params |
| 76 | * @return DOMDocument |
| 77 | */ |
| 78 | public static function prepareXsl($xsl, $params = []) |
| 79 | { |
| 80 | // load xsl |
| 81 | $xsl_dom = self::getDOM($xsl); |
| 82 | // a way to use param xsl dynamically |
| 83 | return self::addParams($xsl_dom, $params); // voir avec lionel si peut rester la |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * |
| 88 | * @param mixed $xml (string or DOMDocument |
| 89 | * @param XSLTProcessor $xsl_proc |
| 90 | * @param array $params |
| 91 | * @return string the result of transformation |
| 92 | */ |
| 93 | public static function transform($xml, $xsl, $params = []) |
| 94 | { |
| 95 | // if xml is string tranform to DOM |
| 96 | if (is_string($xml)) { |
| 97 | $xml = self::getDOM($xml); |
| 98 | } |
| 99 | // if xsl is string tranform to DOM |
| 100 | if (is_string($xsl)) { |
| 101 | $xsl = self::getDOM($xsl); |
| 102 | } |
| 103 | //get the processor with the stylesheet |
| 104 | $xsl_proc = self::getProcessor($xsl); |
| 105 | // set values for params |
| 106 | if (sizeof($params)) { |
| 107 | $xsl_proc->setParameter(self::PARAMS_NS, $params); |
| 108 | } |
| 109 | // transformation |
| 110 | return $xsl_proc->transformToXML($xml); |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * return the DOM corresponding to the parameter |
| 115 | * @param string $str |
| 116 | * @return DOMDocument |
| 117 | */ |
| 118 | public static function getDOM($str) |
| 119 | { |
| 120 | //create domdocument |
| 121 | $dom = new DOMDocument(); |
| 122 | //load by file or string |
| 123 | is_file($str) ? $dom->load($str) : $dom->loadXML($str); |
| 124 | // return the DOMDoc |
| 125 | return $dom; |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * insert the tag param for each params noexistent in xsl stylesheet |
| 130 | * @param DOMPDocument $dom |
| 131 | * @return DOMDocument |
| 132 | */ |
| 133 | private static function addParams($dom, $params) |
| 134 | { |
| 135 | if (!sizeof($params)) { |
| 136 | return $dom; |
| 137 | } |
| 138 | // get xpath |
| 139 | $xpath = new DOMXPath($dom); |
| 140 | //registering namespace xsl |
| 141 | $xpath->registerNamespace(self::XSLT_SHORT_NS, self::XSLT_NS); |
| 142 | // get the params tag direct child of the stylesheet (don't touch other params as template for example) |
| 143 | $items = $xpath->query( |
| 144 | '/' . self::XSLT_SHORT_NS . ':stylesheet|' . self::XSLT_SHORT_NS . ':transform/' |
| 145 | . self::XSLT_SHORT_NS . ':param' |
| 146 | ); |
| 147 | // list existing to don't duplicate it |
| 148 | $existing = []; |
| 149 | foreach ($items as $param) { |
| 150 | $existing[] = $param->getAttribute('name'); |
| 151 | } |
| 152 | // for each item in param |
| 153 | foreach ($params as $name => $val) { |
| 154 | // if not exist in dom |
| 155 | if (!in_array($name, $existing)) { |
| 156 | // we add it to the dom |
| 157 | $node = $dom->createElementNS(self::XSLT_NS, self::XSLT_SHORT_NS . ":param"); |
| 158 | $node->setAttribute('name', $name); |
| 159 | // log to say param added dynamically |
| 160 | common_Logger::w( |
| 161 | 'Parameters "' . $name . '"added automatically by the ' . get_class() |
| 162 | . ':addParams \'s function because it was missing in the xsl stylesheet' |
| 163 | ); |
| 164 | } |
| 165 | } |
| 166 | return $dom; |
| 167 | } |
| 168 | } |