/*
 * Copyright 2012 JBoss Inc
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.overlord.sramp.shell;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;

/**
 * An implementation of the {@link ShellCommandReader} that reads data from
 * a file.
 *
 * @author eric.wittmann@redhat.com
 */
public class FileShellCommandReader extends AbstractShellCommandReader {

	private String filePath;
	private BufferedReader fileReader;

	/**
	 * Constructor.
	 * @param factory
	 * @param context
	 * @param filePath
	 */
	public FileShellCommandReader(ShellCommandFactory factory, ShellContextImpl context, String filePath) {
		super(factory, context);
		this.filePath = filePath;
	}

	/**
	 * @see org.overlord.sramp.common.shell.AbstractShellCommandReader#open()
	 */
	@Override
	public void open() throws IOException {
		File file = new File(filePath);
		if (!file.isFile()) {
			throw new FileNotFoundException(filePath);
		}
		this.fileReader = new BufferedReader(new InputStreamReader(FileUtils.openInputStream(file)));
	}

	/**
	 * @see org.overlord.sramp.common.shell.AbstractShellCommandReader#readLine()
	 */
	@Override
	protected String readLine() throws IOException {
		return fileReader.readLine();
	}

	/**
	 * @see org.overlord.sramp.common.shell.ShellCommandReader#close()
	 */
	@Override
	public void close() throws IOException {
		IOUtils.closeQuietly(fileReader);
	}

}

@@ -1939,37 +1939,225 @@
 %7B%0D%0A%09
-%09return fileReader.readLine()
+    String line = null;%0D%0A%09    do %7B%0D%0A            line = fileReader.readLine();%0D%0A            if (line == null)%0D%0A                break;%0D%0A%09    %7D while (line.startsWith(%22#%22) %7C%7C line.trim().length() == 0);%0D%0A%09    return line
 ;%0D%0A%09

Support comments in s-ramp cli command files.