ProcessorXML.php
Go to the documentation of this file.
00001 <?php 00002 00005 00027 class ProcessorXML 00028 { 00029 private $dom; 00030 00031 private $prefixes = array(); 00032 00044 function __construct() 00045 { 00046 $this->dom = new DomDocument("1.0", "utf-8"); 00047 } 00048 00049 function __destruct(){} 00050 00061 function createResultset() 00062 { 00063 $resultset = $this->dom->createElement("resultset"); 00064 00065 return ($resultset); 00066 } 00067 00081 function createSubject($type, $uri = "") 00082 { 00083 $subject = $this->dom->createElement("subject"); 00084 00085 // The TYPE attribute 00086 $type_attr = $this->dom->createAttribute("type"); 00087 $type_attr_value = $this->dom->createTextNode($this->xmlEncode($type)); 00088 00089 $subject->appendChild($type_attr); 00090 $type_attr->appendChild($type_attr_value); 00091 00092 // The URI attribute 00093 $uri_attr = $this->dom->createAttribute("uri"); 00094 $uri_attr_value = $this->dom->createTextNode($this->xmlEncode($uri)); 00095 00096 $subject->appendChild($uri_attr); 00097 $uri_attr->appendChild($uri_attr_value); 00098 00099 return $subject; 00100 } 00101 00115 function createPrefix($entity, $uri) 00116 { 00117 $prefix = $this->dom->createElement("prefix"); 00118 00119 // The TYPE attribute 00120 $prefix_attr = $this->dom->createAttribute("entity"); 00121 $prefix_attr_value = $this->dom->createTextNode($entity); 00122 00123 $prefix->appendChild($prefix_attr); 00124 $prefix_attr->appendChild($prefix_attr_value); 00125 00126 // The URI attribute 00127 $uri_attr = $this->dom->createAttribute("uri"); 00128 $uri_attr_value = $this->dom->createTextNode($uri); 00129 00130 $prefix->appendChild($uri_attr); 00131 $uri_attr->appendChild($uri_attr_value); 00132 00133 return $prefix; 00134 } 00135 00148 function createPredicate($type) 00149 { 00150 $predicate = $this->dom->createElement("predicate"); 00151 00152 // The TYPE attribute 00153 $type_attr = $this->dom->createAttribute("type"); 00154 $type_attr_value = $this->dom->createTextNode($this->xmlEncode($type)); 00155 00156 $predicate->appendChild($type_attr); 00157 $type_attr->appendChild($type_attr_value); 00158 00159 return $predicate; 00160 } 00161 00176 function createObject($type, $uri, $label = "") 00177 { 00178 $object = $this->dom->createElement("object"); 00179 00180 // The TYPE attribute 00181 if ($type != "") 00182 { 00183 $type_attr = $this->dom->createAttribute("type"); 00184 $type_attr_value = $this->dom->createTextNode($this->xmlEncode($type)); 00185 00186 $object->appendChild($type_attr); 00187 $type_attr->appendChild($type_attr_value); 00188 } 00189 00190 // The URI attribute 00191 $uri_attr = $this->dom->createAttribute("uri"); 00192 $uri_attr_value = $this->dom->createTextNode($this->xmlEncode($uri)); 00193 00194 $object->appendChild($uri_attr); 00195 $uri_attr->appendChild($uri_attr_value); 00196 00197 if ($label != "") 00198 { 00199 // The LABEL attribute 00200 $label_attr = $this->dom->createAttribute("label"); 00201 $label_attr_value = $this->dom->createTextNode($this->xmlEncode($label)); 00202 00203 $object->appendChild($label_attr); 00204 $label_attr->appendChild($label_attr_value); 00205 } 00206 00207 return $object; 00208 } 00209 00223 function createObjectContent($content) 00224 { 00225 $objectContent = $this->dom->createElement("object", $this->xmlEncode($content)); 00226 00227 // The TYPE attribute 00228 $type_attr = $this->dom->createAttribute("type"); 00229 $type_attr_value = $this->dom->createTextNode("rdfs:Literal"); 00230 00231 $objectContent->appendChild($type_attr); 00232 $type_attr->appendChild($type_attr_value); 00233 00234 return $objectContent; 00235 } 00236 00237 00254 function createReificationStatement($type, $value) 00255 { 00256 $reify = $this->dom->createElement("reify"); 00257 00258 // The TYPE attribute 00259 $type_attr = $this->dom->createAttribute("type"); 00260 $type_attr_value = $this->dom->createTextNode($this->xmlEncode($type)); 00261 00262 $reify->appendChild($type_attr); 00263 $type_attr->appendChild($type_attr_value); 00264 00265 // The VALUE attribute 00266 $value_attr = $this->dom->createAttribute("value"); 00267 $value_attr_value = $this->dom->createTextNode($this->xmlEncode($value)); 00268 00269 $reify->appendChild($value_attr); 00270 $value_attr->appendChild($value_attr_value); 00271 00272 return $reify; 00273 } 00274 00287 function saveXML($resultset) 00288 { 00289 $this->dom->appendChild($resultset); 00290 00291 $this->dom->formatOutput = true; 00292 00293 return $this->dom->saveXML(); 00294 } 00295 00308 function loadXML($xml_doc) 00309 { 00310 // Get all prefixes for this XML document. 00311 $prefixPos = 0; 00312 00313 while (($prefixPos = stripos($xml_doc, "<prefix", $prefixPos)) !== FALSE) 00314 { 00315 $entityPosStart = stripos($xml_doc, 'entity="', $prefixPos); 00316 $entityPosEnd = stripos($xml_doc, '"', $entityPosStart + 9); 00317 $entity = substr($xml_doc, $entityPosStart + 8, ($entityPosEnd - $entityPosStart - 8)); 00318 00319 $uriPosStart = stripos($xml_doc, 'uri="', $entityPosEnd); 00320 $uriPosEnd = stripos($xml_doc, '"', $uriPosStart + 6); 00321 $uri = substr($xml_doc, $uriPosStart + 5, ($uriPosEnd - $uriPosStart - 5)); 00322 00323 $prefixPos = $uriPosEnd; 00324 00325 $this->prefixes[$entity] = $uri; 00326 } 00327 00328 // Now lets resolve all prefixes in the XML file. 00329 00330 $replace = array(); 00331 $replaceFor = array(); 00332 00333 foreach ($this->prefixes as $prefix => $uri) 00334 { 00335 array_push($replace, "type=\"$prefix:"); 00336 array_push($replaceFor, "type=\"$uri"); 00337 } 00338 00339 $xml_doc = str_ireplace($replace, $replaceFor, $xml_doc); 00340 00341 $this->dom->loadXML($xml_doc); 00342 } 00343 00356 function createResultsetFromElement(&$element) 00357 { 00358 if(get_class($element) != "DOMElement ") 00359 { 00360 $dom = new DomDocument("1.0", "utf-8"); 00361 $resultset = $dom->appendChild($dom->createElement("resultset")); 00362 00363 $domNode = $dom->importNode($element, true); 00364 $resultset->appendChild($domNode); 00365 00366 $dom->formatOutput = true; 00367 00368 return ($dom); 00369 } 00370 00371 return (NULL); 00372 } 00385 function appendElementToRoot(&$element) 00386 { 00387 $domNode = $this->dom->importNode($element, true); 00388 $this->dom->documentElement->appendChild($domNode); 00389 } 00390 00391 function importNode(&$targetElement, $importElement) 00392 { 00393 if(($targetElement != null && (get_class($targetElement) == "DOMNode" || 00394 get_class($targetElement) == "DOMElement")) && 00395 ($importElement != null && (get_class($importElement) == "DOMNode" || 00396 get_class($importElement) == "DOMElement"))) 00397 { 00398 $domNode = $this->dom->importNode($importElement, true); 00399 $targetElement->appendChild($domNode); 00400 } 00401 } 00402 00403 00416 function getSubjectsByType($type) 00417 { 00418 $type = $this->transformPrefix($type); 00419 00420 $xpath = new DOMXPath($this->dom); 00421 00422 $query = '//resultset/subject[attribute::type="' . $type . '"]'; 00423 00424 $subjects = $xpath->query($query); 00425 00426 return ($subjects); 00427 } 00428 00429 function transformPrefix($type) 00430 { 00431 if (strpos($type, ":") !== FALSE) 00432 { 00433 foreach ($this->prefixes as $entity => $uri) 00434 { 00435 if (stripos($type, $entity . ":") !== FALSE) 00436 { 00437 return (str_replace($entity . ":", $uri, $type)); 00438 } 00439 } 00440 } 00441 00442 return $type; 00443 } 00444 00445 00456 function getSubjects() 00457 { 00458 $xpath = new DOMXPath($this->dom); 00459 00460 $query = '//resultset/subject'; 00461 00462 $subjects = $xpath->query($query); 00463 00464 return($subjects); 00465 } 00466 00477 function getPrefixes() 00478 { 00479 $xpath = new DOMXPath($this->dom); 00480 00481 $query = '//resultset/prefix'; 00482 00483 $prefixes = $xpath->query($query); 00484 00485 return ($prefixes); 00486 } 00487 00488 function getSubjectContent(&$subject){} 00489 00490 00503 function getXPath($xpath) 00504 { 00505 $xpath = $this->transformPrefix($xpath); 00506 00507 $xpath_path = new DOMXPath($this->dom); 00508 00509 $nodes = $xpath_path->query($xpath); 00510 00511 return ($nodes); 00512 } 00513 00514 00528 function getPredicatesByType(&$subject, $type) 00529 { 00530 if($subject != null && (get_class($subject) == "DOMNode" || 00531 get_class($subject) == "DOMElement")) 00532 { 00533 $type = $this->transformPrefix($type); 00534 00535 $xpath = new DOMXPath($this->dom); 00536 00537 $query = './/predicate[attribute::type="' . $type . '"]'; 00538 00539 $predicates = $xpath->query($query, $subject); 00540 00541 return($predicates); 00542 } 00543 00544 return(new DOMNodeList()); 00545 } 00546 00559 function getPredicates(&$subject) 00560 { 00561 if($subject != null && (get_class($subject) == "DOMNode" || 00562 get_class($subject) == "DOMElement")) 00563 { 00564 $xpath = new DOMXPath($this->dom); 00565 00566 $query = './/predicate'; 00567 00568 $predicates = $xpath->query($query, $subject); 00569 00570 return ($predicates); 00571 } 00572 00573 return(new DOMNodeList()); 00574 } 00575 00589 function getObjectsByType(&$predicate, $type) 00590 { 00591 if($predicate != null && (get_class($predicate) == "DOMNode" || 00592 get_class($predicate) == "DOMElement")) 00593 { 00594 $type = $this->transformPrefix($type); 00595 00596 $xpath = new DOMXPath($this->dom); 00597 00598 $query = './/object[attribute::type="' . $type . '"]'; 00599 00600 $objects = $xpath->query($query, $predicate); 00601 00602 return ($objects); 00603 } 00604 else 00605 { 00606 return(new DOMNodeList()); 00607 } 00608 } 00609 00622 function getObjects(&$predicate) 00623 { 00624 if($predicate != null && (get_class($predicate) == "DOMNode" || 00625 get_class($predicate) == "DOMElement")) 00626 { 00627 $xpath = new DOMXPath($this->dom); 00628 00629 $query = './/object'; 00630 00631 $objects = $xpath->query($query, $predicate); 00632 00633 return ($objects); 00634 } 00635 00636 return(new DOMNodeList()); 00637 } 00638 00652 function getReificationStatementsByType(&$object, $type) 00653 { 00654 if($object != null && (get_class($object) == "DOMNode" || 00655 get_class($object) == "DOMElement")) 00656 { 00657 $type = $this->transformPrefix($type); 00658 00659 $xpath = new DOMXPath($this->dom); 00660 00661 $query = './/reify[attribute::type="' . $type . '"]'; 00662 00663 $reifies = $xpath->query($query, $object); 00664 00665 return ($reifies); 00666 } 00667 00668 return(new DOMNodeList()); 00669 } 00670 00683 function getReificationStatements(&$object) 00684 { 00685 if($object != null && (get_class($object) == "DOMNode" || 00686 get_class($object) == "DOMElement")) 00687 { 00688 $xpath = new DOMXPath($this->dom); 00689 00690 $query = './/reify'; 00691 00692 $reifies = $xpath->query($query, $object); 00693 00694 return ($reifies); 00695 } 00696 00697 return(new DOMNodeList()); 00698 } 00699 00712 function getURI(&$node) 00713 { 00714 if($node != null && (get_class($node) == "DOMNode" || 00715 get_class($node) == "DOMElement")) 00716 { 00717 if (isset($node->attributes)) 00718 { 00719 return ($node->attributes->getNamedItem("uri")->nodeValue); 00720 } 00721 } 00722 00723 return ""; 00724 } 00725 00738 function getEntity(&$node) 00739 { 00740 if($node != null && (get_class($node) == "DOMNode" || 00741 get_class($node) == "DOMElement")) 00742 { 00743 if(isset($node->attributes)) 00744 { 00745 return ($node->attributes->getNamedItem("entity")->nodeValue); 00746 } 00747 } 00748 return ""; 00749 } 00750 00751 00764 function getLabel(&$node) 00765 { 00766 if($node != null && (get_class($node) == "DOMNode" || 00767 get_class($node) == "DOMElement")) 00768 { 00769 if (isset($node->attributes)) 00770 { 00771 return ($node->attributes->getNamedItem("label")->nodeValue); 00772 } 00773 } 00774 00775 return ""; 00776 } 00777 00790 function getValue(&$node) 00791 { 00792 if($node != null && (get_class($node) == "DOMNode" || 00793 get_class($node) == "DOMElement")) 00794 { 00795 if (isset($node->attributes)) 00796 { 00797 return ($node->attributes->getNamedItem("value")->nodeValue); 00798 } 00799 } 00800 00801 return ""; 00802 } 00803 00817 function getType(&$node, $prefixed = TRUE) 00818 { 00819 if($node != null && (get_class($node) == "DOMNode" || 00820 get_class($node) == "DOMElement")) 00821 { 00822 if (isset($node->attributes)) 00823 { 00824 if ($prefixed === TRUE) 00825 { 00826 foreach ($this->prefixes as $entity => $uri) 00827 { 00828 if (stripos($node->attributes->getNamedItem("type")->nodeValue, $uri) !== FALSE) 00829 { 00830 return (str_replace($uri, $entity . ":", $node->attributes->getNamedItem("type")->nodeValue)); 00831 } 00832 } 00833 } 00834 00835 return ($node->attributes->getNamedItem("type")->nodeValue); 00836 } 00837 } 00838 00839 return ""; 00840 } 00841 00854 function getContent(&$node) 00855 { 00856 if($node != null && (get_class($node) == "DOMNode" || 00857 get_class($node) == "DOMElement")) 00858 { 00859 if(isset($node->attributes)) 00860 { 00861 return($node->nodeValue); 00862 } 00863 } 00864 00865 return(""); 00866 } 00867 00872 function removeChildren(&$node) 00873 { 00874 while ($node->firstChild) 00875 { 00876 while ($node->firstChild->firstChild) 00877 { 00878 $this->removeChildren($node->firstChild); 00879 } 00880 00881 $node->removeChild($node->firstChild); 00882 } 00883 } 00884 00898 function removePredicatesByType(&$subject, $type) 00899 { 00900 $type = $this->transformPrefix($type); 00901 00902 $xpath = new DOMXPath($this->dom); 00903 00904 $query = './/predicate[attribute::type="' . $type . '"]'; 00905 00906 $predicates = $xpath->query($query, $subject); 00907 00908 foreach($predicates as $predicate) 00909 { 00910 $predicate->parentNode->removeChild($predicate); 00911 } 00912 } 00913 00926 function xmlEncode($string) 00927 { 00928 // Replace all the possible entities by their character. That way, we won't "double encode" 00929 // these entities. Otherwise, we can endup with things such as "&amp;" which some 00930 // XML parsers doesn't seem to like (and throws errors). 00931 $string = str_replace(array ("%5C", "&", "<", ">"), array ("\\", "&", "<", ">"), $string); 00932 00933 return str_replace(array ("\\", "&", "<", ">"), array ("%5C", "&", "<", ">"), $string); 00934 } 00935 00936 function saveRdfN3() 00937 { 00938 $rdf_part = "@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n\n"; 00939 $rdf_reification = ""; 00940 00941 $subjects = $this->getSubjects(); 00942 00943 foreach($subjects as $subject) 00944 { 00945 $subjectURI = $this->getURI($subject); 00946 $subjectType = $this->getType($subject, FALSE); 00947 00948 $rdf_part .= "<$subjectURI> a <$subjectType> ;\n"; 00949 00950 $predicates = $this->getPredicates($subject); 00951 00952 foreach($predicates as $predicate) 00953 { 00954 $objects = $this->getObjects($predicate); 00955 00956 foreach($objects as $object) 00957 { 00958 $objectType = $this->getType($object); 00959 $predicateType = $this->getType($predicate, FALSE); 00960 00961 if($objectType == "rdfs:Literal") 00962 { 00963 $objectValue = $this->getContent($object); 00964 $rdf_part .= " <$predicateType> \"" . $this->escapeN3($objectValue) . "\" ;\n"; 00965 } 00966 else 00967 { 00968 $objectURI = $this->getURI($object); 00969 $rdf_part .= " <$predicateType> <$objectURI> ;\n"; 00970 } 00971 00972 00973 // Check for reified statements. 00974 $reifies = $this->getReificationStatements($object); 00975 00976 foreach($reifies as $reify) 00977 { 00978 $reifyPredicate = $this->getType($reify, FALSE); 00979 00980 $rdf_reification .= "_:" . md5($this->getURI($subject) . $predicateType . $this->getURI($object)) 00981 . " a rdf:Statement ;\n"; 00982 $rdf_reification .= " rdf:subject <" . $this->getURI($subject) . "> ;\n"; 00983 $rdf_reification .= " rdf:predicate <" . $predicateType . "> ;\n"; 00984 $rdf_reification .= " rdf:object <" . $this->getURI($object) . "> ;\n"; 00985 $rdf_reification .= " <$reifyPredicate> \"" . $this->escapeN3($this->getValue($reify)) . "\" .\n\n"; 00986 } 00987 } 00988 } 00989 00990 if(strlen($rdf_part) > 0) 00991 { 00992 $rdf_part = substr($rdf_part, 0, strlen($rdf_part) - 2) . ". \n"; 00993 } 00994 } 00995 00996 return($rdf_part.$rdf_reification); 00997 } 00998 00999 private function escapeN3($literal) 01000 { 01001 $literal = str_replace("\\","\\\\", $literal); 01002 01003 return str_replace(array('"', "'"), array('\\"', "\\'"), $literal); 01004 } 01005 } 01006 01008 01009 ?>
