protected override void LoadFromReader(Stream fileStream, StreamReader reader, string streamCharset = null) { fTree.State = GDMTreeState.osLoading; try { ProgressEventHandler progressHandler = fTree.OnProgress; long fileSize = fileStream.Length; int progress = 0; GDMIndividualRecord lastIndividual = null; GDMTag lastTag = null; GEDCOMTagType lastTagType = GEDCOMTagType.Unknown; XmlReaderSettings settings = new XmlReaderSettings(); settings.DtdProcessing = DtdProcessing.Ignore; using (XmlReader xr = XmlReader.Create(fileStream, settings)) { while (xr.Read()) { if (xr.NodeType == XmlNodeType.Element && !xr.IsEmptyElement) { if (xr.Name == "Person") { lastIndividual = fTree.CreateIndividual(); var persName = new GDMPersonalName(lastIndividual); lastIndividual.AddPersonalName(persName); lastIndividual.UID = xr.GetAttribute("Id"); } else if (xr.Name == "Gender") { lastTagType = GEDCOMTagType.SEX; lastTag = null; } else if (xr.Name == "FirstName") { lastTagType = GEDCOMTagType.GIVN; lastTag = null; } else if (xr.Name == "LastName") { lastTagType = GEDCOMTagType.SURN; lastTag = null; } } else if (xr.NodeType == XmlNodeType.Text) { string nodeValue = xr.Value; if (lastTag != null) { lastTag = null; } else { switch (lastTagType) { case GEDCOMTagType.SEX: if (nodeValue == "Male") { lastIndividual.Sex = GDMSex.svMale; } else if (nodeValue == "Female") { lastIndividual.Sex = GDMSex.svFemale; } break; case GEDCOMTagType.GIVN: lastIndividual.PersonalNames[0].FirstPart = nodeValue; break; case GEDCOMTagType.SURN: lastIndividual.PersonalNames[0].Surname = nodeValue; break; } lastTagType = GEDCOMTagType.Unknown; } } if (progressHandler != null) { int newProgress = (int)Math.Min(100, (fileStream.Position * 100.0f) / fileSize); if (progress != newProgress) { progress = newProgress; progressHandler(fTree, progress); } } } } } finally { fTree.State = GDMTreeState.osReady; } }
protected override void LoadFromReader(Stream fileStream, StreamReader reader, string streamCharset = null) { fTree.State = GDMTreeState.osLoading; try { ProgressEventHandler progressHandler = fTree.OnProgress; long fileSize = fileStream.Length; int progress = 0; GDMIndividualRecord lastIndividual = null; FXTag lastTagType = FXTag.Unknown; RelationshipType relationshipType = RelationshipType.None; var families = new List <FamilyRec>(); var children = new List <ChildRec>(); var indiIdents = new Dictionary <string, GDMIndividualRecord>(); XmlReaderSettings settings = new XmlReaderSettings(); settings.DtdProcessing = DtdProcessing.Ignore; using (XmlReader xr = XmlReader.Create(fileStream, settings)) { while (xr.Read()) { if (xr.NodeType == XmlNodeType.Element && !xr.IsEmptyElement) { string nodeType = xr.Name; if (nodeType == "Person") { lastIndividual = fTree.CreateIndividual(); var persName = new GDMPersonalName(); lastIndividual.AddPersonalName(persName); // add an empty birth event AddEvent(lastIndividual, GEDCOMTagName.BIRT); lastIndividual.UID = xr.GetAttribute("Id"); indiIdents.Add(lastIndividual.UID, lastIndividual); } else if (nodeType == "Gender") { lastTagType = FXTag.Gender; } else if (nodeType == "Restriction") { lastTagType = FXTag.Restriction; } else if (nodeType == "FirstName") { lastTagType = FXTag.FirstName; } else if (nodeType == "LastName") { lastTagType = FXTag.LastName; } else if (nodeType == "Suffix") { lastTagType = FXTag.Suffix; } else if (nodeType == "IsLiving") { lastTagType = FXTag.IsLiving; } else if (nodeType == "Note") { lastTagType = FXTag.Note; } else if (nodeType == "BirthDate") { lastTagType = FXTag.BirthDate; } else if (nodeType == "BirthPlace") { lastTagType = FXTag.BirthPlace; } else if (nodeType == "DeathDate") { lastTagType = FXTag.DeathDate; } else if (nodeType == "DeathPlace") { lastTagType = FXTag.DeathPlace; } else if (nodeType == "RelationshipType") { lastTagType = FXTag.Relationship; } else if (nodeType == "PersonId") { switch (relationshipType) { case RelationshipType.Spouse: lastTagType = FXTag.SpouseLink; break; case RelationshipType.Child: lastTagType = FXTag.ChildLink; break; default: lastTagType = FXTag.Unknown; break; } relationshipType = RelationshipType.None; } } else if (xr.NodeType == XmlNodeType.Text) { string nodeValue = xr.Value; if (!string.IsNullOrEmpty(nodeValue)) { switch (lastTagType) { case FXTag.Gender: if (nodeValue == "Male") { lastIndividual.Sex = GDMSex.svMale; } else if (nodeValue == "Female") { lastIndividual.Sex = GDMSex.svFemale; } break; case FXTag.Restriction: // FIXME: parse from nodeValue lastIndividual.Restriction = GDMRestriction.rnNone; break; case FXTag.FirstName: lastIndividual.PersonalNames[0].FirstPart = nodeValue; break; case FXTag.LastName: lastIndividual.PersonalNames[0].Surname = nodeValue; break; case FXTag.Suffix: lastIndividual.PersonalNames[0].LastPart = nodeValue; break; case FXTag.IsLiving: bool isLiving = string.Equals(nodeValue, "true", StringComparison.InvariantCultureIgnoreCase); if (!isLiving) { AddEvent(lastIndividual, GEDCOMTagName.DEAT); } break; case FXTag.BirthDate: case FXTag.BirthPlace: var birthEvent = lastIndividual.FindEvent("BIRT"); if (lastTagType == FXTag.BirthDate) { SetEventDate(birthEvent, nodeValue); } else { birthEvent.Place.StringValue = nodeValue; } break; case FXTag.DeathDate: case FXTag.DeathPlace: var deathEvent = lastIndividual.FindEvent("DEAT"); // IsLiving may be wrong if (deathEvent == null) { deathEvent = AddEvent(lastIndividual, GEDCOMTagName.DEAT); } if (lastTagType == FXTag.DeathDate) { SetEventDate(deathEvent, nodeValue); } else { deathEvent.Place.StringValue = nodeValue; } break; case FXTag.Note: AddNote(lastIndividual, nodeValue); break; case FXTag.Relationship: if (nodeValue == "Spouse") { relationshipType = RelationshipType.Spouse; } else if (nodeValue == "Child") { relationshipType = RelationshipType.Child; } break; case FXTag.SpouseLink: ProcessSpouse(families, lastIndividual, nodeValue); break; case FXTag.ChildLink: ProcessChild(children, lastIndividual, nodeValue); break; } lastTagType = FXTag.Unknown; } } if (progressHandler != null) { int newProgress = (int)Math.Min(100, (fileStream.Position * 100.0f) / fileSize); if (progress != newProgress) { progress = newProgress; progressHandler(fTree, progress); } } } } foreach (var fam in families) { GDMIndividualRecord husbRec; indiIdents.TryGetValue(fam.HusbandId, out husbRec); GDMIndividualRecord wifeRec; indiIdents.TryGetValue(fam.WifeId, out wifeRec); GDMFamilyRecord famRec = fTree.CreateFamily(); famRec.AddSpouse(husbRec); famRec.AddSpouse(wifeRec); } foreach (var child in children) { GDMIndividualRecord fathRec; indiIdents.TryGetValue(child.FatherId, out fathRec); GDMIndividualRecord mothRec; indiIdents.TryGetValue(child.MotherId, out mothRec); GDMIndividualRecord childRec; indiIdents.TryGetValue(child.ChildId, out childRec); GDMFamilyRecord famRec = GetParentsFamily(fathRec, mothRec); famRec.AddChild(childRec); } } finally { fTree.State = GDMTreeState.osReady; } }
public static bool ModifyIndividual(IBaseWindow baseWin, ref GDMIndividualRecord indivRec, GDMIndividualRecord target, TargetMode targetMode, GDMSex needSex) { bool result; try { baseWin.Context.BeginUpdate(); GDMTree tree = baseWin.Context.Tree; using (var dlg = AppHost.ResolveDialog <IPersonEditDlg>(baseWin)) { bool exists = (indivRec != null); if (!exists) { indivRec = new GDMIndividualRecord(tree); indivRec.InitNew(); indivRec.AddPersonalName(new GDMPersonalName(indivRec)); baseWin.Context.CreateEventEx(indivRec, GEDCOMTagName.BIRT, "", ""); } try { baseWin.Context.LockRecord(indivRec); dlg.Person = indivRec; if (targetMode != TargetMode.tmNone) { if (needSex == GDMSex.svMale || needSex == GDMSex.svFemale) { dlg.SetNeedSex(needSex); } dlg.TargetMode = targetMode; dlg.Target = target; } result = (AppHost.Instance.ShowModalX(dlg, false)); } finally { baseWin.Context.UnlockRecord(indivRec); } if (!exists) { if (result) { PostProcessPerson(baseWin, indivRec); tree.AddRecord(indivRec); } else { indivRec.Clear(); indivRec.Dispose(); indivRec = null; } } } } finally { baseWin.Context.EndUpdate(); } return(result); }