001/*
002 * (C) Copyright 2006-2010 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Thierry Delprat
016 */
017package org.nuxeo.apidoc.wiki;
018
019import java.util.regex.Matcher;
020import java.util.regex.Pattern;
021
022import org.nuxeo.ecm.platform.rendering.wiki.WikiFilter;
023import org.nuxeo.ecm.webengine.WebEngine;
024import org.nuxeo.ecm.webengine.model.Resource;
025import org.nuxeo.ecm.webengine.model.WebContext;
026
027public class LinkResolver implements WikiFilter {
028
029    public static final String PATTERN = "(\\.)?([A-Z]+[a-z]+[A-Z][A-Za-z]*\\.)*([A-Z]+[a-z]+[A-Z][A-Za-z]*)";
030
031    public static final Pattern LINK_PATTERN = Pattern.compile(PATTERN);
032
033    public static final String LINK_TEMPLATE = "<a href=\"%s\">%s</a>";
034
035    @Override
036    public String apply(String content) {
037        Matcher m = LINK_PATTERN.matcher(content);
038        StringBuffer sb = new StringBuffer();
039        if (!m.find()) {
040            return content;
041        }
042        do {
043            String s = m.group();
044            String link = buildLinks(s);
045            m.appendReplacement(sb, link);
046        } while (m.find());
047        m.appendTail(sb);
048        return sb.toString();
049    }
050
051    protected String buildLinks(String pageName) {
052        String basePath;
053
054        WebContext ctx = WebEngine.getActiveContext();
055        Resource resource = ctx.getTargetObject();
056        StringBuffer links = new StringBuffer();
057        StringBuffer relativePath = new StringBuffer();
058
059        if (pageName.startsWith(".")) {
060            // Absolute path
061            basePath = ctx.getModulePath();
062            String[] segments = pageName.substring(1).split("\\.");
063
064            Resource parentResource = resource.getPrevious();
065            while (!parentResource.isInstanceOf("site")) {
066                parentResource = parentResource.getPrevious();
067            }
068            relativePath.append("/").append(parentResource.getName());
069            for (String segment : segments) {
070                links.append(".");
071                relativePath.append("/").append(segment);
072                links.append(buildLink(basePath, relativePath, segment));
073            }
074        } else {
075            // Relative path
076            basePath = resource.getPath();
077            String[] segments = pageName.split("\\.");
078            for (String segment : segments) {
079                relativePath.append("/").append(segment);
080                links.append(buildLink(basePath, relativePath, segment));
081                links.append(".");
082            }
083            // Remove last dot
084            links.deleteCharAt(links.length() - 1);
085        }
086
087        return links.toString();
088    }
089
090    protected String buildLink(String basePath, StringBuffer relativePath, String str) {
091        return String.format(LINK_TEMPLATE, basePath + relativePath, str);
092    }
093
094}