示例#1
0
        private void BrowseClicked(object sender, EventArgs e)
        {
            string repeatingElementMarker = _tbRepeatingElementMarker.Text;
            if (string.IsNullOrWhiteSpace(repeatingElementMarker))
            {
                MessageBox.Show(this, "You must enter the name of the repeating xml element.", "Repeating element name is missing",
                                MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            using (var fileDlg = new OpenFileDialog())
            {
                if (fileDlg.ShowDialog(this) != DialogResult.OK)
                    return;
                _tbXmlFile.Text = fileDlg.FileName;
                if (string.IsNullOrWhiteSpace(_tbXmlFile.Text))
                {
                    MessageBox.Show(this, "You must select the name of an xml file.", "Xml file is missing",
                                    MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
            }

            try
            {
                using (var fastSplitter = new FastXmlElementSplitter(_tbXmlFile.Text))
                {
                    var optionalMarker = string.IsNullOrWhiteSpace(_tbOptionFirstElementMarker.Text)
                                             ? null
                                             : _tbOptionFirstElementMarker.Text;
                    bool foundOptionalFirstElement;
                    foreach (var record in fastSplitter.GetSecondLevelElementStrings(optionalMarker, repeatingElementMarker, out foundOptionalFirstElement))
                    { /* Do nothing with it. */ }
                }
            }
            catch (Exception err)
            {
                var msg = err.Message;
                MessageBox.Show(this, msg);
                throw;
            }
            MessageBox.Show(this, "all fine.");
        }
		private static void ProcessContent(FastXmlElementSplitter fastXmlElementSplitter, int expectedCount, string firstElementMarker,
			string recordMarker, Encoding enc)
		{
			bool foundOptionalFirstElement;
			var elementBytes =
				fastXmlElementSplitter.GetSecondLevelElementBytes(firstElementMarker, recordMarker, out foundOptionalFirstElement)
										.ToList();
			Assert.AreEqual(expectedCount, elementBytes.Count);
			var elementStrings =
				fastXmlElementSplitter.GetSecondLevelElementStrings(firstElementMarker, recordMarker,
					out foundOptionalFirstElement).ToList();
			Assert.AreEqual(expectedCount, elementStrings.Count);
			for (var i = 0; i < elementStrings.Count; ++i)
			{
				var currentStr = elementStrings[i];
				Assert.AreEqual(
					currentStr,
					enc.GetString(elementBytes[i]));
				var el = XElement.Parse(currentStr);
			}
		}
示例#3
0
        private static Dictionary<string, string> MakeRecordDictionary(IMergeEventListener mainMergeEventListener,
			IMergeStrategy mergeStrategy,
			string pathname,
			string firstElementMarker,
			string recordStartingTag,
			string identifierAttribute)
        {
            var records = new Dictionary<string, string>(EstimatedObjectCount(pathname),
                                                         StringComparer.InvariantCultureIgnoreCase);
            using (var fastSplitter = new FastXmlElementSplitter(pathname))
            {
                bool foundOptionalFirstElement;
                foreach (var record in fastSplitter.GetSecondLevelElementStrings(firstElementMarker, recordStartingTag, out foundOptionalFirstElement))
                {
                    if (foundOptionalFirstElement)
                    {
                        var key = firstElementMarker.ToLowerInvariant();
                        if (records.ContainsKey(key))
                        {
                            mainMergeEventListener.WarningOccurred(
                                new MergeWarning(string.Format("{0}: There is more than one optional first element '{1}'", pathname, key)));
                        }
                        else
                        {
                            if (RemoveAmbiguousChildNodes)
                            {
                                var possiblyRevisedRecord = RemoveAmbiguousChildren(mainMergeEventListener, mergeStrategy.GetStrategies(), record, pathname);
                                records.Add(key, possiblyRevisedRecord);
                            }
                            else
                            {
                                records.Add(key, record);
                            }
                        }
                        foundOptionalFirstElement = false;
                    }
                    else
                    {
                        var attrValues = XmlUtils.GetAttributes(record, new HashSet<string> {"dateDeleted", identifierAttribute});

                        // Eat tombstones.
                        if (attrValues["dateDeleted"] != null)
                            continue;

                        var identifier = attrValues[identifierAttribute];
                        if (string.IsNullOrEmpty(identifierAttribute))
                        {
                            mainMergeEventListener.WarningOccurred(
                                new MergeWarning(string.Format("{0}: There was no identifier for the record", pathname)));
                            continue;
                        }
                        if (records.ContainsKey(identifier))
                        {
                            mainMergeEventListener.WarningOccurred(
                                new MergeWarning(string.Format("{0}: There is more than one element with the identifier '{1}'", pathname,
                                                               identifier)));
                        }
                        else
                        {
                            if (RemoveAmbiguousChildNodes)
                            {
                                var possiblyRevisedRecord = RemoveAmbiguousChildren(mainMergeEventListener, mergeStrategy.GetStrategies(), record, pathname);
                                records.Add(identifier, possiblyRevisedRecord);
                            }
                            else
                            {
                                records.Add(identifier, record);
                            }
                        }
                    }
                }
            }

            return records;
        }
示例#4
0
		/// <summary>
		/// Sort the provided lift file into canonical order.
		///
		/// The resulting sorted file will be in a canonical order for the attributes and elements
		/// </summary>
		/// <param name="liftPathname">The assumed main lift file in a folder.</param>
		public static void SortLiftFile(string liftPathname)
		{
			Guard.AgainstNullOrEmptyString(liftPathname, "liftPathname");
			Guard.Against(Path.GetExtension(liftPathname).ToLowerInvariant() != ".lift", "Unexpected file extension");
			Guard.Against<FileNotFoundException>(!File.Exists(liftPathname), "Lift file does not exist.");

			using (var tempFile = new TempFile(File.ReadAllText(liftPathname), Utf8))
			{
				var sortedRootAttributes = SortRootElementAttributes(tempFile.Path);
				var sortedEntries = new SortedDictionary<string, XElement>(StringComparer.InvariantCultureIgnoreCase);
				XElement header = null;
				using (var splitter = new FastXmlElementSplitter(tempFile.Path))
				{
					bool hasHeader;
					foreach (var record in splitter.GetSecondLevelElementStrings("header", "entry", out hasHeader))
					{
						XElement element = FixBadTextElements(record);
						SortAttributes(element);

						if (hasHeader)
						{
							hasHeader = false;
							header = element;
							SortHeader(header);
						}
						else
						{
							var guidKey = element.Attribute("guid").Value.ToLowerInvariant();
							if (!sortedEntries.ContainsKey(guidKey))
							{
								SortEntry(element);
								sortedEntries.Add(GetUniqueKey(sortedEntries.Keys, guidKey), element);
							}
						}
					}
				}

				using (var writer = XmlWriter.Create(tempFile.Path, CanonicalXmlSettings.CreateXmlWriterSettings()))
				{
					writer.WriteStartDocument();
					writer.WriteStartElement("lift");

					foreach (var rootAttributeKvp in sortedRootAttributes)
					{
						var keyParts = rootAttributeKvp.Key.Split(':');
						if (keyParts.Length > 1)
							writer.WriteAttributeString(keyParts[0], keyParts[1], null, rootAttributeKvp.Value);
						else
							writer.WriteAttributeString(rootAttributeKvp.Key, rootAttributeKvp.Value);
					}

					if (header != null)
					{
						WriteElement(writer, header);
					}

					foreach (var entryElement in sortedEntries.Values)
					{
						WriteElement(writer, entryElement);
					}

					writer.WriteEndElement();
					writer.WriteEndDocument();
				}
				File.Copy(tempFile.Path, liftPathname, true);
			}
		}