private Element makeLink()

in jspwiki-main/src/main/java/org/apache/wiki/parser/JSPWikiMarkupParser.java [263:384]


    private Element makeLink( int type, final String link, String text, String section, final Iterator< Attribute > attributes ) {
        Element el = null;
        if( text == null ) {
            text = link;
        }
        text = callMutatorChain( m_linkMutators, text );
        section = (section != null) ? ("#"+section) : "";

        // Make sure we make a link name that can be accepted  as a valid URL.
        if( link.isEmpty() ) {
            type = EMPTY;
        }
        final ResourceBundle rb = Preferences.getBundle( m_context, InternationalizationManager.CORE_BUNDLE );

        switch( type ) {
            case READ:
                el = createAnchor( READ, m_context.getURL( ContextEnum.PAGE_VIEW.getRequestContext(), link), text, section );
                break;

            case EDIT:
                el = createAnchor( EDIT, m_context.getURL( ContextEnum.PAGE_EDIT.getRequestContext(),link), text, "" );
                el.setAttribute("title", MessageFormat.format( rb.getString( "markupparser.link.create" ), link ) );
                break;

            case EMPTY:
                el = new Element("u").addContent(text);
                break;

            // These two are for local references - footnotes and references to footnotes.
            // We embed the page name (or whatever WikiContext gives us) to make sure the links are unique across Wiki.
            case LOCALREF:
                el = createAnchor( LOCALREF, "#ref-"+m_context.getName()+"-"+link, "["+text+"]", "" );
                break;

            case LOCAL:
                el = new Element( "a" ).setAttribute( "class", CLASS_FOOTNOTE );
                el.setAttribute( "name", "ref-" + m_context.getName() + "-" + link.substring( 1 ) );
                if( !m_allowHTML ) {
                    el.addContent( "[" + TextUtil.escapeHTMLEntities( text ) + "]" );
                } else {
                    el.addContent( "[" + text + "]" );
                }
                break;

                //  With the image, external and interwiki types we need to make sure nobody can put in Javascript or
                //  something else annoying into the links themselves.  We do this by preventing a haxor from stopping
                //  the link name short with quotes in fillBuffer().
            case IMAGE:
                el = new Element( "img" ).setAttribute( "class", "inline" );
                el.setAttribute( "src", link );
                el.setAttribute( "alt", text );
                break;

            case IMAGELINK:
                el = new Element( "img" ).setAttribute( "class", "inline" );
                el.setAttribute( "src", link );
                el.setAttribute( "alt", text );
                el = createAnchor( IMAGELINK, text, "", "" ).addContent( el );
                break;

            case IMAGEWIKILINK:
                final String pagelink = m_context.getURL( ContextEnum.PAGE_VIEW.getRequestContext(), text );
                el = new Element( "img" ).setAttribute( "class", "inline" );
                el.setAttribute( "src", link );
                el.setAttribute( "alt", text );
                el = createAnchor( IMAGEWIKILINK, pagelink, "", "" ).addContent( el );
                break;

            case EXTERNAL:
                el = createAnchor( EXTERNAL, link, text, section );
                if( m_useRelNofollow ) {
                    el.setAttribute( "rel", "nofollow" );
                }
                break;

            case INTERWIKI:
                el = createAnchor( INTERWIKI, link, text, section );
                break;

            case ATTACHMENT:
                final String attlink = m_context.getURL( ContextEnum.PAGE_ATTACH.getRequestContext(), link );
                final String infolink = m_context.getURL( ContextEnum.PAGE_INFO.getRequestContext(), link );
                final String imglink = m_context.getURL( ContextEnum.PAGE_NONE.getRequestContext(), "images/attachment_small.png" );
                el = createAnchor( ATTACHMENT, attlink, text, "" );
                if(  m_engine.getManager( AttachmentManager.class ).forceDownload( attlink ) ) {
                    el.setAttribute("download", "");
                }

                pushElement( el );
                popElement( el.getName() );

                if( m_useAttachmentImage ) {
                    el = new Element( "img" ).setAttribute( "src", imglink );
                    el.setAttribute( "border", "0" );
                    el.setAttribute( "alt", "(info)" );

                    el = new Element( "a" ).setAttribute( "href", infolink ).addContent( el );
                    el.setAttribute( "class", "infolink" );
                } else {
                    el = null;
                }
                break;

            default:
                break;
        }

        if( el != null && attributes != null ) {
            while( attributes.hasNext() ) {
                final Attribute attr = attributes.next();
                if( attr != null ) {
                    el.setAttribute( attr );
                }
            }
        }

        if( el != null ) {
            flushPlainText();
            m_currentElement.addContent( el );
        }
        return el;
    }