001 /* 002 * The contents of this file are subject to the Mozilla Public 003 * License Version 1.1 (the "License"); you may not use this file 004 * except in compliance with the License. You may obtain a copy of 005 * the License at http://www.mozilla.org/MPL/ 006 * 007 * Software distributed under the License is distributed on an "AS 008 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or 009 * implied. See the License for the specific language governing 010 * rights and limitations under the License. 011 * 012 * The Original Code is Knowtator. 013 * 014 * The Initial Developer of the Original Code is University of Colorado. 015 * Copyright (C) 2005 - 2008. All Rights Reserved. 016 * 017 * Knowtator was developed by the Center for Computational Pharmacology 018 * (http://compbio.uchcs.edu) at the University of Colorado Health 019 * Sciences Center School of Medicine with support from the National 020 * Library of Medicine. 021 * 022 * Current information about Knowtator can be obtained at 023 * http://knowtator.sourceforge.net/ 024 * 025 * Contributor(s): 026 * Philip V. Ogren <philip@ogren.info> (Original Author) 027 */ 028 029 package edu.uchsc.ccp.knowtator.textsource.files; 030 031 import java.io.BufferedReader; 032 import java.io.File; 033 import java.io.FileInputStream; 034 import java.io.IOException; 035 import java.io.InputStream; 036 import java.io.InputStreamReader; 037 import java.nio.charset.Charset; 038 039 import edu.uchsc.ccp.knowtator.textsource.DefaultTextSource; 040 import edu.uchsc.ccp.knowtator.textsource.TextSourceAccessException; 041 042 public class FileTextSource extends DefaultTextSource { 043 044 public static final String CLS_NAME = "file text source"; 045 046 protected File file; 047 048 protected Charset charset; 049 050 /** 051 * The name of the file with the rootPath prefix removed is the name of the 052 * FileTextSource. For example, if file.getPath is 053 * "/home/pogren/my_text.txt" and rootPath is "/home/pogren", then the name 054 * of the text source is my_text.txt. Typically file.getPath() == rootPath + 055 * "/"+ name. 056 */ 057 058 public FileTextSource(File file, String rootPath, Charset charset) { 059 this.file = file; 060 this.charset = charset; 061 062 name = file.getPath(); 063 if (rootPath.endsWith("" + File.separatorChar) || rootPath.equals("")) { 064 name = name.substring(rootPath.length()); 065 } else 066 name = name.substring(rootPath.length() + 1); 067 protegeClsName = CLS_NAME; 068 } 069 070 public File getFile() { 071 return file; 072 } 073 074 public String getText() throws TextSourceAccessException { 075 try { 076 return readFile(new FileInputStream(file), charset); 077 } catch (IOException ioe) { 078 throw new TextSourceAccessException(ioe); 079 } 080 } 081 082 public static String readFile(InputStream inputStream, Charset charset) throws IOException { 083 BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, charset)); 084 StringBuffer contents = new StringBuffer(); 085 086 String line = new String(); 087 while ((line = reader.readLine()) != null) { 088 contents.append(line + "\n"); 089 } 090 return contents.toString(); 091 } 092 093 }