//Source file: H:\\temp\\generated\\modTransf\\rules\\codeGenerator\\WriterDescriptor.java
package modTransf.rules.codeGenerator;
import java.io.Writer;
import javax.script.CompiledScript;
import modTransf.script.CompiledScriptFactory;
import javax.script.ScriptException;
import modTransf.engine.EngineException;
import modTransf.engine.RuleContext;
import javax.script.ScriptContext;
import modTransf.rules.core.RuleContextWrapperForScriptContext;
import modTransf.engine.TransformationException;
import java.io.IOException;
import modTransf.engine.ReaderWriterFactory;
/**
* A writer descriptor. The descriptor specify the path, the filename and the
* extension. All attribute are expressions evaluated at creation time.
*/
public class WriterDescriptor
{
protected CompiledScript path;
protected CompiledScript fileName;
protected CompiledScript fileExt;
/**
* Constructor.
* Initialize the descriptor form specified expr.
* @param path
* @param fileName
* @param fileExt
*/
public WriterDescriptor(String path, String fileName, String fileExt)
throws EngineException
{
this(path, fileName, fileExt, null);
}
/**
* Constructor.
* Initialize the descriptor form specified expr.
* @param path The path to use. Optional.
* @param fileName Name of the file. Mandatory. An exception is throw if this
* is not set.
* @param fileExt The file extension. Optional.
*/
public WriterDescriptor(String path, String fileName, String fileExt, String exprLanguage)
throws EngineException
{
try
{
if(path!=null && path.length()!=0)
{
this.path = CompiledScriptFactory.compileScript(path, exprLanguage);
}
if(fileName!=null && fileName.length()!=0)
{
this.fileName = CompiledScriptFactory.compileScript(fileName, exprLanguage);
}
else
throw new EngineException("Property fileName is mandatory.");
if(fileExt!=null && fileExt.length()!=0)
{
this.fileExt = CompiledScriptFactory.compileScript(fileExt, exprLanguage);
}
}
catch(ScriptException ex)
{
throw new EngineException(ex);
}
}
/**
* @param context
* @return Writer
*/
public Writer create(RuleContext request)
throws TransformationException
{
try
{
ScriptContext context = new RuleContextWrapperForScriptContext(request);
String realPath = evalCompiledScript(path, context);
String realExt = evalCompiledScript(fileExt, context);
String realFileName = evalCompiledScript(fileName, context);
Writer writer = getReaderWriterFactory(request).createWriter(realPath, realFileName, realExt);
return writer;
}
catch(IOException ex)
{
throw new TransformationException(ex);
}
}
/**
* Get the reader/writer factory.
* @return ReaderWriterFactory
*/
private ReaderWriterFactory getReaderWriterFactory(RuleContext context)
{
return context.getTransformation().getReaderWriterFactory();
}
private String evalCompiledScript( CompiledScript script, ScriptContext context )
throws TransformationException
{
if( script == null )
return null;
try
{
return script.eval(context).toString();
}
catch(ScriptException ex)
{
throw new TransformationException(ex);
}
}
}