123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248 |
- <?php
- include_once 'phing/tasks/system/PropertyTask.php';
- class XmlPropertyTask extends PropertyTask {
- private $_keepRoot = true;
- private $_collapseAttr = false;
- private $_delimiter = ',';
-
- function setFile($file) {
- if (is_string($file)) {
- $file = new PhingFile($file);
- }
- $this->file = $file;
- }
-
-
- function getFile() {
- return $this->file;
- }
-
- function setPrefix($prefix) {
- $this->prefix = $prefix;
- if (!StringHelper::endsWith(".", $prefix)) {
- $this->prefix .= ".";
- }
- }
-
- function getPrefix() {
- return $this->prefix;
- }
-
- function setKeepRoot($yesNo) {
- $this->_keepRoot = (bool)$yesNo;
- }
-
- function getKeepRoot() {
- return $this->_keepRoot;
- }
-
- function setCollapseAttributes($yesNo) {
- $this->_collapseAttr = (bool)$yesNo;
- }
-
- function getCollapseAttributes() {
- return $this->_collapseAttr;
- }
-
- function setDelimiter($d) {
- $this->_delimiter = $d;
- }
-
- function getDelimiter() {
- return $this->_delimiter;
- }
-
- function main() {
- if ($this->file === null ) {
- throw new BuildException("You must specify file to load properties from", $this->getLocation());
- }
- $this->loadFile($this->file);
- }
-
- protected function loadFile(PhingFile $file) {
- $props = new Properties();
- $this->log("Loading ". $file->getAbsolutePath(), Project::MSG_INFO);
- try {
- if ($file->exists()) {
- $this->addProperties($this->_getProperties($file));
- } else {
- $this->log("Unable to find property file: ". $file->getAbsolutePath() ."... skipped", Project::MSG_WARN);
- }
- } catch (IOException $ioe) {
- throw new BuildException("Could not load properties from file.", $ioe);
- }
- }
-
- protected function _getProperties($filePath) {
-
-
-
-
- if (($lines = @file($filePath)) === false) {
- throw new IOException("Unable to parse contents of $filePath");
- }
-
- $prop = new Properties;
- $xml = simplexml_load_file($filePath);
- if($xml === false)
- throw new IOException("Unable to parse XML file $filePath");
- $path = array();
- if($this->_keepRoot) {
- $path[] = dom_import_simplexml($xml)->tagName;
- }
- $this->_addNode($xml, $path, $prop);
- return $prop;
- }
-
- protected function _addNode($node, $path, $prop) {
- foreach($node as $tag => $value) {
-
- $prefix = implode('.', $path);
-
-
-
- foreach($value->attributes() as $attribute => $val) {
- if($this->_collapseAttr)
- $prop->setProperty($prefix . ".$attribute", (string)$val);
- else
- $prop->setProperty($prefix . "($attribute)", (string)$val);
- }
-
-
-
- if(count($value->children())) {
-
-
- $path[] = $tag;
- $this->_addNode($value, $path, $prop);
- } else {
-
-
- $val = (string)$value;
-
-
-
-
-
-
-
-
-
-
- $p = empty($prefix) ? $tag : $prefix . ".$tag";
- $prop->append($p, (string)$val, $this->_delimiter);
- }
- }
- }
- }
|