private TagBound HasSubBlock(BlockBound blkBnd) { TagBound tagEnd = EndIndexOf(blkBnd.End); if (tagEnd == null) { blkBnd.End = tmplBuf.Length; return(null); } if (GetEndName(tagEnd).Equals(blkBnd.Name)) { int start = TrimIndexRightFrom(tagEnd.PrefixIndex); blkBnd.End = start; Trim(start, tagEnd.SuffixIndex + endSuffix.Length); return(null); } return(tagEnd); }
public bool Build() { if (tmplBuf == null) { return(false); } //depth first Stack stack = new Stack(); int index = 0; string name = ""; BlockParser block = new BlockParser(); block.Name = name; if (blockVec == null) { blockVec = new ArrayList(); } else { blockVec.Clear(); } index = blockVec.Add(block); BlockBound blkBnd = new BlockBound(name, 0, index); stack.Push(blkBnd); int fromIndex = 0; string tag = null; TagBound tagBegin = null; TagBound tagEnd = null; while (stack.Count > 0) { while ((tagEnd = HasSubBlock(blkBnd)) != null) { tagBegin = StartIndexOf(blkBnd.Begin); if (tagBegin.IsGreat(tagEnd) || (name = GetStartName(tagBegin)).Length == 0) { blockVec.Clear(); return(false); } fromIndex = TrimIndexRightFrom(tagBegin.PrefixIndex); Trim(fromIndex, tagBegin.SuffixIndex + startSuffix.Length); block = new BlockParser(); block.Name = name; index = blockVec.Add(block); blkBnd = new BlockBound(name, fromIndex, index); stack.Push(blkBnd); } stack.Pop(); block = (BlockParser)blockVec[blkBnd.Index]; if (stack.Count == 0) { block.TmplBlock = tmplBuf.ToString(); block.Parent = null; } else { block.TmplBlock = tmplBuf.ToString().Substring(blkBnd.Begin, blkBnd.End - blkBnd.Begin); tag = new StringBuilder().Append(tagPrefix).Append(blkBnd.Name).Append(tagSuffix).ToString(); tmplBuf.Remove(blkBnd.Begin, blkBnd.End - blkBnd.Begin); tmplBuf.Insert(blkBnd.Begin, tag); fromIndex = blkBnd.Begin + tag.Length; blkBnd = (BlockBound)stack.Peek(); block.Parent = (BlockParser)blockVec[blkBnd.Index]; blkBnd.End = fromIndex; } } tmplBuf = null; return(true); }