Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Made XML based property files loadable. #903

Merged
merged 3 commits into from
Apr 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions classes/phing/system/io/FileParserFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,28 @@
*/
class FileParserFactory implements FileParserFactoryInterface
{
private const XML_FILE_EXTENSION = 'xml';
/**
* @const string
*/
const YAMLFILEEXTENSION = 'yml';
private const YAML_FILE_EXTENSION = 'yml';

/**
* @const string
*/
const YAMLFILEEXTENSIONLONG = 'yaml';
private const YAML_FILE_EXTENSION_LONG = 'yaml';

/**
* {@inheritDoc}
*/
public function createParser($fileExtension)
{
switch ($fileExtension) {
case self::YAMLFILEEXTENSION:
case self::YAMLFILEEXTENSIONLONG:
case self::XML_FILE_EXTENSION:
$fileParser = new XmlFileParser();
break;
case self::YAML_FILE_EXTENSION:
case self::YAML_FILE_EXTENSION_LONG:
$fileParser = new YamlFileParser();
break;
default:
Expand Down
167 changes: 167 additions & 0 deletions classes/phing/system/io/XmlFileParser.php
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);
}
}
}
}
115 changes: 9 additions & 106 deletions classes/phing/tasks/ext/XmlPropertyTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,14 +172,20 @@ public function main()
*/
protected function loadFile(PhingFile $file)
{
$props = new Properties();
$this->log("Loading " . $file->getAbsolutePath(), Project::MSG_INFO);
try { // try to load file
if ($file->exists()) {
return $this->_getProperties($file);
$parser = new XmlFileParser();
$parser->setCollapseAttr($this->_collapseAttr);
$parser->setKeepRoot($this->_keepRoot);
$parser->setDelimiter($this->_delimiter);

$properties = $parser->parseFile($file);

return new Properties($properties);
} else {
if ($this->getRequired()) {
throw new BuildException("Could not load required properties file.", $ioe);
throw new BuildException("Could not load required properties file.");
} else {
$this->log(
"Unable to find property file: " . $file->getAbsolutePath() . "... skipped",
Expand All @@ -191,107 +197,4 @@ protected function loadFile(PhingFile $file)
throw new BuildException("Could not load properties from file.", $ioe);
}
}

/**
* Parses an XML file and returns properties
*
* @param string $filePath
*
* @throws IOException
* @return Properties
*/
protected function _getProperties($filePath)
{

// load() already made sure that file is readable
// but we'll double check that when reading the file into
// an array

if (($lines = @file($filePath)) === false) {
throw new IOException("Unable to parse contents of $filePath");
}

$prop = new Properties();

$xml = simplexml_load_string(file_get_contents($filePath));

if ($xml === false) {
throw new IOException("Unable to parse XML file $filePath");
}

$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 . "$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
*/
protected function _addNode($node, $path, $prop)
{
foreach ($node as $tag => $value) {
$prefix = implode('.', $path);

if (!empty($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);
}
}
}
}
17 changes: 17 additions & 0 deletions docs/guide/en/source/appendixes/fileformats.xml
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,23 @@ myapp:
# keys in "${}".
text:
width: "${myapp.window.hsize}"
</programlisting>

Property files may also be formatted in XML format:
<programlisting>
&lt;myapp>
&lt;window>
&lt;hsize>300&lt;/hsize>
&lt;vsize>200&lt;/hsize>
&lt;xpos>10&lt;/hsize>
&lt;ypos>100&lt;/hsize>
&lt;/window>
&lt;/myapp>

myapp.window.hsize=300
myapp.window.vsize=200
myapp.window.xpos=10
myapp.window.ypos=100
</programlisting>
</sect1>
</appendix>