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;
    }
}

@@ -104,18 +104,16 @@
 ource %7B%0A
-  
   privat
@@ -133,18 +133,16 @@
  page;%0A%0A
-  
   public
@@ -181,20 +181,16 @@
 ) %7B%0A    
- 
-   
 this.pag
@@ -201,23 +201,19 @@
 page;%0A  
- 
- 
 %7D%0A%0A
-  
   public
@@ -247,36 +247,32 @@
 (String name) %7B%0A
-    
     Maybe%3CString
@@ -314,36 +314,32 @@
 alue(name);%0A    
-  
-  
 if (!result.isNo
@@ -359,28 +359,24 @@
 rn result;%0A%0A
-    
     result =
@@ -403,28 +403,24 @@
 (name);%0A    
-   
- 
 if (!result.
@@ -448,20 +448,16 @@
 esult;%0A%0A
-    
     Stri
@@ -484,36 +484,32 @@
 tenv(name);%0A    
-   
- 
 if (value != nul
@@ -541,28 +541,24 @@
 g%3E(value);%0A%0A
-    
     value = 
@@ -575,36 +575,32 @@
 Property(name);%0A
-    
     if (value !=
@@ -640,20 +640,16 @@
 alue);%0A%0A
-    
     retu
@@ -667,25 +667,21 @@
 String;%0A
-  
   %7D%0A%0A
-  
   privat
@@ -727,28 +727,24 @@
 ing name) %7B%0A
-    
     Maybe%3CSt
@@ -786,28 +786,24 @@
 able(name);%0A
-    
     if (!loc
@@ -878,28 +878,24 @@
 lue());%0A    
- 
-   
 return lookI
@@ -918,17 +918,13 @@
 e);%0A
-  
   %7D%0A%0A
-  
   pr
@@ -980,20 +980,16 @@
 ) %7B%0A    
- 
-   
 for (Sou
@@ -1006,16 +1006,17 @@
 urcePage
+ 
 : page.g
@@ -1044,30 +1044,24 @@
 ()) %7B%0A      
-     
- 
 if (!page.in
@@ -1089,24 +1089,16 @@
         
-       
- 
 Parser.m
@@ -1165,24 +1165,16 @@
 arse();%0A
-        
         
@@ -1216,24 +1216,16 @@
 ingPage%0A
-        
         
@@ -1280,30 +1280,18 @@
 ;%0A      
-      %7D%0A      
+%7D%0A
       Ma
@@ -1354,22 +1354,16 @@
 ;%0A      
-     
- 
 if (!res
@@ -1394,22 +1394,16 @@
 result;%0A
-      
       //
@@ -1450,26 +1450,18 @@
 t);%0A    
-  
-  %7D%0A    
+%7D%0A
     retu
@@ -1479,16 +1479,14 @@
 String;%0A
-  
   %7D%0A%7D%0A

Reformat VariableFinder