PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/common/libraries/plugin/pear/phing/tasks/ext/XmlPropertyTask.php

https://bitbucket.org/chamilo/chamilo-dev/
PHP | 285 lines | 138 code | 41 blank | 106 comment | 12 complexity | dc508b7e55afbf28bb36f0f8958eae40 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, LGPL-2.1, LGPL-3.0, GPL-3.0, MIT
  1. <?php
  2. /*
  3. * $Id: XmlPropertyTask.php 971 2010-11-08 21:13:11Z mrook $
  4. *
  5. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  6. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  7. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  8. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  9. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  10. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  11. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  12. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  13. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  14. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  15. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16. *
  17. * This software consists of voluntary contributions made by many individuals
  18. * and is licensed under the LGPL. For more information please see
  19. * <http://phing.info>.
  20. */
  21. include_once 'phing/tasks/system/PropertyTask.php';
  22. /**
  23. * Task for setting properties from an XML file in buildfiles.
  24. *
  25. * @author Jonathan Bond-Caron <jbondc@openmv.com>
  26. * @version $Revision: 971 $
  27. * @package phing.tasks.ext
  28. * @since 2.4.0
  29. * @see http://ant.apache.org/manual/CoreTasks/xmlproperty.html
  30. */
  31. class XmlPropertyTask extends PropertyTask
  32. {
  33. private $_keepRoot = true;
  34. private $_collapseAttr = false;
  35. private $_delimiter = ',';
  36. /** Set a file to use as the source for properties. */
  37. public function setFile($file)
  38. {
  39. if (is_string($file))
  40. {
  41. $file = new PhingFile($file);
  42. }
  43. $this->file = $file;
  44. }
  45. /** Get the PhingFile that is being used as property source. */
  46. public function getFile()
  47. {
  48. return $this->file;
  49. }
  50. /**
  51. * Prefix to apply to properties loaded using <code>file</code>.
  52. * A "." is appended to the prefix if not specified.
  53. * @param string $prefix prefix string
  54. * @return void
  55. * @since 2.0
  56. */
  57. public function setPrefix($prefix)
  58. {
  59. $this->prefix = $prefix;
  60. if (! StringHelper :: endsWith(".", $prefix))
  61. {
  62. $this->prefix .= ".";
  63. }
  64. }
  65. /**
  66. * @return string
  67. * @since 2.0
  68. */
  69. public function getPrefix()
  70. {
  71. return $this->prefix;
  72. }
  73. /**
  74. * Keep the xml root tag as the first value in the property name
  75. *
  76. * @param bool $yesNo
  77. */
  78. public function setKeepRoot($yesNo)
  79. {
  80. $this->_keepRoot = (bool) $yesNo;
  81. }
  82. /**
  83. * @return bool
  84. */
  85. public function getKeepRoot()
  86. {
  87. return $this->_keepRoot;
  88. }
  89. /**
  90. * Treat attributes as nested elements.
  91. *
  92. * @param bool $yesNo
  93. */
  94. public function setCollapseAttributes($yesNo)
  95. {
  96. $this->_collapseAttr = (bool) $yesNo;
  97. }
  98. /**
  99. * @return bool
  100. */
  101. public function getCollapseAttributes()
  102. {
  103. return $this->_collapseAttr;
  104. }
  105. /**
  106. * Delimiter for splitting multiple values.
  107. *
  108. * @param string $d
  109. */
  110. public function setDelimiter($d)
  111. {
  112. $this->_delimiter = $d;
  113. }
  114. /**
  115. * @return string
  116. */
  117. public function getDelimiter()
  118. {
  119. return $this->_delimiter;
  120. }
  121. /**
  122. * set the property in the project to the value.
  123. * if the task was give a file or env attribute
  124. * here is where it is loaded
  125. */
  126. public function main()
  127. {
  128. if ($this->file === null)
  129. {
  130. throw new BuildException("You must specify file to load properties from", $this->getLocation());
  131. }
  132. $this->loadFile($this->file);
  133. }
  134. /**
  135. * load properties from an XML file.
  136. * @param PhingFile $file
  137. */
  138. protected function loadFile(PhingFile $file)
  139. {
  140. $props = new Properties();
  141. $this->log("Loading " . $file->getAbsolutePath(), Project :: MSG_INFO);
  142. try
  143. { // try to load file
  144. if ($file->exists())
  145. {
  146. $this->addProperties($this->_getProperties($file));
  147. }
  148. else
  149. {
  150. $this->log("Unable to find property file: " . $file->getAbsolutePath() . "... skipped", Project :: MSG_WARN);
  151. }
  152. }
  153. catch (IOException $ioe)
  154. {
  155. throw new BuildException("Could not load properties from file.", $ioe);
  156. }
  157. }
  158. /**
  159. * Parses an XML file and returns properties
  160. *
  161. * @param string $filePath
  162. *
  163. * @return Properties
  164. */
  165. protected function _getProperties($filePath)
  166. {
  167. // load() already made sure that file is readable
  168. // but we'll double check that when reading the file into
  169. // an array
  170. if (($lines = @file($filePath)) === false)
  171. {
  172. throw new IOException("Unable to parse contents of $filePath");
  173. }
  174. $prop = new Properties();
  175. $xml = simplexml_load_file($filePath);
  176. if ($xml === false)
  177. throw new IOException("Unable to parse XML file $filePath");
  178. $path = array();
  179. if ($this->_keepRoot)
  180. {
  181. $path[] = dom_import_simplexml($xml)->tagName;
  182. $prefix = implode('.', $path);
  183. if (! empty($prefix))
  184. $prefix .= '.';
  185. // Check for attributes
  186. foreach ($xml->attributes() as $attribute => $val)
  187. {
  188. if ($this->_collapseAttr)
  189. $prop->setProperty($prefix . "$attribute", (string) $val);
  190. else
  191. $prop->setProperty($prefix . "($attribute)", (string) $val);
  192. }
  193. }
  194. $this->_addNode($xml, $path, $prop);
  195. return $prop;
  196. }
  197. /**
  198. * Adds an XML node
  199. *
  200. * @param SimpleXMLElement $node
  201. * @param array $path Path to this node
  202. * @param Properties $prop Properties will be added as they are found (by reference here)
  203. *
  204. * @return void
  205. */
  206. protected function _addNode($node, $path, $prop)
  207. {
  208. foreach ($node as $tag => $value)
  209. {
  210. $prefix = implode('.', $path);
  211. if (! empty($prefix) > 0)
  212. $prefix .= '.';
  213. // Check for attributes
  214. foreach ($value->attributes() as $attribute => $val)
  215. {
  216. if ($this->_collapseAttr)
  217. $prop->setProperty($prefix . "$tag.$attribute", (string) $val);
  218. else
  219. $prop->setProperty($prefix . "$tag($attribute)", (string) $val);
  220. }
  221. // Add tag
  222. if (count($value->children()))
  223. {
  224. $this->_addNode($value, array_merge($path, array($tag)), $prop);
  225. }
  226. else
  227. {
  228. $val = (string) $value;
  229. /* Check for * and ** on 'exclude' and 'include' tag / ant seems to do this? could use FileSet here
  230. if($tag == 'exclude') {
  231. }*/
  232. // When property already exists, i.e. multiple xml tag
  233. // <project>
  234. // <exclude>file/a.php</exclude>
  235. // <exclude>file/a.php</exclude>
  236. // </project>
  237. //
  238. // Would be come project.exclude = file/a.php,file/a.php
  239. $p = empty($prefix) ? $tag : $prefix . $tag;
  240. $prop->append($p, (string) $val, $this->_delimiter);
  241. }
  242. }
  243. }
  244. }