Пример #1
0
        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)));
        }
Пример #2
0
        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));
        }
Пример #3
0
        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);
                }
            }));
        }
Пример #4
0
        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));
        }
Пример #5
0
 public void OnInstructionExecute(IXsltProcessor xsltProcessor)
 {
     if (_onExecute != null)
     {
         _onExecute.Invoke(_unknownDebugger, new object[] { xsltProcessor });
     }
 }
Пример #6
0
        static SchematronInvoker With(Uri schemaUri, IXsltProcessor processor, Assembly callingAssembly)
        {
            if (schemaUri == null)
            {
                throw new ArgumentNullException("schemaUri");
            }

            var resolver = new XmlDynamicResolver(callingAssembly);

            if (!schemaUri.IsAbsoluteUri)
            {
                schemaUri = resolver.ResolveUri(null, schemaUri.OriginalString);
            }

            if (processor == null)
            {
                processor = Processors.Xslt.DefaultProcessor;
            }

            ConcurrentDictionary <Uri, SchematronValidator> cache =
                uriCache.GetOrAdd(processor, p => new ConcurrentDictionary <Uri, SchematronValidator>());

            SchematronValidator validator = cache.GetOrAdd(schemaUri, u => {
                using (var schemaSource = (Stream)resolver.GetEntity(schemaUri, null, typeof(Stream))) {
                    IXPathNavigable schemaDoc = processor.ItemFactory.CreateNodeReadOnly(schemaSource, new XmlParsingOptions {
                        BaseUri     = schemaUri,
                        XmlResolver = resolver
                    });

                    return(processor.CreateSchematronValidator(schemaDoc));
                }
            });

            return(new SchematronInvoker(validator, resolver));
        }
Пример #7
0
        internal static XsltInvoker With(int stylesheetHashCode, IXsltProcessor processor)
        {
            if (processor == null)
            {
                processor = Processors.Xslt.DefaultProcessor;
            }

            return(new XsltInvoker(inlineCache[processor][stylesheetHashCode], null));
        }
Пример #8
0
        public XPathItem Compile(XPathItem stylesheet, string processor)
        {
            CompiledStylesheetReference reference;

            if (stylesheet.IsNode)
            {
                IXsltProcessor proc = (processor != null) ?
                                      Processors.Xslt[processor]
               : this.CurrentXsltProcessor ?? Processors.Xslt.DefaultProcessor;

                int hashCode;

                XsltInvoker.With((XPathNavigator)stylesheet, proc, null, out hashCode);

                if (processor == null)
                {
                    return(this.ItemFactory.CreateAtomicValue(hashCode, XmlTypeCode.Integer));
                }

                reference = new CompiledStylesheetReference {
                    HashCode  = hashCode,
                    Processor = processor
                };
            }
            else
            {
                Uri stylesheetUri = StylesheetAsUri(stylesheet);

                if (processor == null ||
                    processor == Processors.Xslt.Default)
                {
                    return(this.ItemFactory.CreateAtomicValue(stylesheetUri.ToString(), XmlTypeCode.String));
                }

                XsltInvoker.With(stylesheetUri, processor);

                reference = new CompiledStylesheetReference {
                    Uri       = stylesheetUri.AbsoluteUri,
                    Processor = processor
                };
            }

            return(this.ItemFactory
                   .CreateDocument(reference)
                   .CreateNavigator());
        }
Пример #9
0
        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);
                }
            }
        }
Пример #10
0
        static SchematronInvoker With(IXPathNavigable schema, IXsltProcessor processor, Assembly callingAssembly)
        {
            if (schema == null)
            {
                throw new ArgumentNullException("schema");
            }

            if (processor == null)
            {
                processor = Processors.Xslt.DefaultProcessor;
            }

            int hashCode = XPathNavigatorEqualityComparer.Instance.GetHashCode(schema.CreateNavigator());

            ConcurrentDictionary <int, SchematronValidator> cache =
                inlineCache.GetOrAdd(processor, p => new ConcurrentDictionary <int, SchematronValidator>());

            SchematronValidator validator = cache.GetOrAdd(hashCode, i => processor.CreateSchematronValidator(schema));
            var resolver = new XmlDynamicResolver(callingAssembly);

            return(new SchematronInvoker(validator, resolver));
        }
