• Login
  • Help/Guide
  • About Trac
  • Preferences
  • Wiki
  • Timeline
  • Roadmap
  • Browse Source
  • View Tickets
  • Search

Context Navigation

  • ← Previous Changeset
  • Next Changeset →

Changeset 126

Show
Ignore:
Timestamp:
11/21/07 15:30:43 (9 months ago)
Author:
callan
Message:

Support a few further things that Curtis needs:

  • Accesors for objects that follow OME XML reference naming conventions
  • New convenience constructors that re-institute the org.openmicroscopy.xml semantics
Location:
Xml/xsd-fu/trunk
Files:
3 modified

  • fu.py (modified) (10 diffs)
  • templates/Class.template (modified) (2 diffs)
  • xsd-fu (modified) (3 diffs)

Legend:

Unmodified
Added
Removed
  • Xml/xsd-fu/trunk/fu.py

    r111 r126  
    3030import logging 
    3131import re 
     32import os 
     33 
    3234from generateDS.generateDS import * 
    3335from xml import sax 
    … …  
    4446        def now(): 
    4547                return datetime.now() 
     48 
     49# Default logger configuration 
     50#logging.basicConfig(level=logging.DEBUG, 
     51#                    format='%(asctime)s %(levelname)s %(message)s') 
    4652 
    4753# A global mapping from XSD Schema types and Java types 
    … …  
    7480DEFAULT_PACKAGE = "org.openmicroscopy.xml2007" 
    7581 
    76 # The base template for class processing. 
     82# The default template for class processing. 
    7783CLASS_TEMPLATE = "templates/Class.template" 
    7884 
    … …  
    8288        """ 
    8389        pass 
     90 
     91class ReferenceDelegate(object): 
     92        """ 
     93        A "virtual" property delegate to be used with "reference"  
     94        OMEModelProperty instances. This delegate conforms loosely to the same 
     95        interface as a delegate coming from generateDS. 
     96        """ 
     97        def __init__(self, dataType): 
     98                self.name = dataType + "_BackReference" 
     99                self.dataType = dataType 
     100                # Ensures property code which is looking for elements or attributes 
     101                # which conform to an enumeration can function. 
     102                self.values = None 
     103         
     104        def getMaxOccurs(self): 
     105                return 9999 
     106 
     107        def getMinOccurs(self): 
     108                return 0 
     109 
     110        def getType(self): 
     111                return self.dataType 
     112 
     113        def getName(self): 
     114                return self.name 
    84115         
    85116class OMEModelProperty(object): 
    86117        """ 
    87118        An aggregate type representing either an OME XML Schema element or  
    88         attribute. 
     119        attribute. This class equates conceptually to an instance variable which 
     120        may be of a singular type or a collection. 
    89121        """ 
    90122         
    … …  
    93125                self.delegate = delegate 
    94126                self.isAttribute = False 
     127                self.isReference = False 
    95128 
    96129        def _get_type(self): 
    … …  
    129162                        return JAVA_TYPE_MAP[self.type] 
    130163                except KeyError: 
     164                        # Hand back the type of references 
     165                        if self.isReference: 
     166                                return self.type 
    131167                        # Hand back the type of complex types 
    132168                        if not self.isAttribute and self.delegate.isComplex(): 
    … …  
    195231                return klass(element, model) 
    196232        fromElement = classmethod(fromElement) 
    197          
     233 
     234        def fromReference(klass, reference, model): 
     235                """ 
     236                Instantiates a property from a "virtual" OME XML schema reference. 
     237                """ 
     238                instance = klass(reference, model) 
     239                instance.isReference = True 
     240                return instance 
     241        fromReference = classmethod(fromReference) 
    198242 
    199243class OMEModelObject(object): 
    … …  
    224268                self.properties[name] = \ 
    225269                        OMEModelProperty.fromElement(element, self.model) 
    226                          
     270 
    227271        def _get_javaBase(self): 
    228272                base = self.element.getBase() 
    … …  
    316360                                self.processTree(children, element) 
    317361 
     362        def postProcessReferences(self): 
     363                """ 
     364                Examines the list of objects in the model for instances that conform 
     365                to the OME XML Schema referential object naming conventions and 
     366                injects properties into referenced objects to provide back links. 
     367                """ 
     368                references = dict() 
     369                for o in self.objects.values(): 
     370                        for prop in o.properties.values(): 
     371                                if prop.type[-3:] == "Ref": 
     372                                        shortName = prop.type[:-3] 
     373                                        if prop.type not in references: 
     374                                                references[shortName] = list() 
     375                                        references[shortName].append(o.name) 
     376 
     377                for o in self.objects.values(): 
     378                        if o.name in references: 
     379                                for ref in references[o.name]: 
     380                                        delegate = ReferenceDelegate(ref) 
     381                                        prop = OMEModelProperty.fromReference(delegate, self) 
     382                                        o.properties[ref] = prop 
     383 
    318384        def process(klass, contentHandler): 
    319385                """ 
    … …  
    326392                model.topLevelSimpleTypes = contentHandler.topLevelSimpleTypes 
    327393                model.processTree(elements) 
     394                model.postProcessReferences() 
    328395                return model 
    329396        process = classmethod(process) 
  • Xml/xsd-fu/trunk/templates/Class.template

    r111 r126  
    3939package ${fu.package}; 
    4040 
     41import ome.xml.DOMUtil; 
     42import ome.xml.OMEXMLNode; 
     43 
    4144import java.util.Vector; 
     45import java.util.List; 
     46 
    4247import org.w3c.dom.Element; 
    4348 
    4449public class ${klass.name}Node extends ${klass.javaBase} 
    4550{ 
    46         // -- Constructor -- 
     51        // -- Constructors -- 
    4752         
     53        /** Constructs a ${klass.name} node with an associated DOM element. */ 
    4854        public ${klass.name}Node(Element element) 
    4955        { 
    5056                super(element); 
    5157        } 
    52          
     58 
     59        /** 
     60         * Constructs a ${klass.name} node with an associated DOM element beneath 
     61         * a given parent. 
     62         */ 
     63        public ${klass.name}Node(OMEXMLNode parent) 
     64        { 
     65                this(parent, true); 
     66        } 
     67 
     68        /** 
     69         * Constructs a ${klass.name} node with an associated DOM element beneath 
     70         * a given parent. 
     71         */ 
     72        public ${klass.name}Node(OMEXMLNode parent, boolean attach) 
     73        { 
     74                super(DOMUtil.createChild(parent.getDOMElement(), 
     75                                          "${klass.name}", attach)); 
     76        } 
     77 
    5378        // -- ${klass.name} API methods -- 
    5479{% for prop in klass.properties.values() %}\ 
    5580  {% choose %}\ 
    56     {% when prop.isAttribute and prop.type[-2:] == "ID" and prop.name not in fu.DO_NOT_PROCESS %} 
     81    {% when prop.isReference %} 
     82        // Virtual, inferred back reference ${prop.name} 
     83        public List get${prop.javaType}List() 
     84        { 
     85                return getReferringNodes("${prop.javaType}"); 
     86        } 
     87         
     88        public int count${prop.javaType}List() 
     89        { 
     90                return getReferringCount("${prop.javaType}"); 
     91        } 
     92    {% end %}\ 
     93    {% when prop.isAttribute and prop.type[-2:] == "ID" and \ 
     94       prop.name not in fu.DO_NOT_PROCESS %} 
    5795        // Attribute which is an OME XML "ID" 
    5896        public ${prop.javaType} get${prop.name}() 
    … …  
    141179        } 
    142180} 
     181 
  • Xml/xsd-fu/trunk/xsd-fu

    r111 r126  
    7777        if outputDirectory is None: 
    7878                usage("Output directory must be specified!") 
     79        if not os.path.exists(outputDirectory): 
     80            print "Output directory '%s' does not exist!" % outputDirectory 
     81            sys.exit(1) 
    7982 
    8083        model = parseXmlSchema(args[0]) 
    … …  
    9699 
    97100        model = parseXmlSchema("tmp/ome.xsd") 
    98         fu = Fu(outputDirectory, package) 
     101        fu = TemplateInfo(outputDirectory, package) 
    99102        template = NewTextTemplate(open(CLASS_TEMPLATE).read()) 
    100103        for obj in model.objects.values(): 
    101                 if obj.name == "Experimenter": 
     104                if obj.name == "Laser": 
    102105                        print " +--", obj.name 
    103106                        for prop in obj.properties.values(): 
    … …  
    110113#       debugMain() 
    111114        main() 
     115 

Download in other formats:

  • Unified Diff
  • Zip Archive

Trac Powered

Powered by Trac 0.11
By Edgewall Software.

Visit the Trac open source project at
http://trac.edgewall.org/