package net.hillsdon.reviki.wiki.renderer.creole;

import java.net.URI;
import java.net.URISyntaxException;

import net.hillsdon.reviki.vc.PageReference;
import net.hillsdon.reviki.vc.PageStoreException;
import net.hillsdon.reviki.vc.SimplePageStore;
import net.hillsdon.reviki.vc.impl.PageReferenceImpl;
import net.hillsdon.reviki.web.urls.InterWikiLinker;
import net.hillsdon.reviki.web.urls.InternalLinker;
import net.hillsdon.reviki.web.urls.UnknownWikiException;

public class LinkResolutionContext {
  private final InternalLinker _internalLinker;
  private final SimplePageStore _store;
  private final InterWikiLinker _interWikiLinker;
  private final PageReference _page;
  
  public LinkResolutionContext(final InternalLinker internalLinker, final InterWikiLinker interWikiLinker, final SimplePageStore store) {
    _internalLinker = internalLinker;
    _interWikiLinker = interWikiLinker;
    _store = store;
    _page = null;
  }
  
  public LinkResolutionContext(final InternalLinker internalLinker, final InterWikiLinker interWikiLinker, final SimplePageStore store, final PageReference page) {
    _internalLinker = internalLinker;
    _interWikiLinker = interWikiLinker;
    _store = store;
    _page = page;
  }

  public URI resolve(String wiki, String pageName) throws UnknownWikiException, URISyntaxException {
    if (wiki!=null) {
      return _interWikiLinker.uri(wiki, pageName, null);
    }
    if (pageName == null) {
      if (_page == null) {
        throw new IllegalStateException("LinkResolutionContext not initialised with a context page.");
      }
      pageName = _page.getName();
    }
    return _internalLinker.uri(pageName);
  }

  public boolean exists(PageReferenceImpl pageReferenceImpl) throws PageStoreException {
    return _store.exists(pageReferenceImpl);
  }

  public LinkResolutionContext derive(PageReference page) {
    return new LinkResolutionContext(_internalLinker, _interWikiLinker, _store, page);
  }

  public SimplePageStore getPageStore() {
    return _store;
  }
}

@@ -102,16 +102,57 @@
 ption;%0A%0A
+import com.google.common.base.Function;%0A%0A
 import n
@@ -188,16 +188,16 @@
 erence;%0A
-
 import n
@@ -285,16 +285,67 @@
 eStore;%0A
+import net.hillsdon.reviki.vc.impl.DummyPageStore;%0A
 import n
@@ -487,32 +487,84 @@
 InternalLinker;%0A
+import net.hillsdon.reviki.web.urls.SimpleWikiUrls;%0A
 import net.hills
@@ -643,16 +643,803 @@
 ntext %7B%0A
+  /**%0A   * For when internal links are relative to some path, and there are no%0A   * inter-wiki links.%0A   */%0A  public static final Function%3CString, LinkResolutionContext%3E SIMPLE_LINKS = new Function%3CString, LinkResolutionContext%3E() %7B%0A    public LinkResolutionContext apply(String base) %7B%0A      // Have all internal relative links start from /jira/browse/%0A      SimpleWikiUrls wikiUrls = SimpleWikiUrls.RELATIVE_TO.apply(base);%0A      InternalLinker linker = new InternalLinker(wikiUrls);%0A%0A      // We know of no other wikis.%0A      InterWikiLinker wikilinker = new InterWikiLinker();%0A%0A      // Or any pages.%0A      SimplePageStore pageStore = new DummyPageStore();%0A%0A      // Finally, construct the resolver.%0A      return new LinkResolutionContext(linker, wikilinker, pageStore);%0A    %7D%0A  %7D;%0A%0A
   privat
@@ -1470,32 +1470,33 @@
 internalLinker;%0A
+%0A
   private final 
@@ -1519,16 +1519,17 @@
 _store;%0A
+%0A
   privat
@@ -1570,16 +1570,17 @@
 Linker;%0A
+%0A
   privat
@@ -1608,19 +1608,17 @@
  _page;%0A
-  
 %0A
+
   public
@@ -1871,11 +1871,9 @@
   %7D%0A
-  
 %0A
+
   pu
@@ -2270,10 +2270,12 @@
 wiki
+ 
 !=
+ 
 null

Add helper to LinkResolutionContexts for really simple contexts