Changeset 211

Show
Ignore:
Timestamp:
02/05/07 20:56:17 (2 years ago)
Author:
schst
Message:

Added possibility to prefix annotation classes,
Renamed XMLSerializer annotations to stub*Annotation

Files:

Legend:

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

    r155 r211  
    2020{ 
    2121    /** 
     22     * Prefixes that can be prepended to class names 
     23     * 
     24     * @var array 
     25     */ 
     26    private static $prefixes = array('stub'); 
     27 
     28    /** 
    2229     * Creates an annotation from the given docblock comment. 
    2330     * 
     
    3441            return $cachedAnnotation = stubAnnotationCache::get($target, $targetName, $annotationName); 
    3542        } 
    36          
     43 
    3744        // Fast annotation check 
    3845        $start = stripos($comment, '@' . $annotationName); 
     
    4249 
    4350        list($annotationClass,$data) = self::parseAnnotation($comment, $annotationName); 
    44         if (class_exists($annotationClass) == false) { 
    45             throw new ReflectionException('Error parsing annotation: Class ' . $annotationClass . ' does not exist'); 
    46         } 
    47  
    4851        if (false === $data) { 
    4952            throw new ReflectionException('Error evaluating annotation: ' . $annotationName . substr($comment, $start, $strlen)); 
    5053        } 
     54 
     55        $annotationClass = self::findAnnotationClass($annotationClass); 
    5156 
    5257        $annotation = new $annotationClass(); 
     
    5762            throw new ReflectionException('The annotation: ' . $annotationName . ' is not applicable for the given type.'); 
    5863        } 
    59          
     64 
    6065        $annotation = self::build($annotation, $data); 
    6166        $annotation->setAnnotationName($annotationName); 
     
    7075     * @param string $annotationName 
    7176     * @return array 
    72      *  
     77     * 
    7378     * @todo Speed up parsing 
    7479     */ 
     
    7681        // remove start and end of the comment 
    7782        $comment = trim(str_replace(array('/**', '*/'), '', $comment)); 
    78          
     83 
    7984        $parts = explode('@', $comment); 
    8085        $annoLength = strlen($annotationName); 
     
    97102                $annotationClass = $annotationName; 
    98103            } 
    99              
    100              
     104 
     105 
    101106            // check for empty annotation 
    102107            $params = trim(str_replace(array('*', '(', ')'), '', $params)); 
     
    109114                return array($annotationClass, array('value' => trim($tmpParams[0]))); 
    110115            } 
    111              
     116 
    112117            $params = array(); 
    113118            foreach ($tmpParams as $param) { 
     
    124129            } 
    125130            return array($annotationClass, $params); 
    126         }         
    127     } 
    128      
     131        } 
     132    } 
     133 
    129134    /** 
    130135     * Checks whether an annotation is applicable for the given type or not. 
     
    138143        return (($annotation->getAnnotationTarget() & $target) != 0); 
    139144    } 
    140      
     145 
    141146    /** 
    142147     * builds the annotation by setting its values from given data 
     
    184189        } catch (ReflectionException $e) { 
    185190        } 
    186          
     191 
    187192        return (null != $annotation); 
     193    } 
     194 
     195    /** 
     196     * Add a new annotation prefix 
     197     * 
     198     * @param string $prefix 
     199     */ 
     200    public static function addAnnotationPrefix($prefix) { 
     201        self::$prefixes[] = $prefix; 
     202    } 
     203 
     204    /** 
     205     * Try to find the annotation class 
     206     * 
     207     * This method checks, whether there is a class named exactly as the annotation class 
     208     * or a method that has one of the prefixes defined in $prefixes and the postfix 'Annotation'. 
     209     * 
     210     * @param string $annotationClass 
     211     * @return string 
     212     * @see    addAnnotationPrefix() 
     213     */ 
     214    private static function findAnnotationClass($annotationClass) { 
     215        if (class_exists($annotationClass, false) == true) { 
     216            return $annotationClass; 
     217        } 
     218        $annotationClass = $annotationClass  . 'Annotation'; 
     219        foreach (self::$prefixes as $prefix) { 
     220            if (class_exists($prefix . $annotationClass)) { 
     221                return $prefix . $annotationClass; 
     222            } 
     223        } 
     224        throw new ReflectionException('Error parsing annotation: Class ' . $annotationClass . ' does not exist'); 
    188225    } 
    189226} 
  • trunk/src/main/php/net/stubbles/xml/serializer/stubXMLSerializer.php

    r210 r211  
    77 * @subpackage  xml_serializer 
    88 */ 
    9 stubClassLoader::load('net.stubbles.xml.serializer.annotations.XMLTag', 
    10                       'net.stubbles.xml.serializer.annotations.XMLAttribute', 
    11                       'net.stubbles.xml.serializer.annotations.XMLIgnore', 
    12                       'net.stubbles.xml.serializer.annotations.XMLMatcher', 
     9stubClassLoader::load('net.stubbles.xml.serializer.annotations.stubXMLTagAnnotation', 
     10                      'net.stubbles.xml.serializer.annotations.stubXMLAttributeAnnotation', 
     11                      'net.stubbles.xml.serializer.annotations.stubXMLIgnoreAnnotation', 
     12                      'net.stubbles.xml.serializer.annotations.stubXMLMatcherAnnotation', 
    1313                      'net.stubbles.relection.stubReflection'); 
    1414