Skip to content

Commit

Permalink
Issue #16.
Browse files Browse the repository at this point in the history
  • Loading branch information
coreation committed Jun 24, 2013
1 parent 9839d6b commit 7935271
Showing 1 changed file with 30 additions and 44 deletions.
74 changes: 30 additions & 44 deletions src/tdt/formatters/strategies/XML.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
*/

namespace tdt\formatters\strategies;

define("NUMBER_TAG_PREFIX","_");

class XML extends \tdt\formatters\AStrategy{
//make a stack of array information, always work on the last one
//for nested array support
Expand Down Expand Up @@ -49,17 +52,16 @@ public function printBody(){
$this->objectToPrint->$rootname = $wrapper;
}

$this->printObject($this->rootname . " version=\"1.0\" timestamp=\"" . time() . "\"",$this->objectToPrint->$rootname);
$this->printObject($this->rootname . " version=\"1.0\" timestamp=\"" . time() . "\"", $this->objectToPrint->$rootname);
echo "</$this->rootname>";
}

private function printObject($name,$object,$nameobject=null){

//check on first character
if(preg_match("/^[0-9]+.*/", $name)){
$name = "i" . $name; // add an i
}
$name = utf8_encode($name);
$name = NUMBER_TAG_PREFIX . $name; // add an i
}
echo "<".$name;
//If this is not an object, it must have been an empty result
//thus, we'll be returning an empty tag
Expand Down Expand Up @@ -87,19 +89,20 @@ private function printObject($name,$object,$nameobject=null){
echo ">" . htmlspecialchars($value, ENT_QUOTES);
$tag_close = TRUE;
}else{
$key = htmlspecialchars(str_replace(" ","",$key));
$key = utf8_encode($key);
$key = htmlspecialchars(str_replace(" ","",$key));

$value = htmlspecialchars($value, ENT_QUOTES);
$value = utf8_encode($value);
$value = htmlspecialchars($value, ENT_QUOTES);

if($this->isNotAnAttribute($key)){
if(!$tag_close){
echo ">";
$tag_close = TRUE;
}

echo "<$key>" . $value . "</$key>";
if(preg_match("/^[0-9]+.*/", $key)){
$key = NUMBER_TAG_PREFIX . $key; // add an i
}
echo "<".$key.">" . $value . "</$key>";
}else{
// To be discussed: strip the _ or not to strip the _
//$key = substr($key, 1);
Expand All @@ -113,70 +116,54 @@ private function printObject($name,$object,$nameobject=null){
echo ">";
}


if($name != $nameobject){
$boom = explode(" ",$name);
if(count($boom) == 1){
$name = utf8_encode($name);
if(count($boom) == 1){
echo "</$name>";
}
}

}
}

private function isNotAnAttribute($key){
//echo strpos($key,"_");
private function isNotAnAttribute($key){
return $key[0] != "_";
}

private function printArray($name,$array){
//check on first character
//check on first character
if(preg_match("/^[0-9]+.*/", $name)){
$name = "i" . $name; // add an i
$name = NUMBER_TAG_PREFIX . $name;
}
$index = 0;

if(empty($array)){
$name = utf8_encode($name);
if(empty($array)){
echo "<$name></$name>";
}

foreach($array as $key => $value){
$nametag = $name;
if(is_object($value)){
$this->printObject($nametag,$value,$name);
$name = utf8_encode($name);
$this->printObject($nametag,$value,$name);
echo "</$name>";
}else if(is_array($value) && !$this->isHash($value)){
$name = utf8_encode($name);
}else if(is_array($value) && !$this->isHash($value)){
echo "<".$name. ">";
$this->printArray($nametag,$value);
$name = utf8_encode($name);
$this->printArray($nametag,$value);
echo "</".$name.">";
}else if(is_array($value) && $this->isHash($value)){
$name = utf8_encode($name);
}else if(is_array($value) && $this->isHash($value)){
echo "<".$name. ">";
$this->printArray($key,$value);
$name = utf8_encode($name);
$this->printArray($key,$value);
echo "</".$name.">";
}else{// no array in arrays are allowed!!
$name = htmlspecialchars(str_replace(" ","",$name));
$name = utf8_encode($name);

$value = htmlspecialchars($value);
$value = utf8_encode($value);
}else{
$name = htmlspecialchars(str_replace(" ","",$name));
$value = htmlspecialchars($value);
$key = htmlspecialchars(str_replace(" ","",$key));

$key = htmlspecialchars(str_replace(" ","",$key));
$key = utf8_encode($key);

if($this->isHash($array)){
//if this is an associative array, don't print it by name of the parent
//check on first character
if($this->isHash($array)){
if(preg_match("/^[0-9]+.*/", $key)){
$key = "i" . $key; // add an i
$key = NUMBER_TAG_PREFIX . $key;
}
echo "<".$key . ">" . $value . "</".$key.">";
echo "<".$key . ">" . $value . "</".$key.">";
}else{
echo "<".$name. ">".$value."</".$name.">";
}
Expand All @@ -186,7 +173,7 @@ private function printArray($name,$array){
}
}

// check if we have an hash or a normal 'numberice array ( php doesn't know the difference btw, it just doesn't care. )
// Check if we have an hash or a normal 'numeric' array ( php doesn't know the difference btw, it just doesn't care. )
private function isHash($arr){
return array_keys($arr) !== range(0, count($arr) - 1);
}
Expand All @@ -203,5 +190,4 @@ public function printGraph() {
/* Serialize a triples array */
echo $ser->getSerializedTriples($triples);
}

}

0 comments on commit 7935271

Please sign in to comment.