package fitnesse.wikitext.parser;

import util.Maybe;

public class VariableFinder implements VariableSource {
  private ParsingPage page;

  public VariableFinder(ParsingPage page) {
    this.page = page;
  }

  public Maybe<String> findVariable(String name) {
    Maybe<String> result = page.getSpecialVariableValue(name);
    if (!result.isNothing()) return result;

    result = findVariableInPages(name);
    if (!result.isNothing()) return result;

    String value = System.getenv(name);
    if (value != null) return new Maybe<String>(value);

    value = System.getProperty(name);
    if (value != null) return new Maybe<String>(value);

    return Maybe.noString;
  }

  private Maybe<String> findVariableInPages(String name) {
    Maybe<String> localVariable = page.findVariable(name);
    if (!localVariable.isNothing()) return new Maybe<String>(localVariable.getValue());
    return lookInParentPages(name);
  }

  private Maybe<String> lookInParentPages(String name) {
    for (SourcePage sourcePage : page.getPage().getAncestors()) {
      if (!page.inCache(sourcePage)) {
        Parser.make(page.copyForPage(sourcePage), sourcePage.getContent()).parse();
        // todo: make this a method on ParsingPage
        page.putVariable(sourcePage, "", Maybe.noString);
      }
      Maybe<String> result = page.findVariable(sourcePage, name);
      if (!result.isNothing()) return result;
      //page.putVariable(sourcePage, name, result);
    }
    return Maybe.noString;
  }
}

@@ -28,16 +28,75 @@
 arser;%0A%0A
+import fitnesse.FitNesse;%0Aimport fitnesse.FitNesseContext;%0A
 import u
@@ -515,160 +515,201 @@
     
-String value = System.getenv(name);%0A    if (value != null) return new Maybe%3CString%3E(value);%0A%0A    value = System.getProperty(name);%0A    if (value != null
+return findVariableInContext(name);%0A  %7D%0A%0A  private Maybe%3CString%3E findVariableInPages(String name) %7B%0A    Maybe%3CString%3E localVariable = page.findVariable(name);%0A    if (!localVariable.isNothing()
 ) re
@@ -735,17 +735,35 @@
 ng%3E(
-v
+localVariable.getV
 alue
+()
 );%0A
-%0A
     
@@ -765,38 +765,47 @@
 %0A    return 
-Maybe.noString
+lookInParentPages(name)
 ;%0A  %7D%0A%0A  pri
@@ -829,37 +829,39 @@
 %3E findVariableIn
-Pages
+Context
 (String name) %7B%0A
@@ -868,185 +868,434 @@
     
-Maybe%3CString%3E localVariable = page.findVariable(name);%0A    if (!localVariable.isNothing()) return new Maybe%3CString%3E(localVariable.getValue());%0A    return lookInParentPages(name)
+final FitNesseContext context = getFitNesseContext();%0A    if (context != null) %7B%0A      String val = context.getProperty(name);%0A      if (val != null) return new Maybe%3CString%3E(val);%0A    %7D%0A    return Maybe.noString;%0A  %7D%0A%0A  private FitNesseContext getFitNesseContext() %7B%0A    // Make this fail safe for unit tests%0A    final FitNesse fitnesse = FitNesse.FITNESSE_INSTANCE;%0A    return fitnesse != null ? fitnesse.getContext() : null
 ;%0A  

Use FitNesseContext in VariableFinder to