Ignore:
Timestamp:
Oct 24, 2011, 3:22:50 PM (14 years ago)
Author:
cla
Message:

rdfint

  • moded general code for filtering triples and handling namespaces to core wrapper class
File:
1 edited

Legend:

Unmodified
Added
Removed
  • php/trunk/classes/rdfa_Data.php

    r72 r74  
    6464
    6565  private $debugger;
     66  private $core;
    6667  private $aTriples;
    67   private $aNamespace;
    6868
    6969  // ---------------------------------------
     
    7777    // setup debugger
    7878    $this->debugger = \fDebug::getInstance();
     79   
     80    $this->core = new \rdfint_core\Core();
    7981
    8082    // initialize some vars
    8183    $this->aTriples = array();
    82     $this->aNamespace = array();
     84
    8385
    8486  } // public function __construct
     
    127129                                                 'o' => $object));
    128130  }
     131 
     132  // --------------------------------------------------------
     133
     134  /**
     135   *  Shrink URI to CURI, but return <URI> if mapping not successful.
     136   */
     137  private function _shrinkFormatted( $uri) {
     138
     139    $curie = $this->_shrink( $uri);
     140    if ($curie == $uri)
     141      $curie = "<$uri>";
     142    return $curie;
     143
     144  }  // private function _shrinkFormatted
    129145
    130146  // --------------------------------------------------------
     
    139155                                   &$debugmessage_result) {
    140156
    141     $searchTriple = array ( 's' =>  $subject, 'p' =>  $predicate, 'o' =>  $object);
    142 
    143     // resolve namespace prefixes
    144     $uriSubject   = $this->_resolve( $subject);
    145     $uriPredicate = $this->_resolve( $predicate);
    146     $uriObject    = $this->_resolve( $object);
    147 
    148     // resolve method may return NULL, then use original value
    149     $uriSubject   = ($uriSubject   === NULL) ? $subject   : $uriSubject;
    150     $uriPredicate = ($uriPredicate === NULL) ? $predicate : $uriPredicate;
    151     $uriObject    = ($uriObject    === NULL) ? $object    : $uriObject;
    152 
    153     // filter all available triples
    154     $aresult = array();
    155     foreach ($this->aTriples as $aTriple) {
    156 
    157       // must be triple
    158       if ((isset( $aTriple[ 'type'])) &&
    159           ($aTriple[ 'type'] != 'triple'))
    160         continue;
    161 
    162       // check subject and predicate match if given
    163       if (($uriSubject != NULL)   && (strcmp( $uriSubject, $aTriple[ 's'])))
    164         continue;
    165 
    166       if (($uriPredicate != NULL) && (strcmp( $uriPredicate, $aTriple[ 'p'])))
    167         continue;
    168 
    169       // check object if given
    170       if ($uriObject != NULL) {
    171 
    172         // check object match
    173         if (strcmp( $uriObject, $aTriple[ 'o']))
    174           continue;
    175 
    176       } //  if ($uriObject != NULL) {
    177 
    178       // store in result array
    179       $aresult[] = $aTriple;
    180 
    181     } // foreach ($aTriples as $aTriple)
     157    $aresult = $this->core->filterTriples( $this->aTriples, $subject, $predicate, $object);
    182158
    183159    // show result in debugger
     
    214190  } // private function _subjectExists
    215191
    216   // --------------------------------------------------------
    217 
    218   /**
    219    * Add mapping to internal namespace list.
    220    * This method is called internally, not producing debug output.
    221    */
    222   private function _addNamespaceMapping( $prefix, $uriNamespace) {
    223 
    224     if ($prefix == '')
    225       return false;
    226 
    227     // add colon to prefix as internally req.
    228     if (substr( $prefix, -1) != ':')
    229       $prefix = "$prefix:";
    230 
    231     if (isset( $this->aNamespace[ $prefix])) {
    232       $oldUriNamespace = $this->aNamespace[ $prefix];
    233       if ($oldUriNamespace == $uriNamespace)
    234         $debugmessage = "Namespace mapping exists: @prefix $prefix <$uriNamespace> .";
    235       else
    236         $debugmessage = "Namespace mapping overwritten: @prefix $prefix <$uriNamespace> .\n" .
    237                         "Old mapping was: @prefix $prefix <$oldUriNamespace>";
    238     } else {
    239       $debugmessage = "Namespace mapping added: @prefix $prefix <$uriNamespace> .";
    240     }
    241 
    242     // set URI
    243     $this->aNamespace[ $prefix] = $uriNamespace;
    244     return $debugmessage;
    245 
    246   } // private function _addNamespaceMapping
    247 
    248   // --------------------------------------------------------
    249 
    250   /**
    251    *  Shrink URI to CURI, but return <URI> if mapping not successful.
    252    */
    253   private function _shrinkFormatted( $uri) {
    254 
    255     $curie = $this->_shrink( $uri);
    256     if ($curie == $uri)
    257       $curie = "<$uri>";
    258     return $curie;
    259 
    260   }  // private function _shrinkFormatted
     192
    261193
    262194  // --------------------------------------------------------
     
    356288   */
    357289  public function setMapping( $prefix, $uriNamespace) {
    358     $debugmessage = $this->_addNamespaceMapping( $prefix, $uriNamespace);
     290    $debugmessage = $this->core->setMapping( $prefix, $uriNamespace);
    359291    if ($debugmessage != false)
    360292      $this->debugger->sendMessage( $debugmessage,
     
    378310  public function _resolve( $curie) {
    379311
    380     $replacecount = 1;
    381     $uri = NULL;
    382     if ($curie != NULL) {
    383       if ($this->aNamespace != NULL) {
    384         if ((strpos( $uri, ":/") !== false) ||
    385             (strpos( $uri, "_:") === 0)) {
    386           $uri = $curi;
    387         } else {
    388           // check for namespaces
    389           foreach ($this->aNamespace as $prefix => $uriNamespace) {
    390             // check for prefix match
    391             $posPrefix = strpos( $curie, $prefix);
    392             if ($posPrefix === 0) {
    393               // replace prefix and bail out
    394               $uri = str_replace( $prefix, $uriNamespace, $curie, $replacecount);
    395               break;
    396             }
    397           } // foreach ($this->aNamespace
    398         } // if ((strpos ...
    399       } // if ($aNamespace != NULL) {
    400     }  // if ($uri != NULL) {
    401 
    402     return $uri;
     312    return $this->core->resolve( $curie);
    403313
    404314  } //  public function _resolve
     
    417327   */
    418328  public function _shrink( $uri) {
    419 
    420     $replacecount = 1;
    421     if ($uri != NULL) {
    422       if ($this->aNamespace != NULL) {
    423         if (strpos( $uri, ":/") !== false) {
    424           foreach ($this->aNamespace as $prefix => $uriNamespace) {
    425             // search namespace URI
    426             $posNamespace = strpos( $uri, $uriNamespace);
    427             if ($posNamespace === false)
    428               continue;
    429             // replace namespace URI and bail out
    430             $uri = str_replace( $uriNamespace, $prefix, $uri, &$replacecount);
    431             break;
    432           } // foreach ($aNamespace
    433         } // if (strpos( $uri, ":/") !== false)
    434       } // if ($aNamespace != NULL) {
    435     }  // if ($uri != NULL) {
    436 
    437     return $uri;
     329   
     330    return $this->core->shrink( $uri);
    438331
    439332  }  //  public function _shrink
     
    1077970  public function _serialize( $type ) {
    1078971 
    1079     $core = new \rdfint_core\Core();   
     972    $core = new \rdfint_core\Core();
    1080973    return $core->serialize( $this->aTriples, $type);
    1081974
Note: See TracChangeset for help on using the changeset viewer.