예제 #1
0
파일: xsd2ldf.py 프로젝트: AmrEledkawy/slps
def mapXSD2LDF(stree,dtree,grammar):
 for nt in stree.findall('/*'):
  if nt.tag not in acceptedtags:
   continue
  section = ET.SubElement(dtree,'core')
  #section.set('id',nt.tag.replace(slpsns.xsd_(''),'')+'-'+nt.attrib['name'])
  # Dunno if it is a wise decision: replaced the "element-inline" naming scheme with just "inline". Probably will lead to name conflicts
  section.set('id',nt.attrib['name'])
  #el = ET.SubElement(section,'id')
  #el.text = nt.tag.replace('{'+xsdns+'}','')+'-'+nt.attrib['name']
  el = ET.SubElement(section,'title')
  el.text = nt.attrib['name']
  if nt.findall(slpsns.xsd_('annotation')):
   el = ET.SubElement(section,'synopsis')
   el = ET.SubElement(el,'content')
   #for p in nt.findall('.//{%s}annotation/{%s}documentation' % (xsdns,xsdns)):
   for p in nt.findall(slpsns.xsd_('annotation')+'/'+slpsns.xsd_('documentation')):
    pel = ET.SubElement(el,'text')
    pel.text = p.text
    # e.g. keywords
    for sub in p:
     pel.append(sub)
  gotit = []
  el = ET.SubElement(section,'syntax')
  el = ET.SubElement(el,'content')
  if nt.findall(slpsns.xsd_('choice')):
   #print 'Found a top level choice in',nt.get('name')
   for alt in nt.findall(slpsns.xsd_('choice')+'/*'):
    if alt.findall(slpsns.xsd_('annotation')+'/'+slpsns.xsd_('documentation')):
     name = alt.get('name')
     if not name:
      name = alt.get('ref').split(':')[-1]
     #print name,'got text!'
     for p in alt.findall(slpsns.xsd_('annotation')+'/'+slpsns.xsd_('documentation')):
      pel = ET.SubElement(el,'text')
      pel.text = p.text
      # e.g. keywords
      for sub in p:
       pel.append(sub)
     for prod in grammar[nt.get('name')]:
      if prod.findall('label'):
       #print 'Checking up on',prod.findtext('label'),'vs',name
       if prod.findtext('label') == name:
        #print 'Got it!'
        el.append(prod)
        gotit.append(prod)
  # Need to decide whether to put productions inside description subsections
  #section.append(grammar[nt.attrib['name']])
  if nt.attrib['name'] not in grammar.keys():
   print '[----] The definition of',nt.attrib['name'],'not found in the grammar!'
   return
  for prod in grammar[nt.attrib['name']]:
   if prod not in gotit:
    el.append(prod)
예제 #2
0
            s += '&' + ` results[key][x] `
        else:
            s += '& ---'
    if cx:
        print s + '&' + ` cx ` + '\\\\'


if __name__ == "__main__":
    if len(sys.argv) != 4:
        print 'This tool generates an overview of a bunch of XBGF scripts.'
        print 'Usage:'
        print '      xbgfover <xbgf.xsd> <lcf> <xbgfs-path>'
        sys.exit(1)
    xsd = ET.parse(sys.argv[1])
    gn = 0
    for x in xsd.findall('/' + slpsns.xsd_('group') + '/' +
                         slpsns.xsd_('choice')):
        names.append([])
        for y in x.findall(slpsns.xsd_('element')):
            if y.attrib.has_key('ref'):
                names[gn].append(y.attrib['ref'])
        gn += 1
    names.remove([])
    lcf = ET.parse(sys.argv[2])
    for x in lcf.findall('/target'):
        name = x.findtext('name')
        targets[name] = []
        for y in x.findall('branch/*/perform'):
            targets[name].append(y.text)
    path = sys.argv[3]
    if path[-1] != '/':
예제 #3
0
            s += '&' + ` results[key][x] `
        else:
            s += '& ---'
    if cx:
        print s + '&' + ` cx ` + '\\\\'


