Chinaunix首页 | 论坛 | 博客
  • 博客访问: 845921
  • 博文数量: 756
  • 博客积分: 40000
  • 博客等级: 大将
  • 技术积分: 4980
  • 用 户 组: 普通用户
  • 注册时间: 2008-10-13 14:40
文章分类

全部博文(756)

文章存档

2011年(1)

2008年(755)

我的朋友

分类:

2008-10-13 16:09:06

0 HEAD
1 SOUR AcmeGen
2 VERS 5.0
2 NAME Acme Genealogy
1 DATE 13 February 2004
2 TIME 03:50:51
1 GEDC
2 VERS 5.5
2 FORM LINEAGE-LINKED
1 CHAR ANSEL
1 SUBM @SUB01@
1 SUBN @N01@
0 @SUB01@ SUBM
1 NAME Aaron Skonnard
1 ADDR 456 Main
2 CONT Salt Lake City, Utah 84150
0 @N01@ SUBN
1 DESC 1
1 ORDI N
0 @I100294220729@ INDI
1 NAME Bernt Olaf /Skonnord/
1 SEX M
1 BIRT
2 DATE 14 JUN 1860
2 PLAC Snertingdal, Oppland, Norway
1 FAMC @F306458249@
1 SOUR @S01@
...


  
    
      
      
    
    
      
    
      
      
...

// standard interface for all XML nodes that we'll be 
// creating from the GEDCOM 5.5 file

public interface IGedcomNode
{
    string Name {get;}
    string Value {get;}
    int LineNumber {get;}
    XmlNodeType NodeType {get;}
}

public class GedcomAttribute : IGedcomNode
{
    ... // represents a value from the GEDCOM file that will 
        // now become an XML attribute node
}

public class GedcomElement : IGedcomNode
{
    ... // represents a tag from the GEDCOM file that will 
        // now become an XML element node
}

public class GedcomEndElement : IGedcomNode
{
    ... // a node that marks the end of an XML element node
}

public class GedcomText : IGedcomNode
{
    ... // represents a value from the GEDCOM file that will 
        // now become an XML text node
}

public class GedcomLine
{
    ... // represents a GEDCOM 5.5 line that has been parsed
        // into an array of IGedcomNode objects (see above)
}

public class GedcomReader :    XmlReader
{
  ...
  private GedcomLine ParseGedcomLine(string lineText)
  {
    string xref_id="", tag="", pointer="", linevalue="";
    int nextPart = 0;

    // split GEDCOM line into parts
    string[] lineParts = lineText.Split(' ');
    // first part is always the line number
    int lineNumber = int.Parse(lineParts[nextPart++]);    
    // check to see if line has an ID and get tag name 
    if (lineParts[nextPart].StartsWith("@"))
    {
       xref_id = lineParts[nextPart++].Replace("@", "");
       tag = lineParts[nextPart++];
    }
    else
      tag    = lineParts[nextPart++];

    // check to see if value is a pointer or text
    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));
    return line;
  }

  public override bool Read()
  {
    try
    {
      switch (readState)
      {
        case ReadState.Initial:
        {
          nodeStack.Push(new GedcomElement("GEDCOM", -1));
        readState =    ReadState.Interactive;
        return true;
        }
        case ReadState.Interactive:
        {
          // deal with attributes and end element nodes
          ... 
          if (currentLineNodes != null && !currentLineNodes.EOF)
          {
            currentLineNodes.MoveNext();
            nodeStack.Push(currentLineNodes.Current);
          }
          else
          {
            // need to parse a new GEDCOM line
            currentLineText = fileReader.ReadLine();
            if (currentLineText == null)
            {
              readState = ReadState.EndOfFile;
              return false;
            }
            currentLineNodes = ParseGedcomLine(currentLineText);
            ...
            nodeStack.Push(currentLineNodes.Current);
          }
          return true;
        } 
        default:
          return false;
      }
    }
    catch(Exception e)
    {
      readState = ReadState.Error;
      throw e;
    }
  }
  ... // remaining methods omitted
}

public class GedcomReader :    XmlReader
{
  ... // remaining methods omitted

  public override string LocalName
  {
    get
    {
      if (readState != ReadState.Interactive)
        return String.Empty;
      IGedcomNode node = nodeStack.Peek() as IGedcomNode;
      return node.Name;
    }
  }

  public override string Name
  {
    get
    {
      if (readState != ReadState.Interactive)
        return String.Empty;
      IGedcomNode node = nodeStack.Peek() as IGedcomNode;
      return node.Name;
    }
  }

  public override XmlNodeType NodeType
  {
    get
    {
      if (readState != ReadState.Interactive)
        return XmlNodeType.None;
      IGedcomNode node = nodeStack.Peek() as IGedcomNode;
      return node.NodeType;
    }
  }

  public override string Value
  {
    get
    {
      IGedcomNode node = nodeStack.Peek() as IGedcomNode;
      return node.Value;
    }
  }
}



  

  
    
      ...
      
      ...
    
  

  
    
      
        
        
       
       
      
        dead         
      
      
      
        
          
          
            
              FamilyRec
              
             
            
              IndividualRec
              
             
            ...
          
           
          
            
           
          
        
      
      
    
  

  ... 



--------------------next---------------------

阅读(386) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~