PageRenderTime 40ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 1ms

/lib/jelix/core/response/jResponseRdf.class.php

http://github.com/jelix/jelix
PHP | 137 lines | 68 code | 13 blank | 56 comment | 8 complexity | 9a286ea6fac8dbac4e7aad9dbfc213c8 MD5 | raw file
Possible License(s): BSD-3-Clause, JSON, GPL-3.0, LGPL-3.0, LGPL-2.1, GPL-2.0
  1. <?php
  2. /**
  3. * @package jelix
  4. * @subpackage core_response
  5. * @author Laurent Jouanneau
  6. * @copyright 2006-2010 Laurent Jouanneau
  7. * @link http://www.jelix.org
  8. * @licence GNU Lesser General Public Licence see LICENCE file or http://www.gnu.org/licenses/lgpl.html
  9. */
  10. /**
  11. * output RDF content.
  12. * This is a basic RDF generator, which generates content from
  13. * an array of data.
  14. * @package jelix
  15. * @subpackage core_response
  16. * @see jResponse
  17. */
  18. final class jResponseRdf extends jResponse {
  19. /**
  20. * @var string
  21. */
  22. protected $_type = 'rdf';
  23. /**
  24. * List of object or array, which will be transform into RDF content
  25. * @var array
  26. */
  27. public $data;
  28. /**
  29. * a template selector for complex RDF content.
  30. * keep empty if you have a simple array of array in $data :
  31. * RDF content will be generated by a simple generator.
  32. * if you specify a template, you don't have to fill other
  33. * properties (except data)
  34. * @var string
  35. */
  36. public $template;
  37. /**
  38. * namespace of the attributes and elements that will content your data.
  39. * @var string
  40. */
  41. public $resNs="http://dummy/rdf#";
  42. /**
  43. * namespace prefix
  44. * @var string
  45. */
  46. public $resNsPrefix='row';
  47. /**
  48. * uri prefix of all ressources
  49. * @var string
  50. */
  51. public $resUriPrefix = "urn:data:row:";
  52. /**
  53. * uri of the root sequence
  54. * @var string
  55. */
  56. public $resUriRoot = "urn:data:row";
  57. /**
  58. * list of array values you want to put in attributes
  59. * @var string
  60. */
  61. public $asAttribute=array();
  62. /**
  63. * list of array values you want to put in elements
  64. * @var string
  65. */
  66. public $asElement=array();
  67. public function output(){
  68. if($this->_outputOnlyHeaders){
  69. $this->sendHttpHeaders();
  70. return true;
  71. }
  72. $this->_httpHeaders['Content-Type']='text/xml;charset='.jApp::config()->charset;
  73. if ($this->template !='') {
  74. $tpl= new jTpl();
  75. $tpl->assign('data',$this->data);
  76. $content = $tpl->fetch($this->template);
  77. }
  78. else {
  79. $content = $this->generateContent();
  80. }
  81. $this->sendHttpHeaders();
  82. echo '<?xml version="1.0" encoding="'.jApp::config()->charset.'"?>';
  83. echo $content;
  84. return true;
  85. }
  86. protected function generateContent() {
  87. ob_start();
  88. $EOL="\n";
  89. echo '<RDF xmlns:RDF="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns="http://www.w3.org/1999/02/22-rdf-syntax-ns#"'.$EOL;
  90. echo ' xmlns:',$this->resNsPrefix,'="',$this->resNs,'" xmlns:NC="http://home.netscape.com/NC-rdf#">',$EOL;
  91. echo '<Bag RDF:about="'.$this->resUriRoot.'">'.$EOL;
  92. foreach($this->data as $dt){
  93. echo "<li>\n<Description ";
  94. // NC:parseType="Integer"
  95. if(is_object($dt))
  96. $dt=get_object_vars ($dt);
  97. if(count($this->asAttribute) || count($this->asElement)){
  98. foreach($this->asAttribute as $name){
  99. echo $this->resNsPrefix,':',$name,'="',htmlspecialchars($dt[$name]),'" ';
  100. }
  101. if(count($this->asElement)){
  102. echo ">\n";
  103. foreach($this->asElement as $name){
  104. echo '<',$this->resNsPrefix,':',$name,'>',htmlspecialchars($dt[$name]),'</',$this->resNsPrefix,':',$name,">\n";
  105. }
  106. echo "</Description>\n</li>\n";
  107. }else
  108. echo "/>\n</li>\n";
  109. }else{
  110. if(count($dt)){
  111. echo ">\n";
  112. foreach($dt as $name=>$val){
  113. echo '<',$this->resNsPrefix,':',$name,'>',htmlspecialchars($val),'</',$this->resNsPrefix,':',$name,">\n";
  114. }
  115. echo "</Description>\n</li>\n";
  116. }else{
  117. echo "/>\n</li>\n";
  118. }
  119. }
  120. }
  121. echo "</Bag>\n</RDF>\n";
  122. return ob_get_clean();
  123. }
  124. }