if __name__ == "__main__":
    if len(sys.argv) != 4:
        print 'This tool generates an overview of a bunch of EXBGF scripts.'
        print 'Usage:'
        print '      exbgfover <exbgf.xsd> <lcf> <exbgfs-path>'
        sys.exit(1)
    xsd = ET.parse(sys.argv[1])
    gn = 0
    for x in xsd.findall('/' + slpsns.xsd_('element') + '/' +
                         slpsns.xsd_('complexType') + '/' +
                         slpsns.xsd_('choice')):
        names.append([])
        for y in x.findall(slpsns.xsd_('element')):
            if y.attrib.has_key('ref'):
                names[gn].append(y.attrib['ref'])
                # print 'added',y.attrib['ref']
        gn += 1
    # names.remove([])
    lcf = ET.parse(sys.argv[2])
    for x in lcf.findall('/target'):
        name = x.findtext('name')
        targets[name] = []
        for y in x.findall('branch/*/perform'):
            targets[name].append(y.text)
예제 #4
0
def mapXSD2LDF(stree, dtree, grammar):
    for nt in stree.findall('/*'):
        if nt.tag not in acceptedtags:
            continue
        section = ET.SubElement(dtree, 'core')
        #section.set('id',nt.tag.replace(slpsns.xsd_(''),'')+'-'+nt.attrib['name'])
        # Dunno if it is a wise decision: replaced the "element-inline" naming scheme with just "inline". Probably will lead to name conflicts
        section.set('id', nt.attrib['name'])
        #el = ET.SubElement(section,'id')
        #el.text = nt.tag.replace('{'+xsdns+'}','')+'-'+nt.attrib['name']
        el = ET.SubElement(section, 'title')
        el.text = nt.attrib['name']
        if nt.findall(slpsns.xsd_('annotation')):
            el = ET.SubElement(section, 'synopsis')
            el = ET.SubElement(el, 'content')
            #for p in nt.findall('.//{%s}annotation/{%s}documentation' % (xsdns,xsdns)):
            for p in nt.findall(
                    slpsns.xsd_('annotation') + '/' +
                    slpsns.xsd_('documentation')):
                pel = ET.SubElement(el, 'text')
                pel.text = p.text
                # e.g. keywords
                for sub in p:
                    pel.append(sub)
        gotit = []
        el = ET.SubElement(section, 'syntax')
        el = ET.SubElement(el, 'content')
        if nt.findall(slpsns.xsd_('choice')):
            #print 'Found a top level choice in',nt.get('name')
            for alt in nt.findall(slpsns.xsd_('choice') + '/*'):
                if alt.findall(
                        slpsns.xsd_('annotation') + '/' +
                        slpsns.xsd_('documentation')):
                    name = alt.get('name')
                    if not name:
                        name = alt.get('ref').split(':')[-1]
                    #print name,'got text!'
                    for p in alt.findall(
                            slpsns.xsd_('annotation') + '/' +
                            slpsns.xsd_('documentation')):
                        pel = ET.SubElement(el, 'text')
                        pel.text = p.text
                        # e.g. keywords
                        for sub in p:
                            pel.append(sub)
                    for prod in grammar[nt.get('name')]:
                        if prod.findall('label'):
                            #print 'Checking up on',prod.findtext('label'),'vs',name
                            if prod.findtext('label') == name:
                                #print 'Got it!'
                                el.append(prod)
                                gotit.append(prod)
        # Need to decide whether to put productions inside description subsections
        #section.append(grammar[nt.attrib['name']])
        if nt.attrib['name'] not in grammar.keys():
            print '[----] The definition of', nt.attrib[
                'name'], 'not found in the grammar!'
            return
        for prod in grammar[nt.attrib['name']]:
            if prod not in gotit:
                el.append(prod)
