Changeset 126
- Timestamp:
- 11/21/07 15:30:43 (9 months ago)
- Location:
- Xml/xsd-fu/trunk
- Files:
-
- 3 modified
Legend:
- Unmodified
- Added
- Removed
-
Xml/xsd-fu/trunk/fu.py
r111 r126 30 30 import logging 31 31 import re 32 import os 33 32 34 from generateDS.generateDS import * 33 35 from xml import sax … … 44 46 def now(): 45 47 return datetime.now() 48 49 # Default logger configuration 50 #logging.basicConfig(level=logging.DEBUG, 51 # format='%(asctime)s %(levelname)s %(message)s') 46 52 47 53 # A global mapping from XSD Schema types and Java types … … 74 80 DEFAULT_PACKAGE = "org.openmicroscopy.xml2007" 75 81 76 # The basetemplate for class processing.82 # The default template for class processing. 77 83 CLASS_TEMPLATE = "templates/Class.template" 78 84 … … 82 88 """ 83 89 pass 90 91 class 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 84 115 85 116 class OMEModelProperty(object): 86 117 """ 87 118 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. 89 121 """ 90 122 … … 93 125 self.delegate = delegate 94 126 self.isAttribute = False 127 self.isReference = False 95 128 96 129 def _get_type(self): … … 129 162 return JAVA_TYPE_MAP[self.type] 130 163 except KeyError: 164 # Hand back the type of references 165 if self.isReference: 166 return self.type 131 167 # Hand back the type of complex types 132 168 if not self.isAttribute and self.delegate.isComplex(): … … 195 231 return klass(element, model) 196 232 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) 198 242 199 243 class OMEModelObject(object): … … 224 268 self.properties[name] = \ 225 269 OMEModelProperty.fromElement(element, self.model) 226 270 227 271 def _get_javaBase(self): 228 272 base = self.element.getBase() … … 316 360 self.processTree(children, element) 317 361 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 318 384 def process(klass, contentHandler): 319 385 """ … … 326 392 model.topLevelSimpleTypes = contentHandler.topLevelSimpleTypes 327 393 model.processTree(elements) 394 model.postProcessReferences() 328 395 return model 329 396 process = classmethod(process) -
Xml/xsd-fu/trunk/templates/Class.template
r111 r126 39 39 package ${fu.package}; 40 40 41 import ome.xml.DOMUtil; 42 import ome.xml.OMEXMLNode; 43 41 44 import java.util.Vector; 45 import java.util.List; 46 42 47 import org.w3c.dom.Element; 43 48 44 49 public class ${klass.name}Node extends ${klass.javaBase} 45 50 { 46 // -- Constructor --51 // -- Constructors -- 47 52 53 /** Constructs a ${klass.name} node with an associated DOM element. */ 48 54 public ${klass.name}Node(Element element) 49 55 { 50 56 super(element); 51 57 } 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 53 78 // -- ${klass.name} API methods -- 54 79 {% for prop in klass.properties.values() %}\ 55 80 {% 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 %} 57 95 // Attribute which is an OME XML "ID" 58 96 public ${prop.javaType} get${prop.name}() … … 141 179 } 142 180 } 181 -
Xml/xsd-fu/trunk/xsd-fu
r111 r126 77 77 if outputDirectory is None: 78 78 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) 79 82 80 83 model = parseXmlSchema(args[0]) … … 96 99 97 100 model = parseXmlSchema("tmp/ome.xsd") 98 fu = Fu(outputDirectory, package)101 fu = TemplateInfo(outputDirectory, package) 99 102 template = NewTextTemplate(open(CLASS_TEMPLATE).read()) 100 103 for obj in model.objects.values(): 101 if obj.name == " Experimenter":104 if obj.name == "Laser": 102 105 print " +--", obj.name 103 106 for prop in obj.properties.values(): … … 110 113 # debugMain() 111 114 main() 115
