Exemple #1
0
        XsltRuntimeOptions GetRuntimeOptions(XPathNavigator input, IEnumerable <XPathNavigator> parameters, XPathItem initialTemplate, XPathItem mode)
        {
            var options = new XsltRuntimeOptions();

            if (input != null)
            {
                options.InitialContextNode = input;
            }

            if (parameters != null)
            {
                foreach (XPathNavigator n in parameters)
                {
                    options.Parameters.Add(new XmlQualifiedName(n.Name, n.NamespaceURI), n.TypedValue);
                }
            }

            if (initialTemplate != null)
            {
                XmlQualifiedName it = initialTemplate.TypedValue as XmlQualifiedName
                                      ?? new XmlQualifiedName(initialTemplate.Value);

                options.InitialTemplate = it;
            }

            if (mode != null)
            {
                XmlQualifiedName m = mode.TypedValue as XmlQualifiedName
                                     ?? new XmlQualifiedName(mode.Value);

                options.InitialMode = m;
            }

            return(options);
        }
      public override IXPathNavigable Run(XsltRuntimeOptions options) {

         XdmDestination xdmDest = new XdmDestination();
         
         Run(xdmDest, options);

         return xdmDest.XdmNode.ToXPathNavigable();
      }
Exemple #3
0
 public void Render(TextWriter writer, XsltRuntimeOptions options)
 {
     try {
         this.Executable.Run(writer, options);
     } catch (ProcessorException ex) {
         throw CreateRenderException(ex);
     }
 }
Exemple #4
0
        public override IXPathNavigable Run(XsltRuntimeOptions options)
        {
            var xdmDest = new XdmDestination();

            Run(xdmDest, options);

            return(xdmDest.XdmNode.ToXPathNavigable());
        }
      public override void Run(TextWriter output, XsltRuntimeOptions options) {

         if (options == null) throw new ArgumentNullException("options");

         Serializer serializer = this.Processor.ItemFactory.CreateSerializer(options.Serialization);
         serializer.SetOutputWriter(output);

         Run(serializer, options);
      }
        public override void Run(XmlWriter output, XsltRuntimeOptions options)
        {
            if (output == null)
            {
                throw new ArgumentNullException("output");
            }
            if (options == null)
            {
                throw new ArgumentNullException("options");
            }

            if (this.possiblyXhtmlMethod ||
                options.Serialization.Method == XPathSerializationMethods.XHtml)
            {
                output = XPathItemFactory.CreateXHtmlWriter(output);
            }

            IXPathNavigable input;

            if (options.InitialContextNode != null)
            {
                input = options.InitialContextNode;
            }
            else
            {
                // this processor doesn't support initial template,
                // a node must be provided
                input = this.Processor.ItemFactory.CreateNodeReadOnly();
            }

            XsltArgumentList args = GetArguments(options);

            XmlResolver        resolver        = options.InputXmlResolver;
            XmlDynamicResolver dynamicResolver = resolver as XmlDynamicResolver;

            if (dynamicResolver != null &&
                dynamicResolver.DefaultBaseUri == null)
            {
                dynamicResolver.DefaultBaseUri = this.StaticBaseUri;
            }

            try {
                if (CLR.IsMono)
                {
                    monoTransform(this.transform, ((input != null) ? input.CreateNavigator() : null), args, output, resolver);
                }
                else
                {
                    net20Transform(this.command, input, resolver, args, output);
                }
            } catch (XsltException ex) {
                throw new SystemXsltException(ex);
            }
        }
        public override IXPathNavigable Run(XsltRuntimeOptions options)
        {
            IXPathNavigable doc = this.Processor.ItemFactory.BuildNode();

            XmlWriter writer = doc.CreateNavigator().AppendChild();

            Run(writer, options);

            writer.Close();

            return(doc);
        }
      void Run(XmlDestination destination, XsltRuntimeOptions options) {

         if (options == null) throw new ArgumentNullException("options");

         XsltTransformer transformer = executable.Load();
         transformer.RecoveryPolicy = RecoveryPolicy.DoNotRecover;

         if (options.InputXmlResolver != null)
            transformer.InputXmlResolver = options.InputXmlResolver;

         // XsltTransformer.BaseOutputUri doesn't accept null
         if (options.BaseOutputUri != null)
            transformer.BaseOutputUri = options.BaseOutputUri;

         // TODO: Bug in Saxon 9.3
         //else if (this.StaticBaseUri != null && this.StaticBaseUri.IsFile)
         //   transformer.BaseOutputUri = new Uri(Path.GetDirectoryName(this.StaticBaseUri.LocalPath), UriKind.Absolute);

         try {
            if (options.InitialTemplate != null)
               transformer.InitialTemplate = new QName(options.InitialTemplate);

         } catch (DynamicError err) {
            throw new SaxonException(err);
         }

         if (options.InitialMode != null)
            transformer.InitialMode = new QName(options.InitialMode);

         if (options.InitialContextNode != null) 
            transformer.InitialContextNode = options.InitialContextNode.ToXdmNode(this.Processor.ItemFactory);

         transformer.MessageListener = new SaxonMessageListener();

         foreach (var pair in options.Parameters) {
            
            QName qname = new QName(pair.Key);
            XdmValue xdmValue = pair.Value.ToXdmValue(this.Processor.ItemFactory);
         
            transformer.SetParameter(qname, xdmValue);
         }

         try {
            transformer.Run(destination);

         } catch (DynamicError ex) {
            throw new SaxonException(ex);

         } catch (Exception ex) {
            throw new SaxonException(ex.Message, ex);
         }
      }
