ProjectExtensionsResponder.java :  » Testing » StoryTestIQ » fitnesse » responders » Java Open Source

Java Open Source » Testing » StoryTestIQ 
StoryTestIQ » fitnesse » responders » ProjectExtensionsResponder.java
// Copyright (C) 2003,2004,2005 by Object Mentor, Inc. All rights reserved.
// Released under the terms of the GNU General Public License version 2 or later.
package fitnesse.responders;

import fitnesse.util.PropertiesUtil;
import fitnesse.http.*;
import fitnesse.*;
import java.io.*;

public class ProjectExtensionsResponder extends BasicResponder {
    private static String PROJECT_EXTENSIONS_FILE_DIRECTORY = "STIQProjectExtensionsFileDirectory";

  public Response makeResponse(FitNesseContext context, Request request) throws Exception {
    String projectExtensionsFileDirectory = PropertiesUtil.getProperty(PROJECT_EXTENSIONS_FILE_DIRECTORY);
    if (projectExtensionsFileDirectory != null && projectExtensionsFileDirectory != "") {
      return responseWith(readProjectExtensionsFileFromDirectory(projectExtensionsFileDirectory));
    } else {
      String defaultProjectExtensionsFileDirectory = (String)System.getProperties().get("user.dir");
      if (!projectExtensionsFileExistsInDirectory(defaultProjectExtensionsFileDirectory)) {
        System.out.println("No value was provided for " + PROJECT_EXTENSIONS_FILE_DIRECTORY + ", and no project-extensions.js was found at default location " + defaultProjectExtensionsFileDirectory + ", so project-extensions.js will not be loaded.");
        return responseWith("");
      } else {
        return responseWith(readProjectExtensionsFileFromDirectory(defaultProjectExtensionsFileDirectory));
      }
    }
  }

  private String fullProjectExtensionsFilePath(String directoryPath) {
    return directoryPath + System.getProperties().get("file.separator") + "project-extensions.js";
  }
  
  private boolean projectExtensionsFileExistsInDirectory(String directoryPath) {
    try {
      FileInputStream fstream = new FileInputStream(fullProjectExtensionsFilePath(directoryPath));
      fstream.markSupported();
      return true;
    } catch (Exception e) {
      return false;
    }
  }
  
  private String readProjectExtensionsFileFromDirectory(String directoryPath) {
    return readFileAsString(fullProjectExtensionsFilePath(directoryPath));
  }

  private String readFileAsString(String filePath) {
    String thisLine;
      try {
        StringBuffer contents = new StringBuffer();
        BufferedReader br = new BufferedReader(new FileReader(filePath));
        while ((thisLine = br.readLine()) != null) {
           contents.append(thisLine + "\n");
        }
        br.close();
      return contents.toString();
      }
      catch (IOException e) {
      return "alert('Unable to read project extensions file [" + filePath + "].');";
      }
  }

}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.