# -*- coding: cp936 -*-
import os
import re
import csv
import linecache
ret = []
new = []
datas =list()
simpleExtension=0
complexExtension=0
complexRestriction=0
simpleTypeRestriction=0
unique=0
key=0
importS=0
namespaces=0
allS=0
redefine=0
substitutionGroup=0
fixed=0
abstract=0
block=0
final=0
def getListFiles(path): 
  assert os.path.isdir(path), '%s not exist.' % path 
  for root, dirs, files in os.walk(path):
    #print '%s' % (files)
    #print '%s, %s' % (root, dirs) 
    #print '%s, %s, %s' % (root, dirs, files) 
    for filespath in files: 
      #ret.append(os.path.join(root,filespath))
      if filespath.find(".xsd")!=-1:#按照文件类型过滤
        ret.append(filespath)
        #new.append(os.path.splitext(filespath)[0]+'.dtd')
  return ret
def writeFile(content,filename):
    myfile = open(filename,'a')  
    myfile.write(content)
    myfile.write('\r\n')
    myfile.close()
num = len(getListFiles('.'))
print num
for m in range(num):
  #print ret[m]
  count = len(open(ret[m]).readlines())
  simpleExtension=0
  complexExtension=0
  complexRestriction=0
  simpleRestriction=0
  unique=0
  key=0
  importS=0
  namespaces=0
  allS=0
  redefine=0
  substitutionGroup=0
  fixed=0
  abstract=0
  block=0
  final=0
  for i in range(count):
    fst_line=linecache.getline(ret[m],i)
    sec_line=linecache.getline(ret[m],i+1)
    if fst_line!="" and sec_line!="":#print fst_line #print sec_line
        if fst_line.find("<xs:simpleContent")!=-1 and sec_line.find("<xs:extension")!=-1:
          simpleExtension =simpleExtension+1
        if fst_line.find("<xs:complexContent")!=-1 and sec_line.find("<xs:extension")!=-1:
          complexExtension =complexExtension+1
        if fst_line.find("<xs:complexContent")!=-1 and sec_line.find("<xs:restriction")!=-1:
          complexRestriction =complexRestriction+1
        if fst_line.find("<xs:simpleType")!=-1 and sec_line.find("<xs:restriction")!=-1:
          simpleRestriction =simpleRestriction+1
        if fst_line.find("<xs:unique")!=-1:
          unique=unique+1
        if fst_line.find("<xs:key")!=-1:
          key=key+1
        if fst_line.find("<xs:import")!=-1:
          importS=importS+1
        if fst_line.find("xmlns:")!=-1:
          namespaces=namespaces+1
        if fst_line.find("<xs:all")!=-1:
          allS=allS+1
        if fst_line.find("<xs:redefine")!=-1:
          redefine=redefine+1
        if fst_line.find("substitutionGroup=")!=-1:
          substitutionGroup=substitutionGroup+1
        if fst_line.find("<xs:attribute")!=-1 and fst_line.find("fixed=")!=-1:
          fixed=fixed+1
        if fst_line.find("abstract=")!=-1:
          abstract=abstract+1
        if fst_line.find("block=")!=-1:
          block=block+1
        if fst_line.find("final=")!=-1:
          final=final+1
          
  datas.append([ret[m],str(simpleExtension),str(simpleRestriction),str(complexExtension),str(complexRestriction),str(abstract),str(final),str(block),str(fixed),str(substitutionGroup),str(redefine),str(allS),str(namespaces),str(importS),str(key),str(unique)])
csvfile = file('xsd.csv', 'wb')
writer = csv.writer(csvfile)
writer.writerow(['XSD', 'simpleTypeExtension','simpleTypeRestriction','complexTypeExtension','complexTypeRestriction','abstract','final','block','fixed','substitutionGroup','redefine','all','namespaces','import','key','unique'])
writer.writerows(datas) 
csvfile.close()


  
