Changeset 210

Show
Ignore:
Timestamp:
02/05/07 19:46:49 (2 years ago)
Author:
schst
Message:

Do not call methods that require any parameters, export public methods and properties by default
Added support for XMLProperties and XMLMethods using the XMLMatcher annotation

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/src/main/php/net/stubbles/xml/serializer/stubXMLSerializer.php

    r209 r210  
    1010                      'net.stubbles.xml.serializer.annotations.XMLAttribute', 
    1111                      'net.stubbles.xml.serializer.annotations.XMLIgnore', 
     12                      'net.stubbles.xml.serializer.annotations.XMLMatcher', 
    1213                      'net.stubbles.relection.stubReflection'); 
    1314 
     
    124125        // export props 
    125126        $properties = $clazz->getProperties(); 
     127 
     128        // get the property-matcher 
     129        if ($clazz->hasAnnotation('XMLProperties')) { 
     130            $matcher = $clazz->getAnnotation('XMLProperties'); 
     131        } else { 
     132            $matcher = null; 
     133        } 
     134 
    126135        foreach ($properties as $property) { 
    127136            if (!$property->isPublic()) { 
     
    131140                continue; 
    132141            } 
     142            if (null !== $matcher && !$matcher->matches($property->getName())) { 
     143                continue; 
     144            } 
     145 
    133146            $propValue = $property->getValue($object); 
    134147            if ($property->hasAnnotation('XMLAttribute')) { 
     
    139152            if ($property->hasAnnotation('XMLTag')) { 
    140153                $xmlTag = $property->getAnnotation('XMLTag'); 
    141  
    142                 if (is_array($propValue)) { 
    143                     $this->serializeArray($propValue, $xmlWriter, $xmlTag->getTagName(), $xmlTag->getElementTagName()); 
    144                 } else { 
    145                     $this->serializeDispatcher($propValue, $xmlWriter, $xmlTag->getTagName()); 
    146                 } 
    147                 continue; 
    148             } 
     154                $tagName = $xmlTag->getTagName(); 
     155                $elementName = $xmlTag->getElementTagName(); 
     156            } else { 
     157                $tagName = $property->getName(); 
     158                $elementName = null; 
     159            } 
     160 
     161            if (is_array($propValue)) { 
     162                $this->serializeArray($propValue, $xmlWriter, $tagName, $elementName); 
     163            } else { 
     164                $this->serializeDispatcher($propValue, $xmlWriter, $tagName); 
     165            } 
    149166        } 
    150167 
    151168        // export methods 
    152169        $methods = $clazz->getMethods(); 
     170 
     171        // get the method-matcher 
     172        if ($clazz->hasAnnotation('XMLMethods')) { 
     173            $matcher = $clazz->getAnnotation('XMLMethods'); 
     174        } else { 
     175            $matcher = null; 
     176        } 
     177 
    153178        foreach ($methods as $method) { 
    154179            if (!$method->isPublic()) { 
     
    161186                continue; 
    162187            } 
     188            if (0 != $method->getNumberOfParameters()) { 
     189                continue; 
     190            } 
    163191            if ($method->hasAnnotation('XMLIgnore')) { 
    164192                continue; 
    165193            } 
     194            if (null !== $matcher && !$matcher->matches($method->getName())) { 
     195                continue; 
     196            } 
     197 
     198 
    166199            $returnValue = $method->invoke($object); 
    167200            if ($method->hasAnnotation('XMLAttribute')) { 
     
    172205            if ($method->hasAnnotation('XMLTag')) { 
    173206                $xmlTag = $method->getAnnotation('XMLTag'); 
    174  
    175                 if (is_array($returnValue)) { 
    176                     $this->serializeArray($returnValue, $xmlWriter, $xmlTag->getTagName(), $xmlTag->getElementTagName()); 
    177                 } else { 
    178                     $this->serializeDispatcher($returnValue, $xmlWriter, $xmlTag->getTagName()); 
    179                 } 
    180                 continue; 
    181             } 
     207                $tagName = $xmlTag->getTagName(); 
     208                $elementName = $xmlTag->getElementTagName(); 
     209            } else { 
     210                $tagName = $method->getName(); 
     211                $elementName = null; 
     212            } 
     213 
     214            if (is_array($propValue)) { 
     215                $this->serializeArray($returnValue, $xmlWriter, $tagName, $elementName); 
     216            } else { 
     217                $this->serializeDispatcher($returnValue, $xmlWriter, $tagName); 
     218            } 
    182219        } 
    183220        $xmlWriter->writeEndElement(); 
  • trunk/src/test/php/net/stubbles/xml/stubXMLSerializerTestCase.php

    r209 r210  
    1212/** 
    1313 * Simple Test class to test the XMLSerializer 
     14 */ 
     15class XMLSerializerDefaultObj { 
     16    private $ignore = 'ignore'; 
     17    public $foo = 'foo'; 
     18 
     19    public function getBar() { 
     20        return 'bar'; 
     21    } 
     22 
     23    public function setBar($bar) { 
     24        throw new Exception('setBar has been called'); 
     25    } 
     26} 
     27 
     28/** 
     29 * Simple Test class to test the XMLSerializer 
    1430 * 
    1531 * @XMLTag(tagName=foo) 
     
    7793    public function getValue() { 
    7894        return "returned"; 
     95    } 
     96} 
     97 
     98/** 
     99 * Simple Test class to test the XMLSerializer 
     100 * 
     101 * @XMLTag(tagName=testObject) 
     102 * @XMLProperties[XMLMatcher](pattern=/^[a-zA-Z]{3}$/) 
     103 */ 
     104class XMLSerializerPropertyMatcher { 
     105    public $one   = 'one'; 
     106    public $two   = 'two'; 
     107    public $three = 'three'; 
     108} 
     109 
     110 
     111/** 
     112 * Simple Test class to test the XMLSerializer 
     113 * 
     114 * @XMLTag(tagName=testObject) 
     115 * @XMLMethods[XMLMatcher](pattern=/^getF/) 
     116 */ 
     117class XMLSerializerMethodMatcher { 
     118    public function getFoo() { 
     119        return "foo"; 
     120    } 
     121    public function getBar() { 
     122        return "bar"; 
    79123    } 
    80124} 
     
    222266        $this->assertEqual('<?xml version="1.0" encoding="ISO-8859-1"?>' . "\n" . '<class method="returned"/>', $writer->asXML()); 
    223267    } 
     268 
     269    /** 
     270     * Test serializing an object with a property matcher 
     271     */ 
     272    public function testObjectPropertyMatcher() { 
     273        $writer = new stubDomXMLStreamWriter(); 
     274        $obj = new XMLSerializerPropertyMatcher(); 
     275        $this->serializer->serialize($obj, $writer); 
     276        $this->assertEqual('<?xml version="1.0" encoding="ISO-8859-1"?>' . "\n" . '<testObject><one>one</one><two>two</two></testObject>', $writer->asXML()); 
     277    } 
     278 
     279    /** 
     280     * Test serializing an object with a method matcher 
     281     */ 
     282    public function testObjectMethodMatcher() { 
     283        $writer = new stubDomXMLStreamWriter(); 
     284        $obj = new XMLSerializerMethodMatcher(); 
     285        $this->serializer->serialize($obj, $writer); 
     286        $this->assertEqual('<?xml version="1.0" encoding="ISO-8859-1"?>' . "\n" . '<testObject><getFoo>foo</getFoo></testObject>', $writer->asXML()); 
     287    } 
     288 
     289    /** 
     290     * Test serializing an object with a method matcher 
     291     */ 
     292    public function testObjectDefault() { 
     293        $writer = new stubDomXMLStreamWriter(); 
     294        $obj = new XMLSerializerDefaultObj(); 
     295        $this->serializer->serialize($obj, $writer); 
     296        $this->assertEqual('<?xml version="1.0" encoding="ISO-8859-1"?>' . "\n" . '<XMLSerializerDefaultObj><foo>foo</foo><getBar>bar</getBar></XMLSerializerDefaultObj>', $writer->asXML()); 
     297    } 
    224298} 
    225299?>