Source for file RdfSerializer.php

Documentation is available at RdfSerializer.php

  1. <?php
  2.  
  3.  
  4. // ----------------------------------------------------------------------------------
  5. // Class: RdfSerializer
  6. // ----------------------------------------------------------------------------------
  7.  
  8.  
  9.  
  10. /**
  11. * An RDF seralizer.
  12. * Seralizes models to RDF syntax. It supports the xml:base, xml:lang, rdf:datatype and
  13. * rdf:nodeID directive.
  14. * You can choose between different output syntaxes by using the configuration methods
  15. * or changing the configuration default values in constants.php.
  16. * This class is based on the java class edu.unika.aifb.rdf.api.syntax.RDFSerializer by Boris Motik.
  17. *
  18. * <BR><BR>History:<UL>
  19. * <LI>12-14-2004 : bug in function serialize() fixed by Sören Auer(auer@informatik.uni-leipzig.de) </LI>
  20. * <LI>12-06-2004 : improved namespace handling added (tobias.gauss@web.de)</LI>
  21. * <LI>01-06-2004 : Empty model bug fixed.</LI>
  22. * <LI>03-25-2004 : Bug in saveAs() fixed by inkel(inkel-php@f14web.com.ar)</LI>
  23. * <LI>03-21-2004 : Support for xml default namespace added
  24. * Methods changed: serialize(), writeNamespaceDeclarations(), getElementText()</LI>
  25. * <LI>11-17-2003 : Support for XMLLiterals added.</LI>
  26. * <LI>07-27-2003 : Functions addapted to the new class tree (MemModel extends Model)</LI>
  27. * <LI>07-24-2003 : Bug in writeAbsoluteResourceReference() fixed by Paul Cowles(paul@semaview.com) </LI>
  28. * <LI>02-21-2003 : saveAs method added.</LI>
  29. * <LI>02-12-2003 : rdf:type and bNode reference bug fixed.</LI>
  30. * <LI>01-15-2003 : pass-by-reference bug fixed.</LI>
  31. * <LI>01-10-2003 : rdf:datatype and $useAttributes bug fixed.</LI>
  32. * <LI>12-04-2002 : Added support for rdf:datatype (writeContentStatements())</LI>
  33. * <LI>12-04-2002 : Added rdf:nodeID attribute for blank nodes (writeSubjectURI, writeResourceReference)</LI>
  34. * <LI>10-03-2002 : Bug in collectNamespaces() fixed.</LI>
  35. * <LI>09-15-2002 : First version of this class.</LI>
  36. * </UL>
  37. *
  38. * @version V0.9.1
  39. * @author Chris Bizer <chris@bizer.de>, Boris Motik <motik@fzi.de>, Daniel Westphal <dawe@gmx.de>, Leandro Mariano Lopez <llopez@xinergiaargentina.com>
  40. *
  41. * @package syntax
  42. * @access public
  43. *
  44. */
  45. class RdfSerializer extends Object {
  46.  
  47. // configuration
  48. var $use_entities;
  49. var $use_attributes;
  50. var $sort_model;
  51. var $rdf_qnames;
  52. var $use_xml_declaration;
  53.  
  54. // properties
  55. var $m_defaultNamespaces = array();
  56. var $m_namespaces = array();
  57. var $m_nextAutomaticPrefixIndex;
  58. var $m_out;
  59. var $m_baseURI;
  60. var $m_statements = array();
  61. var $m_currentSubject;
  62. var $m_rdfIDElementText;
  63. var $m_rdfAboutElementText;
  64. var $m_rdfResourceElementText;
  65. var $m_groupTypeStatement;
  66. var $m_attributeStatements = array();
  67. var $m_contentStatements = array();
  68. var $rdf_qname_prefix;
  69.  
  70. /**
  71. * Constructor
  72. *
  73. * @access public
  74. */
  75. function RdfSerializer() {
  76.  
  77. // default serializer configuration
  78. $this->use_entities = SER_USE_ENTITIES;
  79. $this->use_attributes = SER_USE_ATTRIBUTES;
  80. $this->sort_model = SER_SORT_MODEL;
  81. $this->rdf_qnames = SER_RDF_QNAMES;
  82. $this->use_xml_declaration = SER_XML_DECLARATION;
  83.  
  84. global $default_prefixes;
  85. foreach($default_prefixes as $key => $value){
  86. $this->addNamespacePrefix($key,$value);
  87. }
  88.  
  89. require_once(RDFAPI_INCLUDE_DIR.PACKAGE_UTILITY);
  90. }
  91.  
  92. /**
  93. * Serializer congiguration: Sort Model
  94. * Flag if the serializer should sort the model by subject before serializing.
  95. * TRUE makes the RDF code more compact.
  96. * TRUE is default. Default can be changed in constants.php.
  97. *
  98. * @param boolean
  99. * @access public
  100. */
  101. function configSortModel($bool) {
  102. $this->sort_model = $bool;
  103. }
  104.  
  105. /**
  106. * Serializer congiguration: Use Entities
  107. * Flag if the serializer should use entities for URIs.
  108. * TRUE makes the RDF code more compact.
  109. * FALSE is default. Default can be changed in constants.php.
  110. *
  111. * @param boolean
  112. * @access public
  113. */
  114. function configUseEntities($bool) {
  115. $this->use_entities = $bool;
  116. }
  117.  
  118. /**
  119. * Serializer congiguration: Use Attributes
  120. * Flag if the serializer should serialize triples as XML attributes where possible.
  121. * TRUE makes the RDF code more compact.
  122. * FALSE is default. Default can be changed in constants.php.
  123. *
  124. * @param boolean
  125. * @access public
  126. */
  127. function configUseAttributes($bool) {
  128. $this->use_attributes = $bool;
  129. }
  130.  
  131. /**
  132. * Serializer congiguration: Use Qnames
  133. * Flag if the serializer should use qualified names for RDF reserved words.
  134. * TRUE makes the RDF code more compact.
  135. * TRUE is default. Default can be changed in constants.php.
  136. *
  137. * @param boolean
  138. * @access public
  139. */
  140. function configUseQnames($bool) {
  141. $this->rdf_qnames = $bool;
  142. }
  143.  
  144. /**
  145. * Serializer congiguration: Use XML Declaration
  146. * Flag if the serializer should start documents with the xml declaration
  147. * <?xml version="1.0" encoding="UTF-8" ?>.
  148. * TRUE is default. Default can be changed in constants.php.
  149. *
  150. * @param boolean
  151. * @access public
  152. */
  153. function configUseXmlDeclaration($bool) {
  154. $this->use_xml_declaration = $bool;
  155. }
  156.  
  157.  
  158. /**
  159. * Adds a new prefix/namespace combination.
  160. *
  161. * @param String $prefix
  162. * @param String $namespace
  163. * @access public
  164. */
  165. function addNamespacePrefix($prefix, $namespace) {
  166. $this->m_defaultNamespaces[$prefix] = $namespace;
  167. }
  168.  
  169. /**
  170. * Serializes a model to RDF syntax.
  171. * RDF syntax can be changed by config_use_attributes($boolean), config_use_entities($boolean),
  172. * config_sort_model($boolean).
  173. * NOTE: There is only one default namespace allowed within an XML document.
  174. * Therefore if SER_RDF_QNAMES in constants.php is set to FALSE and you pass
  175. * another $xml_default_namespace as parameter, the model will be serialized
  176. * as if SER_RDF_QNAMES were set to TRUE.
  177. *
  178. * @param object MemModel $model
  179. * @param String $encoding
  180. * @return string
  181. * @access public
  182. */
  183. function & serialize(&$model, $xml_default_namespace = NULL, $encoding = DEFAULT_ENCODING) {
  184.  
  185. if ($xml_default_namespace) {
  186.  
  187. if ($xml_default_namespace == RDF_NAMESPACE_URI) {
  188. $this->rdf_qnames = FALSE;
  189. unset($this->m_defaultNamespaces[RDF_NAMESPACE_PREFIX]);
  190. }
  191. elseif ($xml_default_namespace == RDF_SCHEMA_URI) {
  192. unset($this->m_defaultNamespaces[RDF_SCHEMA_PREFIX]);
  193. }
  194. elseif (!SER_RDF_QNAMES)
  195. $this->rdf_qnames = TRUE;
  196.  
  197. $this->addNamespacePrefix(NULL, $xml_default_namespace);
  198. }
  199.  
  200. //copy parsed namespaces
  201. $nsps = array();
  202. $nsps = $model->getParsedNamespaces();
  203. foreach($this->m_defaultNamespaces as $prefix => $namespace){
  204. if(!isset ($nsps[$namespace]))
  205. $nsps[$namespace] = $prefix;
  206. }
  207.  
  208. // define rdf prefix (qname or not)
  209. if ($this->rdf_qnames){
  210. if(isset($nsps[RDF_NAMESPACE_URI])){
  211. $this->rdf_qname_prefix = $nsps[RDF_NAMESPACE_URI].':';
  212. }else{
  213. $this->rdf_qname_prefix = RDF_NAMESPACE_PREFIX . ':';
  214. }
  215.  
  216. }else{
  217. $this->rdf_qname_prefix = '';
  218. }
  219. // check if model is empty
  220. if ($model->size() == 0) return "<". $this->rdf_qname_prefix . RDF_RDF ." xmlns:rdf='".RDF_NAMESPACE_URI."' />";
  221.  
  222. foreach($nsps as $ns => $pre){
  223. $this->m_namespaces[$pre] = $ns;
  224. }
  225.  
  226.  
  227. // set base URI
  228. if ($model->getBaseURI()==NULL)
  229. $this->m_baseURI="opaque:uri";
  230. else
  231. $this->m_baseURI=$model->getBaseURI();
  232.  
  233.  
  234. if ($this->sort_model) {
  235. // sort the array of statements
  236.  
  237. foreach($model->triples as $key => $statement) {
  238. $stmkey = $statement->subj->getURI() .
  239. $statement->pred->getURI() .
  240. (is_a($statement->obj,'Literal')?'"'.$statement->obj->getLabel().'"@'.$statement->obj->getLanguage().'^^'.$statement->obj->getDatatype():$statement->obj->getURI());
  241. $this->m_statements[$stmkey] = $statement;
  242. }
  243. ksort($this->m_statements);
  244.  
  245. /*
  246. // Sort using the PHP usort() function. Slower :-(
  247. $this->m_statements = $model->triples;
  248. usort($this->m_statements, "statementsorter");
  249. */
  250. } else {
  251. $this->m_statements = $model->triples;
  252. }
  253. // collects namespaces
  254. $this->m_nextAutomaticPrefixIndex=0;
  255. $this->collectNamespaces($model);
  256.  
  257. // start writing the contents
  258. $this->m_out="";
  259. if ($this->use_xml_declaration)
  260. $this->m_out .= '<?xml version="1.0" encoding="' . $encoding . '" ?>' . LINEFEED;
  261. if (!HIDE_ADVERTISE)
  262. $this->m_out.="<!-- Generated by RdfSerializer.php from RDF RAP.".LINEFEED."# http://www.wiwiss.fu-berlin.de/suhl/bizer/rdfapi/index.html !-->".LINEFEED.LINEFEED ;
  263.  
  264. // write entitie declarations
  265. if($this->use_entities) {
  266. $this->m_out .= '<!DOCTYPE ' . $this->rdf_qname_prefix .
  267. RDF_RDF.' [' . LINEFEED;
  268. $this->writeEntityDeclarations();
  269. $this->m_out .= LINEFEED . ']>' . LINEFEED;
  270. }
  271.  
  272. // start the RDF text
  273. $this->m_out .= '<' . $this->rdf_qname_prefix . RDF_RDF;
  274.  
  275. // write the xml:base
  276. if ($model->getBaseURI() != NULL)
  277. $this->m_out .= LINEFEED. INDENTATION .'xml:base="'.$model->getBaseURI().'"';
  278.  
  279. // write namespaces declarations
  280. $this->writeNamespaceDeclarations();
  281. $this->m_out .='>'. LINEFEED;
  282.  
  283. // write triples
  284. $this->writeDescriptions();
  285.  
  286. $this->m_out .= LINEFEED;
  287. $this->m_out .='</' . $this->rdf_qname_prefix . RDF_RDF .'>';
  288.  
  289. $this->m_namespaces=null;
  290. $this->m_statements=null;
  291. $this->m_currentSubject=null;
  292. $this->m_groupTypeStatement=null;
  293. $this->m_attributeStatements=null;
  294. $this->m_contentStatements=null;
  295. $this->m_rdfResourceElementText=null;
  296.  
  297. return $this->m_out;
  298. }
  299.  
  300. /**
  301. * Serializes a model and saves it into a file.
  302. * Returns FALSE if the model couldn't be saved to the file.
  303. *
  304. * @param object MemModel $model
  305. * @param String $encoding
  306. * @return boolean
  307. * @access public
  308. */
  309. function saveAs(&$model, $filename, $encoding = DEFAULT_ENCODING) {
  310. // serialize model
  311. $RDF = $this->serialize($model, NULL, $encoding);
  312.  
  313. //write serialized model to file
  314. $file_handle = @fopen($filename, 'w');
  315. if ($file_handle) {
  316. fwrite($file_handle, $RDF);
  317. fclose($file_handle);
  318. return TRUE;
  319. } else {
  320. return FALSE;
  321. };
  322. }
  323.  
  324. /**
  325. * @access private
  326. */
  327. function writeEntityDeclarations() {
  328. foreach($this->m_namespaces as $prefix => $namespace) {
  329. $this->m_out .= INDENTATION . '<!ENTITY '. $prefix . " '" . $namespace ."'>".LINEFEED;
  330. }
  331. }
  332.  
  333. /**
  334. * @access private
  335. */
  336. function writeNamespaceDeclarations() {
  337. foreach($this->m_namespaces as $prefix => $namespace) {
  338.  
  339. if ($prefix == RDF_NAMESPACE_PREFIX && !$this->rdf_qnames) {
  340.  
  341. if($this->use_entities) {
  342. $this->m_out .= LINEFEED . INDENTATION .XML_NAMESPACE_DECLARATION_PREFIX .
  343. '="&' . $prefix . ';"';
  344. } else {
  345. $this->m_out .= LINEFEED . INDENTATION .XML_NAMESPACE_DECLARATION_PREFIX .
  346. '="' . $namespace . '"';
  347. }
  348. } else {
  349. if ($prefix == NULL)
  350. $colon_prefix = $prefix;
  351. else
  352. $colon_prefix = ":" .$prefix;
  353.  
  354. if($this->use_entities) {
  355. $this->m_out .= LINEFEED . INDENTATION .XML_NAMESPACE_DECLARATION_PREFIX
  356. .$colon_prefix .'="&' . $prefix . ';"';
  357. } else {
  358. $this->m_out .= LINEFEED . INDENTATION .XML_NAMESPACE_DECLARATION_PREFIX
  359. .$colon_prefix .'="' . $namespace . '"';
  360. }
  361. }
  362. }
  363. }
  364.  
  365. /**
  366. * @access private
  367. */
  368. function writeDescriptions() {
  369.  
  370. $this->m_groupTypeStatement = NULL;
  371. $this->m_attributeStatements = array();
  372. $this->m_contentStatements = array();
  373. $this->m_currentSubject = NULL;
  374.  
  375. foreach($this->m_statements as $key => $statement) {
  376. $subject = $statement->getSubject();
  377. $predicate = $statement->getPredicate();
  378. $object = $statement->getobject();
  379.  
  380. // write Group and update current subject if nessesary
  381. if ($this->m_currentSubject==NULL || !$this->m_currentSubject->equals($subject)) {
  382. $this->writeGroup();
  383. $this->m_currentSubject=$subject;
  384. }
  385.  
  386. // classify the statement
  387. if (($predicate->getURI() == RDF_NAMESPACE_URI.RDF_TYPE) && is_a($object, 'Resource')) {
  388. if ($this->m_groupTypeStatement)
  389. $this->writeGroup();
  390. $this->m_groupTypeStatement = $statement;
  391. }
  392. elseif ($this->canAbbreviateValue($object) &&
  393. $this->use_attributes &&
  394. $this->checkForDoubleAttributes($predicate))
  395. {
  396. if (is_a($object, 'Literal')) {
  397. if ($object->getDatatype() == NULL) {
  398. $this->m_attributeStatements[] = $statement;
  399. } else {
  400. $this->m_contentStatements[] = $statement;
  401. }
  402. } else {
  403. $this->m_attributeStatements[] = $statement;
  404. }
  405. } else {
  406. $this->m_contentStatements[] = $statement;
  407. }
  408. }
  409. $this->writeGroup();
  410. }
  411.  
  412. /**
  413. * @access private
  414. */
  415. function writeGroup() {
  416. if ($this->m_currentSubject==NULL || ($this->m_groupTypeStatement==NULL && (count($this->m_attributeStatements)==0) && (count($this->m_contentStatements)==0)))
  417. return;
  418. if ($this->m_groupTypeStatement!=NULL)
  419. $outerElementName=$this->getElementText($this->m_groupTypeStatement->obj->getURI());
  420. else
  421. $outerElementName = $this->rdf_qname_prefix . RDF_DESCRIPTION;
  422. $this->m_out .= LINEFEED . '<';
  423. $this->m_out .= $outerElementName;
  424.  
  425. $this->m_out .= ' ';
  426.  
  427.  
  428. $this->writeSubjectURI($this->m_currentSubject);
  429.  
  430. // attribute Statements
  431. if ($this->use_attributes)
  432. $this->writeAttributeStatements();
  433.  
  434. if (count($this->m_contentStatements)==0)
  435. $this->m_out .= '/>' . LINEFEED;
  436. else {
  437. $this->m_out .= '>' . LINEFEED;
  438.  
  439. // content statements
  440. $this->writeContentStatements();
  441.  
  442. $this->m_out .= '</';
  443. $this->m_out .= $outerElementName;
  444. $this->m_out .= '>'. LINEFEED;
  445. }
  446. $this->m_groupTypeStatement = NULL;
  447. $this->m_attributeStatements = array();
  448. $this->m_contentStatements = array();
  449. }
  450.  
  451. /**
  452. * @param object Node $predicate
  453. * @access private
  454. */
  455. function checkForDoubleAttributes($predicate) {
  456. foreach($this->m_attributeStatements as $key => $statement) {
  457. if ($statement->pred->equals($predicate))
  458. return FALSE;
  459. }
  460. return TRUE;
  461. }
  462.  
  463. /**
  464. * @param STRING $uri
  465. * @access private
  466. */
  467. function relativizeURI($uri) {
  468. $uri_namespace = RDFUtil::guessNamespace($uri);
  469. if ($uri_namespace == $this->m_baseURI) {
  470. return RDFUtil::guessName($uri);
  471. } else {
  472. return $uri;
  473. }
  474. }
  475.  
  476. /**
  477. * @param object Node $subject_node
  478. *
  479. * @access private
  480. */
  481.  
  482. function writeSubjectURI($subject_node) {
  483. $currentSubjectURI = $subject_node->getURI();
  484. $relativizedURI = $this->relativizeURI($currentSubjectURI);
  485.  
  486. // if submitted subject ist a blank node, use rdf:nodeID
  487. if (is_a($this->m_currentSubject, 'BlankNode')) {
  488. $this->m_out .= $this->rdf_qname_prefix . RDF_NODEID;
  489. $this->m_out .= '="';
  490. $this->m_out .= $relativizedURI;
  491. } else {
  492.  
  493.  
  494. if (!($relativizedURI == $currentSubjectURI)) {
  495. $this->m_out .= $this->rdf_qname_prefix . RDF_ID;
  496. $this->m_out .= '="';
  497. $this->m_out .= $relativizedURI;
  498. } else {
  499. $this->m_out .= $this->rdf_qname_prefix . RDF_ABOUT;
  500. $this->m_out .= '="';
  501. $this->writeAbsoluteResourceReference($relativizedURI);
  502. };
  503. };
  504. $this->m_out .= '"';
  505. }
  506.  
  507. /**
  508. * @access private
  509. */
  510. function writeAttributeStatements() {
  511. foreach($this->m_attributeStatements as $key => $statement) {
  512. $this->m_out .= LINEFEED;
  513. $this->m_out .= INDENTATION;
  514. $this->m_out .= $this->getElementText($statement->pred->getURI());
  515. $this->m_out .= '=';
  516. $value=$statement->obj->getLabel();
  517. $quote=$this->getValueQuoteType($value);
  518. $this->m_out .= $quote;
  519. $this->m_out .= $value;
  520. $this->m_out .= $quote;
  521. }
  522. }
  523.  
  524. /**
  525. * @access private
  526. */
  527. function writeContentStatements() {
  528. foreach($this->m_contentStatements as $key => $statement) {
  529. $this->m_out .= INDENTATION;
  530. $this->m_out .= '<';
  531. $predicateElementText=$this->getElementText($statement->pred->getURI());
  532. $this->m_out .= $predicateElementText;
  533.  
  534. if (is_a($statement->obj, 'Resource')) {
  535. $this->writeResourceReference($statement->obj);
  536. $this->m_out .= '/>' . LINEFEED;
  537. } else {
  538. if(is_a($statement->obj, 'Literal')) {
  539. if ($statement->obj->getDatatype()!= NULL)
  540. if ($statement->obj->getDatatype()== RDF_NAMESPACE_URI . RDF_XMLLITERAL) {
  541. $this->m_out .= ' ' . RDF_NAMESPACE_PREFIX . ':' . RDF_PARSE_TYPE . '="' . RDF_PARSE_TYPE_LITERAL . '"';
  542. } else {
  543. $this->m_out .= ' ' . RDF_NAMESPACE_PREFIX . ':' . RDF_DATATYPE . '="' . $statement->obj->getDatatype() . '"';
  544. }
  545. if ($statement->obj->getLanguage()!= NULL)
  546. $this->m_out .= ' ' . XML_NAMESPACE_PREFIX . ':' . XML_LANG . '="' . $statement->obj->getLanguage() . '"';
  547. };
  548. $this->m_out .= '>';
  549. if ($statement->obj->getDatatype()== RDF_NAMESPACE_URI . RDF_XMLLITERAL) {
  550. $this->m_out .= $statement->obj->getLabel();
  551. } else {
  552. $this->writeTextValue($statement->obj->getLabel());
  553. }
  554. $this->m_out .= '</';
  555. $this->m_out .= $predicateElementText;
  556. $this->m_out .= '>' . LINEFEED;
  557. }
  558. }
  559. }
  560.  
  561. /**
  562. * @param Object $object_node
  563. * @access private
  564. */
  565. function writeResourceReference($object_node) {
  566. $rebaseURI = $object_node->getURI();
  567. $this->m_out .= ' ';
  568. if (is_a($object_node, 'BlankNode')) {
  569. $this->m_out .= $this->rdf_qname_prefix . RDF_NODEID;
  570. } else {
  571. $this->m_out .= $this->rdf_qname_prefix . RDF_RESOURCE;
  572. };
  573.  
  574. $this->m_out .= '="';
  575. $relativizedURI = $this->relativizeURI($rebaseURI);
  576. if (!($relativizedURI == $rebaseURI))
  577. if (!is_a($object_node, 'BlankNode'))
  578. $this->m_out .= '#' . $relativizedURI;
  579. else
  580. $this->m_out .= $relativizedURI;
  581. else
  582. $this->writeAbsoluteResourceReference($rebaseURI);
  583. $this->m_out .= '"';
  584. }
  585.  
  586.  
  587. /**
  588. * @param String $rebaseURI
  589. * @access private
  590. */
  591. function writeAbsoluteResourceReference($rebaseURI) {
  592. $namespace=RDFUtil::guessNamespace($rebaseURI);
  593. $localName=RDFUtil::guessName($rebaseURI);
  594. $text=$rebaseURI;
  595. if ($namespace!='' and $this->use_entities) {
  596. $prefix= array_search($namespace, $this->m_namespaces);
  597. $text='&'.$prefix.';'.$localName;
  598. } else $text = RDFUtil::escapeValue($text);
  599. $this->m_out .= $text;
  600. }
  601.  
  602. /**
  603. * @param STRING $textValue
  604. * @access private
  605. */
  606. function writeTextValue($textValue) {
  607. if ($this->getValueQuoteType($textValue)==USE_CDATA)
  608. $this->writeEscapedCDATA($textValue);
  609. else
  610. $this->m_out .= $textValue;
  611. }
  612.  
  613. /**
  614. * @param STRING $textValue
  615. * @access private
  616. */
  617. function writeEscapedCDATA($textValue) {
  618. $this->m_out .= '<![CDATA[' . $textValue . ']]>';
  619. }
  620.  
  621. /**
  622. * @param STRING $textValue
  623. * @access private
  624. */
  625. function getValueQuoteType($textValue) {
  626. $quote=USE_ANY_QUOTE;
  627. $hasBreaks=FALSE;
  628. $whiteSpaceOnly=TRUE;
  629. for ($i=0; $i<strlen($textValue); $i++) {
  630. $c=$textValue{$i};
  631. if ($c=='<' || $c=='>' || $c=='&')
  632. return USE_CDATA;
  633. if ($c==LINEFEED)
  634. $hasBreaks=TRUE;
  635. if ($c=='"' || $c=="\'") {
  636. if ($quote==USE_ANY_QUOTE)
  637. $quote=($c=='"') ? "\'" : "\"";
  638. elseif ($c==$quote)
  639. return USE_CDATA;
  640. }
  641. if (!($c == ' '))
  642. $whiteSpaceOnly = FALSE;
  643. }
  644. if ($whiteSpaceOnly || $hasBreaks)
  645. return USE_CDATA;
  646. return $quote==USE_ANY_QUOTE ? '"' : $quote;
  647. }
  648.  
  649. /**
  650. * @param object Node $node
  651. * @access private
  652. */
  653. function canAbbreviateValue($node) {
  654. if (is_a($node, 'Literal')) {
  655. $value= $node->getLabel();
  656. if (strlen($value)<MAX_ALLOWED_ABBREVIATED_LENGTH) {
  657. $c=$this->getValueQuoteType($value);
  658. return $c=='"' || $c=='\'';
  659. }
  660. }
  661. return FALSE;
  662. }
  663.  
  664. /**
  665. * @param STRING $elementName
  666. * @access private
  667. */
  668. function getElementText($elementName) {
  669. $namespace=RDFUtil::guessNamespace($elementName);
  670. $localName=RDFUtil::guessName($elementName);
  671. if ($namespace=="")
  672. return $localName;
  673. $prefix=array_search($namespace, $this->m_namespaces);
  674.  
  675. if ($prefix===FALSE) {
  676. $errmsg = RDFAPI_ERROR . "(class: Serializer; method: getElementText): Prefix for element '" . $elementName . "' cannot be found.";
  677. trigger_error($errmsg, E_USER_ERROR);
  678. }
  679. switch ($prefix) {
  680. case RDF_NAMESPACE_PREFIX:
  681. return $this->rdf_qname_prefix . $localName;
  682. case NULL:
  683. return $localName;
  684. default:
  685. return $prefix. ":" .$localName;
  686. }
  687. }
  688.  
  689. /**
  690. * @param object MemModel $model
  691. * @access private
  692. */
  693. function collectNamespaces($model) {
  694. foreach($model->triples as $key => $value) {
  695.  
  696. if ($this->use_entities) {
  697. $this->collectNamespace($value->getSubject());
  698. if(!is_a($value->getObject(), 'Literal'))
  699. $this->collectNamespace($value->getObject());
  700.  
  701. } else {
  702.  
  703. if ($value->pred->getURI() == RDF_NAMESPACE_URI.RDF_TYPE)
  704. $this->collectNamespace($value->getObject());
  705. elseif
  706. (($value->pred->getURI() == RDF_NAMESPACE_URI.RDFS_SUBCLASSOF) ||
  707. ($value->pred->getURI() == RDF_NAMESPACE_URI.RDFS_SUBPROPERTYOF)) {
  708. $this->collectNamespace($value->getSubject());
  709. $this->collectNamespace($value->getObject());
  710. }
  711. }
  712.  
  713. $this->collectNamespace($value->getPredicate());
  714.  
  715. }
  716. }
  717.  
  718. /**
  719. * @param object Resource $resource
  720. * @access private
  721. */
  722. function collectNamespace($resource) {
  723.  
  724. $namespace=RDFUtil::getNamespace($resource);
  725. if (!in_array($namespace, $this->m_namespaces)&&$namespace!='') {
  726. $prefix = array_search( $namespace, $this->m_defaultNamespaces);
  727. if ($prefix===FALSE)
  728. $prefix=$this->getNextNamespacePrefix();
  729. $this->m_namespaces[$prefix] = $namespace;
  730. }
  731. }
  732.  
  733. /**
  734. * @access private
  735. */
  736. function getNextNamespacePrefix() {
  737. $this->m_nextAutomaticPrefixIndex++;
  738. return GENERAL_PREFIX_BASE . $this->m_nextAutomaticPrefixIndex;
  739. }
  740.  
  741. }
  742. ?>

Documentation generated on Fri, 17 Dec 2004 16:17:13 +0100 by phpDocumentor 1.3.0RC3