try
{
final WorkspaceFileResourceStore fileResourceStore = new WorkspaceFileResourceStore( targetSchemaFile );
final XmlResourceStore xmlResourceStore = new XmlResourceStore( fileResourceStore );
final XmlElement root = new XmlElement( xmlResourceStore, xmlResourceStore.getDomDocument().getDocumentElement() );
final String targetNamespace = root.getAttributeText( "targetNamespace" );
String targetNamespacePrefix = null;
for( XmlAttribute attribute : root.getAttributes() )
{
final String name = attribute.getDomNode().getName();
final String value = attribute.getText();
if( name.startsWith( "xmlns:" ) && value.equals( targetNamespace ) )
{
targetNamespacePrefix = name.substring( 6 );
}
}
final Set<String> included = new HashSet<String>();
included.add( sourceSchemaFile.getLocation().toOSString() );
while( inlineIncludes( root, sourceSchemaFile.getParent().getLocation().toOSString(), included ) );
removeComments( root );
removeAnnotations( root );
removeDefaultMinMaxOccurs( root );
if( operation.getRemoveWildcards().content() )
{
removeWildcards( root );
}
exclude( root, operation.getExclusions() );
final Map<String,XmlElement> types = new HashMap<String,XmlElement>();
final Map<String,XmlElement> elements = new HashMap<String,XmlElement>();
final Map<String,XmlElement> groups = new HashMap<String,XmlElement>();
for( XmlElement element : root.getChildElements() )
{
final String elname = element.getLocalName();
if( elname.equals( "simpleType" ) || elname.equals( "complexType" ) )
{
String tname = element.getAttributeText( "name" );
if( tname.length() > 0 )
{
if( targetNamespacePrefix != null )
{
tname = targetNamespacePrefix + ":" + tname;
}
if( types.containsKey( tname ) )
{
element.remove();
}
else
{
types.put( tname, element );
}
}
}
else if( elname.equals( "element" ) )
{
String ename = element.getAttributeText( "name" );
if( ename.length() > 0 )
{
if( targetNamespacePrefix != null )
{
ename = targetNamespacePrefix + ":" + ename;
}
if( elements.containsKey( ename ) )
{
element.remove();
}
else
{
elements.put( ename, element );
}
}
}
else if( elname.equals( "group" ) )
{
String gname = element.getAttributeText( "name" );
if( gname.length() > 0 )
{
if( targetNamespacePrefix != null )
{
gname = targetNamespacePrefix + ":" + gname;
}
if( groups.containsKey( gname ) )
{
element.remove();
}
else
{
groups.put( gname, element );
}
}
}
}
changeSchemaNamespacePrefix( root.getDomNode().getOwnerDocument(), "xsd" );
final Map<String,String> typeSubstitutions = new HashMap<String,String>();
for( TypeSubstitution sub : operation.getTypeSubstitutions() )
{
typeSubstitutions.put( sub.getBefore().content(), sub.getAfter().content() );
}
applyTypeSubstitutions( root, typeSubstitutions );
boolean keepInlining = true;
while( keepInlining )
{
keepInlining =
inlineRestriction( root )
|| inlineExtension( root )
|| inlineSequenceInSequence( root )
|| inlineSequenceInChoice( root )
|| inlineTypes( root, types, SetFactory.<String>empty() )
|| inlineElements( root, elements, SetFactory.<String>empty() )
|| inlineGroups( root, groups, SetFactory.<String>empty() )
|| removeRedundantMinMaxOccursInChoice( root );
}
for( XmlElement element : root.getChildElements() )
{
final String elname = element.getLocalName();
if( elname.equals( "simpleType" ) || elname.equals( "complexType" ) || elname.equals( "group" ) )
{
element.remove();
}
}
// If root elements are specified, remove all other top-level elements.
if( ! operation.getRootElements().empty() )
{
final SetFactory<String> rootsFactory = SetFactory.start();
operation.visit
(
"RootElements/Name",
new PropertyVisitor()
{
@Override
public boolean visit( final Value<?> property )
{
rootsFactory.add( property.text() );
return true;
}
}
);
final Set<String> roots = rootsFactory.result();
for( XmlElement element : root.getChildElements() )
{
final String elname = element.getLocalName();
if( elname.equals( "element" ) && ! roots.contains( element.getAttributeText( "name" ) ) )
{
element.remove();
}
}
}
sortChoiceContent( root );
sortElementContent( root );
if( operation.getSortSequenceContent().content() )
{
sortSequenceContent( root );
}
sort( root );
root.format();
xmlResourceStore.save();
}
catch( ResourceStoreException e )
{