Dear Dave Kuhlman, I used the following schema: sysid.xsd
to generate the following python using generateDS.py: sysidMessage.pyWhen using sysidMessage.py to generate XML I am finding that the XML generated has boolean attributes with their first letter capitalized (the python standard) as opposed to all lowercase (the XML standard). As a result, the validating XML parser I am using with Java is flagging the python generated XML as invalid. Could you please point me to where in generateDS.py I should look to fix this?
Thank you very much, Iris Cheung
#!/usr/bin/env python # # Generated Tue Apr 29 15:49:26 2008 by generateDS.py. # import sys import getopt from xml.dom import minidom from xml.dom import Node # # If you have installed IPython you can uncomment and use the following. # IPython is available from http://ipython.scipy.org/. # ## from IPython.Shell import IPShellEmbed ## args = '' ## ipshell = IPShellEmbed(args, ## banner = 'Dropping into IPython', ## exit_msg = 'Leaving Interpreter, back to program.') # Then use the following line where and when you want to drop into the # IPython shell: # ipshell('<some message> -- Entering ipshell.\nHit Ctrl-D to exit') # # Support/utility functions. # def showIndent(outfile, level): for idx in range(level): outfile.write(' ') def quote_xml(inStr): s1 = inStr s1 = s1.replace('&', '&') s1 = s1.replace('<', '<') s1 = s1.replace('"', '"') return s1 def quote_python(inStr): s1 = inStr if s1.find("'") == -1: if s1.find('\n') == -1: return "'%s'" % s1 else: return "'''%s'''" % s1 else: if s1.find('"') != -1: s1 = s1.replace('"', '\\"') if s1.find('\n') == -1: return '"%s"' % s1 else: return '"""%s"""' % s1 class MixedContainer: # Constants for category: CategoryNone = 0 CategoryText = 1 CategorySimple = 2 CategoryComplex = 3 # Constants for content_type: TypeNone = 0 TypeText = 1 TypeString = 2 TypeInteger = 3 TypeFloat = 4 TypeDecimal = 5 TypeDouble = 6 TypeBoolean = 7 def __init__(self, category, content_type, name, value): self.category = category self.content_type = content_type self.name = name self.value = value def getCategory(self): return self.category def getContenttype(self, content_type): return self.content_type def getValue(self): return self.value def getName(self): return self.name def export(self, outfile, level, name): if self.category == MixedContainer.CategoryText: outfile.write(self.value) elif self.category == MixedContainer.CategorySimple: self.exportSimple(outfile, level, name) else: # category == MixedContainer.CategoryComplex self.value.export(outfile, level, name) def exportSimple(self, outfile, level, name): if self.content_type == MixedContainer.TypeString: outfile.write('<%s>%s</%s>' % (self.name, self.value, self.name)) elif self.content_type == MixedContainer.TypeInteger or \ self.content_type == MixedContainer.TypeBoolean: outfile.write('<%s>%d</%s>' % (self.name, self.value, self.name)) elif self.content_type == MixedContainer.TypeFloat or \ self.content_type == MixedContainer.TypeDecimal: outfile.write('<%s>%f</%s>' % (self.name, self.value, self.name)) elif self.content_type == MixedContainer.TypeDouble: outfile.write('<%s>%g</%s>' % (self.name, self.value, self.name)) def exportLiteral(self, outfile, level, name): if self.category == MixedContainer.CategoryText: showIndent(outfile, level) outfile.write('MixedContainer(%d, %d, "%s", "%s"),\n' % \ (self.category, self.content_type, self.name, self.value)) elif self.category == MixedContainer.CategorySimple: showIndent(outfile, level) outfile.write('MixedContainer(%d, %d, "%s", "%s"),\n' % \ (self.category, self.content_type, self.name, self.value)) else: # category == MixedContainer.CategoryComplex showIndent(outfile, level) outfile.write('MixedContainer(%d, %d, "%s",\n' % \ (self.category, self.content_type, self.name,)) self.value.exportLiteral(outfile, level + 1) showIndent(outfile, level) outfile.write(')\n') class _MemberSpec(object): def __init__(self, name='', data_type='', container=0): self.name = name self.data_type = data_type self.container = container def set_name(self, name): self.name = name def get_name(self): return self.name def set_data_type(self, data_type): self.data_type = data_type def get_data_type(self): return self.data_type def set_container(self, container): self.container = container def get_container(self): return self.container # # Data representation classes. # class Message: subclass = None superclass = None def __init__(self, Action=None, Characterize=None, Result=None): self.Action = Action self.Characterize = Characterize self.Result = Result def factory(*args_, **kwargs_): if Message.subclass: return Message.subclass(*args_, **kwargs_) else: return Message(*args_, **kwargs_) factory = staticmethod(factory) def get_Action(self): return self.Action def set_Action(self, Action): self.Action = Action def get_Characterize(self): return self.Characterize def set_Characterize(self, Characterize): self.Characterize = Characterize def get_Result(self): return self.Result def set_Result(self, Result): self.Result = Result def export(self, outfile, level, name_='Message'): showIndent(outfile, level) outfile.write('<%s>\n' % name_) self.exportChildren(outfile, level + 1, name_) showIndent(outfile, level) outfile.write('</%s>\n' % name_) def exportAttributes(self, outfile, level, name_='Message'): pass def exportChildren(self, outfile, level, name_='Message'): if self.Action: self.Action.export(outfile, level) if self.Characterize: self.Characterize.export(outfile, level) if self.Result: self.Result.export(outfile, level) def exportLiteral(self, outfile, level, name_='Message'): level += 1 self.exportLiteralAttributes(outfile, level, name_) self.exportLiteralChildren(outfile, level, name_) def exportLiteralAttributes(self, outfile, level, name_): pass def exportLiteralChildren(self, outfile, level, name_): if self.Action: showIndent(outfile, level) outfile.write('Action=Action(\n') self.Action.exportLiteral(outfile, level) showIndent(outfile, level) outfile.write('),\n') if self.Characterize: showIndent(outfile, level) outfile.write('Characterize=Characterize(\n') self.Characterize.exportLiteral(outfile, level) showIndent(outfile, level) outfile.write('),\n') if self.Result: showIndent(outfile, level) outfile.write('Result=Result(\n') self.Result.exportLiteral(outfile, level) showIndent(outfile, level) outfile.write('),\n') def build(self, node_): attrs = node_.attributes self.buildAttributes(attrs) for child_ in node_.childNodes: nodeName_ = child_.nodeName.split(':')[-1] self.buildChildren(child_, nodeName_) def buildAttributes(self, attrs): pass def buildChildren(self, child_, nodeName_): if child_.nodeType == Node.ELEMENT_NODE and \ nodeName_ == 'Action': obj_ = Action.factory() obj_.build(child_) self.set_Action(obj_) elif child_.nodeType == Node.ELEMENT_NODE and \ nodeName_ == 'Characterize': obj_ = Characterize.factory() obj_.build(child_) self.set_Characterize(obj_) elif child_.nodeType == Node.ELEMENT_NODE and \ nodeName_ == 'Result': obj_ = Result.factory() obj_.build(child_) self.set_Result(obj_) # end class Message class Action: subclass = None superclass = None def __init__(self, GetNames=0, StopExcitations=0): self.GetNames = GetNames self.StopExcitations = StopExcitations def factory(*args_, **kwargs_): if Action.subclass: return Action.subclass(*args_, **kwargs_) else: return Action(*args_, **kwargs_) factory = staticmethod(factory) def get_GetNames(self): return self.GetNames def set_GetNames(self, GetNames): self.GetNames = GetNames def get_StopExcitations(self): return self.StopExcitations def set_StopExcitations(self, StopExcitations): self.StopExcitations = StopExcitations def export(self, outfile, level, name_='Action'): showIndent(outfile, level) outfile.write('<%s>\n' % name_) self.exportChildren(outfile, level + 1, name_) showIndent(outfile, level) outfile.write('</%s>\n' % name_) def exportAttributes(self, outfile, level, name_='Action'): pass def exportChildren(self, outfile, level, name_='Action'): if self.get_GetNames() != None : showIndent(outfile, level) outfile.write('<GetNames>%d</GetNames>\n' % self.get_GetNames()) if self.get_StopExcitations() != None : showIndent(outfile, level) outfile.write('<StopExcitations>%d</StopExcitations>\n' % self.get_StopExcitations()) def exportLiteral(self, outfile, level, name_='Action'): level += 1 self.exportLiteralAttributes(outfile, level, name_) self.exportLiteralChildren(outfile, level, name_) def exportLiteralAttributes(self, outfile, level, name_): pass def exportLiteralChildren(self, outfile, level, name_): showIndent(outfile, level) outfile.write('GetNames=%d,\n' % self.get_GetNames()) showIndent(outfile, level) outfile.write('StopExcitations=%d,\n' % self.get_StopExcitations()) def build(self, node_): attrs = node_.attributes self.buildAttributes(attrs) for child_ in node_.childNodes: nodeName_ = child_.nodeName.split(':')[-1] self.buildChildren(child_, nodeName_) def buildAttributes(self, attrs): pass def buildChildren(self, child_, nodeName_): if child_.nodeType == Node.ELEMENT_NODE and \ nodeName_ == 'GetNames': if child_.firstChild: sval_ = child_.firstChild.nodeValue if sval_ in ('true', '1'): ival_ = 1 elif sval_ in ('false', '0'): ival_ = 0 else: raise ValueError('requires boolean -- %s' % child_.toxml()) self.GetNames = ival_ elif child_.nodeType == Node.ELEMENT_NODE and \ nodeName_ == 'StopExcitations': if child_.firstChild: sval_ = child_.firstChild.nodeValue if sval_ in ('true', '1'): ival_ = 1 elif sval_ in ('false', '0'): ival_ = 0 else: raise ValueError('requires boolean -- %s' % child_.toxml()) self.StopExcitations = ival_ # end class Action class Characterize: subclass = None superclass = None def __init__(self, ExtraDelayInSeconds=0.0, Excitation=None, Response=None): self.ExtraDelayInSeconds = ExtraDelayInSeconds if Excitation is None: self.Excitation = [] else: self.Excitation = Excitation if Response is None: self.Response = [] else: self.Response = Response def factory(*args_, **kwargs_): if Characterize.subclass: return Characterize.subclass(*args_, **kwargs_) else: return Characterize(*args_, **kwargs_) factory = staticmethod(factory) def get_Excitation(self): return self.Excitation def set_Excitation(self, Excitation): self.Excitation = Excitation def add_Excitation(self, value): self.Excitation.append(value) def insert_Excitation(self, index, value): self.Excitation[index] = value def get_Response(self): return self.Response def set_Response(self, Response): self.Response = Response def add_Response(self, value): self.Response.append(value) def insert_Response(self, index, value): self.Response[index] = value def get_ExtraDelayInSeconds(self): return self.ExtraDelayInSeconds def set_ExtraDelayInSeconds(self, ExtraDelayInSeconds): self.ExtraDelayInSeconds = ExtraDelayInSeconds def export(self, outfile, level, name_='Characterize'): showIndent(outfile, level) outfile.write('<%s' % (name_, )) self.exportAttributes(outfile, level, name_='Characterize') outfile.write('>\n') self.exportChildren(outfile, level + 1, name_) showIndent(outfile, level) outfile.write('</%s>\n' % name_) def exportAttributes(self, outfile, level, name_='Characterize'): if self.get_ExtraDelayInSeconds() is not None: outfile.write(' ExtraDelayInSeconds="%s"' % (self.get_ExtraDelayInSeconds(), )) def exportChildren(self, outfile, level, name_='Characterize'): for Excitation_ in self.get_Excitation(): Excitation_.export(outfile, level) for Response_ in self.get_Response(): showIndent(outfile, level) outfile.write('<Response>%s</Response>\n' % quote_xml(Response_)) def exportLiteral(self, outfile, level, name_='Characterize'): level += 1 self.exportLiteralAttributes(outfile, level, name_) self.exportLiteralChildren(outfile, level, name_) def exportLiteralAttributes(self, outfile, level, name_): showIndent(outfile, level) outfile.write('ExtraDelayInSeconds = "%s",\n' % (self.get_ExtraDelayInSeconds(),)) def exportLiteralChildren(self, outfile, level, name_): showIndent(outfile, level) outfile.write('Excitation=[\n') level += 1 for Excitation in self.Excitation: showIndent(outfile, level) outfile.write('Excitation(\n') Excitation.exportLiteral(outfile, level) showIndent(outfile, level) outfile.write('),\n') level -= 1 showIndent(outfile, level) outfile.write('],\n') showIndent(outfile, level) outfile.write('Response=[\n') level += 1 for Response in self.Response: showIndent(outfile, level) outfile.write('%s,\n' % quote_python(Response)) level -= 1 showIndent(outfile, level) outfile.write('],\n') def build(self, node_): attrs = node_.attributes self.buildAttributes(attrs) for child_ in node_.childNodes: nodeName_ = child_.nodeName.split(':')[-1] self.buildChildren(child_, nodeName_) def buildAttributes(self, attrs): if attrs.get('ExtraDelayInSeconds'): try: self.ExtraDelayInSeconds = float(attrs.get('ExtraDelayInSeconds').value) except: raise ValueError('Bad float/double attribute (ExtraDelayInSeconds)') def buildChildren(self, child_, nodeName_): if child_.nodeType == Node.ELEMENT_NODE and \ nodeName_ == 'Excitation': obj_ = Excitation.factory() obj_.build(child_) self.Excitation.append(obj_) elif child_.nodeType == Node.ELEMENT_NODE and \ nodeName_ == 'Response': Response_ = '' for text__content_ in child_.childNodes: Response_ += text__content_.nodeValue self.Response.append(Response_) # end class Characterize class Excitation: subclass = None superclass = None def __init__(self, divider=None, iterations=None, Name='', File=None): self.divider = divider self.iterations = iterations self.Name = Name self.File = File def factory(*args_, **kwargs_): if Excitation.subclass: return Excitation.subclass(*args_, **kwargs_) else: return Excitation(*args_, **kwargs_) factory = staticmethod(factory) def get_Name(self): return self.Name def set_Name(self, Name): self.Name = Name def get_File(self): return self.File def set_File(self, File): self.File = File def get_divider(self): return self.divider def set_divider(self, divider): self.divider = divider def get_iterations(self): return self.iterations def set_iterations(self, iterations): self.iterations = iterations def export(self, outfile, level, name_='Excitation'): showIndent(outfile, level) outfile.write('<%s' % (name_, )) self.exportAttributes(outfile, level, name_='Excitation') outfile.write('>\n') self.exportChildren(outfile, level + 1, name_) showIndent(outfile, level) outfile.write('</%s>\n' % name_) def exportAttributes(self, outfile, level, name_='Excitation'): if self.get_divider() is not None: outfile.write(' divider="%s"' % (self.get_divider(), )) if self.get_iterations() is not None: outfile.write(' iterations="%s"' % (self.get_iterations(), )) def exportChildren(self, outfile, level, name_='Excitation'): showIndent(outfile, level) outfile.write('<Name>%s</Name>\n' % quote_xml(self.get_Name())) if self.File: self.File.export(outfile, level, name_='File') def exportLiteral(self, outfile, level, name_='Excitation'): level += 1 self.exportLiteralAttributes(outfile, level, name_) self.exportLiteralChildren(outfile, level, name_) def exportLiteralAttributes(self, outfile, level, name_): showIndent(outfile, level) outfile.write('divider = "%s",\n' % (self.get_divider(),)) showIndent(outfile, level) outfile.write('iterations = "%s",\n' % (self.get_iterations(),)) def exportLiteralChildren(self, outfile, level, name_): showIndent(outfile, level) outfile.write('Name=%s,\n' % quote_python(self.get_Name())) if self.File: showIndent(outfile, level) outfile.write('File=file(\n') self.File.exportLiteral(outfile, level, name_='File') showIndent(outfile, level) outfile.write('),\n') def build(self, node_): attrs = node_.attributes self.buildAttributes(attrs) for child_ in node_.childNodes: nodeName_ = child_.nodeName.split(':')[-1] self.buildChildren(child_, nodeName_) def buildAttributes(self, attrs): if attrs.get('divider'): self.divider = attrs.get('divider').value if attrs.get('iterations'): self.iterations = attrs.get('iterations').value def buildChildren(self, child_, nodeName_): if child_.nodeType == Node.ELEMENT_NODE and \ nodeName_ == 'Name': Name_ = '' for text__content_ in child_.childNodes: Name_ += text__content_.nodeValue self.Name = Name_ elif child_.nodeType == Node.ELEMENT_NODE and \ nodeName_ == 'File': obj_ = file.factory() obj_.build(child_) self.set_File(obj_) # end class Excitation class Result: subclass = None superclass = None def __init__(self, Value=None): if Value is None: self.Value = [] else: self.Value = Value def factory(*args_, **kwargs_): if Result.subclass: return Result.subclass(*args_, **kwargs_) else: return Result(*args_, **kwargs_) factory = staticmethod(factory) def get_Value(self): return self.Value def set_Value(self, Value): self.Value = Value def add_Value(self, value): self.Value.append(value) def insert_Value(self, index, value): self.Value[index] = value def export(self, outfile, level, name_='Result'): showIndent(outfile, level) outfile.write('<%s>\n' % name_) self.exportChildren(outfile, level + 1, name_) showIndent(outfile, level) outfile.write('</%s>\n' % name_) def exportAttributes(self, outfile, level, name_='Result'): pass def exportChildren(self, outfile, level, name_='Result'): for Value_ in self.get_Value(): Value_.export(outfile, level) def exportLiteral(self, outfile, level, name_='Result'): level += 1 self.exportLiteralAttributes(outfile, level, name_) self.exportLiteralChildren(outfile, level, name_) def exportLiteralAttributes(self, outfile, level, name_): pass def exportLiteralChildren(self, outfile, level, name_): showIndent(outfile, level) outfile.write('Value=[\n') level += 1 for Value in self.Value: showIndent(outfile, level) outfile.write('Value(\n') Value.exportLiteral(outfile, level) showIndent(outfile, level) outfile.write('),\n') level -= 1 showIndent(outfile, level) outfile.write('],\n') def build(self, node_): attrs = node_.attributes self.buildAttributes(attrs) for child_ in node_.childNodes: nodeName_ = child_.nodeName.split(':')[-1] self.buildChildren(child_, nodeName_) def buildAttributes(self, attrs): pass def buildChildren(self, child_, nodeName_): if child_.nodeType == Node.ELEMENT_NODE and \ nodeName_ == 'Value': obj_ = Value.factory() obj_.build(child_) self.Value.append(obj_) # end class Result class Value: subclass = None superclass = None def __init__(self, isExcitationChannel=0, Name='', File=None): self.isExcitationChannel = isExcitationChannel self.Name = Name self.File = File def factory(*args_, **kwargs_): if Value.subclass: return Value.subclass(*args_, **kwargs_) else: return Value(*args_, **kwargs_) factory = staticmethod(factory) def get_Name(self): return self.Name def set_Name(self, Name): self.Name = Name def get_File(self): return self.File def set_File(self, File): self.File = File def get_isExcitationChannel(self): return self.isExcitationChannel def set_isExcitationChannel(self, isExcitationChannel): self.isExcitationChannel = isExcitationChannel def export(self, outfile, level, name_='Value'): showIndent(outfile, level) outfile.write('<%s' % (name_, )) self.exportAttributes(outfile, level, name_='Value') outfile.write('>\n') self.exportChildren(outfile, level + 1, name_) showIndent(outfile, level) outfile.write('</%s>\n' % name_) def exportAttributes(self, outfile, level, name_='Value'): outfile.write(' isExcitationChannel="%s"' % (self.get_isExcitationChannel(), )) def exportChildren(self, outfile, level, name_='Value'): showIndent(outfile, level) outfile.write('<Name>%s</Name>\n' % quote_xml(self.get_Name())) if self.get_File() != None : if self.File: self.File.export(outfile, level, name_='File') def exportLiteral(self, outfile, level, name_='Value'): level += 1 self.exportLiteralAttributes(outfile, level, name_) self.exportLiteralChildren(outfile, level, name_) def exportLiteralAttributes(self, outfile, level, name_): showIndent(outfile, level) outfile.write('isExcitationChannel = "%s",\n' % (self.get_isExcitationChannel(),)) def exportLiteralChildren(self, outfile, level, name_): showIndent(outfile, level) outfile.write('Name=%s,\n' % quote_python(self.get_Name())) if self.File: showIndent(outfile, level) outfile.write('File=file(\n') self.File.exportLiteral(outfile, level, name_='File') showIndent(outfile, level) outfile.write('),\n') def build(self, node_): attrs = node_.attributes self.buildAttributes(attrs) for child_ in node_.childNodes: nodeName_ = child_.nodeName.split(':')[-1] self.buildChildren(child_, nodeName_) def buildAttributes(self, attrs): if attrs.get('isExcitationChannel'): if attrs.get('isExcitationChannel').value in ('true', '1'): self.isExcitationChannel = 1 elif attrs.get('isExcitationChannel').value in ('false', '0'): self.isExcitationChannel = 0 else: raise ValueError('Bad boolean attribute (isExcitationChannel)') def buildChildren(self, child_, nodeName_): if child_.nodeType == Node.ELEMENT_NODE and \ nodeName_ == 'Name': Name_ = '' for text__content_ in child_.childNodes: Name_ += text__content_.nodeValue self.Name = Name_ elif child_.nodeType == Node.ELEMENT_NODE and \ nodeName_ == 'File': obj_ = file.factory() obj_.build(child_) self.set_File(obj_) # end class Value class file: subclass = None superclass = None def __init__(self, delimiter='', typexx=None, Name=''): self.delimiter = delimiter self.typexx = typexx self.Name = Name def factory(*args_, **kwargs_): if file.subclass: return file.subclass(*args_, **kwargs_) else: return file(*args_, **kwargs_) factory = staticmethod(factory) def get_Name(self): return self.Name def set_Name(self, Name): self.Name = Name def get_delimiter(self): return self.delimiter def set_delimiter(self, delimiter): self.delimiter = delimiter def get_type(self): return self.typexx def set_type(self, typexx): self.typexx = typexx def export(self, outfile, level, name_='file'): showIndent(outfile, level) outfile.write('<%s' % (name_, )) self.exportAttributes(outfile, level, name_='file') outfile.write('>\n') self.exportChildren(outfile, level + 1, name_) showIndent(outfile, level) outfile.write('</%s>\n' % name_) def exportAttributes(self, outfile, level, name_='file'): if self.get_delimiter() is not None: outfile.write(' delimiter="%s"' % (self.get_delimiter(), )) outfile.write(' type="%s"' % (self.get_type(), )) def exportChildren(self, outfile, level, name_='file'): showIndent(outfile, level) outfile.write('<Name>%s</Name>\n' % quote_xml(self.get_Name())) def exportLiteral(self, outfile, level, name_='file'): level += 1 self.exportLiteralAttributes(outfile, level, name_) self.exportLiteralChildren(outfile, level, name_) def exportLiteralAttributes(self, outfile, level, name_): showIndent(outfile, level) outfile.write('delimiter = "%s",\n' % (self.get_delimiter(),)) showIndent(outfile, level) outfile.write('typexx = "%s",\n' % (self.get_type(),)) def exportLiteralChildren(self, outfile, level, name_): showIndent(outfile, level) outfile.write('Name=%s,\n' % quote_python(self.get_Name())) def build(self, node_): attrs = node_.attributes self.buildAttributes(attrs) for child_ in node_.childNodes: nodeName_ = child_.nodeName.split(':')[-1] self.buildChildren(child_, nodeName_) def buildAttributes(self, attrs): if attrs.get('delimiter'): self.delimiter = attrs.get('delimiter').value if attrs.get('type'): self.typexx = attrs.get('type').value def buildChildren(self, child_, nodeName_): if child_.nodeType == Node.ELEMENT_NODE and \ nodeName_ == 'Name': Name_ = '' for text__content_ in child_.childNodes: Name_ += text__content_.nodeValue self.Name = Name_ # end class file from xml.sax import handler, make_parser class SaxStackElement: def __init__(self, name='', obj=None): self.name = name self.obj = obj self.content = '' # # SAX handler # class Sax_MessageHandler(handler.ContentHandler): def __init__(self): self.stack = [] self.root = None def getRoot(self): return self.root def setDocumentLocator(self, locator): self.locator = locator def showError(self, msg): print '*** (showError):', msg sys.exit(-1) def startElement(self, name, attrs): done = 0 if name == 'Message': obj = Message.factory() stackObj = SaxStackElement('Message', obj) self.stack.append(stackObj) done = 1 elif name == 'Action': obj = Action.factory() stackObj = SaxStackElement('Action', obj) self.stack.append(stackObj) done = 1 elif name == 'GetNames': stackObj = SaxStackElement('GetNames', None) self.stack.append(stackObj) done = 1 elif name == 'StopExcitations': stackObj = SaxStackElement('StopExcitations', None) self.stack.append(stackObj) done = 1 elif name == 'Characterize': obj = Characterize.factory() val = attrs.get('ExtraDelayInSeconds', None) if val is not None: try: obj.set_ExtraDelayInSeconds(float(val)) except: self.reportError('"ExtraDelayInSeconds" attribute must be float') stackObj = SaxStackElement('Characterize', obj) self.stack.append(stackObj) done = 1 elif name == 'Excitation': obj = Excitation.factory() val = attrs.get('divider', None) if val is not None: obj.set_divider(val) val = attrs.get('iterations', None) if val is not None: obj.set_iterations(val) stackObj = SaxStackElement('Excitation', obj) self.stack.append(stackObj) done = 1 elif name == 'Name': stackObj = SaxStackElement('Name', None) self.stack.append(stackObj) done = 1 elif name == 'File': obj = file.factory() stackObj = SaxStackElement('File', obj) self.stack.append(stackObj) done = 1 elif name == 'Response': stackObj = SaxStackElement('Response', None) self.stack.append(stackObj) done = 1 elif name == 'Result': obj = Result.factory() stackObj = SaxStackElement('Result', obj) self.stack.append(stackObj) done = 1 elif name == 'Value': obj = Value.factory() val = attrs.get('isExcitationChannel', None) if val is not None: if val in ('true', '1'): obj.set_isExcitationChannel(1) elif val in ('false', '0'): obj.set_isExcitationChannel(0) else: self.reportError('"isExcitationChannel" attribute must be boolean ("true", "1", "false", "0")') stackObj = SaxStackElement('Value', obj) self.stack.append(stackObj) done = 1 if not done: self.reportError('"%s" element not allowed here.' % name) def endElement(self, name): done = 0 if name == 'Message': if len(self.stack) == 1: self.root = self.stack[-1].obj self.stack.pop() done = 1 elif name == 'Action': if len(self.stack) >= 2: self.stack[-2].obj.set_Action(self.stack[-1].obj) self.stack.pop() done = 1 elif name == 'GetNames': if len(self.stack) >= 2: content = self.stack[-1].content if content and content in ('true', '1'): content = 1 else: content = 0 self.stack[-2].obj.set_GetNames(content) self.stack.pop() done = 1 elif name == 'StopExcitations': if len(self.stack) >= 2: content = self.stack[-1].content if content and content in ('true', '1'): content = 1 else: content = 0 self.stack[-2].obj.set_StopExcitations(content) self.stack.pop() done = 1 elif name == 'Characterize': if len(self.stack) >= 2: self.stack[-2].obj.set_Characterize(self.stack[-1].obj) self.stack.pop() done = 1 elif name == 'Excitation': if len(self.stack) >= 2: self.stack[-2].obj.add_Excitation(self.stack[-1].obj) self.stack.pop() done = 1 elif name == 'Name': if len(self.stack) >= 2: content = self.stack[-1].content self.stack[-2].obj.set_Name(content) self.stack.pop() done = 1 elif name == 'File': if len(self.stack) >= 2: self.stack[-2].obj.set_File(self.stack[-1].obj) self.stack.pop() done = 1 elif name == 'Response': if len(self.stack) >= 2: content = self.stack[-1].content self.stack[-2].obj.add_Response(content) self.stack.pop() done = 1 elif name == 'Result': if len(self.stack) >= 2: self.stack[-2].obj.set_Result(self.stack[-1].obj) self.stack.pop() done = 1 elif name == 'Value': if len(self.stack) >= 2: self.stack[-2].obj.add_Value(self.stack[-1].obj) self.stack.pop() done = 1 if not done: self.reportError('"%s" element not allowed here.' % name) def characters(self, chrs, start, end): if len(self.stack) > 0: self.stack[-1].content += chrs[start:end] def reportError(self, mesg): locator = self.locator sys.stderr.write('Doc: %s Line: %d Column: %d\n' % \ (locator.getSystemId(), locator.getLineNumber(), locator.getColumnNumber() + 1)) sys.stderr.write(mesg) sys.stderr.write('\n') sys.exit(-1) #raise RuntimeError USAGE_TEXT = """ Usage: python <Parser>.py [ -s ] <in_xml_file> Options: -s Use the SAX parser, not the minidom parser. """ def usage(): print USAGE_TEXT sys.exit(-1) # # SAX handler used to determine the top level element. # class SaxSelectorHandler(handler.ContentHandler): def __init__(self): self.topElementName = None def getTopElementName(self): return self.topElementName def startElement(self, name, attrs): self.topElementName = name raise StopIteration def parseSelect(inFileName): infile = file(inFileName, 'r') topElementName = None parser = make_parser() documentHandler = SaxSelectorHandler() parser.setContentHandler(documentHandler) try: try: parser.parse(infile) except StopIteration: topElementName = documentHandler.getTopElementName() if topElementName is None: raise RuntimeError, 'no top level element' topElementName = topElementName.replace('-', '_').replace(':', '_') if topElementName not in globals(): raise RuntimeError, 'no class for top element: %s' % topElementName topElement = globals()[topElementName] infile.seek(0) doc = minidom.parse(infile) finally: infile.close() rootNode = doc.childNodes[0] rootObj = topElement.factory() rootObj.build(rootNode) # Enable Python to collect the space used by the DOM. doc = None sys.stdout.write('<?xml version="1.0" ?>\n') rootObj.export(sys.stdout, 0) return rootObj def saxParse(inFileName): parser = make_parser() documentHandler = Sax_MessageHandler() parser.setDocumentHandler(documentHandler) parser.parse('file:%s' % inFileName) root = documentHandler.getRoot() sys.stdout.write('<?xml version="1.0" ?>\n') root.export(sys.stdout, 0) return root def saxParseString(inString): parser = make_parser() documentHandler = Sax_MessageHandler() parser.setDocumentHandler(documentHandler) parser.feed(inString) parser.close() rootObj = documentHandler.getRoot() #sys.stdout.write('<?xml version="1.0" ?>\n') #rootObj.export(sys.stdout, 0) return rootObj def parse(inFileName): doc = minidom.parse(inFileName) rootNode = doc.documentElement rootObj = Message.factory() rootObj.build(rootNode) # Enable Python to collect the space used by the DOM. doc = None sys.stdout.write('<?xml version="1.0" ?>\n') rootObj.export(sys.stdout, 0, name_="Message") return rootObj def parseString(inString): doc = minidom.parseString(inString) rootNode = doc.documentElement rootObj = Message.factory() rootObj.build(rootNode) # Enable Python to collect the space used by the DOM. doc = None sys.stdout.write('<?xml version="1.0" ?>\n') rootObj.export(sys.stdout, 0, name_="Message") return rootObj def parseLiteral(inFileName): doc = minidom.parse(inFileName) rootNode = doc.documentElement rootObj = Message.factory() rootObj.build(rootNode) # Enable Python to collect the space used by the DOM. doc = None sys.stdout.write('from sysidMessage import *\n\n') sys.stdout.write('rootObj = Message(\n') rootObj.exportLiteral(sys.stdout, 0, name_="Message") sys.stdout.write(')\n') return rootObj def main(): args = sys.argv[1:] if len(args) == 2 and args[0] == '-s': saxParse(args[1]) elif len(args) == 1: parse(args[0]) else: usage() if __name__ == '__main__': main() #import pdb #pdb.run('main()')
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <xsd:annotation> <xsd:documentation xml:lang="en"> First cut at making the schema for system identification messaging. </xsd:documentation> </xsd:annotation> <xsd:element name="Message"> <xsd:complexType> <xsd:sequence> <xsd:choice minOccurs="1" maxOccurs="1"> <xsd:element name="Action"> <xsd:complexType> <xsd:sequence> <xsd:element name="GetNames" type="xsd:boolean" minOccurs="0" /> <xsd:element name="StopExcitations" type="xsd:boolean" minOccurs="0" /> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:element name="Characterize"> <xsd:complexType> <xsd:sequence> <xsd:element name="Excitation" minOccurs="1" maxOccurs="unbounded"> <xsd:complexType> <xsd:sequence> <xsd:element name="Name" type="xsd:normalizedString" /> <xsd:element name="File" type="file" /> </xsd:sequence> <xsd:attribute name="divider" type="xsd:unsignedInt" default="1" /> <xsd:attribute name="iterations" type="xsd:unsignedInt" default="1" /> </xsd:complexType> </xsd:element> <xsd:element name="Response" type="xsd:normalizedString" minOccurs="1" maxOccurs="unbounded" /> </xsd:sequence> <!-- Because RTC is not synchronous we need to allow the user to --> <!-- define some added delay after the excitations are complete to --> <!-- all of the telemetry come in. Good luck to the user!! --> <!-- Any value less than 0 means no delay --> <xsd:attribute name="ExtraDelayInSeconds" type="xsd:decimal" default="-1.0" /> </xsd:complexType> </xsd:element> <xsd:element name="Result"> <xsd:complexType> <xsd:sequence> <xsd:element name="Value" minOccurs="1" maxOccurs="unbounded"> <xsd:complexType> <xsd:sequence> <xsd:element name="Name" type="xsd:normalizedString" /> <xsd:element name="File" type="file" minOccurs="0" /> </xsd:sequence> <xsd:attribute name="isExcitationChannel" type="xsd:boolean" use="required" /> </xsd:complexType> </xsd:element> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:choice> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:complexType name="file"> <xsd:sequence> <xsd:element name="Name" type="xsd:normalizedString" /> </xsd:sequence> <xsd:attribute name="type" type="file_type_enum" use="required" /> <!-- csv files are to use the following 'delimiter' --> <xsd:attribute name="delimiter" type="xsd:normalizedString" default=" " /> </xsd:complexType> <xsd:simpleType name="file_type_enum"> <xsd:restriction base="xsd:normalizedString"> <xsd:enumeration value="csv" /> <!-- for 1-D arrays --> <xsd:enumeration value="fits" /> <!-- for N-D arrays like images --> </xsd:restriction> </xsd:simpleType> </xsd:schema>
------------------------------------------------------------------------- Check out the new SourceForge.net Marketplace. It's the best place to buy or sell services for just about anything Open Source. http://sourceforge.net/services/buy/index.php
_______________________________________________ generateds-users mailing list generateds-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/generateds-users