// Copyright (C) 2003-2009 by Object Mentor, Inc. All rights reserved.
// Released under the terms of the CPL Common Public License version 1.0.
package fitnesse.fixtures;

import java.util.StringTokenizer;

import fit.ColumnFixture;
import fitnesse.wiki.*;

public class
  PageCreator extends ColumnFixture {
  public String pageName;
  public String pageContents;
  public String pageAttributes = "";

  public boolean valid() throws Exception {
    try {
      if (pageContents != null)  {
        pageContents = pageContents.replaceAll("<br>", "\n");
        pageContents = pageContents.replaceAll("<br/>", "\n");
      }
      WikiPage root = FitnesseFixtureContext.root;
      WikiPagePath pagePath = PathParser.parse(pageName);
      WikiPage thePage = WikiPageUtil.addPage(root, pagePath, pageContents);
      if (!"".equals(pageAttributes)) {
        PageData data = thePage.getData();
        setAttributes(data);
        thePage.commit(data);
        setPageAttributes("");
      }
    }
    catch (Exception e) {
      e.printStackTrace();
      return false;
    }
    return true;
  }

  private void setAttributes(PageData data) throws Exception {
    StringTokenizer tokenizer = new StringTokenizer(pageAttributes, ",");
    while (tokenizer.hasMoreTokens()) {
      String nameValuePair = tokenizer.nextToken();
      int equals = nameValuePair.indexOf("=");
      if (equals < 0)
        throw new Exception("Attribute must have form name=value");
      String name = nameValuePair.substring(0, equals);
      String value = nameValuePair.substring(equals + 1);
      data.setAttribute(name, value);
    }
  }

  public void setPageName(String pageName) {
    this.pageName = pageName;
  }

  public void setPageContents(String pageContents) {
    this.pageContents = pageContents;
  }

  public void setPageAttributes(String pageAttributes) {
    this.pageAttributes = pageAttributes;
  }
}


@@ -264,18 +264,16 @@
 ic class
-%0A 
  PageCre
@@ -443,20 +443,8 @@
 n %7B%0A
-    try %7B%0A  
     
@@ -472,18 +472,16 @@
 ull)  %7B%0A
-  
       pa
@@ -528,26 +528,24 @@
 r%3E%22, %22%5Cn%22);%0A
-  
       pageCo
@@ -597,22 +597,18 @@
 %22);%0A    
-  %7D%0A  
+%7D%0A
     Wiki
@@ -648,26 +648,24 @@
 t.root;%0A    
-  
 WikiPagePath
@@ -704,18 +704,16 @@
 eName);%0A
-  
     Wiki
@@ -779,26 +779,24 @@
 tents);%0A    
-  
 if (!%22%22.equa
@@ -813,26 +813,24 @@
 ributes)) %7B%0A
-  
       PageDa
@@ -864,18 +864,16 @@
 ;%0A      
-  
 setAttri
@@ -891,18 +891,16 @@
 ;%0A      
- 
- 
 thePage.
@@ -919,18 +919,16 @@
 ;%0A      
-  
 setPageA
@@ -946,95 +946,8 @@
 %22);%0A
-      %7D%0A    %7D%0A    catch (Exception e) %7B%0A      e.printStackTrace();%0A      return false;%0A
     

Clean up page creator class