{
ValidateErrorHandler errorHandler = new ValidateErrorHandler();
//errorHandler.getErrorMessages().clear();
try
{
XMLGrammarPreparser grammarPreparser = new XMLGrammarPreparser();
grammarPreparser.registerPreparser(XMLGrammarDescription.XML_SCHEMA,null/*schemaLoader*/);
grammarPreparser.setFeature(Constants.XERCES_FEATURE_PREFIX + Constants.CONTINUE_AFTER_FATAL_ERROR_FEATURE, false);
grammarPreparser.setFeature(Constants.XERCES_FEATURE_PREFIX + Constants.NAMESPACES_FEATURE, true);
grammarPreparser.setFeature(Constants.XERCES_FEATURE_PREFIX + Constants.NAMESPACE_PREFIXES_FEATURE, true);
grammarPreparser.setFeature(Constants.XERCES_FEATURE_PREFIX + Constants.VALIDATION_FEATURE, true);
grammarPreparser.setFeature(Constants.XERCES_FEATURE_PREFIX + Constants.SCHEMA_VALIDATION_FEATURE, true);
grammarPreparser.setFeature(Constants.XERCES_FEATURE_PREFIX + Constants.SCHEMA_FULL_CHECKING, false);
grammarPreparser.setFeature(Constants.XERCES_FEATURE_PREFIX + Constants.EXTERNAL_GENERAL_ENTITIES_FEATURE, true);
grammarPreparser.setFeature(Constants.XERCES_FEATURE_PREFIX + Constants.EXTERNAL_PARAMETER_ENTITIES_FEATURE, true);
grammarPreparser.setFeature(Constants.XERCES_FEATURE_PREFIX + Constants.WARN_ON_DUPLICATE_ATTDEF_FEATURE, true);
try
{
grammarPreparser.setFeature(HONOUR_ALL_SCHEMA_LOCATIONS_FEATURE_ID, true);
}
catch (Exception e)
{
// Catch the exception and ignore if we can't set this feature.
}
// cs : toggle the comments on these 2 lines to easily disable caching
// grammarPreparser.setGrammarPool(new XMLGrammarPoolImpl());
grammarPreparser.setGrammarPool(grammarPool != null ? grammarPool : new XMLGrammarPoolImpl());
grammarPreparser.setErrorHandler(errorHandler);
if (entityResolver != null)
{
grammarPreparser.setEntityResolver(entityResolver);
}
try
{
XMLInputSource is = null;
List oldGrammars = null;
XMLGrammarPoolImpl pool = null;
// This allows support for the inline schema in WSDL documents.
if (inlineXSD)
{
Reader reader = new StringReader(schema);
is = new XMLInputSource(null,filelocation,filelocation,reader,null);
((InlineXSDResolver)inlineSchemaEntityResolver).addReferringSchema(is,targetNamespace);
// In the case that 'shared' grammar pool is used we need to consider
// that we might have already added a schema to the pool with the same target namespace
// as the inline schema we're hoping to construct. In this case we need to remove
// the schema from the pool before constructing our inline schema. We can add it
// back when we're all done.
pool = (XMLGrammarPoolImpl)grammarPreparser.getGrammarPool();
oldGrammars = new ArrayList();
// Remove the inline schema namespace if it is listed directly
// in the pool. If it is indirectly listed as an import of a grammar listed
// directly in the pool hide the grammar to continue to get caching benefits
// from the cached grammar.
Grammar[] grammars = pool.retrieveInitialGrammarSet(XMLGrammarDescription.XML_SCHEMA);
int numGrammars = grammars.length;
for(int i = 0; i < numGrammars; i++)
{
XMLGrammarDescription desc = grammars[i].getGrammarDescription();
if(targetNamespace.equals(desc.getNamespace()))
{
oldGrammars.add(pool.removeGrammar(desc));
}
else
{
if(grammars[i] instanceof XSGrammar)
{
XSGrammar grammar = (XSGrammar)grammars[i];
StringList namespaces = grammar.toXSModel().getNamespaces();
if(namespaces.contains(targetNamespace))
{
oldGrammars.add(pool.removeGrammar(desc));
//pool.putGrammar(new XSGrammarHider(grammar, targetNamespace));
}
}
}
}
Set inlineNSs = ((InlineXSDResolver)inlineSchemaEntityResolver).getInlineSchemaNSs();
Iterator nsiter = inlineNSs.iterator();
while(nsiter.hasNext())
{
XSDDescription desc = new XSDDescription();
desc.setNamespace((String)nsiter.next());
Grammar oldGrammar = pool.removeGrammar(desc);
if(oldGrammar != null)
oldGrammars.add(oldGrammar);
}
}
// get the input source for an external schema file
else
{
is = new XMLInputSource(null,schema,schema);
}
XSGrammar grammar = (XSGrammar)grammarPreparser.preparseGrammar(XMLGrammarDescription.XML_SCHEMA,is);
xsModel = grammar.toXSModel();
// Here we add the temporiliy removed schema back.
if (inlineXSD && oldGrammars != null)
{