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

Documentation generated on Fri, 1 Jun 2007 16:50:50 +0200 by phpDocumentor 1.3.2