private String figureOutURL()

in jspwiki-main/src/main/java/org/apache/wiki/tags/LinkTag.java [171:262]


    private String figureOutURL() throws ProviderException {
        String url = null;
        final Engine engine = m_wikiContext.getEngine();

        if( m_pageName == null ) {
            final Page page = m_wikiContext.getPage();
            if( page != null ) {
                m_pageName = page.getName();
            }
        }

        if( m_templatefile != null ) {
            final String params = addParamsForRecipient( null, m_containedParams );
            final String template = engine.getTemplateDir();
            url = engine.getURL( ContextEnum.PAGE_NONE.getRequestContext(), "templates/"+template+"/"+m_templatefile, params );
        } else if( m_jsp != null ) {
            final String params = addParamsForRecipient( null, m_containedParams );
            //url = m_wikiContext.getURL( ContextEnum.PAGE_NONE.getRequestContext(), m_jsp, params );
            url = engine.getURL( ContextEnum.PAGE_NONE.getRequestContext(), m_jsp, params );
        } else if( m_ref != null ) {
            final int interwikipoint;
            if( new LinkParsingOperations( m_wikiContext ).isExternalLink(m_ref) ) {
                url = m_ref;
            } else if( ( interwikipoint = m_ref.indexOf( ":" ) ) != -1 ) {
                final String extWiki = m_ref.substring( 0, interwikipoint );
                final String wikiPage = m_ref.substring( interwikipoint+1 );

                url = engine.getInterWikiURL( extWiki );
                if( url != null ) {
                    url = TextUtil.replaceString( url, "%s", wikiPage );
                }
            } else if( m_ref.startsWith("#") ) {
                // Local link
            } else if( TextUtil.isNumber(m_ref) ) {
                // Reference
            } else {
                final int hashMark;

                final String parms = (m_version != null) ? "version="+getVersion() : null;

                //  Internal wiki link, but is it an attachment link?
                final Page p = engine.getManager( PageManager.class ).getPage( m_pageName );
                if( p instanceof Attachment ) {
                    url = m_wikiContext.getURL( ContextEnum.PAGE_ATTACH.getRequestContext(), m_pageName );
                } else if( (hashMark = m_ref.indexOf('#')) != -1 ) {
                    // It's an internal Wiki link, but to a named section

                    final String namedSection = m_ref.substring( hashMark+1 );
                    String reallink     = m_ref.substring( 0, hashMark );
                    reallink = MarkupParser.cleanLink( reallink );

                    String matchedLink;
                    String sectref = "";
                    if( ( matchedLink = engine.getFinalPageName( reallink ) ) != null ) {
                        sectref = "section-" + engine.encodeName( matchedLink ) + "-" + namedSection;
                        sectref = "#" + sectref.replace( '%', '_' );
                    } else {
                        matchedLink = reallink;
                    }

                    url = makeBasicURL( m_context, matchedLink, parms ) + sectref;
                } else {
                    final String reallink = MarkupParser.cleanLink( m_ref );
                    url = makeBasicURL( m_context, reallink, parms );
                }
            }
        } else if( m_pageName != null && !m_pageName.isEmpty() ) {
            final Page p = engine.getManager( PageManager.class ).getPage( m_pageName );

            String parms = (m_version != null) ? "version="+getVersion() : null;

            parms = addParamsForRecipient( parms, m_containedParams );

            if( p instanceof Attachment ) {
                String ctx = m_context;
                // Switch context appropriately when attempting to view an
                // attachment, but don't override the context setting otherwise
                if( m_context == null || m_context.equals( ContextEnum.PAGE_VIEW.getRequestContext() ) ) {
                    ctx = ContextEnum.PAGE_ATTACH.getRequestContext();
                }
                url = engine.getURL( ctx, m_pageName, parms );
                //url = m_wikiContext.getURL( ctx, m_pageName, parms );
            } else {
                url = makeBasicURL( m_context, m_pageName, parms );
            }
        } else {
            final String page = engine.getFrontPage();
            url = makeBasicURL( m_context, page, null );
        }

        return url;
    }