static XsltInvoker With(Uri stylesheetUri, IXsltProcessor processor, Assembly callingAssembly) { if (stylesheetUri == null) { throw new ArgumentNullException("stylesheetUri"); } var resolver = new XmlDynamicResolver(callingAssembly); if (!stylesheetUri.IsAbsoluteUri) { stylesheetUri = resolver.ResolveUri(null, stylesheetUri.OriginalString); } if (processor == null) { processor = Processors.Xslt.DefaultProcessor; } ConcurrentDictionary <Uri, XsltExecutable> cache = uriCache.GetOrAdd(processor, p => new ConcurrentDictionary <Uri, XsltExecutable>()); XsltExecutable executable = cache.GetOrAdd(stylesheetUri, u => { using (var stylesheetSource = (Stream)resolver.GetEntity(stylesheetUri, null, typeof(Stream))) { return(processor.Compile(stylesheetSource, new XsltCompileOptions { BaseUri = stylesheetUri, XmlResolver = resolver })); } }); return(new XsltInvoker(executable, callingAssembly)); }
internal static XsltInvoker With(IXPathNavigable stylesheet, IXsltProcessor processor, Assembly callingAssembly, out int hashCode) { if (stylesheet == null) { throw new ArgumentNullException("stylesheet"); } if (processor == null) { processor = Processors.Xslt.DefaultProcessor; } hashCode = XPathNavigatorEqualityComparer.Instance.GetHashCode(stylesheet.CreateNavigator()); ConcurrentDictionary <int, XsltExecutable> cache = inlineCache.GetOrAdd(processor, p => new ConcurrentDictionary <int, XsltExecutable>()); XsltExecutable exec = cache.GetOrAdd(hashCode, i => { var resolver = new XmlDynamicResolver(callingAssembly); return(processor.Compile(stylesheet, new XsltCompileOptions { XmlResolver = resolver })); }); return(new XsltInvoker(exec, callingAssembly)); }
public static SchematronValidator CreateSchematronValidator(this IXsltProcessor processor, IXPathNavigable schemaDoc) { if (processor == null) { throw new ArgumentNullException("processor"); } if (schemaDoc == null) { throw new ArgumentNullException("schemaDoc"); } IXPathNavigable stylesheetDoc = processor.ItemFactory.BuildNode(); XmlWriter builder = stylesheetDoc.CreateNavigator().AppendChild(); processor.BuildSchematronValidatorStylesheet(schemaDoc, builder); builder.Close(); var compileOptions = new XsltCompileOptions(); XPathNavigator schemaNav = schemaDoc.CreateNavigator(); if (!String.IsNullOrEmpty(schemaNav.BaseURI)) { compileOptions.BaseUri = new Uri(schemaNav.BaseURI); } return(new XsltSchematronValidator(processor.Compile(stylesheetDoc, compileOptions))); }
public static decimal GetXsltVersion(this IXsltProcessor processor) { return(versions.GetOrAdd(processor, p => { string stylesheet = @"<xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'> <xsl:output method='text'/> <xsl:template match='/' name='main'> <xsl:value-of select=""system-property('xsl:version')""/> </xsl:template> </xsl:stylesheet>"; using (var writer = new StringWriter(CultureInfo.InvariantCulture)) { processor .Compile(new StringReader(stylesheet), new XsltCompileOptions()) .Run(writer, new XsltRuntimeOptions { InitialTemplate = new XmlQualifiedName("main"), Serialization = { Method = XPathSerializationMethods.Text } }); return Decimal.Parse(writer.ToString(), CultureInfo.InvariantCulture); } })); }
public override void GenerateCode(AssemblyBuilder assemblyBuilder) { base.GenerateCode(assemblyBuilder); // test compilation XsltPageParser pageParser = (XsltPageParser)this.Parser; IXsltProcessor proc = Processors.Xslt[pageParser.ProcessorName]; using (Stream source = OpenStream(pageParser.XsltVirtualPath)) { try { proc.Compile(source, new XsltCompileOptions(baseUri: pageParser.XsltPhysicalUri)); } catch (ProcessorException ex) { throw CreateCompileException(ex); } } }
static XsltInvoker With(Uri stylesheetUri, IXsltProcessor processor, Assembly callingAssembly) { if (stylesheetUri == null) throw new ArgumentNullException("stylesheetUri"); var resolver = new XmlDynamicResolver(callingAssembly); if (!stylesheetUri.IsAbsoluteUri) { stylesheetUri = resolver.ResolveUri(null, stylesheetUri.OriginalString); } if (processor == null) { processor = Processors.Xslt.DefaultProcessor; } ConcurrentDictionary<Uri, XsltExecutable> cache = uriCache.GetOrAdd(processor, p => new ConcurrentDictionary<Uri, XsltExecutable>()); XsltExecutable executable = cache.GetOrAdd(stylesheetUri, u => { using (var stylesheetSource = (Stream)resolver.GetEntity(stylesheetUri, null, typeof(Stream))) { return processor.Compile(stylesheetSource, new XsltCompileOptions { BaseUri = stylesheetUri, XmlResolver = resolver }); } }); return new XsltInvoker(executable, callingAssembly); }
internal static XsltInvoker With(IXPathNavigable stylesheet, IXsltProcessor processor, Assembly callingAssembly, out int hashCode) { if (stylesheet == null) throw new ArgumentNullException("stylesheet"); if (processor == null) { processor = Processors.Xslt.DefaultProcessor; } hashCode = XPathNavigatorEqualityComparer.Instance.GetHashCode(stylesheet.CreateNavigator()); ConcurrentDictionary<int, XsltExecutable> cache = inlineCache.GetOrAdd(processor, p => new ConcurrentDictionary<int, XsltExecutable>()); XsltExecutable exec = cache.GetOrAdd(hashCode, i => { var resolver = new XmlDynamicResolver(callingAssembly); return processor.Compile(stylesheet, new XsltCompileOptions { XmlResolver = resolver }); }); return new XsltInvoker(exec, callingAssembly); }
public static void BuildSchematronValidatorStylesheet(this IXsltProcessor processor, IXPathNavigable schemaDoc, XmlWriter output) { if (processor == null) { throw new ArgumentNullException("processor"); } if (schemaDoc == null) { throw new ArgumentNullException("schemaDoc"); } if (output == null) { throw new ArgumentNullException("output"); } XPathNavigator nav = schemaDoc.CreateNavigator(); if (nav.NodeType != XPathNodeType.Root) { throw new ArgumentException("The schema must be a document node.", "schemaDoc"); } string queryBinding = nav.GetAttribute("queryBinding", ""); decimal procXsltVersion = processor.GetXsltVersion(); string xsltVersion; if (String.IsNullOrEmpty(queryBinding)) { int maxMajorVersion = (procXsltVersion >= 3m) ? 2 : (int)Decimal.Floor(procXsltVersion); xsltVersion = "xslt" + maxMajorVersion.ToStringInvariant(); } else { string qbLower = queryBinding.ToLowerInvariant(); switch (qbLower) { case "xslt": case "xslt1": case "xpath": case "xpath1": xsltVersion = "xslt1"; break; case "xslt2": case "xpath2": if (procXsltVersion < 2) { throw new ArgumentException( "The queryBinding '{0}' is not supported by this processor. Lower the language version or use a different processor.".FormatInvariant(queryBinding), "schemaDoc" ); } xsltVersion = "xslt2"; break; default: throw new ArgumentException( "The queryBinding '{0}' is not supported. Valid values are: {1}.".FormatInvariant(queryBinding, String.Join(", ", GetQueryBindings())), "schemaDoc" ); } } Assembly assembly = Assembly.GetExecutingAssembly(); Uri baseUri = new UriBuilder { Scheme = XmlEmbeddedResourceResolver.UriSchemeClires, Host = null, Path = String.Concat(assembly.GetName().Name, "/", xsltVersion, "/") }.Uri; var compileOptions = new XsltCompileOptions(baseUri) { XmlResolver = new XmlDynamicResolver() // use calling assembly as default }; string[] stages = { "iso_dsdl_include.xsl", "iso_abstract_expand.xsl", String.Concat("iso_svrl_for_", xsltVersion, ".xsl") }; IXPathNavigable input = schemaDoc; for (int i = 0; i < stages.Length; i++) { var stageUri = new Uri(baseUri, stages[i]); using (var stageDoc = (Stream)compileOptions.XmlResolver.GetEntity(stageUri, null, typeof(Stream))) { XsltExecutable executable = processor.Compile(stageDoc, compileOptions); var runtimeOptions = new XsltRuntimeOptions { InitialContextNode = input, InputXmlResolver = compileOptions.XmlResolver }; if (i < stages.Length - 1) { // output becomes the input for the next stage input = executable.Run(runtimeOptions); } else { // last stage is output to writer executable.Run(output, runtimeOptions); } } } }