protected String transformHtmlLinks ( String content , ReferenceContext references ) { if ( ! isPureHtmlReferenceTransformation ( ) ) { return content ; } // Prepare replacement data structures final Map < String , String > replacements = new TreeMap <> ( ) ; // Visit the links and record the transformations final org . jsoup . select . NodeVisitor visitor = new org . jsoup . select . NodeVisitor ( ) { @ Override public void tail ( org . jsoup . nodes . Node node , int index ) { // } @ Override public void head ( org . jsoup . nodes . Node node , int index ) { if ( node instanceof Element ) { final Element tag = ( Element ) node ; if ( "a" . equals ( tag . nodeName ( ) ) && tag . hasAttr ( "href" ) ) { //$NON-NLS-1$ //$NON-NLS-2$ final String href = tag . attr ( "href" ) ; //$NON-NLS-1$ if ( ! Strings . isEmpty ( href ) ) { URL url = FileSystem . convertStringToURL ( href , true ) ; url = transformURL ( url , references ) ; if ( url != null ) { replacements . put ( href , convertURLToString ( url ) ) ; } } } } } } ; final Document htmlDocument = Jsoup . parse ( content ) ; htmlDocument . traverse ( visitor ) ; // Apply the replacements if ( ! replacements . isEmpty ( ) ) { String buffer = content ; for ( final Entry < String , String > entry : replacements . entrySet ( ) ) { final String source = entry . getKey ( ) ; final String dest = entry . getValue ( ) ; buffer = buffer . replaceAll ( Pattern . quote ( source ) , Matcher . quoteReplacement ( dest ) ) ; } return buffer ; } return content ; }
Apply link transformation on the HTML links .