Changeset 4

Show
Ignore:
Timestamp:
08/25/06 22:12:25 (2 years ago)
Author:
schst
Message:

Fixed PHP 5.0.x compatibility

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/XJConf/DefinitionParser.php

    r2 r4  
    1313/** 
    1414 * Parse tag definitions files. 
    15  *  
     15 * 
    1616 * @author Stephan Schmidt <stephan.schmidt@schlund.de> 
    1717 */ 
     
    1919{ 
    2020    const TAG_NAMESPACE      = 'namespace'; 
    21      
     21 
    2222        /** 
    2323     * Stack for currently open definitions 
    24      *  
     24     * 
    2525     * @var  array<Definition> 
    2626     */ 
    2727    private $defStack = array(); 
    28      
     28 
    2929    private $defHandlerStack = array(); 
    30      
     30 
    3131    /** 
    3232     * Constant for the default namespace 
    3333     */ 
    3434    const DEFAULT_NAMESPACE = '__default'; 
    35      
     35 
    3636    /** 
    3737     * The current namespace 
    38      *  
     38     * 
    3939     * @var  string 
    4040     */ 
    41     private $currentNamespace = self::DEFAULT_NAMESPACE
    42      
     41    private $currentNamespace
     42 
    4343    /** 
    4444     * All extracted namespace definitions 
    45      *  
     45     * 
    4646     * @var  NamespaceDefinitions 
    4747     */ 
    4848    private $defs; 
    49      
     49 
    5050    private $reader; 
    51      
     51 
     52    private $nodeTypes   = array(); 
     53 
     54 
    5255    /** 
    5356     * constructor 
     
    5659    { 
    5760        $this->defs = new NamespaceDefinitions(); 
     61        $this->currentNamespace = self::DEFAULT_NAMESPACE; 
     62 
     63        if (!defined('XMLREADER_ELEMENT')) { 
     64            $this->nodeTypes = array('startTag' => XMLReader::ELEMENT, 
     65                                     'text'     => XMLReader::TEXT, 
     66                                     'endTag'   => XMLReader::END_ELEMENT 
     67                               ); 
     68        } else { 
     69            $this->nodeTypes = array('startTag' => XMLREADER_ELEMENT, 
     70                                     'text'     => XMLREADER_TEXT, 
     71                                     'endTag'   => XMLREADER_END_ELEMENT 
     72                               ); 
     73        } 
    5874    } 
    59      
     75 
    6076    public function getCurrentNamespace() 
    6177    { 
    6278        return $this->currentNamespace; 
    6379    } 
    64      
     80 
    6581    public function getNamespaceDefinitions() 
    6682    { 
    6783        return $this->defs; 
    6884    } 
    69      
     85 
    7086    public function &getDefStack() 
    7187    { 
    7288        return $this->defStack; 
    7389    } 
    74      
     90 
    7591    private function initParser() 
    7692    { 
     
    8399     * parse a tag definitions file and return 
    84100     * an instance of NamespaceDefinition 
    85      *  
     101     * 
    86102     * @param   string               $filename  filename of the defintions file 
    87103     * @return  NamespaceDefinition 
     
    94110        while ($this->reader->read()) { 
    95111            switch ($this->reader->nodeType) { 
    96                 case XMLReader::ELEMENT
     112                case $this->nodeTypes['startTag']
    97113                    $nameSpaceURI = $this->reader->namespaceURI; 
    98114                    $elementName  = $this->reader->localName; 
     
    107123                                } 
    108124                            } 
    109                              
     125 
    110126                    $this->startElement($nameSpaceURI, $elementName, $attributes); 
    111127                    break; 
    112                      
    113                 case XMLReader::TEXT
     128 
     129                case $this->nodeTypes['text']
    114130                    $this->characters($this->reader->value); 
    115131                    break; 
    116                      
    117                 case XMLReader::END_ELEMENT
     132 
     133                case $this->nodeTypes['endTag']
    118134                    $this->endElement($this->reader->namespaceURI, $this->reader->localName); 
    119135                    break; 
    120136            } 
    121137        } 
    122          
     138 
    123139        $this->reader->close($filename); 
    124          
     140 
    125141        return $this->defs; 
    126142    } 
    127      
     143 
    128144    /** 
    129145     * Start Element handler 
    130      *  
     146     * 
    131147     * Creates the TagDefinition object and places it on 
    132148     * the stack. 
    133      *  
     149     * 
    134150     * @param   string  $namespaceURI  namespace of start tag 
    135151     * @param   string  $sName         name of start tag 
     
    143159                throw new InvalidNamespaceDefinitionException('The <' . self::TAG_NAMESPACE . '> tag is missing the uri attribute.'); 
    144160            } 
    145                  
     161 
    146162            $this->currentNamespace = $atts['uri']; 
    147163                return; 
    148164        } 
    149          
     165 
    150166        $defHandler = DefinitionHandlerFactory::create($sName, $this); 
    151167        $def        = $defHandler->startElement($namespaceURI, $sName, $atts); 
     
    153169            array_push($this->defStack, $def); 
    154170        } 
    155          
     171 
    156172        if ($defHandler->needsEnd() == true) { 
    157173            array_push($this->defHandlerStack, $defHandler); 
     
    161177    /** 
    162178     * End Element handler 
    163      *  
     179     * 
    164180     * Fetches the TagDefinition from the stack and 
    165181     * adds it to the NamespaceDefinition object. 
     
    172188            return; 
    173189        } 
    174          
     190 
    175191        $defHandler = array_pop($this->defHandlerStack); 
    176192        $defHandler->endElement($namespaceURI, $sName); 
  • trunk/XJConf/XmlParser.php

    r2 r4  
    1010{ 
    1111    private $tagStack    = array(); 
    12      
     12 
    1313    private $config      = array(); 
    14      
     14 
    1515    private $tagDefs; 
    16      
     16 
    1717    private $depth       = 0; 
    18      
     18 
    1919    private $extensions  = array(); 
    20      
     20 
    2121    private $myNamespace = 'http://www.schst.net/XJConf'; 
    22      
     22 
    2323    private $openFiles   = array(); 
    24      
     24 
    2525    private $reader; 
    26      
     26 
    2727    private $nodeTypes   = array(); 
    28      
     28 
    2929    public function __construct() 
    3030    { 
    31         if (version_compare(phpversion(), '5.1') >= 0) { 
     31        if (!defined('XMLREADER_ELEMENT')) { 
    3232            $this->nodeTypes = array('startTag' => XMLReader::ELEMENT, 
    3333                                     'text'     => XMLReader::TEXT, 
     
    4141        } 
    4242    } 
    43      
     43 
    4444    public function setTagDefinitions(NamespaceDefinitions $tagDefs) 
    4545    { 
    4646        $this->tagDefs = $tagDefs; 
    4747    } 
    48      
     48 
    4949    public function addTagDefinitions(NamespaceDefinitions $tagDefs) 
    5050    { 
     
    5353            return; 
    5454        } 
    55          
     55 
    5656        $this->tagDefs->appendNamespaceDefinitions($tagDefs); 
    5757    } 
    58      
     58 
    5959    public function addExtension($namespace, Extension $ext) 
    6060    { 
    6161        $this->extensions[$namespace] = $ext; 
    6262    } 
    63      
     63 
    6464    public function parse($filename) 
    6565    { 
     
    8282                                } 
    8383                            } 
    84                              
     84 
    8585                    $this->startElement($nameSpaceURI, $elementName, $attributes); 
    8686                    break; 
    87                      
     87 
    8888                case $this->nodeTypes['text']: 
    8989                    $this->characters($this->reader->value); 
    9090                    break; 
    91                      
     91 
    9292                case $this->nodeTypes['endTag']: 
    9393                    $this->endElement($this->reader->namespaceURI, $this->reader->localName); 
     
    9595            } 
    9696        } 
    97          
     97 
    9898        $this->reader->close($filename); 
    9999        array_pop($this->openFiles); 
    100          
    101     } 
    102      
     100 
     101    } 
     102 
    103103    public function getConfigValue($name) 
    104104    { 
    105105        return $this->config[$name]; 
    106106    } 
    107      
     107 
    108108    public function getCurrentFile() 
    109109    { 
    110110        return end($this->openFiles); 
    111111    } 
    112      
     112 
    113113    private function initParser() 
    114114    { 
     
    117117        } 
    118118    } 
    119      
     119 
    120120    /** 
    121121     * Start element 
    122      *  
     122     * 
    123123     * Creates a new Tag object and pushes it 
    124124     * onto the stack. 
    125      *  
     125     * 
    126126     * @param  string  $namespaceURI  namespace of start tag 
    127127     * @param  string  $sName         name of start tag 
     
    130130    private function startElement($namespaceURI, $sName, $atts) 
    131131    { 
    132          
     132 
    133133        if ($this->myNamespace == $namespaceURI && 0 < $this->depth) { 
    134134            return; 
    135135        } 
    136136        $this->depth++; 
    137          
     137 
    138138        // no namespace defined, use the default namespace 
    139139        if (strlen($namespaceURI) == 0) { 
     
    144144        if (1 == $this->depth) { 
    145145            return; 
    146         }       
     146        } 
    147147 
    148148        // This tag needs to be handled by an extension 
     
    155155                throw new UnknownNamespaceException('Unknown namespace ' . $namespaceURI . ' in file ' . end($this->openFiles)); 
    156156            } 
    157              
     157 
    158158            if ($this->tagDefs->isTagDefined($namespaceURI, $sName) == false) { 
    159159                throw new UnknownTagException('Unknown tag ' . $sName . ' in namespace ' . $namespaceURI); 
    160160            } 
    161              
     161 
    162162            $tag = new DefinedTag($sName, $atts); 
    163163            // fetch the defintion for this tag 
     
    165165            $tag->setDefinition($tagDef); 
    166166        } 
    167          
     167 
    168168        array_push($this->tagStack, $tag); 
    169169    } 
    170      
     170 
    171171    /** 
    172172     * End element. 
    173      *  
     173     * 
    174174     * Fetches the current element from the stack and 
    175175     * converts it to the correct type. 
    176      *  
     176     * 
    177177     * @param  string  $namespaceURI  namespace of end tag 
    178178     * @param  string  $sName         name of end tag 
     
    197197        // get the last tag from the stack 
    198198        $tag = array_pop($this->tagStack); 
    199          
     199 
    200200        if (isset($this->extensions[$namespaceURI]) == true) { 
    201201            $result = $this->extensions[$namespaceURI]->endElement($this, $tag); 
     
    221221        } 
    222222    } 
    223      
     223 
    224224    /** 
    225225     * Character data handler 
    226      *  
     226     * 
    227227     * Fetches the current tag from the stack and 
    228228     * appends the data. 
     
    233233                return; 
    234234        } 
    235          
     235 
    236236        $tag = end($this->tagStack); 
    237237        $tag->addData($buf);