Annotation of xmlschema/indNorm.py, revision 1.6

1.1       ht          1: # Build class instances from an individual normal form XML document
1.6     ! ht          2: # $Id: indNorm.py,v 1.5 2002/06/15 19:01:40 ht Exp $
1.1       ht          3: 
                      4: import PyLTXML
                      5: import types
                      6: 
                      7: NilVal=(None,)
                      8: 
                      9: def fromIndFile(filename,constructInd):
                     10:   flags=(PyLTXML.NSL_read|PyLTXML.NSL_read_namespaces)
                     11:   try:
                     12:     file=PyLTXML.Open(filename,None,flags)
                     13:   except:
                     14:     return
                     15:   root=buildInd(file,constructInd)
                     16:   if root and file.seenValidityError:
                     17:     root=None
                     18:   PyLTXML.Close(file)
                     19:   return root
                     20: 
                     21: eaTab={}
                     22: 
                     23: def eltAttrs(lab,file):
                     24:   # we don't actually use this for the time being
                     25:   try:
                     26:     attrs=eaTab[lab]
                     27:   except:
                     28:     ets=file.elementTypes[lab]
                     29:     if ets is None:
                     30:       eaTab[lab]=None
                     31:       return
                     32:     attrs=ets.attrDefns.keys()
                     33:     eaTab[lab]=attrs
                     34:   return attrs
                     35: 
                     36: def buildInd(file,constructInd,b=None):
                     37:   if not b:
                     38:     b = PyLTXML.GetNextBit(file)
                     39:   while b:
                     40:     if b.type == "start" or b.type == "empty":
                     41:       inst=constructInd(b.llabel,b.item)
                     42:       # is this too XMLSchema-specific?  Note we can't handle this in
                     43:       # Component.rebuild itself, as there are circular references
1.4       ht         44: #      print ('ni',inst,inst.reffed)
1.1       ht         45:       if inst is not None and inst.reffed:
                     46:         inst.reffed=0
                     47:         rebuild=0
                     48:       else:
                     49:         rebuild=1
                     50:       if b.type=='empty' and PyLTXML.GetAttrVal(b.item,'i:nil')=='true':
                     51:         pass
                     52:       elif inst is not None:
                     53:         buildRels(file,inst,constructInd,b.item,b.type=='empty')
                     54:         # buildRels consumes our end tag if non-empty
                     55:       else:
                     56:         if b.type=="start":
                     57:           # skip this, assume non-recursive
                     58:           lab=b.label
                     59:           b=PyLTXML.GetNextBit(file)
                     60:           while b is not None and (b.type!="end" or b.label!=lab):
                     61:             b=PyLTXML.GetNextBit(file)
                     62:       if rebuild and (inst is not None) and inst.rebuild:
                     63:         inst.rebuild()
                     64:       return inst
                     65:     elif  b.type == "bad":
                     66:       raise Exception, "parse error"
                     67:     elif b.type=="end":
                     68:       shouldnt('bend')
                     69:     b = PyLTXML.GetNextBit(file)
                     70:   # shouldn't be possible to fall out without errror
                     71:   raise Exception, "oops, ran off end of XML file"
                     72: 
                     73: def buildRels(file,ind,constructInd,item,noContent=0):
                     74:   b=None
                     75:   readEnd=noContent
1.2       ht         76:   # skip=1 means we've found a daughter ahead of time, spin through reflMap
                     77:   #  until we hit it
                     78:   # skip=2 means we've hit our parent's end, spin through reflMap until the end
                     79:   if noContent:
                     80:     skip=2
                     81:   else:
                     82:     skip=0
1.4       ht         83: #  print ('br',ind,item.llabel)
1.5       ht         84:   for (xName,tt,opt,pAttr) in ind.reflectionInMap:
1.1       ht         85:     if tt in ('string','boolean','list','aspecial'):
1.6     ! ht         86: #      print ('which',pAttr,(tt=='aspecial' and xName) or pAttr,
        !            87: #               PyLTXML.GetAttrVal(item,xName))
1.4       ht         88:       setattr(ind,(tt=='aspecial' and xName) or pAttr,
                     89:               PyLTXML.GetAttrVal(item,xName))
1.1       ht         90:       if skip:
                     91:         continue
                     92:     else:
                     93:       if tt=='components' or tt=='esspecial':
                     94:         lv=[]
                     95:       else:
                     96:         lv=None
1.4       ht         97:       if tt=='esspecial' or tt=='especial':
                     98:         rAttr=xName[0]
                     99:       else:
                    100:         rAttr=pAttr
1.2       ht        101:       if not skip:
1.1       ht        102:         b = PyLTXML.GetNextBit(file)
                    103:         if b is None:
                    104:           shouldnt('nobit')
                    105:       while b:
1.2       ht        106:         if skip==2 or (skip==0 and b.type == "end"):
1.1       ht        107:           readEnd=1
                    108:           # this is just this overhead ind's end
                    109:           if lv is None and not opt:
1.3       ht        110:             shouldnt ('fell off: %s, %s, %s'%(b.llabel,xName,pAttr))
1.1       ht        111:           else:
1.4       ht        112:             setattr(ind,rAttr,lv)
1.2       ht        113:             skip=2
1.1       ht        114:             break
                    115:         elif skip==1 or b.type == "start" or b.type == "empty":
                    116:           if not skip:
                    117:             rval=buildInd(file,constructInd,b)
                    118:           if b.llabel not in xName:
                    119:             if lv is None and not opt:
1.3       ht        120:               shouldnt('missing: %s, %s, %s'%(b.llabel,xName,pAttr))
1.4       ht        121:             setattr(ind,rAttr,lv)
1.2       ht        122:             skip=1
1.1       ht        123:             break
                    124:           elif lv is not None:
                    125:             lv.append(rval)
1.2       ht        126:             skip=0
1.1       ht        127:           else:
1.4       ht        128:             setattr(ind,rAttr,rval)
1.2       ht        129:             skip=0
1.1       ht        130:             break
                    131:         elif  b.type == "bad":
                    132:           raise Exception, "parse error"
                    133:         # note we ignore pis, comments, text -- text is whitespace or broken
                    134:         b=PyLTXML.GetNextBit(file)
1.2       ht        135:       if not skip:
                    136:         b=PyLTXML.GetNextBit(file)
1.1       ht        137:   if not readEnd:
                    138:     # do we ever need to look at the existing b here?
                    139:     b=PyLTXML.GetNextBit(file)
                    140:     while b:
                    141:       if b.type=="end":
                    142:         if b.llabel!=item.llabel:
                    143:           shouldnt('unsync: %s,%s'%(item.llabel,b.llabel))
                    144:         return
                    145:       elif b.type=="start" or b.type=="empty":
                    146:         shouldnt('unsync2: %s,%s'%(item.llabel,b.llabel))
                    147:       elif b.type=="bad":
                    148:         raise Exception, "parse error"
                    149:       b=PyLTXML.GetNextBit(file)
                    150:     shouldnt('eof???')
                    151: 
                    152: def av(item,name):
                    153:   return PyLTXML.GetAttrVal(item,name)
                    154: 
                    155: def shouldnt(msg):
                    156:   error("Shouldn't happen "+msg)
                    157: 
                    158: def error(msg):
                    159:   raise UnmarshallError,msg
                    160: 
                    161: class UnmarshallError(Exception):
                    162:   pass

Webmaster