package fitnesse.wikitext.parser;

import fitnesse.FitNesse;
import fitnesse.FitNesseContext;
import fitnesse.FitNesseVersion;
import util.Maybe;

import java.util.HashMap;

public class ParsingPage {

    private SourcePage page;
    private SourcePage namedPage;
    private HashMap<String, HashMap<String, Maybe<String>>> cache;

    public ParsingPage(SourcePage page) {
        this(page, page, new HashMap<String, HashMap<String, Maybe<String>>>());
    }

  public ParsingPage copy() {
      return new ParsingPage(page, page, this.cache);
  }

  public ParsingPage copyForPage(SourcePage page) {
      return new ParsingPage(page, page, this.cache);
  }

    public ParsingPage copyForNamedPage(SourcePage namedPage) {
        return new ParsingPage(this.page, namedPage, this.cache);
    }

    private ParsingPage(SourcePage page, SourcePage namedPage, HashMap<String, HashMap<String, Maybe<String>>> cache) {
        this.page = page;
        this.namedPage = namedPage;
        this.cache = cache;
    }

    public SourcePage getPage() { return page; }
    public SourcePage getNamedPage() { return namedPage; }

    public boolean inCache(SourcePage page) {
        return cache.containsKey(page.getFullName());
    }

    public Maybe<String> findVariable(SourcePage page, String name) {
        String key = page.getFullName();
        if (!cache.containsKey(key)) return Maybe.noString;
        if (!cache.get(key).containsKey(name)) return Maybe.noString;
        return cache.get(key).get(name);
    }

    public Maybe<String> findVariable(String name) {
        return findVariable(page, name);
    }

    public void putVariable(SourcePage page, String name, Maybe<String> value) {
        String key = page.getFullName();
        if (!cache.containsKey(key)) cache.put(key, new HashMap<String, Maybe<String>>());
        cache.get(key).put(name, value);
    }

    public void putVariable(String name, String value) {
        putVariable(page, name, new Maybe<String>(value));
    }
}

@@ -195,18 +195,16 @@
 Page %7B%0A%0A
-  
   privat
@@ -218,26 +218,24 @@
 ePage page;%0A
-  
   private So
@@ -254,18 +254,17 @@
 edPage;%0A
-  
+%0A
   privat
@@ -319,26 +319,24 @@
 %3E cache;%0A%0A  
- 
- 
 public Parsi
@@ -357,28 +357,24 @@
 age page) %7B%0A
-    
     this(pag
@@ -434,26 +434,24 @@
 ring%3E%3E%3E());%0A
-  
   %7D%0A%0A  publi
@@ -469,34 +469,32 @@
 ge copy() %7B%0A    
-  
 return new Parsi
@@ -578,26 +578,24 @@
 age page) %7B%0A
-  
     return n
@@ -635,26 +635,24 @@
 ache);%0A  %7D%0A%0A
-  
   public Par
@@ -701,28 +701,24 @@
 Page) %7B%0A    
-  
-  
 return new P
@@ -763,25 +763,21 @@
 cache);%0A
-  
   %7D%0A%0A
-  
   privat
@@ -886,28 +886,24 @@
 ache) %7B%0A    
- 
-   
 this.page = 
@@ -904,28 +904,24 @@
 age = page;%0A
-    
     this.nam
@@ -944,20 +944,16 @@
 ge;%0A    
- 
-   
 this.cac
@@ -964,25 +964,21 @@
  cache;%0A
-  
   %7D%0A%0A
-  
   public
@@ -992,32 +992,36 @@
 Page getPage() %7B
+%0A   
  return page; %7D%0A
@@ -1021,15 +1021,16 @@
 age;
+%0A 
  %7D%0A
- 
+%0A
   
- 
 publ
@@ -1059,16 +1059,20 @@
 Page() %7B
+%0A   
  return 
@@ -1081,22 +1081,22 @@
 medPage;
+%0A 
  %7D%0A%0A
-  
   public
@@ -1127,28 +1127,24 @@
 age page) %7B%0A
-    
     return c
@@ -1181,25 +1181,21 @@
 ame());%0A
-  
   %7D%0A%0A
-  
   public
@@ -1250,28 +1250,24 @@
 ing name) %7B%0A
-    
     String k
@@ -1287,36 +1287,32 @@
 FullName();%0A    
- 
-   
 if (!cache.conta
@@ -1343,28 +1343,24 @@
 e.noString;%0A
-    
     if (!cac
@@ -1413,28 +1413,24 @@
 String;%0A    
- 
-   
 return cache
@@ -1446,33 +1446,29 @@
 .get(name);%0A
-  
   %7D%0A%0A
-  
   public May
@@ -1502,28 +1502,24 @@
 ing name) %7B%0A
-    
     return f
@@ -1539,33 +1539,29 @@
 age, name);%0A
-  
   %7D%0A%0A
-  
   public voi
@@ -1623,28 +1623,24 @@
 g%3E value) %7B%0A
-    
     String k
@@ -1668,20 +1668,16 @@
 ();%0A    
-   
- 
 if (!cac
@@ -1751,20 +1751,16 @@
 g%3E%3E());%0A
-    
     cach
@@ -1792,19 +1792,15 @@
 e);%0A
-  
   %7D%0A%0A  
- 
- 
 publ
@@ -1852,20 +1852,16 @@
 ) %7B%0A    
-   
- 
 putVaria
@@ -1903,16 +1903,14 @@
 alue));%0A
-  
   %7D%0A%7D%0A

Reformat ParsingPage