2022-12-07 15:04:41 +03:00
#!/usr/bin/env python3
2005-04-01 19:55:45 +04:00
import sys , os
import libxml2
libxml2 . debugMemory ( 1 )
baseDir = os . path . join ( ' msxsdtest ' , ' Particles ' )
filenames = os . listdir ( baseDir )
mainXSD = str ( )
signature = str ( )
dictXSD = dict ( )
2022-12-07 15:01:39 +03:00
def gatherFiles ( ) :
2005-04-01 19:55:45 +04:00
for file in filenames :
if ( file [ - 5 ] in [ " a " , " b " , " c " ] ) and ( file [ - 3 : ] == ' xsd ' ) :
# newfilename = string.replace(filename, ' ', '_')
signature = file [ : - 5 ]
mainXSD = signature + " .xsd "
imports = [ ]
for sub in filenames :
if ( mainXSD != sub ) and ( sub [ - 3 : ] == ' xsd ' ) and sub . startswith ( signature ) :
imports . append ( sub )
if len ( imports ) != 0 :
dictXSD [ mainXSD ] = imports
def debugMsg ( text ) :
#pass
2022-12-07 15:04:41 +03:00
print ( " DEBUG: " , text )
2022-12-07 15:01:39 +03:00
def fixup ( ) :
2005-04-01 19:55:45 +04:00
for mainXSD in dictXSD :
2022-12-07 15:01:39 +03:00
debugMsg ( " fixing ' %s ' ... " % mainXSD )
schemaDoc = None
xpmainCtx = None
2005-04-01 19:55:45 +04:00
# Load the schema document.
schemaFile = os . path . join ( baseDir , mainXSD )
schemaDoc = libxml2 . parseFile ( schemaFile )
2022-12-07 15:01:39 +03:00
if ( schemaDoc is None ) :
2022-12-07 15:04:41 +03:00
print ( " ERROR: doc ' %s ' not found " % mainXSD )
2005-04-01 19:55:45 +04:00
sys . exit ( 1 )
2022-12-07 15:01:39 +03:00
try :
xpmainCtx = schemaDoc . xpathNewContext ( )
2022-12-07 15:04:41 +03:00
xpmainCtx . xpathRegisterNs ( " xs " , " http://www.w3.org/2001/XMLSchema " )
2005-04-01 19:55:45 +04:00
xpres = xpmainCtx . xpathEval ( " /xs:schema " )
if len ( xpres ) == 0 :
2022-12-07 15:04:41 +03:00
print ( " ERROR: doc ' %s ' has no <schema> element " % mainXSD )
2005-04-01 19:55:45 +04:00
sys . exit ( 1 )
2022-12-07 15:01:39 +03:00
schemaElem = xpres [ 0 ]
schemaNs = schemaElem . ns ( )
# Select all <import>s.
xpres = xpmainCtx . xpathEval ( " /xs:schema/xs:import " )
if len ( xpres ) != 0 :
for elem in xpres :
loc = elem . noNsProp ( " schemaLocation " )
if ( loc is not None ) :
debugMsg ( " imports ' %s ' " % loc )
if loc in dictXSD [ mainXSD ] :
dictXSD [ mainXSD ] . remove ( loc )
for loc in dictXSD [ mainXSD ] :
# Read out the targetNamespace.
impTargetNs = None
impFile = os . path . join ( baseDir , loc )
impDoc = libxml2 . parseFile ( impFile )
try :
2005-04-01 19:55:45 +04:00
xpimpCtx = impDoc . xpathNewContext ( )
2022-12-07 15:01:39 +03:00
try :
2005-04-01 19:55:45 +04:00
xpimpCtx . setContextDoc ( impDoc )
2022-12-07 15:04:41 +03:00
xpimpCtx . xpathRegisterNs ( " xs " , " http://www.w3.org/2001/XMLSchema " )
2022-12-07 15:01:39 +03:00
xpres = xpimpCtx . xpathEval ( " /xs:schema " )
impTargetNs = xpres [ 0 ] . noNsProp ( " targetNamespace " )
finally :
2005-04-01 19:55:45 +04:00
xpimpCtx . xpathFreeContext ( )
2022-12-07 15:01:39 +03:00
finally :
2005-04-01 19:55:45 +04:00
impDoc . freeDoc ( )
2022-12-07 15:01:39 +03:00
# Add the <import>.
debugMsg ( " adding <import namespace= ' %s ' schemaLocation= ' %s ' /> " % ( impTargetNs , loc ) )
newElem = schemaDoc . newDocNode ( schemaNs , " import " , None )
if ( impTargetNs is not None ) :
2005-04-01 19:55:45 +04:00
newElem . newProp ( " namespace " , impTargetNs )
2022-12-07 15:01:39 +03:00
newElem . newProp ( " schemaLocation " , loc )
if schemaElem . children is not None :
2005-04-01 19:55:45 +04:00
schemaElem . children . addPrevSibling ( newElem )
schemaDoc . saveFile ( schemaFile )
2022-12-07 15:01:39 +03:00
finally :
2005-04-01 19:55:45 +04:00
xpmainCtx . xpathFreeContext ( )
schemaDoc . freeDoc ( )
2022-12-07 15:01:39 +03:00
2005-04-01 19:55:45 +04:00
try :
gatherFiles ( )
fixup ( )
finally :
libxml2 . cleanupParser ( )
if libxml2 . debugMemory ( 1 ) != 0 :
2022-12-07 15:04:41 +03:00
print ( " Memory leak %d bytes " % ( libxml2 . debugMemory ( 1 ) ) )
2005-04-01 19:55:45 +04:00