Changeset 74


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
Location:
php/trunk/classes
Files:
2 edited

Legend:

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

    r73 r74  
    2424
    2525  private $usedLibrary = NULL;
     26  private $aNamespace;
    2627
    2728  // ---------------------------------------
     
    3132    // determ ine library to use
    3233    $this->usedLibrary = 'arc';
     34   
     35    $this->aNamespace = array();
    3336
    3437  } // public function __construct
     
    118121    }
    119122    return $result;
     123
    120124  }
    121125
     126  // ---------------------------------------
     127
     128  public function filterTriples( $aTriples,
     129                                 $subject = NULL,
     130                                 $predicate = NULL,
     131                                 $object = NULL) {
     132
     133    $searchTriple = array ( 's' =>  $subject, 'p' =>  $predicate, 'o' =>  $object);
     134
     135    // resolve namespace prefixes
     136    $uriSubject   = $this->resolve( $subject);
     137    $uriPredicate = $this->resolve( $predicate);
     138    $uriObject    = $this->resolve( $object);
     139
     140    // resolve method may return NULL, then use original value
     141    $uriSubject   = ($uriSubject   === NULL) ? $subject   : $uriSubject;
     142    $uriPredicate = ($uriPredicate === NULL) ? $predicate : $uriPredicate;
     143    $uriObject    = ($uriObject    === NULL) ? $object    : $uriObject;
     144
     145    // filter all available triples
     146    $aresult = array();
     147    foreach ($aTriples as $aTriple) {
     148
     149      // must be triple
     150      if ((isset( $aTriple[ 'type'])) &&
     151          ($aTriple[ 'type'] != 'triple'))
     152        continue;
     153
     154      // check subject and predicate match if given
     155      if (($uriSubject != NULL)   && (strcmp( $uriSubject, $aTriple[ 's'])))
     156        continue;
     157
     158      if (($uriPredicate != NULL) && (strcmp( $uriPredicate, $aTriple[ 'p'])))
     159        continue;
     160
     161      // check object if given
     162      if ($uriObject != NULL) {
     163
     164        // check object match
     165        if (strcmp( $uriObject, $aTriple[ 'o']))
     166          continue;
     167
     168      } //  if ($uriObject != NULL) {
     169
     170      // store in result array
     171      $aresult[] = $aTriple;
     172
     173    } // foreach ($aTriples as $aTriple)
     174
     175    return $aresult;
     176
     177  } //  static function filterTriples
     178 
     179  // ---------------------------------------
     180 
     181  public function setMapping( $prefix, $uriNamespace) {
     182
     183    if ($prefix == '')
     184      return false;
     185
     186    // add colon to prefix as internally req.
     187    if (substr( $prefix, -1) != ':')
     188      $prefix = "$prefix:";
     189
     190    if (isset( $this->aNamespace[ $prefix])) {
     191      $oldUriNamespace = $this->aNamespace[ $prefix];
     192      if ($oldUriNamespace == $uriNamespace)
     193        $debugmessage = "Namespace mapping exists: @prefix $prefix <$uriNamespace> .";
     194      else
     195        $debugmessage = "Namespace mapping overwritten: @prefix $prefix <$uriNamespace> .\n" .
     196                        "Old mapping was: @prefix $prefix <$oldUriNamespace>";
     197    } else {
     198      $debugmessage = "Namespace mapping added: @prefix $prefix <$uriNamespace> .";
     199    }
     200
     201    // set URI
     202    $this->aNamespace[ $prefix] = $uriNamespace;
     203    return $debugmessage;
     204
     205  } // public function setMapping
     206 
     207  // ---------------------------------------
     208
     209  public function resolve( $curie) {
     210
     211    $replacecount = 1;
     212    $uri = NULL;
     213    if ($curie != NULL) {
     214      if ($this->aNamespace != NULL) {
     215        if ((strpos( $uri, ":/") !== false) ||
     216            (strpos( $uri, "_:") === 0)) {
     217          $uri = $curi;
     218        } else {
     219          // check for namespaces
     220          foreach ($this->aNamespace as $prefix => $uriNamespace) {
     221            // check for prefix match
     222            $posPrefix = strpos( $curie, $prefix);
     223            if ($posPrefix === 0) {
     224              // replace prefix and bail out
     225              $uri = str_replace( $prefix, $uriNamespace, $curie, $replacecount);
     226              break;
     227            }
     228          } // foreach ($this->aNamespace
     229        } // if ((strpos ...
     230      } // if ($aNamespace != NULL) {
     231    }  // if ($uri != NULL) {
     232
     233    return $uri;
     234
     235  } //  public function resolve
     236
     237  // ---------------------------------------
     238
     239  public function shrink( $uri) {
     240
     241    $replacecount = 1;
     242    if ($uri != NULL) {
     243      if ($this->aNamespace != NULL) {
     244        if (strpos( $uri, ":/") !== false) {
     245          foreach ($this->aNamespace as $prefix => $uriNamespace) {
     246            // search namespace URI
     247            $posNamespace = strpos( $uri, $uriNamespace);
     248            if ($posNamespace === false)
     249              continue;
     250            // replace namespace URI and bail out
     251            $uri = str_replace( $uriNamespace, $prefix, $uri, &$replacecount);
     252            break;
     253          } // foreach ($aNamespace
     254        } // if (strpos( $uri, ":/") !== false)
     255      } // if ($aNamespace != NULL) {
     256    }  // if ($uri != NULL) {
     257
     258    return $uri;
     259
     260  }  //  public function shrink
     261
     262
    122263} // class Core
    123264
  • 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.