Parses the specified block of a text

///<summary>

///Parses the specified block of a text

///</summary>

///<return>

///Returns the end position of a parsed block.

///</return>

protected int ParseBlock(XmlNode parentNode,TagBase parentTag,string text,int position)

{

  #region  check the arguments

  if(parentNode==null)

  {

    throw new ArgumentNullException("parentNode");

  }

  checkTextAndPositionArguments(text,position);

  #endregion

  while(position<text.Length)

  {

    if(IsSkipWhiteSpace){SkipWhiteSpace(text,ref position);}

    if(position==text.Length){break:}

    #region Read the parent tag ending

    if(parentTag!=null)

    {

      int myParentTagEndingEndPosition=parentTag.MatchEnd(text,position);

      if(myParentTagEndPosition>=0)

      {

        position=myParentTagEndingEndPosition;

        return position;

      }

    }

    #endregion

 

    Type myTagType=IsTag(text,position);

    if(myTagType!=null)

    {

      #region Read a tag

      #region Create the tag class instance

      TypeBase myTag=Activator.CreateInstance(myTagType) as TagBase;

      position=myTag.InitializeFromText(this,text,position,parentTag);

      #endregion

      #region Create an xml node for the tag

      XmlNode myTagXmlNode=CreateTagXmlNode(myTag);

      parentNode.AppendChild(myTagXmlNode);

      #endregion

      if(myTag.HasContents){position=ParseBlock(myTagXmlNode,myTag,text,position);}

      #endregion

    }

    else

    {

    #region Read text

    string myText=ReadWordOrSeparator(text,ref position,!IsSkipWhiteSpace);

    parentNode.AppendChild(CreateTextXmlNode(myText));

    #endregion

    #endregion

    }

  }

  if(parentTag!=null&&!parentTag.CanTerminateByStringEnd)

  {

    throw new Exception("Invalidate format");

  }

  return position;

 

}

 

 

=========

注意:

///<summary>

///Return the match attribute of the specified class.This attribute is used to identify a tag in a text.

///</summary>

public static MatchTagAttributeBase GetMatchAttribute(Type tag)

{

  #region check the arguments

  if(tag==null)

  {

    throw new ArgumentNullException("tag");

  }

  #endregion

  object[] myMatchTagAttribute=tag.GetCustomAttributes(typeof(MatchTagAttributeBase),true);

  if(myMatchTagAttributes==null||myMatchTagAttributes.Length==0){throw new Exception("Canneot find a match tag attribute.");}

  if(myMatchTagAttributes.Length>1){throw new Exception("Ambiguous Match tag");}

  return myMatchTagAttributes[0] as MatchTagAttributeBase;

}

posted @ 2012-05-07 20:08  szjdw  阅读(180)  评论(0编辑  收藏  举报