syntax
[ class tree: syntax ] [ index: syntax ] [ all elements ]

Source for file Serializer.php

Documentation is available at Serializer.php

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

Documentation generated on Mon, 11 Mar 2019 15:39:30 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.