package org.pinae.nala.xb.util;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;

import org.pinae.nala.xb.exception.NoSuchPathException;
import org.pinae.nala.xb.exception.UnmarshalException;


/**
 * 资源内容读取工具
 * 
 * @author Huiyugeng
 * 
 */
public class ResourceReader {
	
	/**
	 * 逐行读取文件内容
	 * 
	 * @param filename 文件名
	 * 
	 * @return 文件内容
	 * 
	 * @throws IOException 文件读取异常
	 */
	public StringBuffer readFile(String filename) throws IOException {
		BufferedReader reader = new BufferedReader(new FileReader(filename));
		StringBuffer xml = new StringBuffer();
		String line = null;
        while((line = reader.readLine()) != null){
            xml.append(line);
        }
		reader.close();
		return xml;
	}
	
	/**
	 * 将文件读出为输出流
	 * 
	 * @param filename 需要读取的文件名称
	 * 
	 * @return 文件内容输出流
	 * 
	 * @throws NoSuchPathException 无法找到对应的文件或者路径
	 * 
	 */
	public InputStreamReader getFileStream(String filename)
			throws NoSuchPathException{
		try {
			return new InputStreamReader(new FileInputStream(filename));
		} catch (FileNotFoundException e) {
			throw new NoSuchPathException(e);
		}
	}
	
	/**
	 * 将文件读出为输出流
	 * 
	 * @param filename 需要读取的文件名称
	 * @param encoding 文件编码
	 * 
	 * @return 文件内容输出流
	 * 
	 * @throws NoSuchPathException 无法找到对应的文件或者路径
	 * @throws UnmarshalException 解组失败(通常由于编码问题引起)
	 * 
	 */
	public InputStreamReader getFileStream(String filename, String encoding)
			throws NoSuchPathException, UnmarshalException{
		try {
			return new InputStreamReader(new FileInputStream(filename), encoding);
		} catch (FileNotFoundException e) {
			throw new NoSuchPathException(e);
		} catch (UnsupportedEncodingException e){
			throw new UnmarshalException(e);
		}
	}

	/**
	 * 将URL资源读出为输出流
	 * 
	 * @param path URL资源
	 * 
	 * @return 资源内容输出流
	 * 
	 * @throws NoSuchPathException 搜索URL等资源时, 无法发现路径引发的异常
	 * @throws IOException IO异常 (通常出现在读写过程中的异常连接)
	 */
	public InputStream getURLStream(String path) throws NoSuchPathException, IOException {
		URL url = null;
		try {
			url = new URL(path);
		} catch (MalformedURLException e) {
			throw new NoSuchPathException(e);
		}

		return url.openStream();
		
	}
}

@@ -49,32 +49,53 @@
 BufferedReader;%0A
+import java.io.File;%0A
 import java.io.F
@@ -1971,16 +1971,584 @@
 ;%0A%09%09%7D%0A%09%7D
+%0A%09%0A%09/**%0A%09 * %E5%B0%86%E6%96%87%E4%BB%B6%E8%AF%BB%E5%87%BA%E4%B8%BA%E8%BE%93%E5%87%BA%E6%B5%81%0A%09 * %0A%09 * @param file %E9%9C%80%E8%A6%81%E8%AF%BB%E5%8F%96%E7%9A%84%E6%96%87%E4%BB%B6%0A%09 * @param encoding %E6%96%87%E4%BB%B6%E7%BC%96%E7%A0%81%0A%09 * %0A%09 * @return %E6%96%87%E4%BB%B6%E5%86%85%E5%AE%B9%E8%BE%93%E5%87%BA%E6%B5%81%0A%09 * %0A%09 * @throws NoSuchPathException %E6%97%A0%E6%B3%95%E6%89%BE%E5%88%B0%E5%AF%B9%E5%BA%94%E7%9A%84%E6%96%87%E4%BB%B6%E6%88%96%E8%80%85%E8%B7%AF%E5%BE%84%0A%09 * @throws UnmarshalException %E8%A7%A3%E7%BB%84%E5%A4%B1%E8%B4%A5(%E9%80%9A%E5%B8%B8%E7%94%B1%E4%BA%8E%E7%BC%96%E7%A0%81%E9%97%AE%E9%A2%98%E5%BC%95%E8%B5%B7)%0A%09 * %0A%09 */%0A%09public InputStreamReader getFileStream(File file, String encoding)%0A%09%09%09throws NoSuchPathException, UnmarshalException%7B%0A%09%09try %7B%0A%09%09%09return new InputStreamReader(new FileInputStream(file), encoding);%0A%09%09%7D catch (FileNotFoundException e) %7B%0A%09%09%09throw new NoSuchPathException(e);%0A%09%09%7D catch (UnsupportedEncodingException e)%7B%0A%09%09%09throw new UnmarshalException(e);%0A%09%09%7D%0A%09%7D
 %0A%0A%09/**%0A%09

Add getFileStream(File), read xml file from FileObject