private String discoverTagName(RSyntaxDocument doc,int dot){
  Stack stack=new Stack();
  Element root=doc.getDefaultRootElement();
  int curLine=root.getElementIndex(dot);
  for (int i=0; i <= curLine; i++) {
    Token t=doc.getTokenListForLine(i);
    while (t != null && t.isPaintable()) {
      if (t.type == Token.MARKUP_TAG_DELIMITER) {
        if (t.isSingleChar('<') || t.isSingleChar('[')) {
          t=t.getNextToken();
          while (t != null && t.isPaintable()) {
            if (t.type == Token.MARKUP_TAG_NAME || t.type == Token.MARKUP_TAG_ATTRIBUTE) {
              stack.push(t.getLexeme());
              break;
            }
            t=t.getNextToken();
          }
        }
 else         if (t.textCount == 2 && t.text[t.textOffset] == '/' && (t.text[t.textOffset + 1] == '>' || t.text[t.textOffset + 1] == ']')) {
          if (!stack.isEmpty()) {
            stack.pop();
          }
        }
 else         if (t.textCount == 2 && (t.text[t.textOffset] == '<' || t.text[t.textOffset] == '[') && t.text[t.textOffset + 1] == '/') {
          String tagName=null;
          if (!stack.isEmpty()) {
            tagName=(String)stack.pop();
          }
          if (t.offset + t.textCount >= dot) {
            return tagName;
          }
        }
      }
      t=t.getNextToken();
    }
  }
  return null;
}

private String discoverTagName ( RSyntaxDocument doc , int dot ) { Stack stack = new Stack ( ) ; Element root = doc . getDefaultRootElement ( ) ; int curLine = root . getElementIndex ( dot ) ; for ( int i = 0 ; i <= curLine ; i ++ ) { Token t = doc . getTokenListForLine ( i ) ; while ( t != null && t . isPaintable ( ) ) { if ( t . type == Token . MARKUP_TAG_DELIMITER ) { if ( t . isSingleChar ( '<' ) || t . isSingleChar ( '[' ) ) { t = t . getNextToken ( ) ; while ( t != null && t . isPaintable ( ) ) { if ( t . type == Token . MARKUP_TAG_NAME || t . type == Token . MARKUP_TAG_ATTRIBUTE ) { stack . push ( t . getLexeme ( ) ) ; break ; } t = t . getNextToken ( ) ; } } else if ( t . textCount == 2 && t . text [ t . textOffset ] == '/' && ( t . text [ t . textOffset + 1 ] == '>' || t . text [ t . textOffset + 1 ] == ']' ) ) { if ( ! stack . isEmpty ( ) ) { stack . pop ( ) ; } } else if ( t . textCount == 2 && ( t . text [ t . textOffset ] == '<' || t . text [ t . textOffset ] == '[' ) && t . text [ t . textOffset + 1 ] == '/' ) { String tagName = null ; if ( ! stack . isEmpty ( ) ) { tagName = ( String ) stack . pop ( ) ; } if ( t . offset + t . textCount >= dot ) { return tagName ; } } } t = t . getNextToken ( ) ; } } return null ; }
discovers the name of the tag being closed . assumes standard sgml - style markup tags .
