Parser.php 7.33 KB
<?php
/**  @class Parser
*    @version $Id: Parser.php 2938 2015-06-05 16:23:33Z elena $ 
*    @brief 
*    
*/
class Parser {

       public $wsXML, $constantsXML, $functionsXML, $aliasesXML;
       public $dom;
       public $idl_pairs = array(">" => " GT ", "<" => " LT ", "&" => " AND ", "|" => " OR ", "=" => " EQ "); 

       public $file;       
   
    function __construct(){ 
          $this->wsXML = USERWSDIR."WsParams.xml";         
          $this->functionsXML = Functions."functions.xml";
          $this->constantsXML = Functions."constants.xml";
          $this->aliasesXML = USERWSDIR."Alias.xml";
          $this->dom = new DomDocument("1.0");
      
       }

/*
*  Replace Constants, Aliases and WS params by their real values
*/
    public function replaceAll($chain){
 
      $newChain = $this->replaceWsParams($chain);
      $newNewChain = $this->replaceAliases($newChain);
      $newNewNewChain = $this->replaceConstants($newNewChain);
  
     return $newNewNewChain;
    }

/*
*  Replace Constants, Aliases and WS params by their real values
*/
    public function replaceAllButWS($chain){
       
      $newChain = $this->replaceAliases($chain);
      $newNewChain = $this->replaceConstants($newChain);
  
     return $newNewChain;
    }
/*
*  Return array of Variables
*/ 
  
   public function getVars($chain){
   
     $arr = $this->delimitIt($chain);
 
     $arr = $this->selectVariables($arr);   
     $arr = $this->deSelectFunctions($arr);
  
     return $arr;
   }

/*
*
*/
    public function convert2Idl($chain) {

      return (strtr($chain,$this->idl_pairs));

   }

   public function getFunctionsNames() { 

        $this->dom->load($this->functionsXML);    
        $functions_ = $this->dom->getElementsByTagName("function");
   
        for ($i = 0; $i < $functions_->length; $i++) {   
            $tempArr =  explode('(', $functions_->item($i)->getAttribute("name"));
            $functions[$i] = $tempArr[0];
        }

      return $functions;
    }

    public function deSelectFunctions($arr) { 
 
      $functions = $this->getFunctionsNames();
 
      $arr = array_values($arr);

      $N_arr = count($arr);
      for ($i = 0; $i < $N_arr; $i++)     
        foreach($functions as $the_function)        
            if (strcasecmp(trim($the_function),trim($arr[$i])) == 0) {
                                         unset($arr[$i]);
                                         continue 2;
           }  
 
      return array_values($arr);
    } 

    public function replaceAliases($chain) { 
      
         if ($this->isThereAlias($chain)) {
            
              $this->dom->load($this->aliasesXML);    
              $aliases_ = $this->dom->getElementsByTagName("alias");            
       
              if ($aliases_->length > 0 ) {
                  for ($i = 0; $i <  $aliases_->length; $i++) {            
                    $aliases["#".$aliases_->item($i)->getAttribute("name")] = "(".$aliases_->item($i)->nodeValue.")";
      
                  }
    
                  $chainReplaced = strtr($chain, $aliases);  
        
                  return $chainReplaced;
            }
         }
       return $chain;
    }
   
  
     public function replaceConstants($chain) { 
      
         if ($this->isThereConstant($chain)) {
            
              $this->dom->load($this->constantsXML);    
              $constants_ = $this->dom->getElementsByTagName("constant");             

              for ($i = 0; $i <  $constants_->length; $i++) {    
        
                $constants["@".$constants_->item($i)->getAttribute("name")] = "(".$constants_->item($i)->nodeValue.")";
  
              }
 
              $chainReplaced = strtr($chain, $constants);  
       
              return $chainReplaced;
            }
       return $chain;
    }

    public function replaceWsParams($chain) {

         if ($this->isThereWs($chain)) {
              $this->dom->load($this->wsXML);    
              $wspar = $this->dom->getElementsByTagName("param");
            
              if ($wspar->length > 0) {
                  for ($i = 0; $i < $wspar->length; $i++) {  
//Changes: low 'ws'  and attributes        
                    $wsName = "ws_".$wspar->item($i)->getAttribute('name');
                    $wsChain = $wspar->item($i)->getAttribute("buildchain");
                    $wsParams[$wsName]= $wsChain;
                  } 
              $chainReplaced = strtr($chain, $wsParams); 
              return $chainReplaced; 
             }
           }
      return $chain;
    }

   public function substrAliasWs($chain, $aliasName, $paramName) {

        $aliasName="#".$aliasName;
        $pos = strpos($chain, $aliasName);
        while ( $pos !== FALSE ) {
            $pos = $pos+strlen($aliasName);
            if (preg_match('/[-+*,^<>&|=\/\[\]\(\)]/', $chain[$pos]) || $chain[$pos] === '') {
                $chain = substr_replace($chain, $paramName,$pos-strlen($aliasName),strlen($aliasName));
            }
            $pos = strpos($chain, $aliasName, $pos);
        }

        return $chain;
    }

     public function substrParamWs($chain, $paramName, $aliasName) {

        $aliasName="#".$aliasName;
        $pos = strpos($chain, $paramName);
        while ( $pos !== FALSE ) {
            $pos = $pos+strlen($paramName);
            if (preg_match('/[-+*,^<>&|=\/\[\]\(\)]/', $chain[$pos]) || $chain[$pos] === '') {
                $chain = substr_replace($chain, $aliasName,$pos-strlen($paramName),strlen($paramName));
            }
            $pos = strpos($chain, $paramName, $pos);
        }

        return $chain;
    }

    public function modifyParamWs($chain, $oldAliasName, $newAliasName) {

        $oldAliasName="#".$oldAliasName;
        $newAliasName="#".$newAliasName;
        $pos = strpos($chain, $oldAliasName);
        while ( $pos !== FALSE ) {
            $pos = $pos+strlen($oldAliasName);
            if (preg_match('/[-+*,^<>&|=\/\[\]\(\)]/', $chain[$pos]) || $chain[$pos] === '') {
                $chain = substr_replace($chain, $newAliasName,$pos-strlen($oldAliasName),strlen($oldAliasName));
            }
            $pos = strpos($chain, $oldAliasName, $pos);
        }

        return $chain;
    }


    public function delimitIt($chain) {
       
       $delimiter = '[]+,/*()^<>&|=[-]+';    
       $array = split($delimiter,$chain);
       if (trim(end($array)) == "") array_pop($array);

       return $array;
   }


  public function selectVariables($arr) {   

      $N_arr = count($arr);
      for ($i = 0; $i < $N_arr; $i++)  {  
         $arr[$i] =  trim($arr[$i]);       
         if (!$this->isVariable($arr[$i]))                                    
                                       unset($arr[$i]);
        }

      return array_unique(array_values($arr));
   }

   
    public function isNumber($value) { 
     
         return preg_match('/^[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?$/', $value);
   }

    public function isVariable($value) {

         return preg_match('/^[a-zA-Z]/', $value);
   }

     public function isConstant($value) {

         return preg_match('/^@/', $value);
   }

   public function isAlias($value) {

         return preg_match('/^#/', $value);
   }

   public function isThereConstant($value) {

         return preg_match('/@/', $value);
   }

    public function isThereWS($value) {

         return preg_match('/ws_/', $value);
   } 
     
  public function isThereAlias($value) {

         return preg_match('/#/', $value);
   }
}