예제 #5
0
def main(xsdfile, bgffile, ldffile):
    grammar = {}
    gtree = ET.parse(bgffile)
    stree = ET.parse(xsdfile)
    dtree = ET.Element('ldf:document')

    cx = 0
    for prod in gtree.findall('//' + slpsns.bgf_('production')):
        cx += 1
        nt = prod.findtext('nonterminal')
        if nt not in grammar.keys():
            grammar[nt] = []
        grammar[nt].append(prod)
    print 'Found', cx, 'productions'

    dtree.set('xmlns:ldf', slpsns.ldfns)
    dtree.set('xmlns:bgf', slpsns.bgfns)
    dtree.set('xmlns:html', slpsns.htmlns)

    section = ET.SubElement(dtree, 'titlePage')
    el = ET.SubElement(section, 'author')
    el.text = 'XSD2LDF generator'
    el = ET.SubElement(section, 'topic')
    el.text = stree.findall('/' + slpsns.xsd_('annotation') + '/' +
                            slpsns.xsd_('documentation'))[0].text
    el = ET.SubElement(section, 'version')
    el.text = '1.0'
    el = ET.SubElement(section, 'status')
    el.text = 'unknown'
    el = ET.SubElement(section, 'date')
    # generate!!!
    el.text = '2008-08-01'

    section = ET.SubElement(dtree, 'frontMatter')
    el = ET.SubElement(section, 'foreword')
    el = ET.SubElement(el, 'content')
    for p in stree.findall('/' + slpsns.xsd_('annotation') + '/' +
                           slpsns.xsd_('documentation'))[1:]:
        pel = ET.SubElement(el, 'text')
        pel.text = p.text
    if not el.findall('*'):
        ET.SubElement(el, 'empty')

    if stree.findall('/' + slpsns.xsd_('import')):
        el = ET.SubElement(section, 'normativeReferences')
        el = ET.SubElement(el, 'content')
        el = ET.SubElement(el, 'list')
        part = ET.SubElement(dtree, 'part')
        for p in stree.findall('/' + slpsns.xsd_('import')):
            pel = ET.SubElement(el, 'item')
            pel.text = p.attrib['schemaLocation']
            istree = ET.parse('/'.join(xsdfile.split('/')[:-1]) + '/' +
                              p.attrib['schemaLocation'])
            mapXSD2LDF(istree, part, grammar)
        print len(stree.findall(
            '/' + slpsns.xsd_('import'))), 'external schema(ta) imported.'

    if dtree.findall('part'):
        part = dtree.findall('part')[0]
    else:
        part = ET.SubElement(dtree, 'part')
    mapXSD2LDF(stree, part, grammar)
    # bit of normalisation
    fullTree = ET.ElementTree(dtree)
    for e in fullTree.findall('//content'):
        if not len(e):
            print '[++++] Empty content made explicit.'
            ET.SubElement(e, 'empty')
    # serialise!
    fullTree.write(ldffile)
    return
예제 #6
0
#!/usr/bin/python
import sys
import slpsns
import elementtree.ElementTree as ET

acceptedtags = (slpsns.xsd_('complexType'), slpsns.xsd_('element'),
                slpsns.xsd_('simpleType'), slpsns.xsd_('group'))


def mapXSD2LDF(stree, dtree, grammar):
    for nt in stree.findall('/*'):
        if nt.tag not in acceptedtags:
            continue
        section = ET.SubElement(dtree, 'core')
        #section.set('id',nt.tag.replace(slpsns.xsd_(''),'')+'-'+nt.attrib['name'])
        # Dunno if it is a wise decision: replaced the "element-inline" naming scheme with just "inline". Probably will lead to name conflicts
        section.set('id', nt.attrib['name'])
        #el = ET.SubElement(section,'id')
        #el.text = nt.tag.replace('{'+xsdns+'}','')+'-'+nt.attrib['name']
        el = ET.SubElement(section, 'title')
        el.text = nt.attrib['name']
        if nt.findall(slpsns.xsd_('annotation')):
            el = ET.SubElement(section, 'synopsis')
            el = ET.SubElement(el, 'content')
            #for p in nt.findall('.//{%s}annotation/{%s}documentation' % (xsdns,xsdns)):
            for p in nt.findall(
                    slpsns.xsd_('annotation') + '/' +
                    slpsns.xsd_('documentation')):
                pel = ET.SubElement(el, 'text')
                pel.text = p.text
                # e.g. keywords