Source for file SimpleParser.inc

Documentation is available at SimpleParser.inc

  1. <?php /*-*- mode: php; tab-width:4 -*-*/
  2.  
  3.   /* java_SimpleParser.php -- PHP/Java Bridge protocol parser.
  4.  
  5.   Copyright (C) 2003-2007 Jost Boekemeier
  6.  
  7.   This file is part of the PHP/Java Bridge.
  8.  
  9.   The PHP/Java Bridge ("the library") is free software; you can
  10.   redistribute it and/or modify it under the terms of the GNU General
  11.   Public License as published by the Free Software Foundation; either
  12.   version 2, or (at your option) any later version.
  13.  
  14.   The library is distributed in the hope that it will be useful, but
  15.   WITHOUT ANY WARRANTY; without even the implied warranty of
  16.   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  17.   General Public License for more details.
  18.  
  19.   You should have received a copy of the GNU General Public License
  20.   along with the PHP/Java Bridge; see the file COPYING.  If not, write to the
  21.   Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  22.   02111-1307 USA.
  23.  
  24.   Linking this file statically or dynamically with other modules is
  25.   making a combined work based on this library.  Thus, the terms and
  26.   conditions of the GNU General Public License cover the whole
  27.   combination.
  28.  
  29.   As a special exception, the copyright holders of this library give you
  30.   permission to link this library with independent modules to produce an
  31.   executable, regardless of the license terms of these independent
  32.   modules, and to copy and distribute the resulting executable under
  33.   terms of your choice, provided that you also meet, for each linked
  34.   independent module, the terms and conditions of the license of that
  35.   module.  An independent module is a module which is not derived from
  36.   or based on this library.  If you modify this library, you may extend
  37.   this exception to your version of the library, but you are not
  38.   obligated to do so.  If you do not wish to do so, delete this
  39.   exception statement from your version. */
  40.  
  41. require_once("${JAVA_BASE}/Client.inc");
  42.  
  43. /**
  44.  * @access private
  45.  */
  46. class java_ParserString {
  47.   public $string$off$length;
  48.   function toString({
  49.     return $this->getString();
  50.   }
  51.   function getString({
  52.     return substr($this->string$this->off$this->length);
  53.   }
  54. }
  55. /**
  56.  * @access private
  57.  */
  58. class java_ParserTag {
  59.   public $n$strings;
  60.   function java_ParserTag({
  61.     $this->strings array();
  62.     $this->0;
  63.   }
  64. }
  65. /**
  66.  * @access private
  67.  */
  68. class java_SimpleParser {
  69.   public $SLEN=256// initial length of the parser string
  70.  
  71.   public $handler;
  72.   
  73.   public $tag$buf$len$s;
  74.   public $type;
  75.  
  76.   function java_SimpleParser($handler{
  77.     $this->handler $handler;
  78.     $this->tag array(new java_ParserTag()new java_ParserTag()new java_ParserTag());
  79.     $this->len $this->SLEN;
  80.     $this->str_repeat(" "$this->SLEN);
  81.     $this->type $this->VOJD;
  82.   }
  83.   
  84.   // VOJD is VOID for f... windows (VOID is in winsock2.h)
  85.   public $BEGIN=0$KEY=1$VAL=2$ENTITY=3$VOJD=5$END=6
  86.   public $level=0$eor=0public $in_dquote$eot=false;
  87.   public $pos=0$c=0$i=0$i0=0$e;
  88.  
  89.   function RESET({
  90.     $this->type=$this->VOJD;
  91.     $this->level=0;
  92.     $this->eor=0;
  93.     $this->in_dquote=false;
  94.     $this->i=0;
  95.     $this->i0=0;
  96.   }
  97.     
  98.   function APPEND($c{
  99.     if($this->i>=$this->len-1{
  100.       $this->s=str_repeat($this->s,2);
  101.       $this->len*=2;
  102.     
  103.     $this->s[$this->i++]=$c
  104.   }
  105.   function CALL_BEGIN({
  106.     $pt=&$this->tag[1]->strings;
  107.     $st=&$this->tag[2]->strings;
  108.     $t=&$this->tag[0]->strings[0];
  109.     $name=$t->string[$t->off];
  110.     $n $this->tag[2]->n;
  111.     $ar array();
  112.     for($i=0$i<$n$i++{
  113.       $ar[$pt[$i]->getString()$st[$i]->getString();
  114.     }
  115.     $this->handler->begin($name$ar);
  116.   }
  117.   function CALL_END({
  118.     $t=&$this->tag[0]->strings[0];
  119.     $name=$t->string[$t->off];
  120.     $this->handler->end($name);
  121.   }
  122.   function PUSH($t
  123.     $str &$this->tag[$t]->strings;
  124.     $n &$this->tag[$t]->n;
  125.     $this->s[$this->i]='|';
  126.     if(!isset($str[$n])){$h=$this->handler$str[$n]=$h->createParserString();}
  127.     $str[$n]->string=&$this->s;
  128.     $str[$n]->off=$this->i0;
  129.     $str[$n]->length=$this->i-$this->i0;
  130.     ++$this->tag[$t]->n;
  131.     $this->APPEND('|');
  132.     $this->i0=$this->i;
  133.   }
  134.   function parse({
  135.     while($this->eor==0{
  136.       if($this->c>=$this->pos
  137.         
  138.         $this->buf=$this->handler->read(JAVA_RECV_SIZE)
  139.         if(is_null($this->buf|| strlen($this->buf== 0
  140.           $this->handler->protocol->handler->shutdownBrokenConnection("protocol error. Check the back end log for OutOfMemoryErrors.");
  141.         $this->pos=strlen($this->buf);
  142.         if($this->pos==0break;
  143.         $this->c=0
  144.       }
  145.       switch(($ch=$this->buf[$this->c])) 
  146.         {/* --- This block must be compilable with an ansi C compiler or javac --- */
  147.         case '<'if($this->in_dquote{$this->APPEND($ch)break;}
  148.           $this->level+=1;
  149.           $this->type=$this->BEGIN;
  150.           break;
  151.         case '\t'case '\f'case '\n'case '\r'case ' 'if($this->in_dquote{$this->APPEND($ch)break;}
  152.           if($this->type==$this->BEGIN{
  153.             $this->PUSH($this->type)
  154.             $this->type $this->KEY
  155.           }
  156.           break;
  157.         case '='if($this->in_dquote{$this->APPEND($ch)break;}
  158.           $this->PUSH($this->type);
  159.           $this->type=$this->VAL;
  160.           break;
  161.         case '/'if($this->in_dquote{$this->APPEND($ch)break;}
  162.           if($this->type==$this->BEGIN$this->type=$this->END$this->level-=1}
  163.           $this->level-=1;
  164.           $this->eot=true// used for debugging only
  165.           break;
  166.         case '>'if($this->in_dquote{$this->APPEND($ch)break;}
  167.           if($this->type==$this->END){
  168.             $this->PUSH($this->BEGIN);
  169.             $this->CALL_END();
  170.           else {
  171.             if($this->type==$this->VAL$this->PUSH($this->type);
  172.             $this->CALL_BEGIN();
  173.           }
  174.           $this->tag[0]->n=$this->tag[1]->n=$this->tag[2]->n=0$this->i0=$this->i=0;              /* RESET */
  175.           $this->type=$this->VOJD;
  176.           if($this->level==0$this->eor=1
  177.           break;
  178.         case ';':
  179.           if($this->type==$this->ENTITY{
  180.             switch ($this->s[$this->e+1]{
  181.             case 'l'$this->s[$this->e]='<'$this->i=$this->e+1break/* lt */
  182.             case 'g'$this->s[$this->e]='>'$this->i=$this->e+1break/* gt */
  183.             case 'a'$this->s[$this->e]=($this->s[$this->e+2]=='m'?'&':'\'')$this->i=$this->e+1break/* amp, apos */
  184.             case 'q'$this->s[$this->e]='"'$this->i=$this->e+1break/* quot */
  185.             default$this->APPEND($ch);
  186.             }
  187.             $this->type=$this->VAL//& escapes may only appear in values
  188.           else {
  189.             $this->APPEND($ch);
  190.           }
  191.           break;
  192.         case '&'
  193.           $this->type $this->ENTITY;
  194.           $this->e=$this->i;
  195.           $this->APPEND($ch);
  196.           break;
  197.         case '"':
  198.           $this->in_dquote !$this->in_dquote;
  199.           if(!$this->in_dquote && $this->type==$this->VAL{
  200.             $this->PUSH($this->type);
  201.             $this->type $this->KEY;
  202.           }
  203.           break;
  204.         default:
  205.           $this->APPEND($ch);
  206.         /* ------------------ End of ansi C block ---------------- */
  207.       $this->c+=1;
  208.     }
  209.        $this->RESET();
  210.   }
  211.  
  212.   function getData($str{
  213.     return $str;
  214.   }
  215.  
  216.   function parserError({
  217.     $this->handler->protocol->handler->shutdownBrokenConnection(
  218.         sprintf("protocol error: %s. Check the back end log for details."$this->s));
  219.   }
  220. }
  221. ?>

Documentation generated on Mon, 05 Jan 2009 21:11:05 +0100 by phpDocumentor 1.4.2