Example #1
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;
            }
        }
Example #2
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;
            }
        }
Example #3
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);
        }
Example #4
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;
        }