Annotation of xmlschema/XML.py, revision 1.16.2.1

1.9       ht          1: # Copyright (C) 2000 LTG -- See accompanying COPYRIGHT and COPYING files
1.14      ht          2: from PyLTXML import *
1.2       ht          3: from string import *
                      4: import sys
                      5: import types
                      6: 
                      7: class Pcdata:
                      8: 
1.3       ht          9:   def __init__(self, value, where=None):
1.2       ht         10:     self.value = value
1.3       ht         11:     self.where = where
1.2       ht         12: 
1.15      richard    13:   def __getattr__(self,name):
                     14:     # infoset-like slot aliases
                     15:     if name == 'characters':
                     16:       return self.value
                     17:     else:
                     18:       raise AttributeError, name
                     19:     
1.2       ht         20:   def printme(self, file):
1.16.2.1! richard    21:     printsomehow(file, escape(self.value))
1.2       ht         22: 
                     23: class Attribute:
1.11      ht         24:   cv=None
1.5       richard    25:   def __init__(self, name, value, spec=None, uri=None, local=None):
1.2       ht         26:     self.name = name
                     27:     self.value = value
                     28:     self.spec = None
1.7       ht         29:     self.uri=uri
                     30:     self.local=local
1.2       ht         31: 
1.15      richard    32:   def __getattr__(self,name):
                     33:     # infoset-like slot aliases
                     34:     if name == 'originalName':
                     35:       return self.name
                     36:     elif name == 'localName':
                     37:       return self.local
                     38:     elif name == 'namespaceName':
                     39:       return self.uri
                     40:     elif name == 'normalizedValue':
                     41:       return self.value
                     42:     else:
                     43:       raise AttributeError, name
                     44:     
1.2       ht         45:   def printme(self, file):
1.11      ht         46:     if self.value!=None:
                     47:       if self.cv:
                     48:         val=self.cv
                     49:       else:
1.16.2.1! richard    50:         val=escape(self.value, 1)
1.11      ht         51:         self.cv=val
1.16.2.1! richard    52:       printsomehow(file, " %s='%s'" % (self.name, val))
1.2       ht         53: 
                     54: class Element:
                     55: 
1.3       ht         56:   def __init__(self, arg1, arg2=None, arg3=None):
1.2       ht         57:     self.attrs = {}
1.5       richard    58:     self.nsattrs = {}
1.2       ht         59:     self.children = []
                     60: 
1.10      ht         61:     if somestring(type(arg1)):
1.3       ht         62:       self.fromString(arg1)
                     63:     elif type(arg1) == BitType: 
                     64:       self.fromBit(arg1, arg2, arg3)
                     65:     elif type(arg1) == ItemType:
                     66:       self.fromItem(arg1, arg2)
                     67:     elif type(arg1) == FileType:
                     68:       self.fromFile(arg1, arg2)
                     69:   
1.15      richard    70:   def __getattr__(self,name):
                     71:     # infoset-like slot aliases
                     72:     if name == 'originalName':
                     73:       return self.name
                     74:     elif name == 'localName':
                     75:       return self.local
                     76:     elif name == 'namespaceName':
                     77:       return self.uri
                     78:     elif name == 'attributes':
                     79:       return self.nsattrs
                     80:     elif name == 'chunkedChildren':
                     81:       return self.children
                     82:     else:
                     83:       raise AttributeError, name
                     84:     
1.3       ht         85:   def fromString(self, name):
                     86:     self.name = name
                     87:     self.spec = None
1.4       richard    88:     self.local = None
                     89:     self.uri = None
1.3       ht         90:     return
                     91: 
                     92:   def fromBit(self, bit, file, where):
                     93:     doctype = file.doctype
                     94:     if doctype.elementTypes.has_key(bit.label):
                     95:       self.spec = doctype.elementTypes[bit.label]
                     96:     else:
1.2       ht         97:       self.spec = None
1.3       ht         98:     self.name = bit.label
1.4       richard    99:     self.local = bit.llabel
                    100:     self.uri = bit.nsuri
1.8       richard   101:     self.nsdict = bit.item.nsdict
1.3       ht        102:     self.getAttrs(bit.item)
                    103:     self.where = where
                    104:     if bit.type == "empty":
                    105:       if where:
                    106:        self.where2 = file.where
                    107:       else:
                    108:        self.where2 = None
