Esempio n. 1
0
        public override bool Equals(object obj)
        {
            if (obj == null)
            {
                throw new ArgumentNullException();
            }

            if (!(obj is DateHebrew dh))
            {
                return(false);
            }

            if (!GedcomLine.CompareObjects(Day, dh.Day))
            {
                return(false);
            }

            if (!GedcomLine.CompareObjects(Month, dh.Month))
            {
                return(false);
            }

            if (!GedcomLine.CompareObjects(Year, dh.Year))
            {
                return(false);
            }

            return(true);
        }
        public void Parse(Stream stream)
        {
            if (stream == null)
            {
                throw new InvalidOperationException("Stream cannot be null");
            }

            GedcomLine lastLine = default;

            using (var reader = new StreamReader(stream))
            {
                var firstRawLine = reader.ReadLine();

                if (firstRawLine == null)
                {
                    throw new InvalidOperationException("File empty");
                }

                var firstLine = ParserHelper.ParseLine(firstRawLine);
                if (firstLine.Level != 0 && firstLine.GetTagOrRef() != "HEAD")
                {
                    throw new InvalidOperationException("GEDCOM Header Not Found");
                }

                LineProvider lineProvider = new LineProvider(reader);

                var gedcomHeaderParse = HeaderParser.Parse(firstLine, lineProvider);
                _headerCallback?.Invoke(gedcomHeaderParse.Result);

                var newLine = gedcomHeaderParse.Line;
                lastLine = newLine;

                while (!newLine.Equals(default))
Esempio n. 3
0
        public static ParseResult <GedcomEvent> Parse(GedcomLine first, ILineProvider lineProvider)
        {
            GedcomEvent gedcomEvent = new GedcomEvent();

            var initialLevel = first.Level;

            GedcomLine line = default;
            string     currentRawLine;

            while ((currentRawLine = lineProvider.ReadLine()) != null)
            {
                line = ParserHelper.ParseLine(currentRawLine);

                if (line.Level <= first.Level)
                {
                    break;
                }

                switch (line.GetTagOrRef())
                {
                case "DATE":
                    // If checks we're parsing actual date and not
                    // CREA or CHAN tags
                    // TODO: should actually put CREA and CHAN into different parser
                    if (line.Level == initialLevel + 1)
                    {
                        gedcomEvent.Date = line.GetLineContent();
                    }
                    break;

                case "PLAC":
                    // If checks we're parsing actual date and not
                    // CREA or CHAN tags
                    // TODO: should actually put CREA and CHAN into different parser
                    if (line.Level == initialLevel + 1)
                    {
                        gedcomEvent.Location = line.GetLineContent();
                    }
                    break;
                }
            }

            return(ParseResult.Create(gedcomEvent, line));
        }
Esempio n. 4
0
        private GedcomLine GetNextLine()
        {
            var line        = _reader.ReadLine();
            var currentLine = _nextLine;

            if (line != null)
            {
                _nextLine = new GedcomLine(line);

                if (currentLine == null)
                {
                    currentLine = _nextLine;
                    GetNextLine();
                }
            }
            else
            {
                _nextLine = null;
            }

            return(currentLine);
        }
Esempio n. 5
0
        /// <summary>
        /// returns the tag of the given gedcom line
        /// </summary>
        /// <param name="tags">the types with their tags of the object</param>
        /// <param name="obj">an gedcom line</param>
        /// <returns>the tag for the given gedcom line</returns>
        private static string GetTag(IDictionary <Type, string> tags, GedcomLine obj)
        {
            /* one property might have multiple types, each with multiple tags.
             * now we search for the correct type */
            foreach (var(key, value) in tags)
            {
                if (key == obj.GetType())
                {
                    /* t.Value == null indicates, that there are multiple tag with the same type
                     * so we take the correct tagStr from the object's tagStr-property */
                    var tag = value ?? obj.Tag;

                    if (tag == "")
                    {
                        continue;
                    }

                    return(tag);
                }
            }

            return("");
        }
Esempio n. 6
0
        public static ParseResult <GedcomHeader> Parse(GedcomLine first, ILineProvider lineProvider)
        {
            CurrentLevel currentLevel = CurrentLevel.None;

            var header = new GedcomHeader();

            GedcomLine line = default;
            string     currentRawLine;

            while ((currentRawLine = lineProvider.ReadLine()) != null)
            {
                line = ParserHelper.ParseLine(currentRawLine);

                if (line.Level == 0)
                {
                    break;
                }

                if (line.Level == 1)
                {
                    switch (line.GetTagOrRef())
                    {
                    case "SOUR":
                        currentLevel = CurrentLevel.Sour;
                        break;

                    case "GEDC":
                        currentLevel = CurrentLevel.Gedc;
                        break;

                    case "CHAR":
                        header.GedcomCharacterSet = line.GetLineContent();
                        break;
                    }
                }
                else if (line.Level == 2)
                {
                    if (currentLevel == CurrentLevel.Sour)
                    {
                        switch (line.GetTagOrRef())
                        {
                        case "NAME":
                            header.SourceName = line.GetLineContent();
                            break;

                        case "VERS":
                            header.SourceVers = line.GetLineContent();
                            break;

                        case "CORP":
                            header.SourceCorp = line.GetLineContent();
                            break;
                        }
                    }
                    else if (currentLevel == CurrentLevel.Gedc)
                    {
                        if (line.GetTagOrRef() == "VERS")
                        {
                            header.GedcomVers = line.GetLineContent();
                        }
                    }
                }
            }

            return(ParseResult.Create(header, line));
        }
Esempio n. 7
0
	public override bool Read()
	{
		try
		{
			switch (readState)
			{
				case ReadState.Initial:
				{
					nodeStack.Push(new GedcomElement("GEDCOM", -1));
					readState = ReadState.Interactive;
					return true;
				}
				case ReadState.Interactive:
				{
					if (GetCurrentNode() is GedcomElement)
					{
						GedcomElement ge = GetCurrentNode() as GedcomElement;
						if (ge != null && ge.IsAttribute || ge.IsText)
							ge.MoveToElement();
					}

					if (GetCurrentNode().NodeType == XmlNodeType.EndElement)
					{
						// pop until you hit an element node
						while ( ((IGedcomNode)nodeStack.Pop()).NodeType != XmlNodeType.Element) ;

						int curLineNumber = GetCurrentLineNumber();
						if (currentLineNodes.LineNumber <= curLineNumber)
							// need to close previous element
							nodeStack.Push(new GedcomEndElement(curLineNumber));
						else 
							// just push new elements, they're children
							nodeStack.Push(currentLineNodes.Current);
					}
					else if (currentLineNodes != null && !currentLineNodes.EOF)
					{
						currentLineNodes.MoveNext();
						nodeStack.Push(currentLineNodes.Current);
					}
					else
					{
						// need to parse a new line
						currentLineText = fileReader.ReadLine();
						// detect EOF
						if (currentLineText == null)
						{
							readState = ReadState.EndOfFile;
							return false;
						}
						// parse text into logical nodes
						currentLineNodes = ParseGedcomLine(currentLineText);
						// see if we need to insert end element
						int curLineNumber = GetCurrentLineNumber();
						if (currentLineNodes.LineNumber <= curLineNumber)
							// need to close previous element
							nodeStack.Push(new GedcomEndElement(curLineNumber));
						else 
							// just push new elements, they're children
							nodeStack.Push(currentLineNodes.Current);
					}
					return true;
				}
				default:
					return false;
			}
		}
		catch(Exception e)
		{
			readState = ReadState.Error;
			throw e;
		}
	}
Esempio n. 8
0
	private GedcomLine ParseGedcomLine(string lineText)
	{
		string xref_id="", tag="", pointer="", linevalue="";
		string[] lineParts = lineText.Split(' ');
		int nextPart = 0;

		if (lineParts.Length < 2)
			throw new Exception("invalid GEDCOM line");

		// first part is always the line number
		int lineNumber = int.Parse(lineParts[nextPart++]);	
		if (lineParts[nextPart].StartsWith("@"))
		{
			if (lineParts.Length < 3)
				throw new Exception("invalid GEDCOM line");
			// this is an xref_id, next part is the tag
			xref_id = lineParts[nextPart++].Replace("@", "");
			tag = lineParts[nextPart++];
		}
		else
			// no xref_id, first part is tag
			tag = lineParts[nextPart++];

		if (lineParts.Length > nextPart)
		{
			if (lineParts[nextPart].StartsWith("@"))
				pointer = lineParts[nextPart++].Replace("@", "");
		}
		if (lineParts.Length > nextPart)
			linevalue = GetRemainingValue(lineParts, nextPart);

		GedcomLine line = new GedcomLine(lineNumber);
		GedcomElement e = new GedcomElement(tag, lineNumber);
		if (xref_id != "")
			e.AddAttribute(new GedcomAttribute("id", xref_id, lineNumber));
		if (pointer != "")
			e.AddAttribute(new GedcomAttribute("idref", pointer, lineNumber));
		line.Add(e);
		if (linevalue != "")
		{
			e.AddAttribute(new GedcomAttribute("value", linevalue, lineNumber));
			//GedcomText t = new GedcomText(linevalue, lineNumber);
			//line.Add(t);
		}
		return line;
	}
Esempio n. 9
0
        public static ParseResult <Family> Parse(GedcomLine first, ILineProvider lineProvider)
        {
            var family = new Family();

            family.ID = ParserHelper.ParseID(first.GetTagOrRef());

            bool inMarriage = false;

            var initialLevel = first.Level;

            GedcomLine line = default;
            string     currentRawLine;

            while ((currentRawLine = lineProvider.ReadLine()) != null)
            {
                line = ParserHelper.ParseLine(currentRawLine);

                if (line.Level == first.Level)
                {
                    break;
                }

                switch (line.GetTagOrRef())
                {
                case "MARR":
                {
                    inMarriage = true;
                    break;
                }

                case "DATE":
                    if (inMarriage)     // TODO: should have MARR parser
                    {
                        var date = line.GetLineContent();
                        if (family.Marriage == null)
                        {
                            family.Marriage = new GedcomEvent();
                        }
                        family.Marriage.Date = date;
                    }
                    break;

                case "PLAC":
                    if (inMarriage)     // Assume level + 1 is MARR
                    {
                        var place = line.GetLineContent();
                        if (family.Marriage == null)
                        {
                            family.Marriage = new GedcomEvent();
                        }
                        family.Marriage.Location = place;
                    }
                    break;

                case "HUSB":
                    // Ignore any husband and wife information in the middle of a marriage tag.
                    // Present for torture test files - and info redundant?
                    // can have e.g. "2 HUSB", with no additional info
                    var contentHusb = line.GetLineContent();
                    if (!string.IsNullOrEmpty(contentHusb))
                    {
                        family.HusbandID = ParserHelper.ParseID(contentHusb);
                    }
                    break;

                case "WIFE":
                    // Ignore any husband and wife information in the middle of a marriage tag.
                    // Present for torture test files - and info redundant?
                    // can have e.g. "2 HUSB", with no additional info
                    var contentWife = line.GetLineContent();
                    if (!string.IsNullOrEmpty(contentWife))
                    {
                        family.WifeID = ParserHelper.ParseID(contentWife);
                    }
                    break;

                case "CHIL":
                    family.ChildIDs.Add(ParserHelper.ParseID(line.GetLineContent()));
                    break;

                default:
                    inMarriage = false;
                    break;
                }
            }

            return(ParseResult.Create(family, line));
        }
Esempio n. 10
0
 public ParseResult(T result, GedcomLine line)
 {
     Result = result;
     _line  = line;
 }
        public void Parse(ILineProvider lineProvider)
        {
            GedcomLine lastLine = default;

            var firstRawLine = lineProvider.ReadLine();

            if (firstRawLine == null)
            {
                throw new InvalidOperationException("File empty");
            }

            var firstLine = ParserHelper.ParseLine(firstRawLine);

            if (firstLine.Level != 0 && !ParserHelper.Equals(firstLine.GetFirstItem(), "HEAD"))
            {
                throw new InvalidOperationException("GEDCOM Header Not Found");
            }

            var gedcomHeaderParse = HeaderParser.Parse(firstLine, lineProvider);

            _headerCallback?.Invoke(gedcomHeaderParse.Result);

            var newLine = gedcomHeaderParse.Line;

            lastLine = newLine;

            while (newLine.LineContent.Length > 0)
            {
                var content = newLine.GetLineContent();
                if (content.Length == 0)
                {
                    var unrecognisedRawLine = lineProvider.ReadLine();
                    if (unrecognisedRawLine != null)
                    {
                        newLine  = ParserHelper.ParseLine(unrecognisedRawLine);
                        lastLine = newLine;
                    }
                    else
                    {
                        newLine = default;
                    }
                    continue;
                }

                var unknown = false;

                if (ParserHelper.Equals(content, "INDI"))
                {
                    var individualParseResult = IndividualParser.Parse(newLine, lineProvider);
                    _individualCallback?.Invoke(individualParseResult.Result);
                    newLine  = individualParseResult.Line;
                    lastLine = newLine;
                }
                else if (ParserHelper.Equals(content, "FAM"))
                {
                    var familyParseResult = FamilyParser.Parse(newLine, lineProvider);
                    _familyCallback?.Invoke(familyParseResult.Result);
                    newLine  = familyParseResult.Line;
                    lastLine = newLine;
                }
                else if (ParserHelper.Equals(content, "NOTE"))
                {
                    var noteParseResult = NoteParser.Parse(newLine, lineProvider);
                    _noteCallback?.Invoke(noteParseResult.Result);
                    newLine  = noteParseResult.Line;
                    lastLine = newLine;
                }
                else if (ParserHelper.Equals(content, "OBJE"))
                {
                    var objParserResult = ObjectParser.Parse(newLine, lineProvider);
                    _imageCallback?.Invoke(objParserResult.Result);
                    newLine  = objParserResult.Line;
                    lastLine = newLine;
                }
                else
                {
                    var unrecognisedRawLine = lineProvider.ReadLine();
                    if (unrecognisedRawLine != null)
                    {
                        newLine  = ParserHelper.ParseLine(unrecognisedRawLine);
                        lastLine = newLine;
                    }
                    else
                    {
                        newLine = default;
                    }
                    unknown = true;
                }

                if (unknown)
                {
                    continue;
                }
            }

            if (lastLine.LineContent.Length == 0)
            {
                throw new InvalidOperationException("file contains no content");
            }

            if (lastLine.Level != 0 || !ParserHelper.Equals(lastLine.GetFirstItem(), "TRLR"))
            {
                throw new InvalidOperationException("GEDCOM TRLR not found");
            }
        }