IndexOf() public method

Gets the index of the part matching the specified URI.

Finds the index of the part matching the specified URI, if it exists.

If the URI scheme is "cid", then matching is performed based on the Content-Id header values, otherwise the Content-Location headers are used. If the provided URI is absolute and a child part's Content-Location is relative, then then the child part's Content-Location URI will be combined with the value of its Content-Base header, if available, otherwise it will be combined with the multipart/related part's Content-Base header in order to produce an absolute URI that can be compared with the provided absolute URI.

/// is null. ///
public IndexOf ( Uri uri ) : int
uri Uri The URI of the MIME part.
return int
		public void TestArgumentExceptions ()
		{
			var related = new MultipartRelated ();
			string mimeType, charset;

			Assert.Throws<ArgumentNullException> (() => new MultipartRelated ((MimeEntityConstructorArgs) null));
			Assert.Throws<ArgumentNullException> (() => related.Open (null, out mimeType, out charset));
			Assert.Throws<ArgumentNullException> (() => related.Open (null));
			Assert.Throws<ArgumentNullException> (() => related.Contains ((Uri) null));
			Assert.Throws<ArgumentNullException> (() => related.IndexOf ((Uri) null));
			Assert.Throws<ArgumentNullException> (() => related.Accept (null));
			Assert.Throws<ArgumentNullException> (() => related.Root = null);

			Assert.Throws<FileNotFoundException> (() => related.Open (new Uri ("http://www.xamarin.com/logo.png"), out mimeType, out charset));
			Assert.Throws<FileNotFoundException> (() => related.Open (new Uri ("http://www.xamarin.com/logo.png")));
		}
Beispiel #2
0
		void Render (MultipartRelated related)
		{
			var root = related.Root;
			var multipart = root as Multipart;
			var text = root as TextPart;

			if (multipart != null) {
				// Note: the root document can sometimes be a multipart/alternative.
				// A multipart/alternative is just a collection of alternate views.
				// The last part is the format that most closely matches what the
				// user saw in his or her email client's WYSIWYG editor.
				for (int i = multipart.Count; i > 0; i--) {
					var body = multipart[i - 1] as TextPart;

					if (body == null)
						continue;

					// our preferred mime-type is text/html
					if (body.ContentType.Matches ("text", "html")) {
						text = body;
						break;
					}

					if (text == null)
						text = body;
				}
			}

			if (text != null && text.ContentType.Matches ("text", "html")) {
				var doc = new HtmlAgilityPack.HtmlDocument ();
				var saved = new Dictionary<MimePart, string> ();
				TextPart html;

				doc.LoadHtml (text.Text);

				// find references to related MIME parts and replace them with links to links to the saved attachments
				foreach (var img in doc.DocumentNode.SelectNodes ("//img[@src]")) {
					var src = img.Attributes["src"];
					int index;
					Uri uri;

					if (src == null || src.Value == null)
						continue;

					// parse the <img src=...> attribute value into a Uri
					if (Uri.IsWellFormedUriString (src.Value, UriKind.Absolute))
						uri = new Uri (src.Value, UriKind.Absolute);
					else
						uri = new Uri (src.Value, UriKind.Relative);

					// locate the index of the attachment within the multipart/related (if it exists)
					if ((index = related.IndexOf (uri)) != -1) {
						var attachment = related[index] as MimePart;

						// make sure the referenced part is a MimePart (as opposed to another Multipart or MessagePart)
						if (attachment != null) {
							string fileName;

							// save the attachment (if we haven't already saved it)
							if (!saved.TryGetValue (attachment, out fileName)) {
								fileName = attachment.FileName;

								if (string.IsNullOrEmpty (fileName))
									fileName = Guid.NewGuid ().ToString ();

								using (var stream = File.Create (fileName))
									attachment.ContentObject.DecodeTo (stream);

								saved.Add (attachment, fileName);
							}

							// replace the <img src=...> value with the local file name
							src.Value = "file://" + Path.GetFullPath (fileName);
						}
					}
				}

				if (saved.Count > 0) {
					// we had to make some modifications to the original html part, so create a new
					// (temporary) text/html part to render
					html = new TextPart ("html");
					using (var writer = new StringWriter ()) {
						doc.Save (writer);

						html.Text = writer.GetStringBuilder ().ToString ();
					}
				} else {
					html = text;
				}

				Render (html);
			} else {
				Render (related.Root);
			}
		}