Skip to content

Commit

Permalink
Initial push from SVN
Browse files Browse the repository at this point in the history
With the change from google-code we are placing several of our files
into the git. This should enable furthre collaboration.

Please note, that we remain unable to provide detailed data set as we do
not have any information that is shareable on aircraft configurations.
  • Loading branch information
Daniel Böhnke committed Apr 13, 2015
1 parent e41cab6 commit ed873b4
Show file tree
Hide file tree
Showing 77 changed files with 116,928 additions and 0 deletions.
2 changes: 2 additions & 0 deletions cleanNamespaces.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
python .\converter\convert.py
java -jar D:\Tools\Saxon\saxon9he.jar -s:.\schema\cpacs_schema.xsd -xsl:.\converter\namespace-cleanup.xsl -o:.\schema\cpacs_schema.xsd
144 changes: 144 additions & 0 deletions converter/ElementBuilder.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
""" ElementBuilder.py - construct ElementTrees with friendly syntax
(C) 2005 Oren Tirosh. Released under the terms of the MIT License.
http://www.opensource.org/licenses/mit-license.php
* Extended Element factory
Backward-compatible with the standard ElementTree Element factory with
the following extensions:
Sub-elements may be supplied as arguments:
Element('tag', {'a': '5'}, Element('othertag'))
Attribute dictionary is optional:
Element('tag', Element('othertag'))
Element text may be supplied as an argument:
Element('tag', 'some text')
Element text and sub-elements:
Element('tag', 'some text', Element('othertag'))
Element text, sub-elements and sub-element tails:
Element('tag', 'some text', Element('othertag'), 'tail')
* Namespaces
A namespace is a factory for QNames.
ns.tag == ns+'tag' == QName('http://namespace/uri', 'tag')
where:
ns = Namespace('http://namespace/uri')
Namespace(None) or LocalNamespace generates LocalName objects instead
of QNames but is otherwise similar.
A second optional argument to Namespace is prefix which will be used
when generating XML instead of automatically-generated numeric namespace
prefixes unless it collides with another defined prefix or uri.
* Callable names
QName and LocalName objects are callable, taking the same arguments as
the Element factory, except the tag argument which is implicitly set to
the QName/LocalName itself.
ns.tag(a='5') == Element(QName('http://namespace/uri', 'tag', a='5')
"""

from xml.etree import ElementTree
basefactory = ElementTree.Element
if 0:
from cElementTree import Element as basefactory

__all__ = 'Element', 'Namespace', 'LocalNamespace'


def Element(tag, attrib={}, *children, **extra):
""" Element(tag (,attrdict)? (,subelement|string)* ) -> Element """

if isinstance(attrib, dict):
attrib = attrib.copy()
else:
children = (attrib,) + children
attrib = {}
attrib.update(extra)

element = basefactory(tag, attrib)

prevelem = None
for arg in children:
if ElementTree.iselement(arg):
element.append(arg)
prevelem = arg
else:
if isinstance(arg, basestring):
if prevelem is None:
element.text = (element.text or '') + arg
else:
prevelem.tail = (prevelem.tail or '') + arg
else:
try:
it = iter(arg)
except:
raise TypeError, "argument type to Element"
for item in it:
if not ElementTree.iselement(item):
raise TypeError, "invalid argument type to Element"
element.append(item)
return element


class QName(unicode, ElementTree.QName):
""" Calling a QName creates an Element with the name as its tag """
def __new__(cls, *args):
tmp = ElementTree.QName(*args)
new = unicode.__new__(cls, tmp.text)
new.text = new
return new

# Use Python's binding of first argument as self
__call__ = Element


class LocalName(unicode):
""" Calling LocalName creates an Element with the name as its tag """
# Use Python's binding of first argument as self
__call__ = Element


class Namespace:
""" Namespace(uri [, prefix hint]) -> Namespace object """

def __init__(self, uri=None, prefix=None):
self.uri = uri or None

