public IXmlSerializable Send(XPathNavigator message) { if (message == null) { throw new ArgumentNullException("message"); } XPathItemFactory itemFactory = this.ItemFactory; if (itemFactory == null) { throw new InvalidOperationException("ItemFactory cannot be null."); } MailMessage mailMessage = GetMailMessage(message, itemFactory); var smtp = new SmtpClient(); try { smtp.Send(mailMessage); } catch (SmtpException ex) { return(GetError(ex)); } return(new XPathSmtpSuccess()); }
MailMessage GetMailMessage(XPathNavigator message, XPathItemFactory itemFactory) { var xpathMessage = new XPathMailMessage(); xpathMessage.ReadXml(message); return xpathMessage.ToMailMessage(itemFactory); }
public object Into(Type outputType) { XmlSerializer serializer = XPathItemFactory.GetSerializer(outputType); IXPathNavigable outputDoc = Result(); return(serializer.Deserialize(outputDoc.CreateNavigator().ReadSubtree())); }
static MailMessage GetMailMessage(XPathNavigator message, XPathItemFactory itemFactory) { var xpathMessage = new XPathMailMessage(); xpathMessage.ReadXml(message); return(xpathMessage.ToMailMessage(itemFactory)); }
public IEnumerable <XPathItem> Eval(XPathItem module, XPathItem input, IEnumerable <XPathNavigator> parameters) { XQueryInvoker invoker; IXQueryProcessor currentOrDefaultProc = this.CurrentXQueryProcessor ?? Processors.XQuery.DefaultProcessor; if (module.IsNode) { XPathNavigator node = ((XPathNavigator)module).Clone(); if (node.NodeType == XPathNodeType.Root) { node.MoveToChild(XPathNodeType.Element); } if (node.NodeType == XPathNodeType.Element && node.NamespaceURI == Namespace) { XmlSerializer serializer = XPathItemFactory.GetSerializer(typeof(CompiledQueryReference)); var reference = (CompiledQueryReference)serializer.Deserialize(node.ReadSubtree()); IXQueryProcessor specifiedProcessor = (reference.Processor != null) ? Processors.XQuery[reference.Processor] : null; invoker = (reference.HashCode > 0) ? XQueryInvoker.WithQuery(reference.HashCode, specifiedProcessor ?? currentOrDefaultProc) : XQueryInvoker.With(reference.Uri, specifiedProcessor); } else { invoker = XQueryInvoker.WithQuery(module.Value, currentOrDefaultProc); } } else { object value = module.TypedValue; if (value.GetType().IsPrimitive) { int hashCode = Convert.ToInt32(value, CultureInfo.InvariantCulture); invoker = XQueryInvoker.WithQuery(hashCode, currentOrDefaultProc); } else { Uri moduleUri = ResolveUri(module); invoker = XQueryInvoker.WithQuery(module.Value, currentOrDefaultProc); } } XQueryRuntimeOptions options = GetRuntimeOptions(input, parameters); return(invoker.Query(options).Result()); }
public void Deserialize(Stream source, Uri sourceUri, XPathItemFactory itemFactory, XmlQualifiedName method) { if (source == null) { throw new ArgumentNullException("source"); } if (itemFactory == null) { throw new ArgumentNullException("itemFactory"); } XPathItem content; TextReader textReader = (this.Encoding != null) ? new StreamReader(source, this.Encoding) : new StreamReader(source); if (method == XPathSerializationMethods.Xml || method == XPathSerializationMethods.XHtml) { content = itemFactory.CreateNodeReadOnly(textReader, new XmlParsingOptions { BaseUri = sourceUri }).CreateNavigator(); } else if (method == XPathSerializationMethods.Html) { var htmlParser = XPathHttpClient.HtmlParser; if (htmlParser != null) { content = htmlParser(textReader).CreateNavigator(); } else { content = itemFactory.CreateAtomicValue(textReader.ReadToEnd(), XmlTypeCode.String); } } else if (method == XPathSerializationMethods.Text) { content = itemFactory.CreateAtomicValue(textReader.ReadToEnd(), XmlTypeCode.String); } else { byte[] buffer; using (var memStream = new MemoryStream()) { source.CopyTo(memStream); buffer = memStream.ToArray(); } byte[] base64 = Encoding.UTF8.GetBytes(Convert.ToBase64String(buffer)); content = itemFactory.CreateAtomicValue(base64, XmlTypeCode.Base64Binary); } this.Content = content; }
public void Deserialize(Stream source, Uri sourceUri, XPathItemFactory itemFactory, string overrideMediaType) { string mediaType = !String.IsNullOrEmpty(overrideMediaType) ? overrideMediaType : this.MediaType; XmlQualifiedName method = GetMethodFromMediaType(mediaType, ExtensionMethods.Base64Binary); Deserialize(source, sourceUri, itemFactory, method); }
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 MailMessage ToMailMessage(XPathItemFactory itemFactory) { var mailMessage = new MailMessage(); if (this.From != null) { mailMessage.From = this.From.ToMailAddress(); } for (int i = 0; i < this.To.Count; i++) { mailMessage.To.Add(this.To[i].ToMailAddress()); } for (int i = 0; i < this.CC.Count; i++) { mailMessage.CC.Add(this.CC[i].ToMailAddress()); } for (int i = 0; i < this.Bcc.Count; i++) { mailMessage.Bcc.Add(this.Bcc[i].ToMailAddress()); } for (int i = 0; i < this.ReplyTo.Count; i++) { mailMessage.ReplyToList.Add(this.ReplyTo[i].ToMailAddress()); } if (this.Sender != null) { mailMessage.Sender = this.Sender.ToMailAddress(); } if (this.Subject.HasValue()) { mailMessage.Subject = this.Subject.Replace('\r', ' ').Replace('\n', ' '); } if (this.Body != null) { using (var writer = new StringWriter(CultureInfo.CurrentCulture)) { this.Body.Serialize(writer, itemFactory); mailMessage.Body = writer.ToString(); } mailMessage.IsBodyHtml = this.Body.Method == XPathSerializationMethods.Html || this.Body.Method == XPathSerializationMethods.XHtml; } return(mailMessage); }
public void Serialize(TextWriter output, XPathItemFactory itemFactory) { if (this.Content == null) { return; } var serialization = new XPathSerializationOptions { Method = this.Method, }; itemFactory.Serialize(this.Content, output, serialization); }
public long PrepareContent(XPathItemFactory itemFactory, XmlResolver resolver) { XPathItem item = this.Content; if (item != null) { XmlQualifiedName method = _Method ?? GetMethodFromMediaType(this.MediaType, this.Method); this.contentStream = new MemoryStream(); Serialize(this.contentStream, itemFactory, method); this.contentStream.Position = 0; } else if (this.Src != null) { if (resolver == null) { throw new ArgumentNullException("resolver"); } Stream source = resolver.GetEntity(this.Src, null, typeof(Stream)) as Stream; if (source != null) { if (source.CanSeek) { this.contentStream = source; } else { this.contentStream = new MemoryStream(); source.CopyTo(this.contentStream); this.contentStream.Position = 0; source.Dispose(); } } else { throw new InvalidOperationException(String.Format(CultureInfo.InvariantCulture, "Could not resolve {0}.", this.Src.AbsoluteUri)); } } return((this.contentStream != null) ? this.contentStream.Length : 0); }
public void Serialize(Stream output, XPathItemFactory itemFactory, XmlQualifiedName method) { if (this.Content == null) { throw new InvalidOperationException("Content cannot be null."); } XPathItem item = this.Content; if (method == ExtensionMethods.Base64Binary) { byte[] buffer = (!item.IsNode && item.XmlType.TypeCode == XmlTypeCode.Base64Binary) ? (byte[])item.TypedValue : Convert.FromBase64String(item.Value); output.Write(buffer, 0, buffer.Length); } else if (method == ExtensionMethods.HexBinary) { byte[] buffer = (!item.IsNode && item.XmlType.TypeCode == XmlTypeCode.HexBinary) ? (byte[])item.TypedValue : fromBinHexString(item.Value); output.Write(buffer, 0, buffer.Length); } else { var serialization = new XPathSerializationOptions { Indent = this.Indent, OmitXmlDeclaration = this.OmitXmlDeclaration, MediaType = this.MediaType, Method = method, DocTypePublic = this.DocTypePublic, DocTypeSystem = this.DocTypeSystem, Encoding = this.Encoding, ByteOrderMark = this.ByteOrderMark }; itemFactory.Serialize(item, output, serialization); } }
public XPathItem[] SendRequest(XPathNavigator request, string href, IEnumerable <XPathItem> bodies) { XPathItemFactory itemFactory = this.ItemFactory; XmlResolver resolver = this.Resolver; if (itemFactory == null) { throw new InvalidOperationException("ItemFactory cannot be null."); } int bodiesLength = (bodies != null) ? bodies.Count() : 0; XPathHttpRequest xpathRequest; if (request == null) { if (String.IsNullOrEmpty(href)) { throw new ArgumentException("href cannot be null or empty if request is null.", "href"); } xpathRequest = new XPathHttpRequest { Method = WebRequestMethods.Http.Get, Href = new Uri(href, UriKind.Absolute), Resolver = resolver, ItemFactory = itemFactory }; if (bodiesLength > 0) { throw new ArgumentException("Cannot use the bodies parameter when request is null.", "bodies"); } } else { xpathRequest = new XPathHttpRequest { Resolver = resolver, ItemFactory = itemFactory }; xpathRequest.ReadXml(request); if (String.IsNullOrEmpty(href)) { if (xpathRequest.Href == null) { throw new ArgumentException("href cannot be null or empty if request.Href is null.", "href"); } } else { xpathRequest.Href = new Uri(href); } if (xpathRequest.Body != null) { if (bodiesLength > 0) { if (bodiesLength > 1) { throw new ArgumentException("bodies must have a single item when request.Body is not null.", "bodies"); } xpathRequest.Body.Content = bodies.Single(); } } else if (xpathRequest.Multipart != null) { if (bodiesLength > 0) { if (bodiesLength != xpathRequest.Multipart.Items.Count) { throw new ArgumentException("The number of items in bodies must match the multipart request bodies.", "bodies"); } for (int i = 0; i < xpathRequest.Multipart.Items.Count; i++) { XPathHttpMultipartItem item = xpathRequest.Multipart.Items[i]; if (item.Body != null) { item.Body.Content = bodies.Skip(i).First(); } } } } else if (bodiesLength > 0) { throw new ArgumentException("If bodies is not empty request.Body or request.Multipart cannot be null.", "request"); } } XPathHttpResponse xpathResponse = xpathRequest.GetResponse(); XPathNavigator responseEl = itemFactory.CreateElement(xpathResponse); var result = new List <XPathItem>(); result.Add(responseEl); if (xpathResponse.Body != null) { if (xpathResponse.Body.Content != null) { result.Add(xpathResponse.Body.Content); } } else if (xpathResponse.Multipart != null) { foreach (XPathHttpMultipartItem item in xpathResponse.Multipart.Items) { if (item.Body.Content != null) { result.Add(item.Body.Content); } } } return(result.ToArray()); }
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 void Deserialize(Stream source, Uri sourceUri, XPathItemFactory itemFactory, XmlQualifiedName method) { if (source == null) throw new ArgumentNullException("source"); if (itemFactory == null) throw new ArgumentNullException("itemFactory"); XPathItem content; TextReader textReader = (this.Encoding != null) ? new StreamReader(source, this.Encoding) : new StreamReader(source); if (method == XmlSerializationOptions.Methods.Xml || method == XmlSerializationOptions.Methods.XHtml) { content = itemFactory.CreateNodeReadOnly(textReader, new XmlParsingOptions { BaseUri = sourceUri }).CreateNavigator(); } else if (method == XmlSerializationOptions.Methods.Html) { var htmlParser = XPathHttpClient.HtmlParser; if (htmlParser != null) content = htmlParser(textReader).CreateNavigator(); else content = itemFactory.CreateAtomicValue(textReader.ReadToEnd(), XmlTypeCode.String); } else if (method == XmlSerializationOptions.Methods.Text) { content = itemFactory.CreateAtomicValue(textReader.ReadToEnd(), XmlTypeCode.String); } else { byte[] buffer = StreamUtil.ReadFully(source); byte[] base64 = Encoding.UTF8.GetBytes(Convert.ToBase64String(buffer)); content = itemFactory.CreateAtomicValue(base64, XmlTypeCode.Base64Binary); } this.Content = content; }
public void Serialize(TextWriter output, XPathItemFactory itemFactory) { if (this.Content == null) return; XmlSerializationOptions serialization = new XmlSerializationOptions { Method = this.Method, }; itemFactory.Serialize(this.Content, output, serialization); }
public long PrepareContent(XPathItemFactory itemFactory, XmlResolver resolver) { XPathItem item = this.Content; if (item != null) { XmlQualifiedName method; if (_Method != null) method = _Method; else method = GetMethodFromMediaType(this.MediaType, this.Method); contentStream = new MemoryStream(); Serialize(contentStream, itemFactory, method); contentStream.Position = 0; } else if (this.Src != null) { if (resolver == null) throw new ArgumentNullException("resolver"); Stream source = resolver.GetEntity(this.Src, null, typeof(Stream)) as Stream; if (source != null) { if (source.CanSeek) { contentStream = source; } else { contentStream = new MemoryStream(); source.CopyTo(contentStream); contentStream.Position = 0; source.Dispose(); } } else { throw new InvalidOperationException(String.Format(CultureInfo.InvariantCulture, "Could not resolve {0}.", this.Src.AbsoluteUri)); } } return (contentStream != null) ? contentStream.Length : 0; }
public MailMessage ToMailMessage(XPathItemFactory itemFactory) { var mailMessage = new MailMessage(); if (this.From != null) mailMessage.From = this.From.ToMailAddress(); for (int i = 0; i < this.To.Count; i++) mailMessage.To.Add(this.To[i].ToMailAddress()); for (int i = 0; i < this.CC.Count; i++) mailMessage.CC.Add(this.CC[i].ToMailAddress()); for (int i = 0; i < this.Bcc.Count; i++) mailMessage.Bcc.Add(this.Bcc[i].ToMailAddress()); for (int i = 0; i < this.ReplyTo.Count; i++) mailMessage.ReplyToList.Add(this.ReplyTo[i].ToMailAddress()); if (this.Sender != null) mailMessage.Sender = this.Sender.ToMailAddress(); if (this.Subject != null) mailMessage.Subject = this.Subject; if (this.Body != null) { using (var writer = new StringWriter()) { this.Body.Serialize(writer, itemFactory); mailMessage.Body = writer.ToString(); } mailMessage.IsBodyHtml = this.Body.Method == XmlSerializationOptions.Methods.Html || this.Body.Method == XmlSerializationOptions.Methods.XHtml; } return mailMessage; }
public void Serialize(Stream output, XPathItemFactory itemFactory, XmlQualifiedName method) { if (this.Content == null) throw new InvalidOperationException("Content cannot be null."); XPathItem item = this.Content; if (method == ExtensionMethods.Base64Binary) { byte[] buffer = (!item.IsNode && item.XmlType.TypeCode == XmlTypeCode.Base64Binary) ? (byte[])item.TypedValue : Convert.FromBase64String(item.Value); output.Write(buffer, 0, buffer.Length); } else if (method == ExtensionMethods.HexBinary) { byte[] buffer = (!item.IsNode && item.XmlType.TypeCode == XmlTypeCode.HexBinary) ? (byte[])item.TypedValue : fromBinHexString(item.Value); output.Write(buffer, 0, buffer.Length); } else { XmlSerializationOptions serialization = new XmlSerializationOptions { Indent = this.Indent, OmitXmlDeclaration = this.OmitXmlDeclaration, MediaType = this.MediaType, Method = method, DocTypePublic = this.DocTypePublic, DocTypeSystem = this.DocTypeSystem, Encoding = this.Encoding, ByteOrderMark = this.ByteOrderMark }; itemFactory.Serialize(item, output, serialization); } }
static bool[] FunctionsAvailable(QName[] names, Processor processor, XPathItemFactory itemFactory) { const string xsltNs = "http://www.w3.org/1999/XSL/Transform"; IXPathNavigable stylesheetDoc = itemFactory.BuildNode(); XmlWriter builder = stylesheetDoc.CreateNavigator().AppendChild(); builder.WriteStartElement("stylesheet", xsltNs); builder.WriteAttributeString("version", "2.0"); for (int i = 0; i < names.Length; i++) { QName item = names[i]; builder.WriteAttributeString("xmlns", "p" + i.ToStringInvariant(), null, item.Uri); } builder.WriteStartElement("output", xsltNs); builder.WriteAttributeString("method", "text"); builder.WriteEndElement(); builder.WriteStartElement("template", xsltNs); builder.WriteAttributeString("name", "main"); for (int i = 0; i < names.Length; i++) { QName item = names[i]; if (i > 0) { builder.WriteElementString("text", xsltNs, "|"); } builder.WriteStartElement("value-of", xsltNs); builder.WriteAttributeString("select", "function-available('{0}:{1}')".FormatInvariant("p" + i.ToStringInvariant(), item.LocalName)); builder.WriteEndElement(); } builder.WriteEndElement(); // template builder.WriteEndElement(); // stylesheet builder.Close(); XsltCompiler compiler = processor.NewXsltCompiler(); compiler.BaseUri = new Uri("foo:bar"); compiler.XmlResolver = null; XsltTransformer transform = compiler.Compile(stylesheetDoc.CreateNavigator().ReadSubtree()).Load(); transform.InitialTemplate = new QName("main"); using (var output = new StringWriter(CultureInfo.InvariantCulture)) { var serializer = new Serializer(); serializer.SetOutputWriter(output); transform.Run(serializer); return(output.ToString().Trim().Split('|').Select(s => XmlConvert.ToBoolean(s)).ToArray()); } }