Changeset 1385

Show
Ignore:
Timestamp:
02/27/08 17:47:38 (4 months ago)
Author:
mikey
Message:

added possibility to only retrieve properties/methods which satisfy a given matcher

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/src/main/php/net/stubbles/reflection/stubBaseReflectionClass.php

    r1281 r1385  
    88 */ 
    99stubClassLoader::load('net::stubbles::reflection::annotations::stubAnnotatable', 
    10                       'net::stubbles::reflection::stubReflectionType' 
     10                      'net::stubbles::reflection::stubReflectionType', 
     11                      'net::stubbles::reflection::matcher::stubMethodMatcher', 
     12                      'net::stubbles::reflection::matcher::stubPropertyMatcher' 
    1113); 
    1214/** 
     
    5961 
    6062    /** 
     63     * returns a list of all methods which satify the given matcher 
     64     * 
     65     * @param   stubMethodMatcher            $methodMatcher 
     66     * @return  array<stubReflectionMethod> 
     67     */ 
     68    public function getMethodsByMatcher(stubMethodMatcher $methodMatcher); 
     69 
     70    /** 
    6171     * returns the specified property or null if it does not exist 
    6272     * 
     
    7282     */ 
    7383    public function getProperties(); 
     84 
     85    /** 
     86     * returns a list of all properties which satify the given matcher 
     87     * 
     88     * @param   stubPropertyMatcher            $propertyMatcher 
     89     * @return  array<stubReflectionProperty> 
     90     */ 
     91    public function getPropertiesByMatcher(stubPropertyMatcher $propertyMatcher); 
    7492 
    7593    /** 
  • trunk/src/main/php/net/stubbles/reflection/stubReflectionClass.php

    r1281 r1385  
    173173 
    174174    /** 
     175     * returns a list of all methods which satify the given matcher 
     176     * 
     177     * @param   stubMethodMatcher            $methodMatcher 
     178     * @return  array<stubReflectionMethod> 
     179     */ 
     180    public function getMethodsByMatcher(stubMethodMatcher $methodMatcher) 
     181    { 
     182        $methods     = parent::getMethods(); 
     183        $stubMethods = array(); 
     184        foreach ($methods as $method) { 
     185            if ($methodMatcher->matchesMethod($method) === true) { 
     186                $stubMethod = new stubReflectionMethod($this, $method->getName()); 
     187                if ($methodMatcher->matchesAnnotatableMethod($stubMethod) === true) { 
     188                    $stubMethods[] = $stubMethod; 
     189                } 
     190            } 
     191        } 
     192         
     193        return $stubMethods; 
     194    } 
     195 
     196    /** 
    175197     * returns the specified property or null if it does not exist 
    176198     * 
     
    205227 
    206228    /** 
     229     * returns a list of all properties which satify the given matcher 
     230     * 
     231     * @param   stubPropertyMatcher            $propertyMatcher 
     232     * @return  array<stubReflectionProperty> 
     233     */ 
     234    public function getPropertiesByMatcher(stubPropertyMatcher $propertyMatcher) 
     235    { 
     236        $properties     = parent::getProperties(); 
     237        $stubProperties = array(); 
     238        foreach ($properties as $property) { 
     239            if ($propertyMatcher->matchesProperty($property) === true) { 
     240                $stubProperty = new stubReflectionProperty($this, $property->getName()); 
     241                if ($propertyMatcher->matchesAnnotatableProperty($stubProperty) === true) { 
     242                    $stubProperties[] = $stubProperty; 
     243                } 
     244            } 
     245        } 
     246         
     247        return $stubProperties; 
     248    } 
     249 
     250    /** 
    207251     * returns a list of all interfaces 
    208252     * 
  • trunk/src/main/php/net/stubbles/reflection/stubReflectionObject.php

    r1281 r1385  
    190190 
    191191    /** 
     192     * returns a list of all methods which satify the given matcher 
     193     * 
     194     * @param   stubMethodMatcher            $methodMatcher 
     195     * @return  array<stubReflectionMethod> 
     196     */ 
     197    public function getMethodsByMatcher(stubMethodMatcher $methodMatcher) 
     198    { 
     199        $methods     = parent::getMethods(); 
     200        $stubMethods = array(); 
     201        foreach ($methods as $method) { 
     202            if ($methodMatcher->matchesMethod($method) === true) { 
     203                $stubMethod = new stubReflectionMethod($this, $method->getName()); 
     204                if ($methodMatcher->matchesAnnotatableMethod($stubMethod) === true) { 
     205                    $stubMethods[] = $stubMethod; 
     206                } 
     207            } 
     208        } 
     209         
     210        return $stubMethods; 
     211    } 
     212 
     213    /** 
    192214     * returns the specified property or null if it does not exist 
    193215     * 
     
    222244 
    223245    /** 
     246     * returns a list of all properties which satify the given matcher 
     247     * 
     248     * @param   stubPropertyMatcher            $propertyMatcher 
     249     * @return  array<stubReflectionProperty> 
     250     */ 
     251    public function getPropertiesByMatcher(stubPropertyMatcher $propertyMatcher) 
     252    { 
     253        $properties     = parent::getProperties(); 
     254        $stubProperties = array(); 
     255        foreach ($properties as $property) { 
     256            if ($propertyMatcher->matchesProperty($property) === true) { 
     257                $stubProperty = new stubReflectionProperty($this, $property->getName()); 
     258                if ($propertyMatcher->matchesAnnotatableProperty($stubProperty) === true) { 
     259                    $stubProperties[] = $stubProperty; 
     260                } 
     261            } 
     262        } 
     263         
     264        return $stubProperties; 
     265    } 
     266 
     267    /** 
    224268     * returns a list of all interfaces 
    225269     * 
  • trunk/src/main/php/net/stubbles/xml/serializer/stubXMLSerializer.php

    r1301 r1385  
    1515                      'net::stubbles::xml::serializer::annotations::stubXMLMatcherAnnotation', 
    1616                      'net::stubbles::xml::serializer::annotations::stubXMLStrategyAnnotation', 
     17                      'net::stubbles::xml::serializer::matcher::stubXMLSerializerMethodPropertyMatcher', 
    1718                      'net::stubbles::reflection::reflection' 
    1819); 
     
    6869     */ 
    6970    private $opts; 
     71    /** 
     72     * the matcher to be used for methods and properties 
     73     * 
     74     * @var  stubXMLSerializerMethodPropertyMatcher 
     75     */ 
     76    protected $methodAndPropertyMatcher; 
     77 
     78    /** 
     79     * constructor 
     80     */ 
     81    public function __construct() 
     82    { 
     83        $this->methodAndPropertyMatcher = new stubXMLSerializerMethodPropertyMatcher(); 
     84    } 
    7085 
    7186    /** 
     
    142157     * Serialize an object 
    143158     * 
    144      * @param   object               $object 
    145      * @param   stubXMLStreamWriter  $xmlWriter 
    146      * @param   string               $tagName 
    147      * @throws  stubXMLException 
    148      * @todo    fetch a strategy from the annotation 
    149      * @todo    Refactor this method into smaller methods for improved readability 
     159     * @param  object               $object 
     160     * @param  stubXMLStreamWriter  $xmlWriter 
     161     * @param  string               $tagName 
     162     * @todo   Refactor this method into smaller methods for improved readability 
    150163     */ 
    151164    protected function serializeObject($object, stubXMLStreamWriter $xmlWriter, $tagName) 
    152165    { 
    153166        $clazz = new stubReflectionClass(get_class($object)); 
    154  
    155         if ($tagName === null) { 
    156             if ($clazz->hasAnnotation('XMLTag')) { 
    157                 $xmlTag = $clazz->getAnnotation('XMLTag'); 
    158                 $tagName = $xmlTag->getTagName(); 
     167        if (null === $tagName) { 
     168            if ($clazz->hasAnnotation('XMLTag') === true) { 
     169                $tagName = $clazz->getAnnotation('XMLTag')->getTagName(); 
    159170            } else { 
    160171                $tagName = get_class($object); 
     
    163174 
    164175        $xmlWriter->writeStartElement($tagName); 
    165  
    166176        if ($clazz->hasAnnotation('XMLStrategy')) { 
    167177            $strategy = $clazz->getAnnotation('XMLStrategy')->getValue(); 
     
    171181 
    172182        // export props 
    173         $properties = $clazz->getProperties(); 
    174  
     183        $properties = $clazz->getPropertiesByMatcher($this->methodAndPropertyMatcher); 
     184        $matcher    = null; 
    175185        // get the property-matcher 
    176186        if ($clazz->hasAnnotation('XMLProperties')) { 
    177187            $matcher = $clazz->getAnnotation('XMLProperties'); 
    178         } else { 
    179             $matcher = null; 
    180188        } 
    181189 
    182190        foreach ($properties as $property) { 
    183             if (!$property->isPublic()) { 
     191            $propValue = $property->getValue($object); 
     192            if ($this->writeAnnotatedElement($property, $xmlWriter, $propValue) === true) { 
    184193                continue; 
    185194            } 
    186             if ($property->hasAnnotation('XMLIgnore')) { 
    187                 continue; 
    188             } 
    189             $propValue = $property->getValue($object); 
    190  
    191             if ($property->hasAnnotation('XMLAttribute') && $property->hasAnnotation('XMLTag')) { 
    192                 throw new stubXMLException('It is not possible specify @XMLAttribute and @XMLTag for a property.'); 
    193             } 
    194             if ($property->hasAnnotation('XMLAttribute')) { 
    195                 $xmlAttribute = $property->getAnnotation('XMLAttribute'); 
    196                 if ((string) $propValue === '' && $xmlAttribute->shouldSkipEmpty()) { 
    197                     continue; 
    198                 } 
    199                 $xmlWriter->writeAttribute($xmlAttribute->getAttributeName(), (string) $propValue); 
    200                 continue; 
    201             } elseif ($property->hasAnnotation('XMLFragment')) { 
    202                 $xmlFragment = $property->getAnnotation('XMLFragment'); 
    203                 $tagName = $xmlFragment->getTagName(); 
    204                 if ($tagName != null) { 
    205                     $xmlWriter->writeStartElement($tagName); 
    206                     $xmlWriter->writeXmlFragment($propValue); 
    207                     $xmlWriter->writeEndElement(); 
    208                 } else { 
    209                     $xmlWriter->writeXmlFragment($propValue); 
    210                 } 
    211                 continue; 
    212             } elseif ($property->hasAnnotation('XMLTag')) { 
    213                 $xmlTag = $property->getAnnotation('XMLTag'); 
    214                 $tagName = $xmlTag->getTagName(); 
     195             
     196            if ($property->hasAnnotation('XMLTag') === true) { 
     197                $xmlTag      = $property->getAnnotation('XMLTag'); 
     198                $tagName     = $xmlTag->getTagName(); 
    215199                $elementName = $xmlTag->getElementTagName(); 
    216200            } else { 
    217201                if (null !== $matcher) { 
    218202                    $tagName = $matcher->getTagnameForProperty($property, $propValue); 
    219                     if ($tagName === false) { 
     203                    if (false === $tagName) { 
    220204                        continue; 
    221205                    } 
     
    224208                        continue; 
    225209                    } 
    226                     if ($property->isStatic() && $this->opts[self::OPT_STATIC] === false) { 
    227                         continue; 
    228                     } 
     210                     
     211                    if ($this->opts[self::OPT_STATIC] === false && $property->isStatic() === true) { 
     212                        continue; 
     213                    } 
     214                     
    229215                    $tagName = $property->getName(); 
    230216                } 
    231217                $elementName = null; 
    232218            } 
    233             if (is_array($propValue)) { 
     219             
     220            if (is_array($propValue) === true) { 
    234221                $this->serializeArray($propValue, $xmlWriter, $tagName, $elementName); 
    235222            } else { 
     
    239226 
    240227        // export methods 
    241         $methods = $clazz->getMethods(); 
    242  
     228        $methods = $clazz->getMethodsByMatcher($this->methodAndPropertyMatcher); 
     229        $matcher = null; 
    243230        // get the method-matcher 
    244231        if ($clazz->hasAnnotation('XMLMethods')) { 
    245232            $matcher = $clazz->getAnnotation('XMLMethods'); 
    246         } else { 
    247             $matcher = null; 
    248         } 
     233        } 
     234         
    249235        foreach ($methods as $method) { 
    250             if (!$method->isPublic()) { 
     236            $returnValue = $method->invoke($object); 
     237            if ($this->writeAnnotatedElement($method, $xmlWriter, $returnValue) === true) { 
    251238                continue; 
    252239            } 
    253             if ($method->isConstructor() || $method->isDestructor()) { 
    254                 continue; 
    255             } 
    256             if (0 == strncmp($method->getName(), '__', 2)) { 
    257                 continue; 
    258             } 
    259             if (0 != $method->getNumberOfParameters()) { 
    260                 continue; 
    261             } 
    262             if ($method->hasAnnotation('XMLIgnore')) { 
    263                 continue; 
    264             } 
    265  
    266             $returnValue = $method->invoke($object); 
    267             if ($method->hasAnnotation('XMLAttribute') && $method->hasAnnotation('XMLTag')) { 
    268                 throw new stubXMLException('It is not possible specify @XMLAttribute and @XMLTag for a method.'); 
    269             } 
    270             if ($method->hasAnnotation('XMLAttribute')) { 
    271                 $xmlAttribute = $method->getAnnotation('XMLAttribute'); 
    272                 if ((string) $returnValue === '' && $xmlAttribute->shouldSkipEmpty()) { 
    273                     continue; 
    274                 } 
    275                 $xmlWriter->writeAttribute($xmlAttribute->getAttributeName(), (string) $returnValue); 
    276                 continue; 
    277             } elseif ($method->hasAnnotation('XMLFragment')) { 
    278                 $xmlFragment = $method->getAnnotation('XMLFragment'); 
    279                 $tagName = $xmlFragment->getTagName(); 
    280                 if ($tagName != null) { 
    281                     $xmlWriter->writeStartElement($tagName); 
    282                     $xmlWriter->writeXmlFragment($returnValue); 
    283                     $xmlWriter->writeEndElement(); 
    284                 } else { 
    285                     $xmlWriter->writeXmlFragment($returnValue); 
    286                 } 
    287                 continue; 
    288             } elseif ($method->hasAnnotation('XMLTag')) { 
    289                 $xmlTag = $method->getAnnotation('XMLTag'); 
    290                 $tagName = $xmlTag->getTagName(); 
     240             
     241            if ($method->hasAnnotation('XMLTag') === true) { 
     242                $xmlTag      = $method->getAnnotation('XMLTag'); 
     243                $tagName     = $xmlTag->getTagName(); 
    291244                $elementName = $xmlTag->getElementTagName(); 
    292245            } else { 
    293246                if (null !== $matcher) { 
    294247                    $tagName = $matcher->getTagnameForMethod($method, $returnValue); 
    295                     if ($tagName === false) { 
     248                    if (false === $tagName) { 
    296249                        continue; 
    297250                    } 
     
    300253                        continue; 
    301254                    } 
    302                     if ($method->isStatic() && $this->opts[self::OPT_STATIC] === false) { 
     255                     
     256                    if ($this->opts[self::OPT_STATIC] === false && $method->isStatic() === true) { 
    303257                        continue; 
    304258                    } 
     
    306260                    $tagName = $method->getName(); 
    307261                } 
     262                 
    308263                $elementName = null; 
    309264            } 
    310265 
    311             if (is_array($returnValue)) { 
     266            if (is_array($returnValue) === true) { 
    312267                $this->serializeArray($returnValue, $xmlWriter, $tagName, $elementName); 
    313268            } else { 
     
    315270            } 
    316271        } 
     272         
    317273        $xmlWriter->writeEndElement(); 
     274    } 
     275 
     276    /** 
     277     * serializes annotated element 
     278     * 
     279     * Returns true if element was serialized, else false. 
     280     * 
     281     * @param   stubAnnotatable      $annotatable  the annotatable element to serialize 
     282     * @param   stubXMLStreamWriter  $xmlWriter    the xml writer to use 
     283     * @param   mixed                $value        the value to serialize 
     284     * @return  bool 
     285     */ 
     286    protected function writeAnnotatedElement(stubAnnotatable $annotatable, stubXMLStreamWriter $xmlWriter, $value) 
     287    { 
     288        if ($annotatable->hasAnnotation('XMLAttribute') === true) { 
     289            $xmlAttribute = $annotatable->getAnnotation('XMLAttribute'); 
     290            if ('' === (string) $value && $xmlAttribute->shouldSkipEmpty() === true) { 
     291                return true; 
     292            } 
     293             
     294            $xmlWriter->writeAttribute($xmlAttribute->getAttributeName(), (string) $value); 
     295            return true; 
     296        } elseif ($annotatable->hasAnnotation('XMLFragment') === true) { 
     297            $tagName = $annotatable->getAnnotation('XMLFragment')->getTagName(); 
     298            if (null != $tagName) { 
     299                $xmlWriter->writeStartElement($tagName); 
     300                $xmlWriter->writeXmlFragment($value); 
     301                $xmlWriter->writeEndElement(); 
     302            } else { 
     303                $xmlWriter->writeXmlFragment($value); 
     304            } 
     305             
     306            return true; 
     307        } 
     308         
     309        return false; 
    318310    } 
    319311