Exemple #9
0
        public override void Run(TextWriter output, XsltRuntimeOptions options)
        {
            if (options == null)
            {
                throw new ArgumentNullException("options");
            }

            Serializer serializer = this.Processor.ItemFactory.CreateSerializer(options.Serialization);

            serializer.SetOutputWriter(output);

            Run(serializer, options);
        }
Exemple #10
0
        static XsltRuntimeOptions GetXsltOptions(SchematronRuntimeOptions options)
        {
            var xsltOptions = new XsltRuntimeOptions {
                InitialContextNode = options.Instance
            };

            if (!String.IsNullOrEmpty(options.Phase))
            {
                xsltOptions.Parameters.Add(new XmlQualifiedName("phase"), options.Phase);
            }

            if (options.Parameters != null)
            {
                foreach (var p in options.Parameters)
                {
                    xsltOptions.Parameters.Add(p);
                }
            }

            return(xsltOptions);
        }
Exemple #11
0
        public override void Run(TextWriter output, XsltRuntimeOptions options)
        {
            if (output == null)
            {
                throw new ArgumentNullException("output");
            }
            if (options == null)
            {
                throw new ArgumentNullException("options");
            }

            XmlWriterSettings settings = this.transform.OutputSettings;

            options.Serialization.CopyTo(settings);

            XmlWriter writer = XmlWriter.Create(output, settings);

            Run(writer, options);

            // NOTE: don't close writer if Run fails
            writer.Close();
        }
Exemple #12
0
        XsltArgumentList GetArguments(XsltRuntimeOptions options)
        {
            var list = new XsltArgumentList();

            foreach (var item in options.Parameters)
            {
                object val = ExtensionObjectConvert.ToInputOrEmpty(item.Value);

                if (!ExtensionObjectConvert.IsEmpty(val))
                {
                    list.AddParam(item.Key.Name, item.Key.Namespace, val);
                }
            }

            foreach (var pair in ExtensionObjects)
            {
                list.AddExtensionObject(pair.Key, InitializeExtensionObject(pair.Value, options.InputXmlResolver));
            }

            list.XsltMessageEncountered += new XsltMessageEncounteredEventHandler(args_XsltMessageEncountered);

            return(list);
        }
