ARC2_POSHRDFSerializer.php 2.42 KB
Newer Older
tobyink's avatar
tobyink committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
<?php
/*
homepage: http://arc.semsol.org/
license:  http://arc.semsol.org/license

class:    ARC2 POSH RDF Serializer
author:   Benjamin Nowack
version:  2008-11-18 (Tweak: Updated to poshRDF spec draft)
*/

ARC2::inc('RDFSerializer');

class ARC2_POSHRDFSerializer extends ARC2_RDFSerializer {

  function __construct($a = '', &$caller) {
    parent::__construct($a, $caller);
  }
  
  function ARC2_POSHRDFSerializer($a = '', &$caller) {/* ns */
    $this->__construct($a, $caller);
  }

  function __init() {
    parent::__init();
    $this->content_header = 'text/html';
  }

  /*  */
  
  function getLabel($res, $ps = '') {
    if (!$ps) $ps = array();
    foreach ($ps as $p => $os) {
      if (preg_match('/[\/\#](name|label|summary|title|fn)$/i', $p)) {
        return $os[0]['value'];
      }
    }
    if (preg_match('/^\_\:/', $res)) return "An unnamed resource";
    return preg_replace("/^(.*[\/\#])([^\/\#]+)$/", '\\2', str_replace('_', ' ', $res));
  }
  
  function getSerializedIndex($index, $res = '') {
    $r = '';
    $n = "\n";
    if ($res) $index = array($res => $index[$res]);
    //return Trice::dump($index);
    foreach ($index as $s => $ps) {
      /* node */
      $r .= '
        <div class="rdf-node dump">
          <h3><a class="rdf-s" href="' . $s . '">' . $this->getLabel($s, $ps)  . '</a></h3>
      ';
      /* arcs */
      foreach ($ps as $p => $os) {
        $r .= '
          <div class="rdf-arcs">
            <a class="rdf-p" href="' . $p . '">' . ucfirst($this->getLabel($p)) . '</a>
        ';
        foreach ($os as $o) {
          $r .= $n . $this->getObjectValue($o);
        }
        $r .= '    
          </div>
        ';
      }
      /* node */
      $r .= '
        <div class="clb"></div>
        </div>
      ';
    }
    return $r;
  }
  
  function getObjectValue($o) {
    if ($o['type'] == 'uri') {
      if (preg_match('/(jpe?g|gif|png)$/i', $o['value'])) {
        return '<img class="rdf-o" src="' . htmlspecialchars($o['value']) . '" alt="img" />';
      }
      return '<a class="rdf-o" href="' . htmlspecialchars($o['value']) . '">' . preg_replace('/^https?\:\/\/(www\.)?/', '', $o['value']) . '</a>';
    }
    if ($o['type'] == "bnode") {
      return '<div class="rdf-o" title="' . $o['value']. '">An unnamed resource</div>';
    }
    return '<div class="rdf-o">' . $o['value'] . '</div>';
  }
  
  /*  */

}