private Stylesheet LoadStylesheet(XmlReader reader, bool include) {
string baseUri = reader.BaseURI;
Debug.Assert(!documentUriInUse.Contains(baseUri), "Circular references must be checked while processing xsl:include and xsl:import");
documentUriInUse.Add(baseUri, null);
Stylesheet prevStylesheet = curStylesheet;
XsltInput prevInput = input;
Stylesheet thisStylesheet = include ? curStylesheet : compiler.CreateStylesheet();
input = new XsltInput(reader, compiler);
curStylesheet = thisStylesheet;
try {
LoadDocument();
if (!include) {
compiler.MergeWithStylesheet(curStylesheet);
List<Uri> importHrefs = curStylesheet.ImportHrefs;
curStylesheet.Imports = new Stylesheet[importHrefs.Count];
// We can't reverce imports order. Template lookup relies on it after compilation
// Imports should be compiled in the reverse order
for (int i = importHrefs.Count - 1; 0 <= i; i--) {
curStylesheet.Imports[i] = LoadStylesheet(importHrefs[i], /*include:*/false);
}
}
}
catch (XslLoadException) {
throw;
}
catch (Exception e) {
if (!XmlException.IsCatchableException(e)) {
throw;
}
XmlException ex = e as XmlException;
if (ex != null) {
SourceLineInfo lineInfo = new SourceLineInfo(input.Uri, ex.LineNumber, ex.LinePosition, ex.LineNumber, ex.LinePosition);
throw new XslLoadException(ex, lineInfo);
}
input.FixLastLineInfo();
throw new XslLoadException(e, input.BuildLineInfo());
}
finally {
documentUriInUse.Remove(baseUri);
input = prevInput;
curStylesheet = prevStylesheet;
}
return thisStylesheet;
}