Beispiel #1
0
        protected override void RenderView(ViewContext viewContext, TextWriter writer, object instance)
        {
            if (viewContext == null) throw new ArgumentNullException("viewContext");

             XsltPage page = instance as XsltPage;

             if (page == null) {
            throw new InvalidOperationException(String.Format(CultureInfo.InvariantCulture, "Compiled view '{0}' must inherit from {1}.", this.ViewPath, typeof(XsltPage).AssemblyQualifiedName));
             }

             var options = new XsltRuntimeOptions();

             page.SetIntrinsics(HttpContext.Current);
             page.AddFileDependencies();
             page.InitializeRuntimeOptions(options);

             IXPathNavigable inputNode = page.Executable.Processor.ItemFactory.CreateDocument(viewContext.ViewData.Model);

             if (inputNode != null) {

            if (options.InitialContextNode == null) {
               options.InitialTemplate = null;
            }

            options.InitialContextNode = inputNode;
             }

             foreach (var item in viewContext.ViewData) {
            options.Parameters[new XmlQualifiedName(item.Key)] = item.Value;
             }

             page.Render(viewContext.Writer, options);
        }
        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);
        }
        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;
        }
Beispiel #4
0
 public abstract void Run(XmlWriter output, XsltRuntimeOptions options);
Beispiel #5
0
 public abstract IXPathNavigable Run(XsltRuntimeOptions options);
Beispiel #6
0
 public abstract void Run(Stream output, XsltRuntimeOptions options);
Beispiel #7
0
 public abstract void Run(Stream output, XsltRuntimeOptions options);
Beispiel #8
0
 public abstract void Run(XmlWriter output, XsltRuntimeOptions 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 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);
               }
            }
             }
        }
Beispiel #11
0
 public abstract IXPathNavigable Run(XsltRuntimeOptions options);
Beispiel #12
0
        public XsltResultHandler Transform(IXPathNavigable input, object parameters)
        {
            if (input == null) throw new ArgumentNullException("input");

             var options = new XsltRuntimeOptions {
            InitialContextNode = input,
            InputXmlResolver = new XmlDynamicResolver(this.withCallingAssembly)
             };

             if (parameters != null) {

            foreach (PropertyDescriptor property in TypeDescriptor.GetProperties(parameters)) {
               options.Parameters.Add(new XmlQualifiedName(property.Name), property.GetValue(parameters));
            }
             }

             return Transform(options);
        }
Beispiel #13
0
        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) {

            XmlDynamicResolver dynamicResolver = options.InputXmlResolver as XmlDynamicResolver;

            if (dynamicResolver != null
               && dynamicResolver.DefaultBaseUri == null) {

               dynamicResolver.DefaultBaseUri = this.StaticBaseUri;
            }

            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) {

            XdmNode node = options.InitialContextNode.ToXdmNode(this.Processor.ItemFactory);

            BugHandler.ThrowIfBug1675(node);

            transformer.InitialContextNode = node;
             }

             foreach (var pair in options.Parameters) {

            var qname = new QName(pair.Key);
            XdmValue xdmValue = pair.Value.ToXdmValue(this.Processor.ItemFactory);

            transformer.SetParameter(qname, xdmValue);
             }

             transformer.MessageListener = new TraceMessageListener();

             try {
            transformer.Run(destination);

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

             } catch (Exception ex) {
            throw new SaxonException(ex.Message, ex);
             }
        }
Beispiel #14
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;
        }
Beispiel #15
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();
        }
        public override IXPathNavigable Run(XsltRuntimeOptions options)
        {
            IXPathNavigable doc = this.Processor.ItemFactory.BuildNode();

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

             Run(writer, options);

             writer.Close();

             return doc;
        }
        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;
        }
Beispiel #18
0
        public override void Run(XmlWriter output, XsltRuntimeOptions options)
        {
            XmlDestination builder = new TextWriterDestination(output);

             Run(builder, options);
        }
Beispiel #19
0
        public override IXPathNavigable Run(XsltRuntimeOptions options)
        {
            var xdmDest = new XdmDestination();

             Run(xdmDest, options);

             return xdmDest.XdmNode.ToXPathNavigable();
        }
Beispiel #20
0
 public XsltResultHandler Transform(XsltRuntimeOptions options)
 {
     return new XsltResultHandler(this.executable, 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);

             // NOTE: don't close writer if Run fails
             writer.Close();
        }