Source for file RdfSerializer.php

Documentation is available at RdfSerializer.php

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

Documentation generated on Fri, 13 Jan 2006 07:49:05 +0100 by phpDocumentor 1.3.0RC4