1.2       ht        109:       return
1.3       ht        110:     if where:
                    111:       w = file.where
                    112:     else:
                    113:       w = None
                    114:     b = GetNextBit(file)
                    115:     while b.type != "end":
                    116:       if  b.type == error:
                    117:        raise Exception, "parse error"
                    118:       if b.type == "start" or b.type == "empty":
                    119:        self.children.append(Element(b, file, w))
                    120:       elif b.type == "text":
                    121:        self.children.append(Pcdata(b.body, w))
                    122:       if where:
                    123:        w = file.where
                    124:       b = GetNextBit(file)
                    125:     self.where2 = w
                    126:     return
                    127: 
                    128:   def fromItem(self, item, doctype):
                    129:     if doctype.elementTypes.has_key(item.label):
                    130:       self.spec = doctype.elementTypes[item.label]
                    131:     else:
                    132:       self.spec = None
1.2       ht        133:     self.name = item.label
1.4       richard   134:     self.local = item.llabel
                    135:     self.uri = item.nsuri
1.16      ht        136:     self.nsdict = item.nsdict
1.3       ht        137:     self.getAttrs(item)
1.2       ht        138: 
                    139:     for child in item.data:
                    140:       if type(child) == ItemType:
1.3       ht        141:        self.children.append(Element(child, doctype))
1.2       ht        142:       else:
1.3       ht        143:        self.children.append(Pcdata(child))
                    144: 
                    145:   def fromFile(self, file, notewhere):
                    146:     if notewhere:
                    147:       w = file.where
                    148:     else:
                    149:       w = None
                    150:     b = GetNextBit(file)
1.11      ht        151:     while b:
                    152:       if b.type != "start" and b.type != "empty":
                    153:         if  b.type == error:
                    154:           raise Exception, "parse error"
                    155:       else:
                    156:         if notewhere:
                    157:           w = file.where
                    158:         self.fromBit(b, file, w)
                    159:         return
1.3       ht        160:       b = GetNextBit(file)
1.11      ht        161:     # if we fall through the file was broken
                    162:     raise Exception, "I/O error/empty file"
1.3       ht        163: 
                    164:   def getAttrs(self, item):
1.5       richard   165:     atts = ItemActualAttributesNS(item)
                    166:     for (name,value,uri,local) in atts:
1.6       ht        167:       if self.spec and self.spec.attrDefns.has_key(name):
1.5       richard   168:         a = Attribute(name, value, self.spec.attrDefns[name], uri, local)
                    169:       else:
                    170:         a = self.attrs[name] = Attribute(name, value, None, uri, local)
                    171:       self.attrs[name] = a
                    172:       self.nsattrs[(uri,local)] = a
                    173:       
1.11      ht        174:   def printme(self, file, notElementOnly=0):
1.16.2.1! richard   175:     printsomehow(file, "<%s" % self.name)
1.13      ht        176:     ans=self.attrs.keys()
                    177:     ans.sort()
                    178:     for an in ans:
                    179:       self.attrs[an].printme(file)
1.11      ht        180:     if not self.children:
1.16.2.1! richard   181:       printsomehow(file,"/>")
1.11      ht        182:       return
1.16.2.1! richard   183:     printsomehow(file, ">")
1.11      ht        184:     eo=1
                    185:     if notElementOnly:
                    186:       eo=0
                    187:     else:
                    188:       for child in self.children:
                    189:         if isinstance(child,Pcdata):
                    190:           eo=0
                    191:           break
                    192:     if eo:
1.16.2.1! richard   193:       printsomehow(file,"\n")
1.2       ht        194:     for child in self.children:
                    195:       child.printme(file)
1.12      ht        196:       if eo:
1.16.2.1! richard   197:         printsomehow(file,"\n")
        !           198:     printsomehow(file, "</%s>" % self.name)
1.12      ht        199: 
                    200:   def addAttr(self,name,value):
                    201:     self.attrs[name]=Attribute(name,value)
1.10      ht        202: 
                    203: if types.__dict__.has_key('UnicodeType'):
                    204:   def somestring(type):
1.11      ht        205:    return type==types.StringType or type==types.UnicodeType
1.10      ht        206: else:
                    207:   def somestring(type):
                    208:     return type==types.StringType
1.5       richard   209: 
1.16.2.1! richard   210: def escape(string, isattr=0):
        !           211:   if "&" in string:
        !           212:     string=replace(string,"&","&amp;")
        !           213:   if isattr and "'" in string:
        !           214:     string=replace(string,"'","&apos;")
        !           215:   if "<" in string:
        !           216:     string=replace(string,"<","&lt;")
        !           217:   if not isattr and find(string, "]]>") >= 0:
        !           218:     string=replace(string,"]]>","]]&gt;")
        !           219:   return string
        !           220: 
        !           221: def printsomehow(file, string):
        !           222:   if isinstance(file, types.FileType):
        !           223:     file.write(string)
        !           224:   else:
        !           225:     PrintTextLiteral(file, string)

Webmaster