Source for file RdfUtil.php

Documentation is available at RdfUtil.php

  1. <?php
  2.  
  3. // ----------------------------------------------------------------------------------
  4. // Class: RDFUtil
  5. // ----------------------------------------------------------------------------------
  6.  
  7.  
  8.  
  9. /**
  10. * Useful utility methods.
  11. * Static class.
  12. *
  13. * <BR><BR>History:<UL>
  14. * <LI>12-06-2004 : improved namespace handling in function
  15. * writeAsHTMLTable() added (tobias.gauss@web.de)</LI>
  16. * <LI>09-10-2004 : added support for OWL and infered statements</LI>
  17. * <LI>11-18-2003 : Function writeAsHtmlTable() htmlspecialchars & nl2br
  18. * for displaying literals added.</LI>
  19. * <LI>04-23-2003 : Chunk_split() removed from writeHTMLTable</LI>
  20. * <LI>12-04-2002 : Added support for "rdf:datatype" in writeHTMLTable</LI>
  21. * <LI>10-03-2002 : Green coloring for RDF_SCHEMA nodes added to writeHTMLTable</LI>
  22. * <LI>09-10-2002 : First version of this class.</LI>
  23. *
  24. * </UL>
  25. * @version V0.9.1
  26. * @author Chris Bizer <chris@bizer.de>, Daniel Westphal <dawe@gmx.de>
  27. *
  28. * @package utility
  29. * @access public
  30. */
  31. class RDFUtil extends Object {
  32.  
  33. /**
  34. * Extracts the namespace prefix out of a URI.
  35. *
  36. * @param String $uri
  37. * @return string
  38. * @access public
  39. */
  40. function guessNamespace($uri) {
  41. $l = RDFUtil::getNamespaceEnd($uri);
  42. return $l > 1 ? substr($uri ,0, $l) : "";
  43. }
  44.  
  45. /**
  46. * Delivers the name out of the URI (without the namespace prefix).
  47. *
  48. * @param String $uri
  49. * @return string
  50. * @access public
  51. */
  52. function guessName($uri) {
  53. return substr($uri,RDFUtil::getNamespaceEnd($uri));
  54. }
  55.  
  56. /**
  57. * Extracts the namespace prefix out of the URI of a Resource.
  58. *
  59. * @param Object Resource $resource
  60. * @return string
  61. * @access public
  62. */
  63. function getNamespace($resource) {
  64. return RDFUtil::guessNamespace($resource->getURI());
  65. }
  66.  
  67. /**
  68. * Delivers the Localname (without the namespace prefix) out of the URI of a Resource.
  69. *
  70. * @param Object Resource $resource
  71. * @return string
  72. * @access public
  73. */
  74. function getLocalName($resource) {
  75. return RDFUtil::guessName($resource->getURI());
  76. }
  77.  
  78. /**
  79. * Position of the namespace end
  80. * Method looks for # : and /
  81. * @param String $uri
  82. * @access private
  83. */
  84. function getNamespaceEnd($uri) {
  85. $l = strlen($uri)-1;
  86. do {
  87. $c = substr($uri, $l, 1);
  88. if($c == '#' || $c == ':' || $c == '/')
  89. break;
  90. $l--;
  91. } while ($l >= 0);
  92. $l++;
  93. return $l;
  94. }
  95.  
  96. /**
  97. * Tests if the URI of a resource belongs to the RDF syntax/model namespace.
  98. *
  99. * @param Object Resource $resource
  100. * @return boolean
  101. * @access public
  102. */
  103. function isRDF($resource) {
  104. return ($resource != NULL && RDFUtil::getNamespace($resource) == RDF_NAMESPACE_URI);
  105. }
  106.  
  107. /**
  108. * Escapes < > and &
  109. *
  110. * @param String $textValue
  111. * @return String
  112. * @access public
  113. */
  114. function escapeValue($textValue) {
  115.  
  116. $textValue = str_replace('<', '&lt;', $textValue);
  117. $textValue = str_replace('>', '&gt;', $textValue);
  118. $textValue = str_replace('&', '&amp;', $textValue);
  119.  
  120. return $textValue;
  121. }
  122.  
  123. /**
  124. * Converts an ordinal RDF resource to an integer.
  125. * e.g. Resource(RDF:_1) => 1
  126. *
  127. * @param object Resource $resource
  128. * @return Integer
  129. * @access public
  130. */
  131. function getOrd($resource) {
  132. if($resource == NULL || !is_a($resource, 'Resource') || !RDFUtil::isRDF($resource))
  133. return -1;
  134. $name = RDFUtil::getLocalName($resource);
  135. echo substr($name, 1).' '.RDFUtil::getLocalName($resource);
  136. $n = substr($name, 1);
  137. //noch rein : chekcen ob $n Nummer ist !!!!!!!!!!!!!!!!!!!!!!if($n)
  138. return $n;
  139. return -1;
  140. }
  141.  
  142. /**
  143. * Creates ordinal RDF resource out of an integer.
  144. *
  145. * @param Integer $num
  146. * @return object Resource
  147. * @access public
  148. */
  149. function createOrd($num) {
  150. return new Resource(RDF_NAMESPACE_URI . '_' . $num);
  151. }
  152.  
  153. /**
  154. * Prints a MemModel as HTML table.
  155. * You can change the colors in the configuration file.
  156. *
  157. * @param object MemModel &$model
  158. * @access public
  159. */
  160. function writeHTMLTable(&$model) {
  161. $nms = $model->getParsedNamespaces();
  162. $names = '';
  163. $pre = '';
  164.  
  165.  
  166. echo '<table border="1" cellpadding="3" cellspacing="0" width="100%">' . LINEFEED;
  167. echo INDENTATION . '<tr bgcolor="' . HTML_TABLE_HEADER_COLOR . '">' . LINEFEED . INDENTATION . INDENTATION . '<td td width="68%" colspan="3">';
  168. echo '<p><b>Base URI:</b> ' . $model->getBaseURI() . '</p></td>' . LINEFEED;
  169. echo INDENTATION . INDENTATION . '<td width="32%"><p><b>Size:</b> ' . $model->size() . '</p></td>' . LINEFEED . INDENTATION . '</tr>';
  170.  
  171. echo '<tr><td><b>Prefix:</b>'.'<br/></td><td colspan="3"><b>Namespace:</b>'.'<br/></td></tr>';
  172. $i=0;
  173. if($nms != false){
  174. foreach($nms as $namespace => $prefix){
  175. if($i==0){
  176. $col = HTML_TABLE_NS_ROW_COLOR0;
  177. }else{
  178. $col = HTML_TABLE_NS_ROW_COLOR1;
  179. }
  180. echo '<tr bgcolor="'.$col.'"><td>'.$prefix.'</td><td colspan="3">'.$namespace.'</td></tr>';
  181. $i++;
  182. $i%=2;
  183. }
  184. }else{
  185. echo '<tr><td>-</td><td colspan="3">-</td></tr>';
  186. }
  187.  
  188.  
  189.  
  190.  
  191. echo INDENTATION . '<tr bgcolor="' . HTML_TABLE_HEADER_COLOR . '">' . LINEFEED . INDENTATION . INDENTATION . '<td width="4%"><p align=center><b>No.</b></p></td>' . LINEFEED . INDENTATION . INDENTATION . '<td width="32%"><p><b>Subject</b></p></td>' . LINEFEED . INDENTATION . INDENTATION . '<td width="32%"><p><b>Predicate</b></p></td>' . LINEFEED . INDENTATION . INDENTATION . '<td width="32%"><p><b>Object</b></p></td>' . LINEFEED . INDENTATION . '</tr>' . LINEFEED;
  192.  
  193. $i = 1;
  194. foreach($model->triples as $key => $statement) {
  195. $infered='';
  196. if (is_a($statement,'InfStatement')) $infered='<small>(infered)</small>';
  197. echo INDENTATION . '<tr valign="top">' . LINEFEED . INDENTATION . INDENTATION . '<td><p align=center>' . $i . '.<BR>'.$infered.'</p></td>' . LINEFEED;
  198. // subject
  199. echo INDENTATION . INDENTATION . '<td bgcolor="';
  200. echo RDFUtil::chooseColor($statement->getSubject());
  201. echo '">';
  202. echo '<p>' . RDFUtil::getNodeTypeName($statement->getSubject());
  203. if(is_a($statement->subj,'Resource')){
  204. $ns = $statement->subj->getNamespace();
  205. if(isset($nms[$ns])){
  206. echo $nms[$ns].':'.RDFUtil::getLocalName($statement->subj);
  207. }else{
  208. echo $statement->subj->getLabel();
  209. }
  210. }
  211. echo '</p></td>' . LINEFEED;
  212. // predicate
  213. echo INDENTATION . INDENTATION . '<td bgcolor="';
  214. echo RDFUtil::chooseColor($statement->getPredicate());
  215. echo '">';
  216. echo '<p>' . RDFUtil::getNodeTypeName($statement->getPredicate());
  217. if(is_a($statement->pred,'Resource')){
  218. $ns = $statement->pred->getNamespace();
  219. if(isset($nms[$ns])){
  220. echo $nms[$ns].':'.RDFUtil::getLocalName($statement->pred);
  221. }else{
  222. echo $statement->pred->getLabel();
  223. }
  224. }
  225. echo '</p></td>' . LINEFEED;
  226. // object
  227. echo INDENTATION . INDENTATION . '<td bgcolor="';
  228. echo RDFUtil::chooseColor($statement->getObject());
  229. echo '">';
  230. echo '<p>';
  231. if (is_a($statement->getObject(), 'Literal')) {
  232. if ($statement->obj->getLanguage() != null) {
  233. $lang = ' <b>(xml:lang="' . $statement->obj->getLanguage() . '") </b> ';
  234. } ELSE $lang = '';
  235. if ($statement->obj->getDatatype() != null) {
  236. $dtype = ' <b>(rdf:datatype="' . $statement->obj->getDatatype() . '") </b> ';
  237. } ELSE $dtype = '';
  238. } else {
  239. $lang = '';
  240. $dtype = '';
  241. }
  242. $label = $statement->obj->getLabel();
  243. if(is_a($statement->obj,'Resource')){
  244. $ns = $statement->obj->getNamespace();
  245. if(isset($nms[$ns])){
  246. $label = $nms[$ns].':'.RDFUtil::getLocalName($statement->obj);
  247. }else{
  248. $label = $statement->obj->getLabel();
  249. }
  250. }
  251.  
  252. echo RDFUtil::getNodeTypeName($statement->getObject())
  253. .nl2br(htmlspecialchars($label)) . $lang . $dtype;
  254.  
  255. echo '</p></td>' . LINEFEED;
  256. echo INDENTATION . '</tr>' . LINEFEED;
  257. $i++;
  258. }
  259. echo '</table>' . LINEFEED;
  260. }
  261.  
  262. /**
  263. * Chooses a node color.
  264. * Used by RDFUtil::writeHTMLTable()
  265. *
  266. * @param object Node $node
  267. * @return object Resource
  268. * @access private
  269. */
  270. function chooseColor($node) {
  271. if (is_a($node, 'BlankNode'))
  272. return HTML_TABLE_BNODE_COLOR;
  273. elseif (is_a($node, 'Literal'))
  274. return HTML_TABLE_LITERAL_COLOR;
  275. else {
  276. if (RDFUtil::getNamespace($node) == RDF_NAMESPACE_URI ||
  277. RDFUtil::getNamespace($node) == RDF_SCHEMA_URI ||
  278. RDFUtil::getNamespace($node) == OWL_URI
  279. )
  280.  
  281. return HTML_TABLE_RDF_NS_COLOR;
  282. }
  283. return HTML_TABLE_RESOURCE_COLOR;
  284.  
  285. }
  286.  
  287. /**
  288. * Get Node Type.
  289. * Used by RDFUtil::writeHTMLTable()
  290. *
  291. * @param object Node $node
  292. * @return object Resource
  293. * @access private
  294. */
  295. function getNodeTypeName($node) {
  296. if (is_a($node, "BlankNode"))
  297. return 'Blank Node: ';
  298. elseif (is_a($node, 'Literal'))
  299. return 'Literal: ';
  300. else {
  301. if (RDFUtil::getNamespace($node) == RDF_NAMESPACE_URI ||
  302. RDFUtil::getNamespace($node) == RDF_SCHEMA_URI ||
  303. RDFUtil::getNamespace($node) == OWL_URI)
  304. return 'RDF Node: ';
  305. }
  306. return 'Resource: ';
  307.  
  308. }
  309.  
  310. } // end: RDfUtil
  311.  
  312. ?>

Documentation generated on Fri, 17 Dec 2004 16:17:19 +0100 by phpDocumentor 1.3.0RC3