Пример #11
0
        static SchematronInvoker With(Uri schemaUri, IXsltProcessor processor, Assembly callingAssembly)
        {
            if (schemaUri == null) throw new ArgumentNullException("schemaUri");

             var resolver = new XmlDynamicResolver(callingAssembly);

             if (!schemaUri.IsAbsoluteUri) {
            schemaUri = resolver.ResolveUri(null, schemaUri.OriginalString);
             }

             if (processor == null) {
            processor = Processors.Xslt.DefaultProcessor;
             }

             ConcurrentDictionary<Uri, SchematronValidator> cache =
            uriCache.GetOrAdd(processor, p => new ConcurrentDictionary<Uri, SchematronValidator>());

             SchematronValidator validator = cache.GetOrAdd(schemaUri, u => {

            using (var schemaSource = (Stream)resolver.GetEntity(schemaUri, null, typeof(Stream))) {

               IXPathNavigable schemaDoc = processor.ItemFactory.CreateNodeReadOnly(schemaSource, new XmlParsingOptions {
                  BaseUri = schemaUri,
                  XmlResolver = resolver
               });

               return processor.CreateSchematronValidator(schemaDoc);
            }
             });

             return new SchematronInvoker(validator, resolver);
        }
Пример #12
0
 public static XsltInvoker With(Uri stylesheetUri, IXsltProcessor processor)
 {
     return(With(stylesheetUri, processor, Assembly.GetCallingAssembly()));
 }
Пример #13
0
 static XsltInvoker With(string stylesheetUri, IXsltProcessor processor, Assembly callingAssembly)
 {
     return(With(new Uri(stylesheetUri, UriKind.RelativeOrAbsolute), processor, callingAssembly));
 }
Пример #14
0
        static SchematronInvoker With(IXPathNavigable schema, IXsltProcessor processor, Assembly callingAssembly)
        {
            if (schema == null) throw new ArgumentNullException("schema");

             if (processor == null) {
            processor = Processors.Xslt.DefaultProcessor;
             }

             int hashCode = XPathNavigatorEqualityComparer.Instance.GetHashCode(schema.CreateNavigator());

             ConcurrentDictionary<int, SchematronValidator> cache =
            inlineCache.GetOrAdd(processor, p => new ConcurrentDictionary<int, SchematronValidator>());

             SchematronValidator validator = cache.GetOrAdd(hashCode, i => processor.CreateSchematronValidator(schema));
             var resolver = new XmlDynamicResolver(callingAssembly);

             return new SchematronInvoker(validator, resolver);
        }
Пример #15
0
        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);
                    }
                }
            }
        }
Пример #16
0
        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);
        }
Пример #17
0
        internal static XsltInvoker With(int stylesheetHashCode, IXsltProcessor processor)
        {
            if (processor == null) {
            processor = Processors.Xslt.DefaultProcessor;
             }

             return new XsltInvoker(inlineCache[processor][stylesheetHashCode], null);
        }
Пример #18
0
 public static SchematronInvoker With(IXPathNavigable schema, IXsltProcessor processor)
 {
     return With(schema, processor, Assembly.GetCallingAssembly());
 }
Пример #19
0
 public static XsltInvoker With(Uri stylesheetUri, IXsltProcessor processor)
 {
     return With(stylesheetUri, processor, Assembly.GetCallingAssembly());
 }
Пример #20
0
 public static SchematronInvoker With(Uri schemaUri, IXsltProcessor processor)
 {
     return(With(schemaUri, processor, Assembly.GetCallingAssembly()));
 }
Пример #21
0
 static SchematronInvoker With(string schemaUri, IXsltProcessor processor, Assembly callingAssembly)
 {
     return(With(new Uri(schemaUri, UriKind.RelativeOrAbsolute), processor, callingAssembly));
 }
Пример #22
0
 public static SchematronInvoker With(IXPathNavigable schema, IXsltProcessor processor)
 {
     return(With(schema, processor, Assembly.GetCallingAssembly()));
 }
