package urban;
import static urban.parser.UrbanParser.parse;
import java.io.File;
import java.io.PrintWriter;
import java.io.StringWriter;
import org.antlr.runtime.RecognitionException;
import org.apache.commons.collections15.Transformer;
import urban.model.Model;
import urban.transformers.AddRatesFromShapesTransformer;
import urban.transformers.ResolveImports;
import urban.transformers.ShapeToRuleTransformer;
/**
* Urban to kappa transformer
*/
public class UrbanToKappaTransformer implements Transformer<String, String> {
private final File baseDir;
/**
* Constructor. Base directory for resolving imports is the current directory.
*/
public UrbanToKappaTransformer() {
this.baseDir = null;
}
/**
* Constructor. Base directory for resolving imports is the directory given.
* @param base
*/
public UrbanToKappaTransformer(File base) {
this.baseDir = base;
}
@Override
public String transform(String text) {
try {
Model m = parse(text+"\n").model();
m = new ResolveImports(baseDir).transform(m);
m = new ShapeToRuleTransformer().transform(m);
// m = new AddPinchesModelTransformer().transform(m);
m = new AddRatesFromShapesTransformer().transform(m);
return m.toString().trim();
} catch (RecognitionException e) {
return stacktrace(e);
}
}
private String stacktrace(RecognitionException e) {
StringWriter out = new StringWriter();
PrintWriter writer = new PrintWriter(out);
e.printStackTrace(writer);
return out.toString();
}
}