Exemple #13
0
        public XPathNavigator TransformStartingAt(XPathItem stylesheet, XPathItem initialTemplate, XPathNavigator input, IEnumerable <XPathNavigator> parameters)
        {
            XsltRuntimeOptions options = GetRuntimeOptions(input, parameters, initialTemplate, null);

            return(ExecuteStylesheet(stylesheet, options));
        }
Exemple #14
0
        public override void Run(XmlWriter output, XsltRuntimeOptions options)
        {
            XmlDestination builder = new TextWriterDestination(output);

            Run(builder, options);
        }
      public override void Run(TextWriter output, XsltRuntimeOptions options) {

         if (output == null) throw new ArgumentNullException("output");
         if (options == null) throw new ArgumentNullException("options");

         XmlWriterSettings settings = this.transform.OutputSettings;
         options.Serialization.CopyTo(settings);

         XmlWriter writer = XmlWriter.Create(output, settings);

         Run(writer, options);
      }
Exemple #16
0
 internal XsltResultHandler(XsltExecutable executable, XsltRuntimeOptions options)
 {
     this.executable           = executable;
     this.options              = options;
     this.defaultSerialization = options.Serialization;
 }
      public override void Run(XmlWriter output, XsltRuntimeOptions options) {

         if (output == null) throw new ArgumentNullException("output");
         if (options == null) throw new ArgumentNullException("options");

         if (this.possiblyXhtmlMethod || options.Serialization.Method == XmlSerializationOptions.Methods.XHtml)
            output = new XHtmlWriter(output);

         IXPathNavigable input;

         if (options.InitialContextNode != null)
            input = options.InitialContextNode;

         else
            // this processor doesn't support initial template,
            // a node must be provided
            input = this.Processor.ItemFactory.CreateNodeReadOnly();

         XsltArgumentList args = GetArguments(options);
         XmlResolver resolver = options.InputXmlResolver;

         try {
            if (CLR.IsMono) 
               monoTransform(this.transform, ((input != null) ? input.CreateNavigator() : null), args, output, resolver);
            else 
               net20Transform(this.command, input, resolver, args, output);

         } catch (XsltException ex) {
            throw new SystemXsltException(ex);
         }
      }
      public override IXPathNavigable Run(XsltRuntimeOptions options) {

         IXPathNavigable doc = this.Processor.ItemFactory.CreateNodeEditable();

         using (XmlWriter writer = doc.CreateNavigator().AppendChild())
            Run(writer, options);

         return doc;
      }
      public override void Run(XmlWriter output, XsltRuntimeOptions options) {

         XmlDestination builder = new TextWriterDestination(output);

         Run(builder, options);
      }
      XsltArgumentList GetArguments(XsltRuntimeOptions options) {

         XsltArgumentList list = new XsltArgumentList();

         foreach (var item in options.Parameters) {
            object val = ExtensionObjectConvert.ToInputOrEmpty(item.Value);

            if (!ExtensionObjectConvert.IsEmpty(val))
               list.AddParam(item.Key.Name, item.Key.Namespace, val);
         }

         foreach (var pair in ExtensionObjects)
            list.AddExtensionObject(pair.Key, Activator.CreateInstance(pair.Value));

         list.XsltMessageEncountered += new XsltMessageEncounteredEventHandler(args_XsltMessageEncountered);

         return list;
      }
Exemple #21
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);
                    }
                }
            }
        }
Exemple #22
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());
        }
Exemple #23
0
 public virtual void InitializeRuntimeOptions(XsltRuntimeOptions options)
 {
     SetBoundParameters(options.Parameters);
 }
Exemple #24
0
        public XPathNavigator Transform(XPathItem stylesheet, XPathNavigator input, IEnumerable <XPathNavigator> parameters, XPathItem mode)
        {
            XsltRuntimeOptions options = GetRuntimeOptions(input, parameters, null, mode);

            return(ExecuteStylesheet(stylesheet, options));
        }