Пример #23
0
 static SchematronInvoker With(string schemaUri, IXsltProcessor processor, Assembly callingAssembly)
 {
     return With(new Uri(schemaUri, UriKind.RelativeOrAbsolute), processor, callingAssembly);
 }
Пример #24
0
 public static XsltInvoker With(IXPathNavigable stylesheet, IXsltProcessor processor)
 {
     return(With(stylesheet, processor, Assembly.GetCallingAssembly()));
 }
Пример #25
0
        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);
        }
Пример #26
0
        static XsltInvoker With(IXPathNavigable stylesheet, IXsltProcessor processor, Assembly callingAssembly)
        {
            int hashCode;

            return(With(stylesheet, processor, callingAssembly, out hashCode));
        }
Пример #27
0
 static XsltInvoker With(string stylesheetUri, IXsltProcessor processor, Assembly callingAssembly)
 {
     return With(new Uri(stylesheetUri, UriKind.RelativeOrAbsolute), processor, callingAssembly);
 }
Пример #28
0
 public static SchematronInvoker With(Uri schemaUri, IXsltProcessor processor)
 {
     return With(schemaUri, processor, Assembly.GetCallingAssembly());
 }
Пример #29
0
        static XsltInvoker With(IXPathNavigable stylesheet, IXsltProcessor processor, Assembly callingAssembly)
        {
            int hashCode;

             return With(stylesheet, processor, callingAssembly, out hashCode);
        }
Пример #30
0
        XPathNavigator ExecuteStylesheet(XPathItem stylesheet, XsltRuntimeOptions options)
        {
            XsltInvoker invoker;

            IXsltProcessor currentOrDefaultProc = this.CurrentXsltProcessor ?? Processors.Xslt.DefaultProcessor;

            if (stylesheet.IsNode)
            {
                XPathNavigator node = ((XPathNavigator)stylesheet).Clone();

                if (node.NodeType == XPathNodeType.Root)
                {
                    node.MoveToChild(XPathNodeType.Element);
                }

                if (node.NodeType != XPathNodeType.Element)
                {
                    throw new ArgumentException("if stylesheet is a node() it must be either a document-node(element()) or an element() node.", "stylesheet");
                }

                if (node.NamespaceURI == Namespace)
                {
                    XmlSerializer serializer = XPathItemFactory.GetSerializer(typeof(CompiledStylesheetReference));

                    var reference = (CompiledStylesheetReference)serializer.Deserialize(node.ReadSubtree());

                    IXsltProcessor specifiedProcessor = (reference.Processor != null) ?
                                                        Processors.Xslt[reference.Processor]
                  : null;

                    invoker = (reference.HashCode > 0) ?
                              XsltInvoker.With(reference.HashCode, specifiedProcessor ?? currentOrDefaultProc)
                  : XsltInvoker.With(reference.Uri, specifiedProcessor);
                }
                else
                {
                    invoker = XsltInvoker.With((XPathNavigator)stylesheet, currentOrDefaultProc);
                }
            }
            else
            {
                object value = stylesheet.TypedValue;

                if (value.GetType().IsPrimitive)
                {
                    int hashCode = Convert.ToInt32(value, CultureInfo.InvariantCulture);

                    invoker = XsltInvoker.With(hashCode, currentOrDefaultProc);
                }
                else
                {
                    Uri stylesheetUri = StylesheetAsUri(stylesheet);

                    invoker = XsltInvoker.With(stylesheetUri);
                }
            }

            return(invoker.Transform(options)
                   .Result()
                   .CreateNavigator());
        }
Пример #31
0
 public static XsltInvoker With(IXPathNavigable stylesheet, IXsltProcessor processor)
 {
     return With(stylesheet, processor, Assembly.GetCallingAssembly());
 }
Пример #32
0
 public void OnInstructionExecute(IXsltProcessor xsltProcessor)
 {
     if (_onExecute != null)
     {
         _onExecute.Invoke(_unknownDebugger, new object[] { xsltProcessor });
     }
 }