-
-
Notifications
You must be signed in to change notification settings - Fork 319
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Made XML based property files loadable. (#903)
- Loading branch information
Showing
4 changed files
with
201 additions
and
110 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,167 @@ | ||
<?php | ||
/** | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
* | ||
* This software consists of voluntary contributions made by many individuals | ||
* and is licensed under the LGPL. For more information please see | ||
* <http://phing.info>. | ||
*/ | ||
|
||
/** | ||
* Implements an XmlFileParser. | ||
* | ||
* @author Siad Ardroumli <siad.ardroumli@gmail.com> | ||
* @package phing.system.io | ||
*/ | ||
class XmlFileParser implements FileParserInterface | ||
{ | ||
private $keepRoot = true; | ||
private $collapseAttr = true; | ||
private $delimiter = ','; | ||
|
||
/** | ||
* @param bool $keepRoot | ||
*/ | ||
public function setKeepRoot(bool $keepRoot): void | ||
{ | ||
$this->keepRoot = $keepRoot; | ||
} | ||
|
||
/** | ||
* @param bool $collapseAttr | ||
*/ | ||
public function setCollapseAttr(bool $collapseAttr): void | ||
{ | ||
$this->collapseAttr = $collapseAttr; | ||
} | ||
|
||
/** | ||
* @param string $delimiter | ||
*/ | ||
public function setDelimiter(string $delimiter): void | ||
{ | ||
$this->delimiter = $delimiter; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function parseFile(PhingFile $file) | ||
{ | ||
$properties = $this->getProperties($file); | ||
|
||
return $properties->getProperties(); | ||
} | ||
|
||
/** | ||
* Parses an XML file and returns properties | ||
* | ||
* @param PhingFile $file | ||
* | ||
* @throws IOException | ||
* @return Properties | ||
*/ | ||
private function getProperties(PhingFile $file) | ||
{ | ||
// load() already made sure that file is readable | ||
// but we'll double check that when reading the file into | ||
// an array | ||
|
||
if ((@file($file)) === false) { | ||
throw new IOException("Unable to parse contents of $file"); | ||
} | ||
|
||
$prop = new Properties(); | ||
|
||
$xml = simplexml_load_string(file_get_contents($file)); | ||
|
||
if ($xml === false) { | ||
throw new IOException("Unable to parse XML file $file"); | ||
} | ||
|
||
$path = []; | ||
|
||
if ($this->keepRoot) { | ||
$path[] = dom_import_simplexml($xml)->tagName; | ||
|
||
$prefix = implode('.', $path); | ||
|
||
if (!empty($prefix)) { | ||
$prefix .= '.'; | ||
} | ||
|
||
// Check for attributes | ||
foreach ($xml->attributes() as $attribute => $val) { | ||
if ($this->collapseAttr) { | ||
$prop->setProperty($prefix . (string) $attribute, (string) $val); | ||
} else { | ||
$prop->setProperty($prefix . "($attribute)", (string) $val); | ||
} | ||
} | ||
} | ||
|
||
$this->addNode($xml, $path, $prop); | ||
|
||
return $prop; | ||
} | ||
|
||
/** | ||
* Adds an XML node | ||
* | ||
* @param SimpleXMLElement $node | ||
* @param array $path Path to this node | ||
* @param Properties $prop Properties will be added as they are found (by reference here) | ||
* | ||
* @return void | ||
*/ | ||
private function addNode($node, $path, $prop) | ||
{ | ||
foreach ($node as $tag => $value) { | ||
$prefix = implode('.', $path); | ||
|
||
if ($prefix !== '') { | ||
$prefix .= '.'; | ||
} | ||
|
||
// Check for attributes | ||
foreach ($value->attributes() as $attribute => $val) { | ||
if ($this->collapseAttr) { | ||
$prop->setProperty($prefix . "$tag.$attribute", (string) $val); | ||
} else { | ||
$prop->setProperty($prefix . "$tag($attribute)", (string) $val); | ||
} | ||
} | ||
|
||
// Add tag | ||
if (count($value->children())) { | ||
$this->addNode($value, array_merge($path, [$tag]), $prop); | ||
} else { | ||
$val = (string) $value; | ||
|
||
/* Check for * and ** on 'exclude' and 'include' tag / ant seems to do this? could use FileSet here | ||
if ($tag == 'exclude') { | ||
}*/ | ||
|
||
// When property already exists, i.e. multiple xml tag | ||
// <project> | ||
// <exclude>file/a.php</exclude> | ||
// <exclude>file/a.php</exclude> | ||
// </project> | ||
// | ||
// Would be come project.exclude = file/a.php,file/a.php | ||
$p = empty($prefix) ? $tag : $prefix . $tag; | ||
$prop->append($p, (string) $val, $this->delimiter); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters