Location: PHPKode > scripts > Base460 Parser > base460-parser/base460parser.class.php
<?

/*
*****************************************************************
**                                                             **
**                BASE460 file parser class                    **
**                                                             **
**    BASE460 is a fiile fiormat used in UNISCAN/GS1 RUS       **
**    association, an EAN representative in Russia.            **
**    Actually is a zipped  XML file.                          **
**    Since GTINApp doesn't support data export in linear      **
**    text format, I wrote this class to do it manually.       **
**                                                             **
*****************************************************************

*/

//State constants
define('B460_STATE_NONE',0);
define('B460_STATE_B460',1);
define('B460_STATE_ENTERPRISE',2);
define('B460_STATE_GLN',3);
define('B460_STATE_ROOTGRP',4);
define('B460_STATE_RECORD',5);
define('B460_STATE_EAN13GRP',7);
define('B460_STATE_EAN13RECORD',8);
define('B460_STATE_ITF14GRP',9);
define('B460_STATE_ITF14RECORD',10);


//The parser class itself
class Base460Parser
{

 private $parser;
 private $result;
 
 private $state=B460_STATE_NONE;
 private $GLN;
 private $ROOTGRP;
 private $RECORD;
 private $EAN13RECORD;
 private $ITF14RECORD;
 private $PARAM;

// Here you can configure other parameter IDs of BASE460
 private $B460_PARAMS=
 array(
  96 => 'NAME',
  129 => 'GCP',
  131 => 'GCP',
  102 => 'NUM',
  132 => 'NUM',
  133 => 'CHECKSUM',
  136 => 'CHECKSUM'
 );


 function __construct()
 {
  $this->parser=xml_parser_create();
  xml_set_element_handler($this->parser, array($this,'_open'), array($this,'_close'));
  xml_set_character_data_handler($this->parser, array($this,'_data'));
 }
 
 function __destruct()
 {
  xml_parser_free($this->parser);
 }

 //Parse a BASE460 file
 function ParseFile($file)
 {
  $zip = new ZipArchive;
  if ($zip->open($file) === TRUE) {
    $data=$zip->getFromName('base460.xml');
    $zip->close();
    return $this->ParseData($data);
  } else {
    return false;
  }
 }
 
 //Parse raw XML data
 function ParseData($data)
 {
  xml_parse($this->parser,$data);
  return $this->result;
 }
 
 private
 
 function _open($parser,$name,$attrs)
 {
  switch($this->state)
  {
   case B460_STATE_NONE:
    if($name=='BASE460')$this->state=B460_STATE_B460;
   break;
   case B460_STATE_B460:
    if($name=='ENTERPRISE')$this->state=B460_STATE_ENTERPRISE;
    if($name=='GROUP')
    {
     $this->ROOTGRP=$attrs['ID_GROUP'];
     $this->state=B460_STATE_ROOTGRP;
    }
   break;
   case B460_STATE_ENTERPRISE:
    if($name=='GLN')$this->state=B460_STATE_GLN;
   break;
   case B460_STATE_ROOTGRP:
    if($name=='RECORD')
    {
     $this->RECORD=$attrs['ID_RECORD'];
     $this->state=B460_STATE_RECORD;
    }
   break;
   case B460_STATE_RECORD:
    if($name=='PARAM')
    {
     if($pname=$this->B460_PARAMS[intval($attrs['ID_PARAM'])])
     {
      $this->PARAM=$pname;
     }
    }
    if($name=='GROUP')$this->state=B460_STATE_EAN13GRP;
   break;
   case B460_STATE_EAN13GRP:
    if($name=='RECORD')
    {
     $this->EAN13RECORD=$attrs['ID_RECORD'];
     $this->state=B460_STATE_EAN13RECORD;
    }
   break;
   case B460_STATE_EAN13RECORD:
    if($name=='PARAM')
    {
     if($pname=$this->B460_PARAMS[$attrs['ID_PARAM']])
     {
      $this->PARAM=$pname;
     }
    }
    if($name=='GROUP')$this->state=B460_STATE_ITF14GRP;
   break;
   case B460_STATE_ITF14GRP:
    if($name=='RECORD')
    {
     $this->ITF14RECORD=$attrs['ID_RECORD'];
     $this->state=B460_STATE_ITF14RECORD;
    }
   break;
   case B460_STATE_ITF14RECORD:
    if($name=='PARAM')
    {
     if($pname=$this->B460_PARAMS[$attrs['ID_PARAM']])
     {
      $this->PARAM=$pname;
     }
    }
   break;
  }
 }
 
 function _close($parser,$name)
 {
  switch($this->state)
  {
   case B460_STATE_NONE:
   break;
   case B460_STATE_B460:
    if($name=='BASE460')$this->state=B460_STATE_NONE;
   break;
   case B460_STATE_ENTERPRISE:
    if($name=='ENTERPRISE')$this->state=B460_STATE_B460;
   break;
   case B460_STATE_GLN:
    if($name=='GLN')$this->state=B460_STATE_ENTERPRISE;
   break;
   case B460_STATE_ROOTGRP:
    if($name=='GROUP')$this->state=B460_STATE_B460;
   break;
   case B460_STATE_RECORD:
    if($name=='PARAM')unset($this->PARAM);
    if($name=='RECORD')$this->state=B460_STATE_ROOTGRP;
   break;
   case B460_STATE_EAN13GRP:
    if($name=='GROUP')$this->state=B460_STATE_RECORD;
   break;
   case B460_STATE_EAN13RECORD:
    if($name=='PARAM')unset($this->PARAM);
    if($name=='RECORD')$this->state=B460_STATE_EAN13GRP;
   break;
   case B460_STATE_ITF14GRP:
    if($name=='GROUP')$this->state=B460_STATE_EAN13RECORD;
   break;
   case B460_STATE_ITF14RECORD:
    if($name=='PARAM')unset($this->PARAM);
    if($name=='RECORD')$this->state=B460_STATE_ITF14GRP;
   break;
  }
 }

 function _data($parser,$data)
 {
  switch($this->state)
  {
   case B460_STATE_NONE:
   break;
   case B460_STATE_B460:
   break;
   case B460_STATE_ENTERPRISE:
   break;
   case B460_STATE_GLN:
    $this->GLN=$data;
   break;
   case B460_STATE_ROOTGRP:
   break;
   case B460_STATE_RECORD:
    if($this->PARAM)
    {
     $this->result[$this->GLN][$this->ROOTGRP][$this->RECORD][$this->PARAM].=$data;
    }
   break;
   case B460_STATE_EAN13GRP:
   break;
   case B460_STATE_EAN13RECORD:
    if($this->PARAM)
    {
     $this->result[$this->GLN][$this->ROOTGRP][$this->RECORD]['EAN13'][$this->EAN13RECORD][$this->PARAM].=$data;
    }
   break;
   case B460_STATE_ITF14GRP:
   break;
   case B460_STATE_ITF14RECORD:
    if($this->PARAM)
    {
     $this->result[$this->GLN][$this->ROOTGRP][$this->RECORD]['EAN13'][$this->EAN13RECORD]['ITF14'][$this->ITF14RECORD][$this->PARAM].=$data;
    }
   break;
  }
 }


}

?>
Return current item: Base460 Parser