001/*
002 * (C) Copyright 2006-2010 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Thierry Delprat
018 */
019package org.nuxeo.apidoc.wiki;
020
021import java.util.regex.Matcher;
022import java.util.regex.Pattern;
023
024import org.nuxeo.ecm.platform.rendering.wiki.WikiFilter;
025import org.nuxeo.ecm.webengine.WebEngine;
026import org.nuxeo.ecm.webengine.model.Resource;
027import org.nuxeo.ecm.webengine.model.WebContext;
028
029public class LinkResolver implements WikiFilter {
030
031    public static final String PATTERN = "(\\.)?([A-Z]+[a-z]+[A-Z][A-Za-z]*\\.)*([A-Z]+[a-z]+[A-Z][A-Za-z]*)";
032
033    public static final Pattern LINK_PATTERN = Pattern.compile(PATTERN);
034
035    public static final String LINK_TEMPLATE = "<a href=\"%s\">%s</a>";
036
037    @Override
038    public String apply(String content) {
039        Matcher m = LINK_PATTERN.matcher(content);
040        StringBuffer sb = new StringBuffer();
041        if (!m.find()) {
042            return content;
043        }
044        do {
045            String s = m.group();
046            String link = buildLinks(s);
047            m.appendReplacement(sb, link);
048        } while (m.find());
049        m.appendTail(sb);
050        return sb.toString();
051    }
052
053    protected String buildLinks(String pageName) {
054        String basePath;
055
056        WebContext ctx = WebEngine.getActiveContext();
057        Resource resource = ctx.getTargetObject();
058        StringBuffer links = new StringBuffer();
059        StringBuffer relativePath = new StringBuffer();
060
061        if (pageName.startsWith(".")) {
062            // Absolute path
063            basePath = ctx.getModulePath();
064            String[] segments = pageName.substring(1).split("\\.");
065
066            Resource parentResource = resource.getPrevious();
067            while (!parentResource.isInstanceOf("site")) {
068                parentResource = parentResource.getPrevious();
069            }
070            relativePath.append("/").append(parentResource.getName());
071            for (String segment : segments) {
072                links.append(".");
073                relativePath.append("/").append(segment);
074                links.append(buildLink(basePath, relativePath, segment));
075            }
076        } else {
077            // Relative path
078            basePath = resource.getPath();
079            String[] segments = pageName.split("\\.");
080            for (String segment : segments) {
081                relativePath.append("/").append(segment);
082                links.append(buildLink(basePath, relativePath, segment));
083                links.append(".");
084            }
085            // Remove last dot
086            links.deleteCharAt(links.length() - 1);
087        }
088
089        return links.toString();
090    }
091
092    protected String buildLink(String basePath, StringBuffer relativePath, String str) {
093        return String.format(LINK_TEMPLATE, basePath + relativePath, str);
094    }
095
096}