if prefix is None:
return
map = ElementTree._namespace_map
if uri in map or prefix in map.values():
# prefix or URI already used
return
if prefix.startswith("ns") and prefix[2:].isdigit():
# names in this form may collide with autogenerated prefixes
return
map[uri] = prefix

def __add__(self, tag):
if self.uri is None:
return LocalName(tag)
else:
return QName(self.uri, tag)

def __getattr__(self, tag):
if tag[0] == '_':
raise AttributeError(tag)
qname = self+tag
self.__dict__[tag] = qname # cache for faster access next time
return qname

LocalNamespace = Namespace(None)
228 changes: 228 additions & 0 deletions converter/convert.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,228 @@
'''
This is a small script for cleaning up CPACS files after editing them in Eclipse
It will do some basic things!
1. If the new type does not have a complexContent and the extension to
complexBaseType this will be added automagically
2. If the documentation is not according to the MAML notation for CPACS
the existing documentation will be translated into MAML notation or
default values will be applied
3. All Elements are sorted in a alphabetical order and CPACS is put on top of the list
4. Some namespace mishaps are cleaned up
5. Finally, the script will sort through all the files and look for obsolete
types. These types will not be deleted and hence attention should be paid to the output.
@author: boeh_da
'''

from xml.etree.ElementTree import Element, SubElement, parse, Comment,tostring
import ElementBuilder


def newDoc(app,doc):
'''
Creates a new xml documentation element according to MAML notation
app and doc are included in the summary and remarks section
'''
docElement = Element("xsd:annotation")

appElement = SubElement(docElement,"xsd:appinfo")
schemaDoc = SubElement(appElement,"schemaDoc")

summary = SubElement(schemaDoc,"ddue:summary")
sumpara = SubElement(summary,"ddue:para")
sumpara.text = app

remarks = SubElement(schemaDoc,"ddue:remarks")
rempara = SubElement(remarks,"ddue:para")
rempara.text = doc

return docElement

def addcomplexContent(type):
'''
If a complex type is found that has no complex Content, complex Content
will be added here. This is essential to clean up Eclipse Editor output
'''
complexContent = Element("xsd:complexContent")
extensionBase = SubElement(complexContent, "xsd:extension")
extensionBase.set('base','complexBaseType')

for node in type:
if node.tag.find('annotation')==-1:
extensionBase.insert(0,node)
type.remove(node)

for node in type:
if cmp(node.tag.find,'{http://www.w3.org/2001/XMLSchema}attribute'):
extensionBase.insert(-1,node)
type.remove(node)

type.insert(1,complexContent)
return type

def getTypeList(root):
'''
This function provides a list of all types included in CPACS
'''
typeList = []
for type in root:
if type.tag.find('complexType')!=-1:
if not type.attrib['name'].find('Base') != -1:
typeList.append(type.attrib['name'])
return typeList


if __name__ == '__main__':
# Read CPACS schema file
cpacs_file = "./schema/cpacs_schema.xsd"
tree = parse(cpacs_file)
ElementBuilder.Namespace('', 'xsd')
elem = tree.getroot()

print "1. Add complex content"
for type in elem:
if type.tag.find('complexType')!=-1:
complexContentFound = False
#Exclude stuff like stringBaseType
if not type.attrib['name'].find('Base') != -1:
#search for complex Content
for part in type:
if part.tag.find('complexContent') !=-1 and not part.tag.find('annotation')!=-1:
complexContentFound = True

if not complexContentFound:
print "adding complex Content to: %s" %type.attrib['name']
type = addcomplexContent(type)

print
print "2. Add documentation stubs"
for type in elem:
if type.tag.find('complexType')!=-1:
annotationFound = False
for part in type:
doc=""
app=""
if part.tag.find('annotation')!=-1:
annotationFound = True
newStyleDoc = False
for item in part:
if item.tag.find('appinfo')!=-1:
app = str(item.text)
for subitem in item :
if subitem.tag.find('schemaDoc') != -1:
newStyleDoc = True

if item.tag.find('documentation')!=-1:
doc = str(item.text)

