XmlPropertyTask.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. <?php
  2. /*
  3. * $Id: XmlPropertyTask.php 905 2010-10-05 16:28:03Z 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: 905 $
  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. private $_keepRoot = true;
  33. private $_collapseAttr = false;
  34. private $_delimiter = ',';
  35. /** Set a file to use as the source for properties. */
  36. function setFile($file) {
  37. if (is_string($file)) {
  38. $file = new PhingFile($file);
  39. }
  40. $this->file = $file;
  41. }
  42. /** Get the PhingFile that is being used as property source. */
  43. function getFile() {
  44. return $this->file;
  45. }
  46. /**
  47. * Prefix to apply to properties loaded using <code>file</code>.
  48. * A "." is appended to the prefix if not specified.
  49. * @param string $prefix prefix string
  50. * @return void
  51. * @since 2.0
  52. */
  53. function setPrefix($prefix) {
  54. $this->prefix = $prefix;
  55. if (!StringHelper::endsWith(".", $prefix)) {
  56. $this->prefix .= ".";
  57. }
  58. }
  59. /**
  60. * @return string
  61. * @since 2.0
  62. */
  63. function getPrefix() {
  64. return $this->prefix;
  65. }
  66. /**
  67. * Keep the xml root tag as the first value in the property name
  68. *
  69. * @param bool $yesNo
  70. */
  71. function setKeepRoot($yesNo) {
  72. $this->_keepRoot = (bool)$yesNo;
  73. }
  74. /**
  75. * @return bool
  76. */
  77. function getKeepRoot() {
  78. return $this->_keepRoot;
  79. }
  80. /**
  81. * Treat attributes as nested elements.
  82. *
  83. * @param bool $yesNo
  84. */
  85. function setCollapseAttributes($yesNo) {
  86. $this->_collapseAttr = (bool)$yesNo;
  87. }
  88. /**
  89. * @return bool
  90. */
  91. function getCollapseAttributes() {
  92. return $this->_collapseAttr;
  93. }
  94. /**
  95. * Delimiter for splitting multiple values.
  96. *
  97. * @param string $d
  98. */
  99. function setDelimiter($d) {
  100. $this->_delimiter = $d;
  101. }
  102. /**
  103. * @return string
  104. */
  105. function getDelimiter() {
  106. return $this->_delimiter;
  107. }
  108. /**
  109. * set the property in the project to the value.
  110. * if the task was give a file or env attribute
  111. * here is where it is loaded
  112. */
  113. function main() {
  114. if ($this->file === null ) {
  115. throw new BuildException("You must specify file to load properties from", $this->getLocation());
  116. }
  117. $this->loadFile($this->file);
  118. }
  119. /**
  120. * load properties from an XML file.
  121. * @param PhingFile $file
  122. */
  123. protected function loadFile(PhingFile $file) {
  124. $props = new Properties();
  125. $this->log("Loading ". $file->getAbsolutePath(), Project::MSG_INFO);
  126. try { // try to load file
  127. if ($file->exists()) {
  128. $this->addProperties($this->_getProperties($file));
  129. } else {
  130. $this->log("Unable to find property file: ". $file->getAbsolutePath() ."... skipped", Project::MSG_WARN);
  131. }
  132. } catch (IOException $ioe) {
  133. throw new BuildException("Could not load properties from file.", $ioe);
  134. }
  135. }
  136. /**
  137. * Parses an XML file and returns properties
  138. *
  139. * @param string $filePath
  140. *
  141. * @return Properties
  142. */
  143. protected function _getProperties($filePath) {
  144. // load() already made sure that file is readable
  145. // but we'll double check that when reading the file into
  146. // an array
  147. if (($lines = @file($filePath)) === false) {
  148. throw new IOException("Unable to parse contents of $filePath");
  149. }
  150. $prop = new Properties;
  151. $xml = simplexml_load_file($filePath);
  152. if($xml === false)
  153. throw new IOException("Unable to parse XML file $filePath");
  154. $path = array();
  155. if($this->_keepRoot) {
  156. $path[] = dom_import_simplexml($xml)->tagName;
  157. }
  158. $this->_addNode($xml, $path, $prop);
  159. return $prop;
  160. }
  161. /**
  162. * Adds an XML node
  163. *
  164. * @param SimpleXMLElement $node
  165. * @param array $path Path to this node
  166. * @param Properties $prop Properties will be added as they are found (by reference here)
  167. *
  168. * @return void
  169. */
  170. protected function _addNode($node, $path, $prop) {
  171. foreach($node as $tag => $value) {
  172. $prefix = implode('.', $path);
  173. // Check for attributes
  174. foreach($value->attributes() as $attribute => $val) {
  175. if($this->_collapseAttr)
  176. $prop->setProperty($prefix . ".$attribute", (string)$val);
  177. else
  178. $prop->setProperty($prefix . "($attribute)", (string)$val);
  179. }
  180. //echo "\r\nCHILDREN ". count($value->children()). is_array($value);
  181. // Add tag
  182. if(count($value->children())) {
  183. //echo "\r\nOBJECT $prefix.$tag ";
  184. $path[] = $tag;
  185. $this->_addNode($value, $path, $prop);
  186. } else {
  187. //echo "\r\nADD $prefix.$tag";
  188. $val = (string)$value;
  189. /* Check for * and ** on 'exclude' and 'include' tag / ant seems to do this? could use FileSet here
  190. if($tag == 'exclude') {
  191. }*/
  192. // When property already exists, i.e. multiple xml tag
  193. // <project>
  194. // <exclude>file/a.php</exclude>
  195. // <exclude>file/a.php</exclude>
  196. // </project>
  197. //
  198. // Would be come project.exclude = file/a.php,file/a.php
  199. $p = empty($prefix) ? $tag : $prefix . ".$tag";
  200. $prop->append($p, (string)$val, $this->_delimiter);
  201. }
  202. }
  203. }
  204. }