Search in sources :

Example 1 with LinkParsingOperations

use of org.apache.wiki.parser.LinkParsingOperations in project jspwiki by apache.

the class LinkTag method figureOutURL.

/**
 *  This method figures out what kind of an URL should be output.  It mirrors heavily
 *  on JSPWikiMarkupParser.handleHyperlinks();
 *
 * @return the URL
 * @throws ProviderException
 */
private String figureOutURL() throws ProviderException {
    String url = null;
    WikiEngine engine = m_wikiContext.getEngine();
    if (m_pageName == null) {
        WikiPage page = m_wikiContext.getPage();
        if (page != null) {
            m_pageName = page.getName();
        }
    }
    if (m_templatefile != null) {
        String params = addParamsForRecipient(null, m_containedParams);
        String template = engine.getTemplateDir();
        url = engine.getURL(WikiContext.NONE, "templates/" + template + "/" + m_templatefile, params, false);
    } else if (m_jsp != null) {
        String params = addParamsForRecipient(null, m_containedParams);
        // url = m_wikiContext.getURL( WikiContext.NONE, m_jsp, params );
        url = engine.getURL(WikiContext.NONE, m_jsp, params, m_absolute);
    } else if (m_ref != null) {
        int interwikipoint;
        if (new LinkParsingOperations(m_wikiContext).isExternalLink(m_ref)) {
            url = m_ref;
        } else if ((interwikipoint = m_ref.indexOf(":")) != -1) {
            String extWiki = m_ref.substring(0, interwikipoint);
            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 {
            int hashMark = -1;
            String parms = (m_version != null) ? "version=" + getVersion() : null;
            // 
            // Internal wiki link, but is it an attachment link?
            // 
            WikiPage p = engine.getPage(m_pageName);
            if (p instanceof Attachment) {
                url = m_wikiContext.getURL(WikiContext.ATTACH, m_pageName);
            } else if ((hashMark = m_ref.indexOf('#')) != -1) {
                // It's an internal Wiki link, but to a named section
                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, m_absolute) + sectref;
            } else {
                String reallink = MarkupParser.cleanLink(m_ref);
                url = makeBasicURL(m_context, reallink, parms, m_absolute);
            }
        }
    } else if (m_pageName != null && m_pageName.length() > 0) {
        WikiPage p = engine.getPage(m_pageName);
        String parms = (m_version != null) ? "version=" + getVersion() : null;
        parms = addParamsForRecipient(parms, m_containedParams);
        if (p instanceof Attachment) {
            String ctx = m_context;
            // attachment, but don't override the context setting otherwise
            if (m_context == null || m_context.equals(WikiContext.VIEW)) {
                ctx = WikiContext.ATTACH;
            }
            url = engine.getURL(ctx, m_pageName, parms, m_absolute);
        // url = m_wikiContext.getURL( ctx, m_pageName, parms );
        } else {
            url = makeBasicURL(m_context, m_pageName, parms, m_absolute);
        }
    } else {
        String page = engine.getFrontPage();
        url = makeBasicURL(m_context, page, null, m_absolute);
    }
    return url;
}
Also used : LinkParsingOperations(org.apache.wiki.parser.LinkParsingOperations) WikiPage(org.apache.wiki.WikiPage) Attachment(org.apache.wiki.attachment.Attachment) WikiEngine(org.apache.wiki.WikiEngine)

Aggregations

WikiEngine (org.apache.wiki.WikiEngine)1 WikiPage (org.apache.wiki.WikiPage)1 Attachment (org.apache.wiki.attachment.Attachment)1 LinkParsingOperations (org.apache.wiki.parser.LinkParsingOperations)1