@ Override public void tag ( final Tag tag ) { if ( ! enabled ) { return ; } TagType tagType = tag . getType ( ) ; Element node ; switch ( tagType ) { case START : if ( domBuilder . config . isIgnoreWhitespacesBetweenTags ( ) ) { removeLastChildNodeIfEmptyText ( parentNode , false ) ; } node = createElementNode ( tag ) ; if ( domBuilder . config . isImpliedEndTags ( ) ) { while ( true ) { String parentNodeName = parentNode . getNodeName ( ) ; if ( ! implRules . implicitlyCloseParentTagOnNewTag ( parentNodeName , node . getNodeName ( ) ) ) { break ; } parentNode = parentNode . getParentNode ( ) ; if ( log . isDebugEnabled ( ) ) { log . debug ( "Implicitly closed tag <" + node . getNodeName ( ) + "> " ) ; } } } parentNode . addChild ( node ) ; if ( ! node . isVoidElement ( ) ) { parentNode = node ; } break ; case END : if ( domBuilder . config . isIgnoreWhitespacesBetweenTags ( ) ) { removeLastChildNodeIfEmptyText ( parentNode , true ) ; } String tagName = tag . getName ( ) . toString ( ) ; Node matchingParent = findMatchingParentOpenTag ( tagName ) ; if ( matchingParent == parentNode ) { // regular situation parentNode = parentNode . getParentNode ( ) ; break ; } if ( matchingParent == null ) { // matching open tag not found, remove it error ( "Orphan closed tag ignored: </" + tagName + "> " + tag . getTagPosition ( ) ) ; break ; } // try to close it implicitly if ( domBuilder . config . isImpliedEndTags ( ) ) { boolean fixed = false ; while ( implRules . implicitlyCloseParentTagOnTagEnd ( parentNode . getNodeName ( ) , tagName ) ) { parentNode = parentNode . getParentNode ( ) ; if ( log . isDebugEnabled ( ) ) { log . debug ( "Implicitly closed tag <" + tagName + ">" ) ; } if ( parentNode == matchingParent ) { parentNode = matchingParent . parentNode ; fixed = true ; break ; } } if ( fixed ) { break ; } } // matching tag found, but it is not a regular situation // therefore close all unclosed tags in between fixUnclosedTagsUpToMatchingParent ( tag , matchingParent ) ; break ; case SELF_CLOSING : if ( domBuilder . config . isIgnoreWhitespacesBetweenTags ( ) ) { removeLastChildNodeIfEmptyText ( parentNode , false ) ; } node = createElementNode ( tag ) ; parentNode . addChild ( node ) ; break ; } }
Visits tags .