From 78af66386ec91625d56881a216e6277dd8744319 Mon Sep 17 00:00:00 2001 From: Jan Vansteenlandt Date: Thu, 30 May 2013 13:34:51 +0200 Subject: [PATCH 1/3] Issue # 17. --- src/tdt/formatters/strategies/CSV.php | 93 +++++++++++++-------------- 1 file changed, 44 insertions(+), 49 deletions(-) diff --git a/src/tdt/formatters/strategies/CSV.php b/src/tdt/formatters/strategies/CSV.php index e9d4374..c83aa73 100644 --- a/src/tdt/formatters/strategies/CSV.php +++ b/src/tdt/formatters/strategies/CSV.php @@ -14,10 +14,10 @@ class CSV extends \tdt\formatters\AStrategy{ public function printHeader(){ header("Access-Control-Allow-Origin: *"); - header("Content-Type: text/csv;charset=UTF-8"); + header("Content-Type: text/html;charset=UTF-8"); } - /** + /** * encloses the $element in double quotes */ private function enclose($element){ @@ -28,73 +28,68 @@ private function enclose($element){ } public function printBody(){ + $keys = array_keys(get_object_vars($this->objectToPrint)); $key = $keys[0]; $this->objectToPrint = $this->objectToPrint->$key; if(!is_array($this->objectToPrint)){ - throw new TDTException(500,array("CSVFormatter - You can only request CSV on an array" , array("CSV", "json", "rdf", "xml", "n3","ttl"))); + $exception_config = array(); + $exception_config["log_dir"] = Config::get("general", "logging", "path"); + $exception_config["url"] = Config::get("general", "hostname") . Config::get("general", "subdir") . "error"; + throw new TDTException(452, array("You can only request a CSV formatter on a tabular datastructure."), $exception_config); } - if(isset($this->objectToPrint[0])){ - //print the header row - $headerrow = array(); - if(is_object($this->objectToPrint[0])){ - $headerrow = array_keys(get_object_vars($this->objectToPrint[0])); - }else{ - $headerrow = array_keys($this->objectToPrint[0]); - } - // we're going to enclose all of our fields in double quotes - $enclosedHeaderrow = array(); - foreach($headerrow as $element){ - array_push($enclosedHeaderrow,$this->enclose($element)); + $header_printed = false; + foreach($this->objectToPrint as $row){ + if(is_object($row)){ + $row = get_object_vars($row); + }else if(!is_array($row)){ + echo $row . "\n"; + continue; } - echo implode(";",$enclosedHeaderrow); - if(sizeof($enclosedHeaderrow) > 0){ - echo "\n"; + if(!$header_printed){ + $i = 0; + foreach($row as $key => $value){ + echo $this->enclose($key); + echo sizeof($row)-1 != $i ? ";" : "\n"; + $i++; + } + $header_printed = true; } - foreach($this->objectToPrint as $row){ - if(is_object($row)){ - $row = get_object_vars($row); - }else if(!is_array($row)){ - echo $row . "\n"; - continue; - } + $i = 0; + foreach($row as $element){ - $i = 0; - foreach($row as $element){ - if(is_object($element)){ - if(isset($element->id)){ - echo $element->id; - }else if(isset($element->name)){ - echo $element->name; - }else{ - echo "OBJECT"; - } - } - elseif(is_array($element)){ - if(isset($element["id"])){ - echo $element["id"]; - }else if(isset($element["name"])){ - echo $element["name"]; - }else{ - echo "OBJECT"; - } + if(is_object($element)){ + if(isset($element->id)){ + echo $element->id; + }else if(isset($element->name)){ + echo $element->name; + }else{ + echo "OBJECT"; } - else{ - echo $this->enclose($element); + } + elseif(is_array($element)){ + if(isset($element["id"])){ + echo $element["id"]; + }else if(isset($element["name"])){ + echo $element["name"]; + }else{ + echo "OBJECT"; } - echo sizeof($row)-1 != $i ? ";" : "\n"; - $i++; } + else{ + echo $this->enclose($element); + } + echo sizeof($row)-1 != $i ? ";" : "\n"; + $i++; } } } - public static function getDocumentation(){ return "A CSV formatter. Works only on tabular data."; } From 9839d6b7c2452665093678e1f35b6e9a7906d292 Mon Sep 17 00:00:00 2001 From: Jan Vansteenlandt Date: Thu, 30 May 2013 14:16:20 +0200 Subject: [PATCH 2/3] Issue #27. --- src/tdt/formatters/strategies/CSV.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/tdt/formatters/strategies/CSV.php b/src/tdt/formatters/strategies/CSV.php index c83aa73..b24554a 100644 --- a/src/tdt/formatters/strategies/CSV.php +++ b/src/tdt/formatters/strategies/CSV.php @@ -14,11 +14,11 @@ class CSV extends \tdt\formatters\AStrategy{ public function printHeader(){ header("Access-Control-Allow-Origin: *"); - header("Content-Type: text/html;charset=UTF-8"); + header("Content-Type: text/csv;charset=UTF-8"); } /** - * encloses the $element in double quotes + * Encloses the $element in double quotes. */ private function enclose($element){ $element = rtrim($element, '"'); From 793527103950a875ed762ab4dab3ab204f0498f6 Mon Sep 17 00:00:00 2001 From: Jan Vansteenlandt Date: Mon, 24 Jun 2013 16:22:50 +0200 Subject: [PATCH 3/3] Issue #16. --- src/tdt/formatters/strategies/XML.php | 74 +++++++++++---------------- 1 file changed, 30 insertions(+), 44 deletions(-) diff --git a/src/tdt/formatters/strategies/XML.php b/src/tdt/formatters/strategies/XML.php index d235dc6..58d29c0 100644 --- a/src/tdt/formatters/strategies/XML.php +++ b/src/tdt/formatters/strategies/XML.php @@ -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 @@ -49,7 +52,7 @@ 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 "rootname>"; } @@ -57,9 +60,8 @@ 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 @@ -87,11 +89,9 @@ 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){ @@ -99,7 +99,10 @@ private function printObject($name,$object,$nameobject=null){ $tag_close = TRUE; } - echo "<$key>" . $value . ""; + if(preg_match("/^[0-9]+.*/", $key)){ + $key = NUMBER_TAG_PREFIX . $key; // add an i + } + echo "<".$key.">" . $value . ""; }else{ // To be discussed: strip the _ or not to strip the _ //$key = substr($key, 1); @@ -113,11 +116,9 @@ 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 ""; } } @@ -125,58 +126,44 @@ private function printObject($name,$object,$nameobject=null){ } } - 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>"; } 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 ""; - }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 ""; - }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 ""; - }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 . ""; + echo "<".$key . ">" . $value . ""; }else{ echo "<".$name. ">".$value.""; } @@ -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); } @@ -203,5 +190,4 @@ public function printGraph() { /* Serialize a triples array */ echo $ser->getSerializedTriples($triples); } - } \ No newline at end of file