<commit_before>﻿using System;
using System.IO;
using System.Linq;
using System.Xml.Linq;

namespace NuCache.Rewriters
{
	public class XmlRewriter
	{
		private readonly UriRewriter _uriRewriter;

		public XmlRewriter(UriRewriter uriRewriter)
		{
			_uriRewriter = uriRewriter;
		}

		public virtual void Rewrite(Uri targetUri, Stream inputStream, Stream outputStream)
		{
			var doc = XDocument.Load(inputStream);

			var root = doc.Root;
			var ns = root.Name.Namespace;

			Func<String, Uri> transform = url => _uriRewriter.TransformHost(targetUri, new Uri(url));

			root
				.Elements(ns + "link")
				.Where(e => e.Attribute("rel").Value == "next")
				.Attributes("href")
				.ForEach(a => a.SetValue(transform(a.Value)));

			root
				.Attributes()
				.Where(a => a.Name.LocalName == "base")
				.ForEach(a => a.SetValue(transform(a.Value)));

			root
				.Elements("id")
				.ForEach(a => a.SetValue(transform(a.Value)));


			var entries = root
				.Elements(ns + "entry")
				.ToList();

			entries
				.Elements(ns + "content")
				.Attributes("src")
				.ForEach(a => a.SetValue(transform(a.Value)));

			entries
				.Elements(ns + "id")
				.ForEach(a => a.SetValue(transform(a.Value)));

			doc.Save(outputStream);
		}
	}
}<commit_msg>xmlRewriter: use an xmlReader and xmlWriter instead of XDocument<commit_after>﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace NuCache.Rewriters
{
	public class XmlRewriter
	{
		private readonly UriRewriter _uriRewriter;

		public XmlRewriter(UriRewriter uriRewriter)
		{
			_uriRewriter = uriRewriter;
		}

		private void ProcessReplacement(XElement node, Func<string, Uri> transform)
		{
			if (node.Name.LocalName == "link" && node.Attribute("rel").Value == "next")
			{
				var attribute = node.Attribute("href");
				attribute.SetValue(transform(attribute.Value));
			}

			/* on FEED root.
			
			node
				.Attributes()
				.Where(a => a.Name.LocalName == "base")
				.ForEach(a => a.SetValue(transform(a.Value)));
			//root
			//	.Attributes()
			//	.Where(a => a.Name.LocalName == "base")
			//	.ForEach(a => a.SetValue(transform(a.Value)));
			
			*/

			if (node.Name.LocalName == "entry")
			{
				node
					.Elements()
					.Where(e => e.Name.LocalName == "content")
					.Attributes("src")
					.ForEach(a => a.SetValue(transform(a.Value)));

				node
					.Elements()
					.Where(e => e.Name.LocalName == "id")
					.ForEach(e => e.SetValue(transform(e.Value)));
			}

			if (node.Name.LocalName == "id")
			{
				node.SetValue(transform(node.Value));
			}
		}

		public virtual void Rewrite(Uri targetUri, Stream inputStream, Stream outputStream)
		{
			Func<String, Uri> transform = url => _uriRewriter.TransformHost(targetUri, new Uri(url));

			using (var reader = XmlReader.Create(inputStream))
			using (var writer = XmlWriter.Create(outputStream))
			{

				writer.WriteStartDocument();
				reader.MoveToContent();

				writer.WriteStartElement(reader.Prefix, reader.LocalName, reader.NamespaceURI);
				writer.WriteAttributes(reader, false);

				while (reader.Read())
				{
					if (reader.NodeType == XmlNodeType.Element)
					{
						var element = (XElement)XNode.ReadFrom(reader);
						ProcessReplacement(element, transform);

						element.WriteTo(writer);
					}
				}

				writer.WriteEndElement();
			}

		

			//var doc = XDocument.Load(inputStream);

			//var root = doc.Root;
			//var ns = root.Name.Namespace;

			

			//root
			//	.Elements(ns + "link")
			//	.Where(e => e.Attribute("rel").Value == "next")
			//	.Attributes("href")
			//	.ForEach(a => a.SetValue(transform(a.Value)));

			//root
			//	.Attributes()
			//	.Where(a => a.Name.LocalName == "base")
			//	.ForEach(a => a.SetValue(transform(a.Value)));

			//root
			//	.Elements("id")
			//	.ForEach(a => a.SetValue(transform(a.Value)));


			//var entries = root
			//	.Elements(ns + "entry")
			//	.ToList();

			//entries
			//	.Elements(ns + "content")
			//	.Attributes("src")
			//	.ForEach(a => a.SetValue(transform(a.Value)));

			//entries
			//	.Elements(ns + "id")
			//	.ForEach(a => a.SetValue(transform(a.Value)));

			//doc.Save(outputStream);
		}
	}
}
{'fraction_non_alphanumeric': 0.16737689393939395, 'fraction_numerical': 0.0, 'mean_word_length': 3.00853889943074, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 30, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 3}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4017854', 'n_tokens_mistral': 1795, 'n_tokens_neox': 1427, 'n_words': 266}