Changeset 14

Show
Ignore:
Timestamp:
08/27/06 18:21:44 (2 years ago)
Author:
schst
Message:

Fixed DefinitionParser?, changed ValueConverter? interface to allow more flexible converters.
This is only a first draft and should probably be cleaned up.

Files:

Legend:

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

    r9 r14  
    22/** 
    33 * Contains data of tag in the default namespace. 
    4  *  
     4 * 
    55 * @author  Stephan Schmidt <me@schst.net> 
    66 * @author  Frank Kleine <frank.kleine@schlund.de> 
     
    1111/** 
    1212 * Contains data of tag in the default namespace. 
    13  *  
     13 * 
    1414 * @package  XJConf 
    1515 */ 
     
    1919        /** 
    2020         * name of the tag 
    21          *  
     21         * 
    2222         * @var  string 
    2323         */ 
     
    2626        /** 
    2727         * character data 
    28          *  
     28         * 
    2929         * @var  string 
    3030         */ 
    3131        private $data = null; 
    32          
     32 
    3333        /** 
    3434         * content of the tag 
    35          *  
     35         * 
    3636         * @var  mixed 
    3737         */ 
    3838        private $content = null; 
    39          
     39 
    4040        /** 
    4141         * attributes of the tag 
    42          *  
     42         * 
    4343         * @var  array 
    4444         */ 
     
    4747        /** 
    4848         * Children of the tag 
    49          *  
     49         * 
    5050         * @var  array 
    5151         */ 
     
    5454        /** 
    5555         * value of the tag 
    56          *  
     56         * 
    5757         * @var  TagDefinition 
    5858         */ 
    5959        private $tagDef = null; 
    60          
     60 
    6161        /** 
    6262         * Create a new tag without attributes 
    63          *  
     63         * 
    6464         * @param name   name of the tag 
    6565         */ 
     
    6969                $this->atts = $atts; 
    7070        } 
    71          
     71 
    7272        /** 
    7373         * Get the name of the tag 
    74          *  
     74         * 
    7575         * @return   name of the tag 
    7676         */ 
     
    7979                return $this->name; 
    8080        } 
    81          
     81 
    8282        /** 
    8383         * Set the key 
    84          *  
     84         * 
    8585         * @param  string  $key 
    8686         */ 
     
    8989                $this->key = $key; 
    9090        } 
    91          
     91 
    9292        /** 
    9393         * Get the key under which the value will be stored 
    94          *  
     94         * 
    9595         * @return  string 
    9696         */ 
     
    9999                return $this->tagDef->getKey($this); 
    100100        } 
    101          
     101 
    102102        /** 
    103103         * Add text data 
    104          *  
     104         * 
    105105         * @param   string  $buf 
    106106         * @return  int     new length of data 
     
    111111                return strlen($this->data); 
    112112        } 
    113          
     113 
    114114        /** 
    115115         * Get the character data of the tag 
    116          *  
     116         * 
    117117         * @return   character data 
    118118         */ 
     
    124124        /** 
    125125         * Check, whether the tag has a certain attribute 
    126          *  
     126         * 
    127127         * @param   string   $name 
    128128         * @return  boolean 
     
    135135        /** 
    136136         * get an attribute 
    137          *  
     137         * 
    138138         * @param   string  $name  name of the attribute 
    139139         * @return  string  value of the attribute 
     
    144144                    return $this->atts[$name]; 
    145145                } 
    146                  
     146 
    147147                return null; 
    148148        } 
    149          
     149 
     150        /** 
     151         * get all attributes 
     152         * 
     153         * @return  array 
     154         */ 
     155        public function getAttributes() 
     156        { 
     157                return $this->atts; 
     158        } 
     159 
    150160        /** 
    151161     * Add a new child to this tag. 
    152      *  
     162     * 
    153163     * @param child  child to add 
    154164     * @return   int    number of childs added 
     
    162172        /** 
    163173         * Get the child with a specific name 
    164          *  
     174         * 
    165175         * @param   string  $name 
    166176         * @return  Tag 
     
    173183                        } 
    174184                } 
    175                  
     185 
    176186                return null; 
    177187        } 
     
    179189        /** 
    180190         * Get all children of the tag 
    181          *  
     191         * 
    182192         * @return  array 
    183193         */ 
     
    189199        /** 
    190200     * Set the content (overrides the character data) 
    191      *  
     201     * 
    192202     * @param  mixed  $content 
    193203     */ 
     
    199209        /** 
    200210         * Get the content 
    201          *  
     211         * 
    202212         * @return  mixed 
    203213         */ 
     
    207217                        return $this->content; 
    208218                } 
    209                  
     219 
    210220                return $this->getData(); 
    211221        } 
    212          
     222 
    213223        /** 
    214224         * Fetch the value 
    215          *  
     225         * 
    216226         * @return      mixed  the value of the tag 
    217227         */ 
     
    223233        /** 
    224234         * Get the type of the value 
    225          *  
     235         * 
    226236         * @param   Tag     $tag 
    227237         * @return  string 
     
    231241                return $this->tagDef->getValueType($tag); 
    232242        } 
    233          
     243 
    234244        /** 
    235245         * Get the setter method 
    236          *  
     246         * 
    237247         * @return  string 
    238248         */ 
     
    241251        return $this->tagDef->getSetterMethod(); 
    242252    } 
    243      
     253 
    244254        /** 
    245255         * Checks, whether the tag supports indexed children 
    246          *  
     256         * 
    247257         * @return  boolean 
    248258         */ 
     
    250260        { 
    251261                return $this->tagDef->supportsIndexedChildren(); 
    252         }       
    253          
     262        } 
     263 
    254264        /** 
    255265    * Set the tag definition object used for this tag 
    256     *   
     266    * 
    257267    * @param  TagDefinition  $tagDef 
    258268    */ 
     
    261271        $this->tagDef = $tagDef; 
    262272    } 
    263      
     273 
    264274    /** 
    265275     * get the tag definition object used for this tag 
    266      *   
     276     * 
    267277     * @return  TagDefinition 
    268278     */ 
  • trunk/XJConf/DefinitionParser.php

    r9 r14  
    7474    /** 
    7575     * constructor 
    76      *  
     76     * 
    7777     * Sets the node types depending on your PHP version using the constants 
    7878     * defined by the XMLReader PHP extension. 
     
    9595        } 
    9696    } 
    97      
     97 
    9898    /** 
    9999     * returns the current namespace 
     
    105105        return $this->currentNamespace; 
    106106    } 
    107      
     107 
    108108    /** 
    109109     * returns the list of created namespace definitions 
     
    115115        return $this->defs; 
    116116    } 
    117      
     117 
    118118    /** 
    119119     * returns the definition stack 
     
    125125        return $this->defStack; 
    126126    } 
    127      
     127 
    128128    /** 
    129129     * initializes the parser 
     
    154154                    $elementName  = $this->reader->localName; 
    155155                    $attributes   = array(); 
     156                    $empty = $this->reader->isEmptyElement; 
    156157                    if (TRUE == $this->reader->hasAttributes) { 
    157158                                // go to first attribute 
     
    165166 
    166167                    $this->startElement($nameSpaceURI, $elementName, $attributes); 
     168                    if (true === $empty) { 
     169                        $this->endElement($this->reader->namespaceURI, $this->reader->localName); 
     170                    } 
    167171                    break; 
    168172 
     
    200204                throw new InvalidNamespaceDefinitionException('The <' . self::TAG_NAMESPACE . '> tag is missing the uri attribute.'); 
    201205            } 
    202              
     206 
    203207            // change current namespace to new namespace 
    204208            $this->currentNamespace = $atts['uri']; 
    205209                return; 
    206210        } 
    207          
     211 
    208212        // create the appropriate definition handler and use this 
    209213        // to create the required definition 
     
    214218        } 
    215219 
    216         if ($defHandler->needsEnd() == true) { 
     220//        if ($defHandler->needsEnd() == true) { 
    217221            array_push($this->defHandlerStack, $defHandler); 
    218         } 
     222//        } 
    219223        } 
    220224 
     
    224228     * Fetches the TagDefinition from the stack and 
    225229     * adds it to the NamespaceDefinition object. 
    226      *  
     230     * 
    227231     * @param   string  $namespaceURI  namespace of end tag 
    228232     * @param   string  $sName         name of end tag 
     
    235239            return; 
    236240        } 
    237          
     241 
    238242        // use definition handler to finalize the definition of the current tag 
    239243        $defHandler = array_pop($this->defHandlerStack); 
  • trunk/XJConf/GenericTag.php

    r9 r14  
    33 * Generic Tag wrapper that can be used by extensions to dynamically add 
    44 * children to other tags. 
    5  *  
     5 * 
    66 * @author Stephan Schmidt <me@schst.net> 
    77 */ 
     
    1010 * Generic Tag wrapper that can be used by extensions to dynamically add 
    1111 * children to other tags. 
    12  *  
     12 * 
    1313 * @package  XJConf 
    1414 */ 
     
    1717        /** 
    1818         * name of the tag 
    19          *  
     19         * 
    2020         * @var  string 
    2121         */ 
     
    2323        /** 
    2424         * character data 
    25          *  
     25         * 
    2626         * @var  string 
    2727         */ 
     
    2929        /** 
    3030         * content of the tag (overrides data) 
    31          *  
     31         * 
    3232         * @var  mixed 
    3333         */ 
     
    3535        /** 
    3636         * attributes of the tag 
    37          *  
     37         * 
    3838         * @var  array 
    3939         */ 
     
    4141        /** 
    4242         * Children of the tag 
    43          *  
     43         * 
    4444         * @var  array 
    4545         */ 
     
    4747        /** 
    4848         * value of the tag 
    49          *  
     49         * 
    5050         * @var  mixed 
    5151         */ 
     
    5353        /** 
    5454         * Key of the tag 
    55          *  
     55         * 
    5656         * @var  string 
    5757         */ 
    5858        private $key      = null; 
    59          
     59 
    6060        /** 
    6161         * Create a new tag with or without attributes 
    62          *  
     62         * 
    6363         * @param  string $name  name of the tag 
    6464         * @param  array  $atts  optional  list of attributes 
     
    6969                $this->atts = $atts; 
    7070        } 
    71      
     71 
    7272        /** 
    7373         * Get the name of the tag 
    74          *  
     74         * 
    7575         * @return  string 
    7676         */ 
     
    7979                return $this->name; 
    8080        } 
    81          
     81 
    8282        /** 
    8383         * Set the key 
    84          *  
     84         * 
    8585         * @param  string  $key 
    8686         */ 
     
    8989                $this->key = $key; 
    9090        } 
    91          
     91 
    9292        /** 
    9393         * Get the key under which the value will be stored 
    94          *  
     94         * 
    9595         * @return  string 
    9696         */ 
     
    9999                return $this->key; 
    100100        } 
    101          
     101 
    102102        /** 
    103103         * Add text data 
    104          *  
     104         * 
    105105         * @param   string  $buf 
    106106         * @return  int     new length of data 
     
    111111                return strlen($this->data); 
    112112        } 
    113          
     113 
    114114        /** 
    115115         * Get the character data of the tag 
    116          *  
     116         * 
    117117         * @return  string 
    118118         */ 
     
    124124        /** 
    125125         * Check, whether the tag has a certain attribute 
    126          *  
     126         * 
    127127         * @param   string   $name 
    128128         * @return  boolean 
     
    135135        /** 
    136136         * get an attribute 
    137          *  
     137         * 
    138138         * @param   string  $name  name of the attribute 
    139139         * @return  string  value of the attribute 
     
    144144                    return $this->atts[$name]; 
    145145                } 
    146                  
     146 
    147147                return null; 
    148148        } 
    149          
     149 
     150        /** 
     151         * get all attributes 
     152         * 
     153         * @return  array 
     154         */ 
     155        public function getAttributes() 
     156        { 
     157                return $this->atts; 
     158        } 
     159 
    150160        /** 
    151161         * Add a new child to this tag. 
    152          *  
     162         * 
    153163         * @param   Tag  $child  child to add 
    154164         * @return  int  number of childs added 
     
    159169                return count($this->children); 
    160170        } 
    161      
     171 
    162172        /** 
    163173         * Get the child with a specific name 
    164          *  
     174         * 
    165175         * @param   string  $name 
    166176         * @return  Tag 
     
    173183                        } 
    174184                } 
    175                  
     185 
    176186                return null; 
    177187        } 
    178          
     188 
    179189        /** 
    180190         * Get all children of the tag 
    181          *  
     191         * 
    182192         * @return  array 
    183193         */ 
     
    186196                return $this->children; 
    187197        } 
    188          
     198 
    189199        /** 
    190200     * Set the content (overrides the character data) 
    191      *  
     201     * 
    192202     * @param  mixed  $content 
    193203     */ 
     
    199209        /** 
    200210         * Get the content 
    201          *  
     211         * 
    202212         * @return  mixed 
    203213         */ 
     
    207217                        return $this->content; 
    208218                } 
    209                  
     219 
    210220                return $this->getData(); 
    211221        } 
    212      
     222 
    213223        /** 
    214224         * Fetch the value 
    215          *  
     225         * 
    216226         * @return      mixed  the value of the tag 
    217227         */ 
     
    220230                return $this->value; 
    221231        } 
    222      
     232 
    223233        /** 
    224234         * Get the type of the value 
    225          *  
     235         * 
    226236         * @return  string 
    227237         */ 
     
    231241                        return null; 
    232242                } 
    233                  
     243 
    234244                if (is_object($this->value) == true) { 
    235245            return get_class($this->value); 
    236246                } 
    237                  
     247 
    238248                return gettype($this->value); 
    239249        } 
    240          
     250 
    241251        /** 
    242252         * Get the setter method 
     
    252262        /** 
    253263         * Checks, whether the tag supports indexed children 
    254          *  
     264         * 
    255265         * @return  boolean 
    256266         */ 
     
    259269                return true; 
    260270        } 
    261          
     271 
    262272        /** 
    263273         * Set the value of the tag 
    264          *  
     274         * 
    265275         * @param  mixed 
    266276         */ 
  • trunk/XJConf/Tag.php

    r9 r14  
    22/** 
    33 * Interface for holding tag data. 
    4  *  
     4 * 
    55 * @author  Stephan Schmidt <me@schst.net> 
    66 * @author  Frank Kleine <frank.kleine@schlund.de> 
     
    1515    /** 
    1616         * Get the name of the tag 
    17          *  
     17         * 
    1818         * @return  name of the tag 
    1919         */ 
    2020        public function getName(); 
    21          
     21 
    2222        /** 
    2323         * Get the key under which the value will be stored 
    24          *  
     24         * 
    2525         * @return  string 
    2626         */ 
    2727        public function getKey(); 
    28          
     28 
    2929        /** 
    3030         * Add text data 
    31          *  
     31         * 
    3232         * @param   string  $buf 
    3333         * @return  int     new length of data 
    3434         */ 
    3535        public function addData($buf); 
    36          
     36 
    3737        /** 
    3838         * Get the character data of the tag 
    39          *  
     39         * 
    4040         * @return   character data 
    4141         */ 
    4242        public function getData(); 
    43          
     43 
    4444        /** 
    4545         * Check, whether the tag has a certain attribute 
    46          *  
     46         * 
    4747         * @param   string   $name 
    4848         * @return  boolean 
    4949         */ 
    5050        public function hasAttribute($name); 
    51          
     51 
    5252        /** 
    5353         * get an attribute 
    54          *  
     54         * 
    5555         * @param   string  $name  name of the attribute 
    5656         * @return  string  value of the attribute 
    5757         */ 
    5858        public function getAttribute($name); 
    59          
     59 
     60        /** 
     61         * get all attributes 
     62         * 
     63         * @return  array 
     64         */ 
     65        public function getAttributes(); 
     66 
    6067        /** 
    6168     * Add a new child to this tag. 
    62      *  
     69     * 
    6370     * @param child  child to add 
    6471     * @return   int    number of childs added 
    6572     */ 
    6673        public function addChild(Tag $child); 
    67          
     74 
    6875        /** 
    6976         * Get the child with a specific name 
    70          *  
     77         * 
    7178         * @param   string  $name 
    7279         * @return  Tag 
    7380         */ 
    7481        public function getChild($name); 
    75          
     82 
    7683        /** 
    7784         * Get all children of the tag 
    78          *  
     85         * 
    7986         * @return  array 
    8087         */ 
    8188        public function getChildren(); 
    82          
     89 
    8390        /** 
    8491     * Set the content (overrides the character data) 
    85      *  
     92     * 
    8693     * @param  mixed  $content 
    8794     */ 
    8895        public function setContent($content); 
    89          
     96 
    9097        /** 
    9198         * Get the content 
    92          *  
     99         * 
    93100         * @return  mixed 
    94101         */ 
    95102        public function getContent(); 
    96          
     103 
    97104        /** 
    98105         * Fetch the value 
    99          *  
     106         * 
    100107         * @return      mixed  the value of the tag 
    101108         */ 
    102109        public function getConvertedValue(); 
    103          
     110 
    104111        /** 
    105112         * Get the type of the value 
    106          *  
     113         * 
    107114         * @param   Tag     $tag 
    108115         * @return  string 
     
    112119        /** 
    113120         * Get the setter method 
    114          *  
     121         * 
    115122         * @return  string 
    116123         */ 
    117124        public function getSetterMethod(); 
    118          
     125 
    119126        /** 
    120127         * Checks, whether the tag supports indexed children 
    121          *  
     128         * 
    122129         * @return  boolean 
    123130         */ 
  • trunk/XJConf/converters/ArrayValueConverter.php

    r12 r14  
    22/** 
    33 * Converter to convert a value to an array. 
    4  *  
     4 * 
    55 * @author  Frank Kleine <frank.kleine@schlund.de> 
    66 */ 
     
    88/** 
    99 * Converter to convert a value to an array. 
    10  *  
     10 * 
    1111 * @package     XJConf 
    1212 * @subpackage  converters 
     
    2020     * @return  array  the converted value 
    2121     */ 
    22     public function convertValue($values
     22    public function convertValue(Tag $tag, Definition $def
    2323    { 
    24         return $values
     24        return array()
    2525    } 
    2626 
    2727   /** 
    2828     * returns the type of the converter 
    29      *  
     29     * 
    3030     * @return  string 
    3131     */ 
  • trunk/XJConf/converters/FactoryMethodValueConverter.php

    r2 r14  
    1515     * @var  string 
    1616     */ 
    17         private $className; 
     17        protected $className; 
     18 
    1819        /** 
    1920         * name of method to use as factory method 
     
    2122         * @var  string 
    2223         */ 
    23         private $methodName; 
    24          
     24        protected $methodName; 
     25 
    2526        /** 
    2627         * constructor 
    27          *  
     28         * 
    2829         * @param  string  $className   name of class to use 
    2930         * @param  string  $methodName  name of method to use 
     
    4243     * @throws  ValueConversionException 
    4344     */ 
    44         public function convertValue($values
     45        public function convertValue(Tag $tag, Definition $def
    4546        { 
     47            /* 
    4648            if (class_exists($this->className) == false) { 
    4749                throw new ValueConversionException('Class "' . $this->className . '" does not exist.'); 
    4850            } 
    49              
     51 
    5052        try { 
    5153            $refClass = new ReflectionClass($this->className); 
     
    5456                throw new ValueConversionException('Could not create instance of "' . $this->className . '" using the factory method "' . $this->methodName . '": ' . $re->getMessage()); 
    5557        } 
    56          
     58 
    5759        return $instance; 
     60        */ 
    5861        } 
    5962 
    60         /** 
    61      * returns the type of the converter 
    62      *  
    63      * @return  string 
     63    /** 
     64     * Get the factory method of the definition, if available 
     65     * 
     66     * @param Definition $def 
     67     * @return FactoryMethodDefinition 
    6468     */ 
    65         public function getType() 
    66         { 
    67         return $this->className; 
    68         } 
     69    public static function getFactoryMethodDefinition(Definition $def) { 
     70        foreach ($def->getChildDefinitions() as $child) { 
     71            if ($child instanceof FactoryMethodDefinition) { 
     72                return $child; 
     73            } 
     74        } 
     75        return null; 
     76    } 
    6977} 
    7078?> 
  • trunk/XJConf/converters/PrimitiveValueConverter.php

    r2 r14  
    11<?php 
    22XJConfLoader::load('converters.ValueConverter', 
     3                   'converters.PrimitiveValueConverter', 
    34                   'exceptions.ValueConversionException' 
    45); 
    56/** 
    67 * Converter to convert a value to a primitive type 
    7  *  
     8 * 
    89 * @author Stephan Schmidt <stephan.schmidt@schlund.de> 
    910 */ 
     
    1314    /** 
    1415     * Type of the primitive 
    15      *  
     16     * 
    1617     * @var  string 
    1718     */ 
     
    3435     * @throws  ValueConversionException 
    3536     */ 
    36     public function convertValue($values) 
    37     { 
    38          
    39         if (count($values) > 1) { 
    40             throw new ValueConversionException('Cannot convert value to primitive type.'); 
    41         } 
    42          
     37    public function convertValue(Tag $tag, Definition $def) { 
     38        $value = $tag->getData(); 
     39 
    4340        switch ($this->type) { 
    4441            case 'boolean': 
    4542            case 'bool': 
    46                 return (boolean) $values[0]
     43                return (boolean) $value
    4744            case 'integer': 
    4845            case 'int': 
    49                 return (integer) $values[0]
     46                return (integer) $value
    5047            case 'double': 
    51                 return (double) $values[0]
     48                return (double) $value
    5249            case 'float': 
    53                 return (float) $values[0]
     50                return (float) $value
    5451            case 'string': 
    55                 return (string) $values[0]
     52  &