Ejemplo n.º 1
0
def vwarn(elt,message,schema):
  if schema.sschema.dontWarn:
    return
  ve=schema.sschema.resElt.newDaughter("warning")
  ve.newText(message)
  if elt is not None:
    addWhere(ve,elt.where)
Ejemplo n.º 2
0
def verror(elt,message,schema,code=None,two=0,daughter=None,iitem=None):
  # code argument identifies CVC
  ve=schema.sschema.resElt.newDaughter("invalid")
  ve.newText(message)
  if code:
    ve.newAttr("code",code)
  if two:
    addWhere(ve,elt.where2)
  else:
    addWhere(ve,elt.where)
  if daughter is not None:
    ve.addChild(daughter)
  schema.sschema.errors=schema.sschema.errors+1
  if iitem is None:
    iitem=elt
  if iitem.errorCode:
    iitem.errorCode.append(" "+code)
  else:
    iitem.errorCode=[code]
Ejemplo n.º 3
0
def runit(en,rns=[],k=0,timing=0,independent=0,reflect=0,
          reflect2=0,scanForLocs=0,useDTD=0,topType=None,
          topElt=None,derefNSNs=1,dw=1,control=2,baseURI=None,
          preserveRedirect=0,serialise=0):
  XMLSchemaInit()
  someBugs=0
  btlist=[]

  ss = s = None

  if ((topType is not None) or
      (topElt is not None)):
    qnp=re.compile("^\{(?P<nsName>.*?)\}(?P<localName>.*)$")
  indNorm.reflect.setup()
  if reflect==2:
    import XSV.infoset.altNorm.reflect
    XSV.infoset.altNorm.reflect.setup()
  if en is not None:
    if baseURI is not None:
      ren=urljoin(baseURI,en)
    else:
      ren=en
  else:
    ren=None
  f=SSchema(ren,not useDTD)
  f.derefNSNs=derefNSNs
  f.dontWarn=dw
  f.errors=0
  
  if baseURI is None:
    base = urlunsplit(('file','',os.getcwd().replace('\\','/')+'/','',''))
    if en is not None:
      ren=f.fileNames[0]
  else:
    base = baseURI
    if en is None:
      f.base = f.fileNames[0] = baseURI

  res=Element(None,xsvNS,"xsv")
  f.resElt=res
  res.newAttr("version",vs)
  if topType is not None:
    res.newAttr("forceDocEltType",topType)
    mres=qnp.match(topType)
    if mres is None:
      typeNsName=None
      typeLocalName=topType
    else:
      (typeNsName,typeLocalName)=mres.group('nsName','localName')
  if topElt is not None:
    res.newAttr("forceDocEltName",topElt)
    mres=qnp.match(topElt)
    if mres is None:
      eltNsName=None
      eltLocalName=topElt
    else:
      (eltNsName,eltLocalName)=mres.group('nsName','localName')
  if independent:
    res.newAttr("target","[standalone schema assessment]")
  else:
    res.newAttr("target",ren or "[stdin]")
    
  if independent:
    rns[0:0]=[ren]
  if rns and rns[0] is not None:
    res.newAttr("schemaDocs",' '.join(rns))

  rdn=tempfile.mktemp("xsverrs")
  redirect=open(rdn,"w+")
  savedstderr=os.dup(2)                        # save stderr
  os.dup2(redirect.fileno(),2)
  if independent:
    e=None
    encoding="UTF-8"
    f.docElt=None
  else:
    doc=safeReadXML(ren,res,"target",btlist)
    if doc is not None:
      e=doc.documentElement
      f.docElt=e
      encoding=doc.characterEncodingScheme
      if timing:
        os.write(savedstderr,"target read:      %6.2f\n"%(time.time()-timing))
      if reflect2:
        dumpInfoset("infoset-before",reflect2,f,0,control)
    else:
      encoding=None
      e=None
      res.newAttr('instanceAssessed',"false")
      registerRawErrors(redirect,res,preserveRedirect)
      # put stderr back
      os.dup2(savedstderr,2)
      return (res,None,btlist,f)

  if topElt is not None:
    if ((e.localName!=eltLocalName) or
        (e.namespaceName!=eltNsName)):
      res.newAttr('instanceAssessed',"false")
      registerRawErrors(redirect,res,preserveRedirect)
      ve=res.newDaughter("invalid")
      ve.newText("document element not {%s}%s as required"%(eltNsName,
                                                            eltLocalName))
      addWhere(ve,e.where)
      # put stderr back
      os.dup2(savedstderr,2)
      return (res,None,btlist,f)
      
  if rns:
    s=f.checkinSchema(0,rns[0],base,"command line",useDTD,k)
    if s is not None:
      someBugs=s.buggy or someBugs
    else:
      someBugs=1
    if timing:
      os.write(savedstderr,"schema read:      %6.2f\n"%(time.time()-timing))
    for rn in rns[1:]:
      ffr=f.checkinSchema(0,rn,base,"command line",useDTD,k)
      if ffr is not None:
        someBugs=ffr.buggy or someBugs
      else:
        someBugs=1
      if timing:
        os.write(savedstderr,"schema read:      %6.2f\n"%(time.time()-timing))
      ss=ss or ffr
  if s is None:
    if ss is not None:
      s=ss
    else:
      s = Schema(f,None)
      s.targetNS='##dummy'

  if not independent:
    someBugs=(doSchemaLocs(e,s,res,scanForLocs,
                           ren or base,timing,savedstderr,useDTD,k) or
              someBugs)
    res.newAttr('docElt',"{%s}%s"%(e.namespaceName,e.localName))
    # TODO: remove the following in favour of a general fallback for unknown
    # namespaces in instances?  No, leave it here, to stop validation
    # altogether if we don't have keepGoing
    if (derefNSNs and e.namespaceName and
        (not f.schemas.has_key(e.namespaceName)) and
        (not f.losingNamespaces.has_key(e.namespaceName))):
      nss=f.checkinSchema(e.namespaceName,e.namespaceName,
                          ren or base,"docElt",useDTD,k,e)
      if nss is not None:
        someBugs=nss.buggy or someBugs
      else:
        f.losingNamespaces[e.namespaceName]=1  
      if timing:
        os.write(savedstderr,
                 "schema read:      %6.2f\n"%(time.time()-timing))

  try:
    ecount=f.prepare()
    if timing:
      os.write(savedstderr,"complete schema prepared: %6.2f\n"%(time.time()-timing))
    if independent:
      for ss in f.schemas.values():
        ss.prepare()
      if timing:
        os.write(savedstderr,"individual namespaces prepared: %6.2f\n"%(time.time()-timing))
  except:
    ecount=-1
    btlist.append(traceback.format_exception(sys.exc_type,
                                             sys.exc_value,
                                             sys.exc_traceback))
    pfe=res.newDaughter("bug")
    pfe.newText("validator crash during complete schema preparation")

  if independent:
    kgm="false"
    kg=0
  else:
    kgm="true"
    kg=1
  if ecount or someBugs:
    if ecount<0:
      kg=0
    else:
      if not k:
        kg=0
    if not kg:
     kgm="false"
  if not kg:
    if independent or ecount<0:
      ecount=f.errors
    for sch in f.schemas.values():
      ecount=ecount+sch.errors
    res.newAttr('instanceAssessed',kgm)
    res.newAttr('schemaErrors',str(ecount))
    registerRawErrors(redirect,res,preserveRedirect)
    # put stderr back
    os.dup2(savedstderr,2)
    return (res,encoding,btlist,f)

  if s is None:
    # any one will do
    s = f.sfors
  
  f.errors=0
  if (e is not None) and (s is not None):
    ttdef=None
    if topType is not None:
      ttn=QName(None,typeLocalName,typeNsName or None)
      if s.vTypeTable.has_key(ttn):
        ttdef=s.vTypeTable[ttn]
        # TODO: this will _not_ override xsi:type on the doc elt, pbly should
      else:
        res.newAttr('instanceAssessed',"false")
        registerRawErrors(redirect,res,preserveRedirect)
        ve=res.newDaughter("invalid")
        ve.newText("no definition found for specified top-level type: {%s}%s"%(typeNsName,
                                                              typeLocalName))
        addWhere(ve,e.where)
        # put stderr back
        os.dup2(savedstderr,2)
        return (res,None,btlist,f)
    res.newAttr('instanceAssessed',kgm)
    try:
      validate(e, s, ttdef)
      e.schemaInformation=collectSchemaInfo(f,reflect)
    except:
      btlist.append(traceback.format_exception(sys.exc_type,
                                               sys.exc_value,
                                               sys.exc_traceback))
      pfe=res.newDaughter("bug")
      pfe.newText("validator crash during validation")
    res.newAttr('instanceErrors',str(s.sschema.errors))
    ec=0
    for sch in f.schemas.values():
      ec=ec+sch.errors
    
    if e.__dict__.has_key('assessedType') and e.assessedType:
      t=e.assessedType
      if t.__dict__.has_key('qname') and t.qname:
        tn=unicode(str(t.qname),'utf-8')
      elif t.name:
        tn=t.name
      else:
        tn='[Anonymous]'
      res.newAttr('rootType',tn)
      res.newAttr('validation','strict')
    else:
      res.newAttr('validation','lax')

    res.newAttr('schemaErrors',str(ec))
    registerRawErrors(redirect,res,preserveRedirect)
    # put stderr back
    os.dup2(savedstderr,2)
    return (res,encoding,btlist,f)