util
[ class tree: util ] [ index: util ] [ all elements ]

Source for file Util.php

Documentation is available at Util.php

  1. <?php
  2. // ----------------------------------------------------------------------------------
  3. // Class: RDF_Util
  4. // ----------------------------------------------------------------------------------
  5. /**
  6.  * Useful utility methods.
  7.  * Static class.
  8.  *
  9.  * @version V0.7
  10.  * @author Chris Bizer <chris@bizer.de>, Daniel Westphal <dawe@gmx.de>
  11.  * @package util
  12.  * @access public
  13.  */
  14. class RDF_Util extends RDF_Object
  15. {
  16.     /**
  17.      * Extracts the namespace prefix out of a URI.
  18.      *
  19.      * @param String $uri 
  20.      * @return string 
  21.      * @access public
  22.      */
  23.     function guessNamespace($uri)
  24.     {
  25.         $l RDF_Util::getNamespaceEnd($uri);
  26.         return $l > 1 ? substr($uri 0$l"";
  27.     }
  28.  
  29.     /**
  30.      * Delivers the name out of the URI (without the namespace prefix).
  31.      *
  32.      * @param String $uri 
  33.      * @return string 
  34.      * @access public
  35.      */
  36.     function guessName($uri)
  37.     {
  38.         return substr($uriRDF_Util::getNamespaceEnd($uri));
  39.     }
  40.  
  41.     /**
  42.      * Extracts the namespace prefix out of the URI of a Resource.
  43.      *
  44.      * @param Object Resource $resource
  45.      * @return string 
  46.      * @access public
  47.      */
  48.     function getNamespace($resource)
  49.     {
  50.         return RDF_Util::guessNamespace($resource->getURI());
  51.     }
  52.  
  53.     /**
  54.      * Delivers the Localname (without the namespace prefix) out of the URI of a Resource.
  55.      *
  56.      * @param Object Resource $resource
  57.      * @return string 
  58.      * @access public
  59.      */
  60.     function getLocalName($resource)
  61.     {
  62.         return RDF_Util::guessName($resource->getURI());
  63.     }
  64.  
  65.     /**
  66.      * Position of the namespace end
  67.      * Method looks for # : and /
  68.      *
  69.      * @param String $uri 
  70.      * @access protected
  71.      */
  72.     function getNamespaceEnd($uri)
  73.     {
  74.         $l strlen($uri)-1;
  75.         do {
  76.             $c substr($uri$l1);
  77.             if ($c == '#' || $c == ':' || $c == '/'{
  78.                 break;
  79.             }
  80.             $l--;
  81.         while ($l >= 0);
  82.         $l++;
  83.         return $l;
  84.     }
  85.  
  86.     /**
  87.      * Tests if the URI of a resource belongs to the RDF syntax/model namespace.
  88.      *
  89.      * @param Object Resource $resource
  90.      * @return boolean 
  91.      * @access public
  92.      */
  93.     function isRDF($resource)
  94.     {
  95.         return ($resource != null && RDF_Util::getNamespace($resource== RDF_NAMESPACE_URI);
  96.     }
  97.  
  98.     /**
  99.      * Escapes < > and &
  100.      *
  101.      * @param String $textValue 
  102.      * @return String 
  103.      * @access public
  104.      */
  105.     function escapeValue($textValue)
  106.     {
  107.         $textValue str_replace('<''&lt;'$textValue);
  108.         $textValue str_replace('>''&gt;'$textValue);
  109.         $textValue str_replace('&''&amp;'$textValue);
  110.  
  111.         return $textValue;
  112.     }
  113.  
  114.     /**
  115.      * Converts an ordinal RDF resource to an integer.
  116.      * e.g. Resource(RDF:_1) => 1
  117.      *
  118.      * @param object Resource $resource 
  119.      * @return Integer 
  120.      * @access public
  121.      */
  122.     function getOrd($resource)
  123.     {
  124.         if ($resource == null || !is_a($resource'RDF_Resource')
  125.             || !RDF_Util::isRDF($resource)
  126.         {
  127.             return -1;
  128.         }
  129.         $name RDF_Util::getLocalName($resource);
  130.         echo substr($name1' ' RDF_Util::getLocalName($resource);
  131.         $n substr($name1);
  132.         // noch rein : checken ob $n Nummer ist !!!!!!!!!!!!!!!!!!!!!!if ($n)
  133.         return $n;
  134.         return -1;
  135.     }
  136.  
  137.     /**
  138.      * Creates ordinal RDF resource out of an integer.
  139.      *
  140.      * @param Integer $num 
  141.      * @return object Resource 
  142.      * @access public
  143.      */
  144.     function createOrd($num)
  145.     {
  146.         return RDF_Resource::factory(RDF_NAMESPACE_URI . '_' $num);
  147.     }
  148.  
  149.     /**
  150.      * Prints a Model_Memory as HTML table.
  151.      * You can change the colors in the configuration file.
  152.      *
  153.      * @param object Model_Memory     &$model 
  154.      * @access public
  155.      */
  156.     function writeHTMLTable(&$model)
  157.     {
  158.         echo '<table border="1" cellpadding="3" cellspacing="0" width="100%">' RDF_LINEFEED;
  159.         echo RDF_INDENTATION . '<tr bgcolor="' RDF_HTML_TABLE_HEADER_COLOR . '">' RDF_LINEFEED . RDF_INDENTATION . RDF_INDENTATION . '<td td width="68%" colspan="3">';
  160.         echo '<p><b>Base URI:</b> ' $model->getBaseURI('</p></td>' RDF_LINEFEED;
  161.         echo RDF_INDENTATION . RDF_INDENTATION . '<td width="32%"><p><b>Size:</b> ' $model->size('</p></td>' RDF_LINEFEED . RDF_INDENTATION . '</tr>';
  162.         echo RDF_INDENTATION . '<tr bgcolor="' RDF_HTML_TABLE_HEADER_COLOR . '">' RDF_LINEFEED . RDF_INDENTATION . RDF_INDENTATION . '<td width="4%"><p align=center><b>No.</b></p></td>' RDF_LINEFEED . RDF_INDENTATION . RDF_INDENTATION . '<td width="32%"><p><b>Subject</b></p></td>' RDF_LINEFEED . RDF_INDENTATION . RDF_INDENTATION . '<td width="32%"><p><b>Predicate</b></p></td>' RDF_LINEFEED . RDF_INDENTATION . RDF_INDENTATION . '<td width="32%"><p><b>Object</b></p></td>' RDF_LINEFEED . RDF_INDENTATION . '</tr>' RDF_LINEFEED;
  163.  
  164.         $i = 1;
  165.         foreach($model->triples as $statement{
  166.             echo RDF_INDENTATION . '<tr valign="top">' RDF_LINEFEED . RDF_INDENTATION . RDF_INDENTATION . '<td><p align=center>' $i '.</p></td>' RDF_LINEFEED;
  167.             // subject
  168.             echo RDF_INDENTATION . RDF_INDENTATION . '<td bgcolor="';
  169.             echo RDF_Util::chooseColor($statement->getSubject());
  170.             echo '">';
  171.             echo '<p>' RDF_Util::getNodeTypeName($statement->getSubject()) $statement->subj->getLabel('</p></td>' RDF_LINEFEED;
  172.             // predicate
  173.             echo RDF_INDENTATION . RDF_INDENTATION . '<td bgcolor="';
  174.             echo RDF_Util::chooseColor($statement->getPredicate());
  175.             echo '">';
  176.             echo '<p>' RDF_Util::getNodeTypeName($statement->getPredicate()) $statement->pred->getLabel('</p></td>' RDF_LINEFEED;
  177.             // object
  178.             echo RDF_INDENTATION . RDF_INDENTATION . '<td bgcolor="';
  179.             echo RDF_Util::chooseColor($statement->getObject());
  180.             echo '">';
  181.             echo '<p>';
  182.             if (is_a($statement->getObject()'RDF_Literal')) {
  183.                 if ($statement->obj->getLanguage(!= null{
  184.                     $lang ' <b>(xml:lang="' $statement->obj->getLanguage('") </b> ';
  185.                 else $lang '';
  186.                 if ($statement->obj->getDatatype(!= null{
  187.                     $dtype ' <b>(rdf:datatype="' $statement->obj->getDatatype('") </b> ';
  188.                 else $dtype '';
  189.             else {
  190.                 $lang '';
  191.                 $dtype '';
  192.             }
  193.             echo RDF_Util::getNodeTypeName($statement->getObject())
  194.              . nl2br(htmlspecialchars($statement->obj->getLabel())) $lang $dtype;
  195.  
  196.             echo '</p></td>' RDF_LINEFEED;
  197.             echo RDF_INDENTATION . '</tr>' RDF_LINEFEED;
  198.             $i++;
  199.         }
  200.         echo '</table>' RDF_LINEFEED;
  201.     }
  202.  
  203.     /**
  204.      * Chooses a node color.
  205.      * Used by RDF_Util::writeHTMLTable()
  206.      *
  207.      * @param object Node   $node 
  208.      * @return object Resource 
  209.      * @access protected
  210.      */
  211.     function chooseColor($node)
  212.     {
  213.         if (is_a($node'RDF_BlankNode')) {
  214.             return RDF_HTML_TABLE_BNODE_COLOR;
  215.         elseif (is_a($node'RDF_Literal')) {
  216.             return RDF_HTML_TABLE_LITERAL_COLOR;
  217.         else {
  218.             if (RDF_Util::getNamespace($node== RDF_NAMESPACE_URI
  219.                 || RDF_Util::getNamespace($node== RDF_SCHEMA_URI
  220.             {
  221.                 return RDF_HTML_TABLE_RDF_NS_COLOR;
  222.             }
  223.         }
  224.         return RDF_HTML_TABLE_RESOURCE_COLOR;
  225.     }
  226.  
  227.     /**
  228.      * Get Node Type.
  229.      * Used by RDF_Util::writeHTMLTable()
  230.      *
  231.      * @param object Node   $node 
  232.      * @return object Resource 
  233.      * @access protected
  234.      */
  235.     function getNodeTypeName($node)
  236.     {
  237.         if (is_a($node'RDF_BlankNode')) {
  238.             return 'Blank Node: ';
  239.         elseif (is_a($node'RDF_Literal')) {
  240.             return 'Literal: ';
  241.         else {
  242.             if (RDF_Util::getNamespace($node== RDF_NAMESPACE_URI
  243.                 || RDF_Util::getNamespace($node== RDF_SCHEMA_URI
  244.             {
  245.                 return 'RDF Node: ';
  246.             }
  247.         }
  248.         return 'Resource: ';
  249.     }
  250. // end: RDF_Util
  251.  
  252. ?>

Documentation generated on Mon, 11 Mar 2019 15:39:30 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.