Changeset 74
- Timestamp:
- Oct 24, 2011, 3:22:50 PM (14 years ago)
- Location:
- php/trunk/classes
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
php/trunk/classes/Core.php
r73 r74 24 24 25 25 private $usedLibrary = NULL; 26 private $aNamespace; 26 27 27 28 // --------------------------------------- … … 31 32 // determ ine library to use 32 33 $this->usedLibrary = 'arc'; 34 35 $this->aNamespace = array(); 33 36 34 37 } // public function __construct … … 118 121 } 119 122 return $result; 123 120 124 } 121 125 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 122 263 } // class Core 123 264 -
php/trunk/classes/rdfa_Data.php
r72 r74 64 64 65 65 private $debugger; 66 private $core; 66 67 private $aTriples; 67 private $aNamespace;68 68 69 69 // --------------------------------------- … … 77 77 // setup debugger 78 78 $this->debugger = \fDebug::getInstance(); 79 80 $this->core = new \rdfint_core\Core(); 79 81 80 82 // initialize some vars 81 83 $this->aTriples = array(); 82 $this->aNamespace = array(); 84 83 85 84 86 } // public function __construct … … 127 129 'o' => $object)); 128 130 } 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 129 145 130 146 // -------------------------------------------------------- … … 139 155 &$debugmessage_result) { 140 156 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); 182 158 183 159 // show result in debugger … … 214 190 } // private function _subjectExists 215 191 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 261 193 262 194 // -------------------------------------------------------- … … 356 288 */ 357 289 public function setMapping( $prefix, $uriNamespace) { 358 $debugmessage = $this-> _addNamespaceMapping( $prefix, $uriNamespace);290 $debugmessage = $this->core->setMapping( $prefix, $uriNamespace); 359 291 if ($debugmessage != false) 360 292 $this->debugger->sendMessage( $debugmessage, … … 378 310 public function _resolve( $curie) { 379 311 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); 403 313 404 314 } // public function _resolve … … 417 327 */ 418 328 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); 438 331 439 332 } // public function _shrink … … 1077 970 public function _serialize( $type ) { 1078 971 1079 $core = new \rdfint_core\Core(); 972 $core = new \rdfint_core\Core(); 1080 973 return $core->serialize( $this->aTriples, $type); 1081 974
Note:
See TracChangeset
for help on using the changeset viewer.