if not newStyleDoc:
type.remove(part)
print ("adding documentation to: %s") %type.attrib['name']
type.insert(0,newDoc(app,doc))

if not annotationFound:
print ("adding annotation to: %s") %type.attrib['name']
type.insert(0,newDoc(type.attrib["name"],''))

print
print "3. Alphabetical sorting"
bla = list(elem)
bla.sort(key=lambda name: name.attrib["name"])


elem.clear()
for item in bla:
elem.append(item)

# CPACS as top type
for type in elem:
#type.insert(0,Comment("****************************************************\n"))
if type.tag.find('element')!=-1:
elem.remove(type)
elem.insert(0,type)

# Export file
tree.write(cpacs_file)

print
print "4. Some basic cleaning up after the export"
myCPACS = open(cpacs_file,"r")
lines = myCPACS.readlines()
myCPACS.close()

res = []
for line in lines:
# Replacing some of the mislead namespace settings.
# Please note, this part of code is probably very fragile as the sorting order of the
# namespaces may change at random
line = line.replace('xs:','xsd:')
line = line.replace('ns0','xsd')
line = line.replace('ns1:','sd:')
line = line.replace('ns2','ddue')
line = line.replace('ns3','xlink')
line = line.replace('xsd:schemaDoc','schemaDoc')
line = line.replace('xsd:summary','ddue:summary')
line = line.replace('xsd:remarks','ddue:remarks')
line = line.replace('xsd:para','ddue:para')
line = line.replace('xsd:content','ddue:content')
line = line.replace('xsd:section','ddue:section')
line = line.replace('xsd:definitionTable','ddue:definitionTable')
line = line.replace('xsd:mediaLink','ddue:mediaLink')
line = line.replace('xsd:image','ddue:image')
line = line.replace('xsd:title','ddue:title')
line = line.replace('xsd:definition','ddue:definition')
line = line.replace('xsd:definedTerm','ddue:definedTerm')
line = line.replace('xsd:entry','ddue:entry')
line = line.replace('xsd:legacyBold','ddue:legacyBold')
line = line.replace('xsd:legacyItalic','ddue:legacyItalic')
line = line.replace('xsd:table','ddue:table')
line = line.replace('xsd:row','ddue:row')
line = line.replace('xsd:code','ddue:code')
line = line.replace('xsd:href','xlink:href')
line = line.replace('xsd:list','ddue:list')
line = line.replace('xsd:listItem','ddue:listItem')



line = line.replace('<schemaDoc','<schemaDoc xmlns="http://schemas.xsddoc.codeplex.com/schemaDoc/2009/3" xmlns:ddue="http://ddue.schemas.microsoft.com/authoring/2003/5"')
res.append(line)

res[0] = '<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:ddue="http://ddue.schemas.microsoft.com/authoring/2003/5" xmlns:html="http://www.w3.org/1999/xhtml" xmlns:sd="http://schemas.xsddoc.codeplex.com/schemaDoc/2009/3" xmlns:xlink="http://www.w3.org/1999/xlink"><xsd:element name="cpacs">\n'
myCPACS = open(cpacs_file,'w')
myCPACS.writelines(res)
myCPACS.close()

print
print "5. Look for unused types in the CPACS schema definition, i.e. types that are not needed anymore"
# In the future this might become a test
original = parse(cpacs_file)
ElementBuilder.Namespace('', 'xsd')
originalTypes = getTypeList(original.getroot())

myCPACS = open(cpacs_file,"r")
lines = myCPACS.readlines()
myCPACS.close()

toomuch = []
for item in originalTypes:
didNotfind = True
for line in lines:
if line.find('type="'+str(item))!=-1:
didNotfind = False

if didNotfind:
toomuch.append(item)

if len(toomuch)>0:
print "The following types are currently unused in CPACS, please check whether these should be deleted"
for item in toomuch:
print item
else:
print "No unsused types could be identified"


Loading

0 comments on commit ed873b4

Please sign in to comment.