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

Source for file XMLImport.php

Documentation is available at XMLImport.php

  1. <?php
  2.  
  3.  /**
  4.  * XMLImport class
  5.  *
  6.  * Description: hart import data from XML
  7.  * (Exported via getExport()->getData()->getXML()->save())
  8.  *
  9.  * @author
  10.  * @copyright (c) 1995-2008 by Steema Software SL. All Rights Reserved. <info@steema.com>
  11.  * @version 1.0
  12.  * @package TeeChartPHP
  13.  * @subpackage imports
  14.  * @link http://www.steema.com
  15.  */
  16.  
  17. class XMLImport extends DataImportFormat
  18. {
  19.  
  20.    private $sSeriesNode "";
  21.    private $sDataMember "";
  22.    private $dom;
  23.  
  24.     // Interceptors
  25.     function __get$property {
  26.       $method ="get{$property}";
  27.       if method_exists$this$method ) ) {
  28.         return $this->$method();
  29.       }
  30.     }
  31.  
  32.     function __set $property,$value {
  33.       $method ="set{$property}";
  34.       if method_exists$this$method ) ) {
  35.         return $this->$method($value);
  36.       }
  37.     }
  38.  
  39.    /** Load XML from file **/
  40.    public function open($file{
  41.       $this->Load($file);
  42.    }
  43.  
  44.    /** Creates a new instance of XMLImport */
  45.    public function XMLImport($c)
  46.    {
  47.       parent::DataImportFormat($c);
  48.    }
  49.  
  50.    private function XMLError($error)/* TODO throws ChartException*/
  51.    {
  52.       throw new Exception($error);
  53.    }
  54.  
  55.    /**
  56.    * Returns Series (if set) into which to import XML data
  57.    */
  58.    public function getSeriesNode()
  59.    {
  60.       return $this->sSeriesNode;
  61.    }
  62.  
  63.    /**
  64.    * Set Series into which to import data. If not set Import process will
  65.    * create Series.
  66.    */
  67.    public function setSeriesNode($value)
  68.    {
  69.       $this->sSeriesNode $value;
  70.       for($t 0$t sizeof($this->chart->getSeries())$t++)
  71.       {
  72.          if($this->chart->getSeries($t)->getTitle()->equals($value))
  73.          {
  74.             $this->series = $this->chart->getSeries($t);
  75.             break;
  76.          }
  77.       }
  78.    }
  79.  
  80.    /*
  81.    * Gets ValueList Datamember if set.
  82.    */
  83.    public function getDataMember()
  84.    {
  85.       return $this->sDataMember;
  86.    }
  87.  
  88.    /**
  89.    * Sets ValueList Datamember.
  90.    */
  91.    public function setDataMember($value)
  92.    {
  93.       $this->sDataMember $value;
  94.    }
  95.  
  96.    private function getSelectedNodes($node$arg)
  97.    {
  98.       //Node tmpNode = node;
  99.  
  100.       for($t 0$t sizeof($node->getChildNodes())$t++)
  101.          if($node->getChildNodes()->item($t)->getNodeName(== $arg)
  102.             $tmpNode->appendChild($node->getChildNodes()->item($t));
  103.  
  104.       return $tmpNode->getChildNodes();
  105.    }
  106.  
  107.    private function LoadSeriesNode($nodeDocument)  /* TODO throws InstantiationException,
  108.        IllegalAccessException, ChartException*/
  109.    {
  110.         $tmpSeries $this->series;
  111.         $tmpSeriesTitle "";
  112.         $tmpSeriesTitle $nodeDocument->getAttribute("title");
  113.  
  114.         if($tmpSeries != null)
  115.             $tmpSeries->clear();
  116.         else
  117.         {
  118.             // Create a new Series...
  119.             $tmpClass null;
  120.             $tmpSeriesName $nodeDocument->getAttribute("type");
  121.             if ($tmpSeriesName != null)
  122.             {
  123.                 $tmpName $tmpSeriesName;
  124.  
  125.                 for($t 0$t sizeof(Utils::$seriesTypesOf)$t++)
  126.                 {
  127.                     if((string)Utils::$seriesTypesOf[$t== (string)$tmpName)
  128.                     {
  129.                         try
  130.                         {
  131.                           $tmpClass Series::newFromType(Utils::$seriesTypesOf[$t]);
  132.                         }
  133.                         catch(Exception $e)
  134.                         {
  135.                         }
  136.                         break;
  137.                    }
  138.                 }
  139.  
  140.                 if($tmpClass == null)
  141.                     $tmpClass Series::newFromType(Utils::$seriesTypesOf[5]);// Bar
  142.  
  143.                 $tmpClass->chart $this->chart;
  144.  
  145.                 $idx $this->chart->addSeries($tmpClass);
  146.                 $tmpSeries $this->chart->getSeries($idx);
  147.  
  148.                 // Series Title
  149.                 $tmpTitle $nodeDocument->getAttribute("title");
  150.  
  151.                 if($tmpTitle != null)
  152.                    $tmpSeries->setTitle($tmpTitle);
  153.  
  154.                 // Series Color
  155.                 $tmpColor $nodeDocument->getAttribute("color");
  156.                 if($tmpColor != null)
  157.                   $tmpSeries->setColor(Color::fromCode($tmpColor->getNodeValue()));
  158.             }
  159.         }
  160.  
  161.         // NodeList
  162.         $tmpPoints $this->dom->getElementsByTagName("points");
  163.  
  164.         if($tmpPoints != null)
  165.         {
  166.           //NodeList
  167.           $tmpPoint $this->dom->getElementsByTagName("point");
  168.  
  169.           if($tmpPoint == null)
  170.           {
  171.               $this->XMLError("No <point> nodes.");
  172.           }
  173.           else
  174.           {
  175.               $tmpName $tmpSeries->getMandatory()->valueSource;
  176.               if(strlen($tmpName== 0{
  177.                   $tmpName $this->getDataMember();
  178.               }
  179.               if(strlen($tmpName== 0{
  180.                   $tmpName $tmpSeries->getMandatory()->getName();
  181.               }
  182.  
  183.               $tmpX $tmpSeries->getNotMandatory()->valueSource;
  184.  
  185.               if(strlen($tmpX== 0{
  186.                   $tmpX $tmpSeries->getNotMandatory()->getName();
  187.               }
  188.  
  189.               for($t 0$t < (int)$tmpPoints->item(0)->getAttribute("count")$t++)
  190.               {
  191.                  $node $tmpPoint->item($t);
  192.                  $parentNode1 $node->parentNode;
  193.                  $parentNode2 $parentNode1->parentNode;
  194.  
  195.                  if($tmpSeriesTitle == $parentNode2->getAttribute("title"))
  196.                  {
  197.                     // If node has attributes - points
  198.                     if(!$tmpPoint->item($t)->hasAttributes())
  199.                     {
  200.                        $this->XMLError("<point> node has no data.");
  201.                        break;
  202.                     }
  203.                     else
  204.                     {
  205.                         $tmpText $tmpPoint->item($t)->getAttribute("text");
  206.  
  207.                         if($tmpText == "")
  208.                           $tmpTex "";
  209.                         else
  210.                           $tmpTex $tmpText->getNodeValue();
  211.  
  212.                         $tmpColor $tmpPoint->item($t)->getAttribute("color");
  213.  
  214.                         if($tmpColor == "")
  215.                             $tmpCol new Color(0,0,0,0,true);
  216.                         else
  217.                             $tmpCol Color::fromCode($tmpColor);
  218.  
  219.                         // Rest of values (if exist)
  220.                         for($tt 2$tt sizeof($tmpSeries->getValuesLists())$tt++)
  221.                         {
  222.                             $tmpList $tmpSeries->getValuesLists()->getValueList($tt)->valueSource;
  223.                             if(sizeof($tmpList== 0)
  224.                               $tmpList $tmpSeries->getValuesLists()->getValueList($tt)->getName();
  225.  
  226.                               // TODO remove $tmpValue = $tmpItems->getNamedItem(tmpList);
  227.                               $tmpValue $tmpPoint->item($t)->getAttribute($tmpList);
  228.  
  229.                               if($tmpValue != "")
  230.                                 $tmpSeries->getValuesLists()->getValueList($tt)->tempValue $tmpValue;
  231.                         }
  232.  
  233.                         // Get X and Y values
  234.                         $tmpValue $tmpPoint->item($t)->getAttribute($tmpName);
  235.                         $tmpValueX $tmpPoint->item($t)->getAttribute($tmpX);
  236.  
  237.                         // Add point !
  238.                         if($tmpValue == "")
  239.                         {
  240.                            if($tmpValueX == "")
  241.                            {
  242.                               $tmpSeries->addNull();//  .Add(tmpTex);
  243.                            }
  244.                            else
  245.                            {
  246.                               $tmpSeries->addXY($tmpValueX0.0$tmpTex);
  247.                            }
  248.                         }
  249.                         else
  250.                         {
  251.                            if($tmpValueX == null)
  252.                            {
  253.                               $tmpSeries->addYTextColor($tmpValue$tmpTex$tmpCol);
  254.                            }
  255.                            else
  256.                            {
  257.                               $tmpSeries->addXYTextColor($tmpValueX$tmpValue$tmpTex$tmpCol);
  258.                            }
  259.                         }
  260.                     }
  261.                }
  262.             }
  263.          }
  264.       }
  265.       else $this->XMLError("No <points> node.");
  266.    }
  267.  
  268.    /**
  269.    * Passes XML Document to be parsed
  270.    */
  271.    public function Load($d)  /* TODO throws ChartException  */
  272.    {
  273.     // Load XML using DOM
  274.     $dom new DOMDocument;
  275.     $dom->load($d);
  276.     $this->dom=$dom;
  277.  
  278.     /*create the xPath object _after_  loading the xml source, otherwise the query won't work:*/
  279.     $xPath new DOMXPath($dom);
  280.  
  281.  
  282.     if(($this->chart != null|| ($this->series != null))
  283.     {
  284.       /*nodelist - now get the nodes in a DOMNodeList:*/
  285.       //$tmpSeries = $xPath->query($anXPathExpr);
  286.  
  287.       $tmpSeries $dom->getElementsByTagName("series");
  288.  
  289.       if($tmpSeries == null{
  290.          $this->XMLError("No <$this->series$this->nodes->");
  291.       }
  292.       else
  293.       {
  294.          if($this->series == null)
  295.          {
  296.             while($this->chart->getSeriesCount(0)
  297.                $this->chart->removeSeries($this->chart->getSeries(0));
  298.             }
  299.  
  300.             if(strlen($this->getSeriesNode()) == 0)
  301.             {
  302.                if(sizeof($tmpSeries0)
  303.                   for($t 0$t sizeof($tmpSeries)$t++)
  304.                   {
  305.                      try
  306.                      {
  307.                         $this->LoadSeriesNode($tmpSeries->item($t)$d);
  308.                      }
  309.                      catch(Exception $ex)
  310.                      {
  311.                      }
  312.                      catch(Exception $ex)
  313.                      {
  314.                      }
  315.                      if($this->series != null)
  316.                        break;
  317.                   }
  318.                   else
  319.                      $this->XMLError("Empty <$series$this->node->");
  320.             }
  321.             else
  322.             {
  323.                $tmpFound false;
  324.  
  325.                if(sizeof($tmpSeries0)
  326.                {
  327.                   for($t 0$t sizeof($tmpSeries)$t++)
  328.                   {
  329.                      /*Node*/ $tmpTitle $tmpSeries->item($t)->getAttribute("title");
  330.                      if($tmpTitle != null)
  331.                      {
  332.                         if(strtolower($tmpTitle== $this->getSeriesNode())
  333.                         {
  334.                            try
  335.                            {
  336.                               $this->LoadSeriesNode($tmpSeries->item($t)$d);
  337.                            }
  338.                            catch(Exception $ex)
  339.                            {
  340.                            }
  341.                            catch(Exception $ex)
  342.                            {
  343.                            }
  344.                            $tmpFound true;
  345.                            break;
  346.                         }
  347.                      }
  348.                   }
  349.                }
  350.  
  351.                if(!$tmpFound)
  352.                {
  353.                  $this->XMLError("Series " $this->getSeriesNode("  found");
  354.                }
  355.             }
  356.          }
  357.       }
  358.    }
  359.  
  360.  
  361.    /* XML USING simpleXML
  362.  
  363.    /**
  364.    * XML File to process for import
  365.    *
  366.    public function open($file)/* TODO throws IOException,
  367.    ClassNotFoundException
  368.    {
  369.  
  370.         $xml_parser = xml_parser_create();
  371.         xml_set_element_handler($xml_parser, "startTag", "endTag");
  372.         xml_set_character_data_handler($xml_parser, "contents");
  373.  
  374.         if (!($fp = fopen($file, "r"))) {
  375.             die("could not open XML input");
  376.         }
  377.  
  378.         while ($data = fread($fp, 4096)) {     //* review 80000 tambe
  379.           if (!xml_parse($xml_parser, $data, feof($fp))) {
  380.                 die(sprintf("XML error: %s at line %d",
  381.                     xml_error_string(xml_get_error_code($xml_parser)),
  382.                     xml_get_current_line_number($xml_parser)));
  383.           }
  384.         }
  385.         xml_parser_free($xml_parser);
  386.         fclose($fp);
  387.  
  388.  
  389.  
  390.       DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory . newInstance();
  391.       try
  392.       {
  393.          DocumentBuilder docBuilder = docBuilderFactory . newDocumentBuilder();
  394.  
  395.          try
  396.          {
  397.             Document m_doc = docBuilder . parse(stream);
  398.             try
  399.             {
  400.                Load(m_doc);
  401.             }
  402.             catch(ChartException cExp)
  403.             {
  404.             }
  405.          }
  406.          catch(org . xml . sax . SAXException sar)
  407.          {
  408.          }
  409.       }
  410.       catch(javax . xml . parsers . ParserConfigurationException err)
  411.       {
  412.       }
  413.       ;
  414.    }
  415.  
  416.    function  startTag($parser, $data){
  417.       echo "<b>";
  418.    }
  419.  
  420.    function contents($parser, $data){
  421.       echo $data;
  422.    }
  423.  
  424.    function endTag($parser, $data){
  425.       echo "</b><br />";
  426.    }   */
  427. }
  428. ?>

Documentation generated on Wed, 16 Jun 2010 12:09:24 +0200 by phpDocumentor 1.4.1