simplify xml parser code

This commit is contained in:
Thomas Müller 2014-11-24 17:26:07 +01:00
parent c503ecd544
commit d4f107d4dd
3 changed files with 56 additions and 65 deletions

View File

@ -33,60 +33,41 @@ class InfoParser {
return null;
}
$content = @file_get_contents($file);
if (!$content) {
$xml = simplexml_load_file($file);
$array = json_decode(json_encode((array)$xml), TRUE);
if (is_null($array)) {
return null;
}
$xml = new SimpleXMLElement($content);
$data['info'] = array();
$data['remote'] = array();
$data['public'] = array();
foreach ($xml->children() as $child) {
/**
* @var $child SimpleXMLElement
*/
if ($child->getName() == 'remote') {
foreach ($child->children() as $remote) {
/**
* @var $remote SimpleXMLElement
*/
$data['remote'][$remote->getName()] = (string)$remote;
}
} elseif ($child->getName() == 'public') {
foreach ($child->children() as $public) {
/**
* @var $public SimpleXMLElement
*/
$data['public'][$public->getName()] = (string)$public;
}
} elseif ($child->getName() == 'types') {
$data['types'] = array();
foreach ($child->children() as $type) {
/**
* @var $type SimpleXMLElement
*/
$data['types'][] = $type->getName();
}
} elseif ($child->getName() == 'description') {
$xml = (string)$child->asXML();
$data[$child->getName()] = substr($xml, 13, -14); //script <description> tags
} elseif ($child->getName() == 'documentation') {
foreach ($child as $subChild) {
$url = (string)$subChild;
if (!array_key_exists('info', $array)) {
$array['info'] = array();
}
if (!array_key_exists('remote', $array)) {
$array['remote'] = array();
}
if (!array_key_exists('public', $array)) {
$array['public'] = array();
}
// If it is not an absolute URL we assume it is a key
// i.e. admin-ldap will get converted to go.php?to=admin-ldap
if (!$this->httpHelper->isHTTPURL($url)) {
$url = $this->urlGenerator->linkToDocs($url);
}
$data["documentation"][$subChild->getName()] = $url;
if (array_key_exists('documentation', $array)) {
foreach ($array['documentation'] as $key => $url) {
// If it is not an absolute URL we assume it is a key
// i.e. admin-ldap will get converted to go.php?to=admin-ldap
if (!$this->httpHelper->isHTTPURL($url)) {
$url = $this->urlGenerator->linkToDocs($url);
}
} else {
$data[$child->getName()] = (string)$child;
$array["documentation"][$key] = $url;
}
}
if (array_key_exists('types', $array)) {
foreach ($array['types'] as $type => $v) {
unset($array['types'][$type]);
$array['types'][] = $type;
}
}
return $data;
return $array;
}
}

View File

@ -0,0 +1,19 @@
{
"info": [],
"remote": [],
"public": [],
"id": "files_encryption",
"name": "Server-side Encryption",
"description": "\n\tThis application encrypts all files accessed by ownCloud at rest, wherever they are stored. As an example, with this application enabled, external cloud based Amazon S3 storage will be encrypted, protecting this data on storage outside of the control of the Admin. When this application is enabled for the first time, all files are encrypted as users log in and are prompted for their password. The recommended recovery key option enables recovery of files in case the key is lost. \n\tNote that this app encrypts all files that are touched by ownCloud, so external storage providers and applications such as SharePoint will see new files encrypted when they are accessed. Encryption is based on AES 128 or 256 bit keys. More information is available in the Encryption documentation \n\t",
"licence": "AGPL",
"author": "Sam Tuke, Bjoern Schiessle, Florin Peter",
"requiremin": "4",
"shipped": "true",
"documentation": {
"user": "https://docs.example.com/server/go.php?to=user-encryption",
"admin": "https://docs.example.com/server/go.php?to=admin-encryption"
},
"rememberlogin": "false",
"types": ["filesystem"],
"ocsid": "166047"
}

View File

@ -19,39 +19,30 @@ class InfoParser extends \PHPUnit_Framework_TestCase {
private $parser;
public function setUp() {
$config = $this->getMockBuilder('\OC\AllConfig')
->disableOriginalConstructor()->getMock();
$httpHelper = $this->getMockBuilder('\OC\HTTPHelper')
->disableOriginalConstructor()
->setConstructorArgs(array($config))
->setMethods(array('getHeaders'))
->getMock();
$httpHelper->expects($this->any())
->method('isHTTPURL')
->will($this->returnCallback(function ($url) {
return stripos($url, 'https://') === 0 || stripos($url, 'http://') === 0;
}));
$urlGenerator = $this->getMockBuilder('\OCP\IURLGenerator')
->disableOriginalConstructor()
->getMock();
//linkToDocs
$httpHelper->expects($this->any())
$urlGenerator->expects($this->any())
->method('linkToDocs')
->will($this->returnCallback(function ($url) {
return $url;
return "https://docs.example.com/server/go.php?to=$url";
}));
$this->parser = new \OC\App\InfoParser($httpHelper, $urlGenerator);
}
public function testParsingValidXml() {
$expectedData = json_decode(file_get_contents(OC::$SERVERROOT.'/tests/data/app/expected-info.json'), true);
$data = $this->parser->parse(OC::$SERVERROOT.'/tests/data/app/valid-info.xml');
$expectedKeys = array(
'id', 'info', 'remote', 'public', 'name', 'description', 'licence', 'author', 'requiremin', 'shipped',
'documentation', 'rememberlogin', 'types', 'ocsid'
);
foreach($expectedKeys as $expectedKey) {
$this->assertArrayHasKey($expectedKey, $data, "ExpectedKey($expectedKey) was missing.");
}
$this->assertEquals($expectedData, $data);
}
}